Video Demo: https://youtu.be/9veYppxT4Js
The Sentiment Analysis Chatbot is a Python-based interactive chat application that analyzes the emotional tone of user messages in real-time. This project was created as a final submission for Harvard's CS50P course, combining natural language processing with user interaction to create an engaging and insightful chatting experience.
-
Sentiment Analysis
- Analyzes text for emotional tone using TextBlob library
- Provides detailed sentiment scores (polarity and subjectivity)
- Categorizes messages into five sentiment levels:
- Very Positive
- Positive
- Neutral
- Negative
- Very Negative
-
Interactive Chat Interface
- Color-coded messages for better readability
- Real-time feedback on sentiment analysis
- User-friendly command system
- Personalized interaction with user name
-
Conversation Management
- Automatic saving of chat history to JSON
- Timestamps for all messages
- Structured conversation data format
- Error handling for data saving
The project is structured into several key Python files:
-
project.py
- Contains the main application logic
- Implements core functions:
analyze_sentiment()
: Processes text and returns sentiment metricsget_response()
: Generates contextual responses based on sentimentformat_message()
: Structures messages with metadatasave_conversation()
: Handles conversation persistence
- Features comprehensive error handling
-
test_project.py
- Contains pytest-compatible test functions
- Covers all core functionalities
- Includes edge cases and error conditions
- Ensures code reliability
-
requirements.txt
- Lists all necessary Python packages:
- textblob: For sentiment analysis
- termcolor: For colored console output
- pytest: For running tests
- Lists all necessary Python packages:
-
Installation
pip install -r requirements.txt
-
Running the Program
python project.py
-
Running Tests
pytest test_project.py
-
Basic Commands
- Type any message to chat
- Use 'quit', 'exit', 'bye', or 'goodbye' to end the conversation
- Press Ctrl+C for emergency exit
-
TextBlob for Sentiment Analysis
- Chosen for its accuracy and ease of use
- Provides both polarity and subjectivity scores
- Well-documented and maintained library
-
Color-Coded Interface
- Green: Bot messages
- Yellow: User messages
- Cyan: System messages
- Magenta: Sentiment analysis results
-
JSON for Data Storage
- Human-readable format
- Easy to process and analyze
- Maintains message structure
-
Modular Function Design
- Separate functions for distinct responsibilities
- Easy to test and maintain
- Follows Python best practices
-
Enhanced Analysis
- Emotion detection
- Language detection
- Topic analysis
-
Data Analysis
- Sentiment trending over time
- User interaction patterns
- Response effectiveness metrics
This project demonstrates the practical application of natural language processing in creating interactive systems, while maintaining code quality through comprehensive testing and error handling. It serves as both a functional chat application and an educational tool for understanding sentiment analysis in text.