Sentiment-Analysis-LSTM

Sentiment Analysis with LSTM

A professional, modular sentiment analysis framework using LSTM neural networks for movie review classification. [![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)](https://www.python.org/downloads/) [![TensorFlow](https://img.shields.io/badge/TensorFlow-2.10+-orange.svg)](https://www.tensorflow.org/) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

Overview

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.

Key Features

Table of Contents

Installation

Prerequisites

Install from Source

# Clone the repository
git clone https://github.com/pyenthusiasts/Sentiment-Analysis-LSTM.git
cd Sentiment-Analysis-LSTM

# Install the package
pip install -e .

Install Dependencies Only

pip install -r requirements.txt

Quick Start

Python API

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})")

Project Structure

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

Usage

Training a Model

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)

Making Predictions

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%}")

Model Architecture

  1. Embedding Layer: 128-dimensional word embeddings
  2. Bidirectional LSTM: 64 units processing in both directions
  3. Dropout: 0.5 rate for regularization
  4. LSTM: 32 units
  5. Dense Output: Sigmoid activation for binary classification

Configuration

Edit src/sentiment_analysis/config.py to customize:

Examples

Run example scripts:

python examples/basic_usage.py
python examples/custom_training.py
python examples/prediction_only.py

Testing

# Run all tests
pytest

# Run with coverage
pytest --cov=sentiment_analysis

Contributing

Contributions welcome! See CONTRIBUTING.md for guidelines.

License

MIT License - see LICENSE for details.

Acknowledgments


Made with passion for NLP and Deep Learning