A comprehensive machine learning project for sentiment analysis using both traditional ML approaches and modern transformer models.
This project implements multiple sentiment analysis approaches:
- Traditional ML Models: Logistic Regression, SVM, Naive Bayes, Random Forest
- Transformer Models: Pre-trained models from Hugging Face
- Text Preprocessing: Comprehensive text cleaning and feature extraction
- Model Comparison: Side-by-side evaluation of different approaches
sentiment_analysis_project/
├── data/ # Data storage directory
├── models/ # Saved model files
├── notebooks/ # Jupyter notebooks for exploration
│ └── 01_explore_and_train.ipynb
├── src/ # Source code
│ ├── preprocessing.py # Text preprocessing utilities
│ └── sentiment_analyzer.py # Main ML models and analysis
├── tests/ # Unit tests
├── requirements.txt # Project dependencies
├── config.yaml # Configuration settings
└── README.md # This file
# Create virtual environment
python -m venv .venv
# Activate virtual environment (Windows PowerShell)
.\.venv\Scripts\Activate.ps1
# Install dependencies
pip install -r requirements.txt
# Download NLTK data (will be done automatically on first run)
python -c "import nltk; nltk.download('punkt'); nltk.download('stopwords'); nltk.download('wordnet')"# Run the main demo script
cd src
python sentiment_analyzer.py# Start Jupyter
jupyter lab
# Open notebooks/01_explore_and_train.ipynb# Test voice functionality
python voice_demo.py
# Or run the GUI with voice support
python launch_gui.py- Microphone: Working microphone connected to your system
- Internet Connection: Required for Google Speech Recognition API
- Audio Dependencies: PyAudio, SpeechRecognition, pydub (installed automatically)
- Supported Formats: WAV, MP3, M4A for file analysis
from src.sentiment_analyzer import TraditionalSentimentAnalyzer, TransformerSentimentAnalyzer
from src.preprocessing import load_sample_data, create_train_test_split
# Load sample data
df = load_sample_data()
X_train, X_test, y_train, y_test = create_train_test_split(df, 'text', 'sentiment')
# Train traditional model
model = TraditionalSentimentAnalyzer(model_type='logistic_regression')
model.train(X_train, y_train)
# Make predictions
texts = ["I love this product!", "This is terrible."]
predictions = model.predict(texts)
print(predictions) # ['positive', 'negative']
# Use transformer model
transformer = TransformerSentimentAnalyzer()
transformer_predictions = transformer.predict(texts)
print(transformer_predictions)from src.sentiment_analyzer import SentimentAnalysisComparator
# Compare multiple models
comparator = SentimentAnalysisComparator()
comparator.add_traditional_model("Logistic Regression", "logistic_regression")
comparator.add_traditional_model("SVM", "svm")
comparator.add_transformer_model("Transformer", "cardiffnlp/twitter-roberta-base-sentiment-latest")
results = comparator.compare_models(X_train, y_train, X_test, y_test)
print(results)from src.voice_processor import VoiceInputProcessor, VoiceSentimentAnalyzer
from src.sentiment_analyzer import TransformerSentimentAnalyzer
# Test microphone setup
processor = VoiceInputProcessor()
test_result = processor.test_microphone(duration=5)
print(f"Microphone test: {test_result}")
# Real-time voice sentiment analysis
transformer_model = TransformerSentimentAnalyzer()
voice_analyzer = VoiceSentimentAnalyzer(transformer_model)
# Set up callbacks for real-time feedback
def on_text_recognized(text):
print(f"Heard: {text}")
def on_sentiment_detected(sentiment_data):
sentiment = sentiment_data['sentiment']
confidence = sentiment_data['confidence']
print(f"Sentiment: {sentiment} (confidence: {confidence:.2f})")
# Start real-time analysis
voice_analyzer.start_real_time_analysis(
on_sentiment_callback=on_sentiment_detected,
on_text_callback=on_text_recognized
)
# Stop and get summary
summary = voice_analyzer.stop_real_time_analysis()
print(f"Overall sentiment: {summary['overall_sentiment']}")
print(f"Total phrases: {summary['total_phrases']}")
# Analyze audio file
result = voice_analyzer.analyze_audio_file('path/to/audio.wav')
if result['success']:
print(f"File text: {result['text']}")
print(f"Sentiment: {result['primary_sentiment']} ({result['confidence']:.2f})")Edit config.yaml to customize:
- Model parameters
- Preprocessing options
- File paths
- API settings
- Multiple Model Types: Traditional ML and modern transformers
- Comprehensive Preprocessing: Text cleaning, tokenization, stemming/lemmatization
- Model Evaluation: Accuracy, precision, recall, F1-score
- Visualization: Model comparison plots
- Voice Input Support: Real-time speech recognition and sentiment analysis
- Audio File Processing: Batch analysis of audio files
- Real-time Feedback: Live sentiment analysis during voice input
- Extensible: Easy to add new models and features
- Production Ready: Model saving/loading, API deployment ready
The Flask server provides REST API endpoints for both text and voice analysis:
POST /api/analyze- Analyze sentiment for single textPOST /api/batch_analyze- Analyze sentiment for multiple textsGET /api/examples- Get sample texts for testingGET /api/model_status- Check model loading status
GET /api/voice/test_microphone- Test microphone functionalityGET /api/voice/devices- List available audio devicesPOST /api/voice/start_recording- Start real-time voice recordingPOST /api/voice/stop_recording- Stop recording and get resultsGET /api/voice/status- Get current recording statusPOST /api/voice/analyze_file- Analyze uploaded audio file
import requests
# Test microphone
response = requests.get('http://localhost:5000/api/voice/test_microphone')
print(response.json())
# Start voice recording
start_data = {'session_id': 'my_session', 'model_type': 'transformer'}
response = requests.post('http://localhost:5000/api/voice/start_recording', json=start_data)
print(response.json())
# Stop recording and get results
stop_data = {'session_id': 'my_session'}
response = requests.post('http://localhost:5000/api/voice/stop_recording', json=stop_data)
result = response.json()
print(f"Overall sentiment: {result['summary']['overall_sentiment']}")# Run tests (when implemented)
python -m pytest tests/The project includes several pre-configured models:
| Model Type | Typical Accuracy | Training Time | Prediction Speed |
|---|---|---|---|
| Logistic Regression | 85-90% | Fast | Very Fast |
| SVM | 80-85% | Moderate | Fast |
| Naive Bayes | 75-80% | Very Fast | Very Fast |
| Transformer | 90-95% | N/A (pre-trained) | Moderate |
- Real-time sentiment analysis API
- Support for multiple languages
- Advanced preprocessing options
- Model ensemble methods
- Docker containerization
- MLOps pipeline integration
- Custom dataset support
- Aspect-based sentiment analysis
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- Hugging Face for transformer models
- scikit-learn for traditional ML algorithms
- NLTK for text processing utilities
- The open-source community for inspiration and tools
Happy Analyzing!