Skip to content

Mayur-Shashidhar/Sarcasm-Detection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

6 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿค– Sarcasm Detection using Deep Learning (Bi-LSTM)

A complete Natural Language Processing (NLP) project that detects sarcasm in text using a Bidirectional LSTM (Bi-LSTM) model. The system is built with a clean, modular architecture and evaluated on multiple datasets to ensure real-world generalization.


๐Ÿš€ Project Overview

Sarcasm detection is a challenging NLP task because sarcastic sentences often appear positive on the surface but convey negative intent.

This project builds an end-to-end pipeline that:

  • Processes raw text data
  • Converts it into numerical representations
  • Trains a deep learning model
  • Evaluates performance using multiple metrics
  • Tests generalization on unseen datasets

๐Ÿง  Problem Statement

Given a sentence:

"Oh great, another assignment"

The model predicts:

Sarcastic ๐Ÿ˜

๐Ÿ—๏ธ Project Structure

sarcasm-detector/
โ”‚
โ”œโ”€โ”€ data/
โ”‚   โ”œโ”€โ”€ dataset.json
โ”‚   โ””โ”€โ”€ new_dataset.json
โ”‚
โ”œโ”€โ”€ model/
โ”‚   โ”œโ”€โ”€ sarcasm_model.keras
โ”‚   โ””โ”€โ”€ history.pkl
โ”‚
โ”œโ”€โ”€ tokenizer/
โ”‚   โ””โ”€โ”€ tokenizer.pkl
โ”‚
โ”œโ”€โ”€ plots/
โ”‚   โ”œโ”€โ”€ accuracy.png
โ”‚   โ””โ”€โ”€ loss.png
โ”‚
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ data_loader.py
โ”‚   โ”œโ”€โ”€ preprocess.py
โ”‚   โ”œโ”€โ”€ model.py
โ”‚   โ”œโ”€โ”€ train.py
โ”‚   โ”œโ”€โ”€ predict.py
โ”‚   โ”œโ”€โ”€ evaluate.py
โ”‚   โ”œโ”€โ”€ visualize.py
โ”‚   โ””โ”€โ”€ test_new_data.py
โ”‚
โ”œโ”€โ”€ requirements.txt
โ””โ”€โ”€ README.md

โš™๏ธ Installation

1. Clone the repository

git clone <your-repo-url>
cd sarcasm-detector

2. Create virtual environment

python3 -m venv venv
source venv/bin/activate

3. Install dependencies

pip install -r requirements.txt

๐Ÿ“Š Dataset

This project uses the News Headlines Sarcasm Dataset from Kaggle.

  • Format: JSON (line-by-line)

  • Fields:

    • headline โ†’ text
    • is_sarcastic โ†’ label (0 or 1)

Download it from: [https://www.kaggle.com/search?q=News+Headlines+Sarcasm+Dataset]

Note: Dataset is subject to its own license.


๐Ÿ”„ Pipeline

1. Data Loading

  • Reads JSON dataset
  • Extracts text and labels

2. Preprocessing

  • Tokenization (word โ†’ integer mapping)
  • Padding (fixed sequence length)

3. Model Architecture

  • Embedding Layer
  • Bidirectional LSTM (64 units)
  • Dropout (regularization)
  • Bidirectional LSTM (32 units)
  • Dense layers
  • Sigmoid output (binary classification)

๐Ÿง  Model Summary

  • Total Parameters: ~748K
  • Type: Deep Learning (Bi-LSTM)
  • Loss: Binary Crossentropy
  • Optimizer: Adam

๐Ÿ‹๏ธ Training

Run:

cd src
python train.py

Features:

  • Early stopping (prevents overfitting)
  • Model saving (.keras format)
  • Tokenizer persistence
  • Training history saved for visualization

๐Ÿ”ฎ Prediction

Run:

python predict.py

Example:

Enter a sentence: Oh great, another exam
Sarcastic ๐Ÿ˜

๐Ÿ“ˆ Evaluation

Run:

python evaluate.py

Metrics:

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

๐Ÿ“Š Visualization

Run:

python visualize.py

Generates:

  • Accuracy vs Epoch graph
  • Loss vs Epoch graph

Saved in:

plots/

๐Ÿ” Cross-Dataset Testing

Run:

python test_new_data.py

This evaluates the model on a completely different dataset to test generalization.


๐Ÿ“Š Results

โœ… Primary Dataset

  • Accuracy: ~85โ€“87%
  • F1 Score: ~0.86

โœ… Cross-Dataset Performance

  • Accuracy: ~91%
  • F1 Score: ~0.90

๐Ÿง  Key Insights

  • The model generalizes well across datasets
  • Slight difficulty in detecting subtle sarcasm
  • Performance depends on dataset style and distribution

โš ๏ธ Challenges

  • Sarcasm is context-dependent
  • Limited understanding of real-world tone
  • Domain mismatch (news vs conversational text)

๐Ÿ”ฅ Future Improvements

  • Replace LSTM with Transformer models (BERT)
  • Add Streamlit UI for interactive predictions
  • Deploy as a web application
  • Fine-tune on conversational sarcasm datasets

๐Ÿง‘โ€๐Ÿ’ป Technologies Used

  • Python
  • TensorFlow / Keras
  • NumPy / Pandas
  • Scikit-learn
  • Matplotlib

๐Ÿ Conclusion

This project demonstrates:

  • End-to-end NLP pipeline design
  • Deep learning model implementation
  • Proper evaluation and validation
  • Real-world testing using multiple datasets

โญ If you like this project

Give it a โญ on GitHub!


About

A complete Natural Language Processing (NLP) project that detects sarcasm in text using a Bidirectional LSTM (Bi-LSTM) model. The system is built with a clean, modular architecture and evaluated on multiple datasets to ensure real-world generalization.

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages