A professional, modular sentiment analysis framework using LSTM neural networks for movie review classification.
This project provides a complete, production-ready implementation of sentiment analysis using Long Short-Term Memory (LSTM) neural networks. Built on TensorFlow/Keras, it offers a modular architecture, comprehensive testing, multiple interfaces (CLI, Python API, Jupyter notebooks), and extensive documentation.
- Modular Architecture: Clean separation of concerns with dedicated modules for data loading, model building, training, prediction, and visualization
- Bidirectional LSTM: Captures dependencies from both forward and backward sequences for improved accuracy
- Multiple Interfaces:
- Python API for programmatic access
- CLI tools for command-line usage
- Jupyter notebooks for interactive exploration
- Comprehensive Testing: Unit tests with pytest for reliability
- Production Ready: Model persistence, logging, configuration management
- Rich Visualizations: Training curves, confusion matrices, ROC curves, prediction distributions
- Easy Installation: Simple pip installation with all dependencies
- Installation
- Quick Start
- Project Structure
- Usage
- Model Architecture
- Configuration
- Examples
- Testing
- Contributing
- License
- Python 3.8 or higher
- pip package manager
# Clone the repository
git clone https://github.com/pyenthusiasts/Sentiment-Analysis-LSTM.git
cd Sentiment-Analysis-LSTM
# Install the package
pip install -e .pip install -r requirements.txtfrom sentiment_analysis.train import Trainer
from sentiment_analysis.predict import Predictor
# Train a model
trainer = Trainer()
(X_train, y_train), (X_test, y_test) = trainer.prepare_data()
history = trainer.train(X_train, y_train, X_test, y_test)
# Make predictions
predictor = Predictor()
result = predictor.predict_text("Amazing movie! Loved it!")
print(f"Sentiment: {result['sentiment']} (Score: {result['score']:.2f})")Sentiment-Analysis-LSTM/
├── src/sentiment_analysis/ # Main package
│ ├── config.py # Configuration
│ ├── data_loader.py # Data loading
│ ├── model.py # Model architecture
│ ├── train.py # Training logic
│ ├── predict.py # Prediction logic
│ ├── utils.py # Utilities
│ └── visualization.py # Visualizations
├── tests/ # Unit tests
├── examples/ # Example scripts
├── notebooks/ # Jupyter notebooks
├── requirements.txt # Dependencies
└── setup.py # Package setup
from sentiment_analysis.train import Trainer
trainer = Trainer()
(X_train, y_train), (X_test, y_test) = trainer.prepare_data()
history = trainer.train(X_train, y_train, X_test, y_test, epochs=5)from sentiment_analysis.predict import Predictor
predictor = Predictor()
result = predictor.predict_text("This movie was amazing!")
print(f"Sentiment: {result['sentiment']}")
print(f"Confidence: {result['confidence']:.2%}")- Embedding Layer: 128-dimensional word embeddings
- Bidirectional LSTM: 64 units processing in both directions
- Dropout: 0.5 rate for regularization
- LSTM: 32 units
- Dense Output: Sigmoid activation for binary classification
Edit src/sentiment_analysis/config.py to customize:
VOCAB_SIZE: 10000 (vocabulary size)MAX_LENGTH: 300 (sequence length)EMBEDDING_DIM: 128BATCH_SIZE: 128EPOCHS: 5
Run example scripts:
python examples/basic_usage.py
python examples/custom_training.py
python examples/prediction_only.py# Run all tests
pytest
# Run with coverage
pytest --cov=sentiment_analysisContributions welcome! See CONTRIBUTING.md for guidelines.
MIT License - see LICENSE for details.
- Built with TensorFlow and Keras
- IMDB dataset from Stanford AI Lab
Made with passion for NLP and Deep Learning