Skip to content

About

Sentiment analysis

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Sentiment Analysis Project

A comprehensive sentiment analysis project using deep learning models (LSTM and BERT) to classify text sentiment as positive, negative, or neutral.

Features

  • Multiple Model Architectures:

    • LSTM (Long Short-Term Memory) with bidirectional layers
    • BERT (Bidirectional Encoder Representations from Transformers)
  • Multiple Dataset Support:

    • IMDB Movie Reviews
    • Sentiment140 (Twitter data)
    • Yelp Reviews
    • Generic CSV format
  • Complete Pipeline:

    • Data preprocessing and cleaning
    • Model training with callbacks
    • Model evaluation and metrics
    • Inference on new text

Quick Start - How to Run

1. Install Dependencies

pip install -r requirements.txt

2. Prepare Your Dataset

Download a dataset (IMDB, Sentiment140, or Yelp) or use your own CSV file with text and sentiment columns.

3. Train a Model

LSTM (Recommended for beginners):

python train.py --model lstm --data_path your_dataset.csv --dataset_type auto --epochs 10

BERT (Better accuracy, needs more memory):

python train.py --model bert --data_path your_dataset.csv --dataset_type auto --epochs 3 --batch_size 16

4. Make Predictions

# LSTM
python predict.py --model lstm --model_path models/saved/lstm_sentiment_model.h5 --tokenizer_path models/saved/lstm_tokenizer.pkl --text "This movie is great!"

# BERT
python predict.py --model bert --model_path models/saved/bert_sentiment_model --text "This movie is great!"

For detailed instructions, see QUICKSTART.md

Windows Users: You can also use the batch scripts:

  • run_training.bat - Interactive training script
  • run_prediction.bat - Interactive prediction script

Installation

  1. Clone or download this repository

  2. Install required dependencies:

pip install -r requirements.txt

Dataset Preparation

IMDB Dataset

Download from Kaggle IMDB Dataset

Expected CSV format:

  • review: Text of the review
  • sentiment: 'positive' or 'negative'

Sentiment140

Download from Sentiment140

Expected CSV format:

  • text: Tweet text
  • target: 0 (negative) or 4 (positive)

Yelp Reviews

Download from Kaggle Yelp Reviews

Expected CSV format:

  • text or review: Review text
  • stars or rating: 1-5 stars (1-2: negative, 4-5: positive)

Usage

Training Models

Train LSTM Model

python train.py --model lstm --data_path path/to/dataset.csv --dataset_type imdb --epochs 20 --batch_size 64

Train BERT Model

python train.py --model bert --data_path path/to/dataset.csv --dataset_type imdb --epochs 3 --batch_size 16

Train Both Models

python train.py --model both --data_path path/to/dataset.csv --dataset_type imdb

Arguments:

  • --model: Model type (lstm, bert, or both)
  • --data_path: Path to your dataset CSV file
  • --dataset_type: Type of dataset (imdb, sentiment140, yelp, or auto)
  • --output_dir: Directory to save models (default: models/saved)
  • --epochs: Number of training epochs (default: 20 for LSTM, 3 for BERT)
  • --batch_size: Batch size (default: 64 for LSTM, 16 for BERT)
  • --test_size: Test set size (default: 0.2)
  • --bert_model: BERT model name (default: bert-base-uncased)

Evaluating Models

Evaluate LSTM Model

python evaluate.py --model lstm --model_path models/saved/lstm_sentiment_model.h5 --tokenizer_path models/saved/lstm_tokenizer.pkl --data_path path/to/test_data.csv

Evaluate BERT Model

python evaluate.py --model bert --model_path models/saved/bert_sentiment_model --data_path path/to/test_data.csv

Making Predictions

Single Text Prediction

python predict.py --model lstm --model_path models/saved/lstm_sentiment_model.h5 --tokenizer_path models/saved/lstm_tokenizer.pkl --text "This movie is amazing!"
python predict.py --model bert --model_path models/saved/bert_sentiment_model --text "This movie is amazing!"

Batch Prediction from File

python predict.py --model lstm --model_path models/saved/lstm_sentiment_model.h5 --tokenizer_path models/saved/lstm_tokenizer.pkl --file texts.txt

Interactive Mode

python predict.py --model lstm --model_path models/saved/lstm_sentiment_model.h5 --tokenizer_path models/saved/lstm_tokenizer.pkl

Project Structure

.
├── data_preprocessing.py    # Data loading and preprocessing
├── models/
│   ├── __init__.py
│   ├── lstm_model.py       # LSTM model implementation
│   └── bert_model.py       # BERT model implementation
├── train.py                # Training script
├── evaluate.py             # Evaluation script
├── predict.py              # Inference script
├── requirements.txt        # Python dependencies
└── README.md              # This file

Model Details

LSTM Model

  • Architecture: Embedding → Bidirectional LSTM → Dense layers
  • Embedding Dimension: 128
  • LSTM Units: 128
  • Max Sequence Length: 200
  • Dropout: 0.3
  • Optimizer: Adam (learning rate: 0.001)

BERT Model

  • Base Model: bert-base-uncased (configurable)
  • Max Sequence Length: 128
  • Learning Rate: 2e-5
  • Optimizer: Adam

Training Features

  • Early Stopping: Stops training when validation loss stops improving
  • Learning Rate Reduction: Reduces learning rate on plateau
  • Model Checkpointing: Saves best model based on validation accuracy
  • Stratified Splitting: Maintains class distribution in train/test splits

Evaluation Metrics

  • Accuracy
  • Precision
  • Recall
  • F1-Score
  • Confusion Matrix

Notes

  • BERT models require more memory and training time but generally achieve better accuracy
  • LSTM models are faster to train and require less memory
  • For large datasets, consider using a GPU for faster training
  • The tokenizer is saved separately for LSTM models to enable inference on new data

Troubleshooting

  1. Out of Memory: Reduce batch size or sequence length
  2. Slow Training: Use GPU or reduce model size
  3. Poor Accuracy: Try different hyperparameters or use more training data
  4. Import Errors: Ensure all dependencies are installed correctly

License

This project is open source and available for educational purposes.

About

Sentiment analysis

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages