YAML Metadata Warning:empty or missing yaml metadata in repo card

Check out the documentation for more information.

๐Ÿง  Sentiment Analysis Using Deep Learning (LSTM)

Python TensorFlow Keras NLTK TextBlob License

๐Ÿš€ Deep Learning Based Twitter Sentiment Analysis using LSTM Neural Networks

Predict whether a tweet expresses Positive, Negative, or Neutral sentiment with a trained LSTM model.


๐Ÿ“Œ Overview

This project implements an End-to-End Sentiment Analysis System using Long Short-Term Memory (LSTM) networks.

The model learns contextual information from tweets after performing extensive Natural Language Processing (NLP) preprocessing.

Unlike traditional Machine Learning models, LSTM captures sequential dependencies between words, making it highly effective for sentiment classification.

The trained model predicts one of the following sentiments:

  • ๐Ÿ˜Š Positive
  • ๐Ÿ˜ Neutral
  • ๐Ÿ˜ž Negative

โœจ Features

โœ… English Tweet Filtering

โœ… Advanced Text Cleaning

โœ… HTML Entity Decoding

โœ… URL Removal

โœ… Stopword Removal

โœ… Negation Word Preservation

โœ… Lemmatization

โœ… Tokenization

โœ… Sequence Padding

โœ… Deep LSTM Network

โœ… Automatic Label Generation using TextBlob

โœ… Confidence Score Prediction

โœ… Save & Reload Model

โœ… Ready for Web Deployment


๐Ÿง  Deep Learning Architecture

Input Tweets
      โ”‚
      โ–ผ
Text Cleaning
      โ”‚
      โ–ผ
Tokenization
      โ”‚
      โ–ผ
Padding Sequences
      โ”‚
      โ–ผ
Embedding Layer
      โ”‚
      โ–ผ
LSTM (128)
      โ”‚
      โ–ผ
Dropout
      โ”‚
      โ–ผ
LSTM (64)
      โ”‚
      โ–ผ
Dropout
      โ”‚
      โ–ผ
LSTM (32)
      โ”‚
      โ–ผ
Global Max Pooling
      โ”‚
      โ–ผ
Dense (64)
      โ”‚
      โ–ผ
Dropout
      โ”‚
      โ–ผ
Dense (Softmax)
      โ”‚
      โ–ผ
Prediction

๐Ÿ“‚ Dataset

The project uses a Twitter sentiment dataset.

Dataset includes:

  • User ID
  • Tweet
  • Language

Only English Tweets are used for training.


โš™๏ธ NLP Preprocessing Pipeline

The preprocessing pipeline performs:

  • Convert text to lowercase
  • Decode HTML entities
  • Remove URLs
  • Remove HTML tags
  • Remove punctuation
  • Remove numbers
  • Remove extra spaces
  • Preserve negation words
  • Remove stopwords
  • Lemmatization
  • Clean sentence generation

Example

Original Tweet

I absolutely LOVE this phone!! ๐Ÿ˜๐Ÿ˜
https://abc.com

Clean Tweet

absolutely love phone

๐Ÿท Label Generation

Instead of manually labeling data, the project automatically generates sentiment labels using TextBlob Polarity.

Polarity Sentiment
< 0 Negative
= 0 Neutral
> 0 Positive

๐Ÿ”ค Tokenization

Tokenizer Configuration

  • Vocabulary Size : 10,000
  • OOV Token : <OOV>

Each cleaned tweet is converted into integer sequences.

Example

I love AI

โ†“

[15, 41, 280]

๐Ÿ“ Sequence Padding

Every tweet is padded to a fixed length.

Maximum Length = 30

Padding Type

Post Padding

Truncation

Post Truncation

๐Ÿง  Model Summary

Embedding Layer

โ†“

LSTM (128)

โ†“

Dropout (0.2)

โ†“

LSTM (64)

โ†“

Dropout (0.2)

โ†“

LSTM (32)

โ†“

Dropout (0.2)

โ†“

GlobalMaxPooling1D

โ†“

Dense (64)

โ†“

Dropout (0.2)

โ†“

Dense (3)

โ†“

Softmax

โš™๏ธ Training Configuration

Parameter Value
Optimizer Adam
Loss Sparse Categorical Crossentropy
Epochs 20
Batch Size 32
Validation Split 20%

Callbacks

  • EarlyStopping
  • ModelCheckpoint
  • ReduceLROnPlateau

๐Ÿ“Š Model Evaluation

Evaluation Metrics

  • Accuracy
  • Loss
  • Classification Report
  • Precision
  • Recall
  • F1 Score
  • Confusion Matrix

๐Ÿ”ฎ Sample Predictions

Tweet Prediction
I absolutely love this phone! ๐Ÿ˜Š Positive
This is the worst service ever. ๐Ÿ˜ž Negative
The meeting starts at 10 AM tomorrow. ๐Ÿ˜ Neutral
India won the cricket match! ๐Ÿ˜Š Positive
Russia attacked Ukraine today. ๐Ÿ˜ž Negative

๐Ÿ’พ Saved Files

best_model.keras

Best model saved during training.

sentiment_lstm.keras

Final trained model.

tokenizer.pkl

Tokenizer object.

label_encoder.pkl

Label encoder.


๐Ÿ“ Repository Structure

sentiment_lstm/

โ”‚โ”€โ”€ best_model.keras
โ”‚โ”€โ”€ sentiment_lstm.keras
โ”‚โ”€โ”€ tokenizer.pkl
โ”‚โ”€โ”€ label_encoder.pkl
โ”‚โ”€โ”€ sentiment.csv
โ”‚โ”€โ”€ README.md

๐Ÿš€ Future Improvements

  • Streamlit Web Application
  • Hugging Face Deployment
  • REST API using FastAPI
  • Docker Containerization
  • ONNX Model Conversion
  • Attention-based Bi-LSTM
  • Transformer Comparison (BERT)
  • Live Twitter/X Sentiment Prediction
  • Batch Prediction Support
  • Explainable AI (LIME/SHAP)

๐Ÿ›  Tech Stack

  • Python
  • TensorFlow
  • Keras
  • NumPy
  • Pandas
  • NLTK
  • TextBlob
  • Scikit-Learn
  • Matplotlib
  • Seaborn

๐Ÿ“ฆ Installation

git clone https://github.com/YOUR_USERNAME/sentiment_lstm.git

cd sentiment_lstm

pip install -r requirements.txt

โ–ถ๏ธ Run

python app.py

or

jupyter notebook

๐ŸŽฏ Applications

  • Social Media Monitoring
  • Customer Feedback Analysis
  • Product Reviews
  • Political Opinion Mining
  • Brand Reputation Analysis
  • Market Research
  • Public Opinion Analysis

๐Ÿ‘จโ€๐Ÿ’ป Author

Sudheer Muthyala

๐ŸŽ“ B.Tech - Electronics & Communication Engineering

๐Ÿ’ป Python Developer | Machine Learning Enthusiast | Data Science Aspirant

GitHub: https://github.com/M-Sudheer18


โญ Support

If you found this project useful, consider giving it a โญ on GitHub.

It helps others discover the project and motivates future improvements.


โญ Star โ€ข ๐Ÿด Fork โ€ข ๐Ÿš€ Contribute

Built with โค๏ธ using TensorFlow, Keras & NLP

Downloads last month
-
Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support