Skip to content

venom312004/emotion-detector-nlp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

14 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧠 Emotion Detector β€” NLP Text Classification

A machine learning web application that detects human emotions from text using Natural Language Processing. Built with TF-IDF vectorization and Logistic Regression, wrapped in an interactive Streamlit UI.

πŸ“Œ Table of Contents


πŸ” Overview

This project builds an end-to-end NLP pipeline that classifies text into one of 6 human emotions. The pipeline includes full text preprocessing, feature extraction using TF-IDF and Bag of Words, and classification using multiple ML models. The best model is served through a Streamlit web app with a clean, interactive UI.


🎬 Demo

Run locally with:

python -m streamlit run emotion_app.py

Features of the UI:

  • Upload your own training data
  • Type any text and get instant emotion prediction
  • View confidence scores for all 6 emotions as a bar chart
  • See cleaned text after the full preprocessing pipeline
  • View training data emotion distribution chart

🎭 Emotions Detected

Emotion Label Emoji
Sadness 0 😒
Anger 1 😠
Love 2 ❀️
Surprise 3 😲
Fear 4 😨
Joy 5 πŸ˜„

πŸ“ Project Structure

emotion-detector-nlp/
β”‚
β”œβ”€β”€ emotion_app.py          # Streamlit web application
β”œβ”€β”€ NLP-emotions.ipynb      # Full model training notebook
β”œβ”€β”€ train.txt               # Training dataset (text;emotion format)
β”œβ”€β”€ requirements.txt        # Python dependencies
└── README.md               # Project documentation

πŸ”„ NLP Pipeline

Every input text goes through the following preprocessing steps before prediction:

Raw Text
   β”‚
   β–Ό
1. Lowercase conversion         β†’ "I AM HAPPY" β†’ "i am happy"
   β”‚
   β–Ό
2. Remove punctuation           β†’ "hello!!!" β†’ "hello"
   β”‚
   β–Ό
3. Remove numbers               β†’ "room 404" β†’ "room "
   β”‚
   β–Ό
4. Remove emojis                β†’ keeps only ASCII characters
   β”‚
   β–Ό
5. Remove URLs                  β†’ strips http/https/www links
   β”‚
   β–Ό
6. Remove stopwords (NLTK)      β†’ removes "is", "the", "and" etc. (198 words)
   β”‚
   β–Ό
Cleaned Text β†’ TF-IDF Vectorizer β†’ Logistic Regression β†’ Predicted Emotion

πŸ“Š Model Comparison

Three models were trained and evaluated. Here are the results:

Model Vectorizer Test Accuracy Notes
Multinomial Naive Bayes Bag of Words (CountVectorizer) ~77% Fast, but lower accuracy
Multinomial Naive Bayes TF-IDF ~74% Worse than BoW for this dataset
Logistic Regression TF-IDF ~86.2% βœ… Best model β€” selected for deployment

Why Logistic Regression won?

  • Handles high-dimensional sparse TF-IDF features well
  • Outputs calibrated probabilities for all classes
  • max_iter=1000 allowed full convergence of the optimizer
  • Significantly outperformed Naive Bayes on this 6-class problem

Why TF-IDF over Bag of Words?

  • TF-IDF penalizes very common words that appear across all documents
  • Gives higher weight to words that are more unique to specific emotions
  • Reduces noise from frequently occurring but uninformative words

πŸ›  Tech Stack

Tool Purpose
Python 3.8+ Core programming language
Pandas Data loading and manipulation
NLTK Stopwords removal and tokenization
Scikit-learn TF-IDF, CountVectorizer, Logistic Regression, Naive Bayes
Matplotlib Probability bar charts and distribution plots
Streamlit Interactive web UI
NumPy Numerical operations

βš™οΈ Installation & Usage

1. Clone the repository

git clone https://github.com/venom312004/emotion-detector-nlp.git
cd emotion-detector-nlp

2. Install dependencies

pip install -r requirements.txt

3. Run the app

python -m streamlit run emotion_app.py

4. Use the app

  • Upload your train.txt file via the sidebar
  • Wait a few seconds for the model to train
  • Type any text in the input box
  • Click Predict Emotion and see the result!

πŸ“‚ Dataset

The dataset is a plain text file (train.txt) with semicolon-separated values:

i feel really happy today;joy
i miss you so much;sadness
this makes me so angry;anger
i love spending time with you;love
i had no idea this would happen;surprise
i am terrified of the dark;fear
  • Format: text;emotion
  • Classes: 6 (sadness, anger, love, surprise, fear, joy)
  • Split: 80% training / 20% testing
  • Random state: 42 (reproducible results)

πŸ“ˆ Results

Best Model   : Logistic Regression + TF-IDF
Test Accuracy: ~86.2%
Classes      : 6
Max Features : 20,000 (TF-IDF vocabulary)
Max Iter     : 1000
Solver       : lbfgs

The model performs particularly well on joy, sadness, and anger β€” emotions with strong distinctive vocabulary. Surprise and fear sometimes overlap due to similar language patterns.


πŸ‘€ Author

Pranjal Pandey


πŸ“„ License

This project is licensed under the MIT License β€” feel free to use, modify, and distribute.


Made with ❀️ using Python & Streamlit

About

"Built an NLP-based emotion detection web app using TF-IDF + Logistic Regression, deployed with Streamlit

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages