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.
# 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.txt
from 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%}")
Edit src/sentiment_analysis/config.py to customize:
VOCAB_SIZE: 10000 (vocabulary size)MAX_LENGTH: 300 (sequence length)EMBEDDING_DIM: 128BATCH_SIZE: 128EPOCHS: 5Run 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_analysis
Contributions welcome! See CONTRIBUTING.md for guidelines.
MIT License - see LICENSE for details.