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.
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
Given a sentence:
"Oh great, another assignment"
The model predicts:
Sarcastic ๐
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
git clone <your-repo-url>
cd sarcasm-detector
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
This project uses the News Headlines Sarcasm Dataset from Kaggle.
-
Format: JSON (line-by-line)
-
Fields:
headlineโ textis_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.
- Reads JSON dataset
- Extracts text and labels
- Tokenization (word โ integer mapping)
- Padding (fixed sequence length)
- Embedding Layer
- Bidirectional LSTM (64 units)
- Dropout (regularization)
- Bidirectional LSTM (32 units)
- Dense layers
- Sigmoid output (binary classification)
- Total Parameters: ~748K
- Type: Deep Learning (Bi-LSTM)
- Loss: Binary Crossentropy
- Optimizer: Adam
Run:
cd src
python train.py
Features:
- Early stopping (prevents overfitting)
- Model saving (
.kerasformat) - Tokenizer persistence
- Training history saved for visualization
Run:
python predict.py
Example:
Enter a sentence: Oh great, another exam
Sarcastic ๐
Run:
python evaluate.py
Metrics:
- Accuracy
- Precision
- Recall
- F1-score
- Confusion Matrix
Run:
python visualize.py
Generates:
- Accuracy vs Epoch graph
- Loss vs Epoch graph
Saved in:
plots/
Run:
python test_new_data.py
This evaluates the model on a completely different dataset to test generalization.
- Accuracy: ~85โ87%
- F1 Score: ~0.86
- Accuracy: ~91%
- F1 Score: ~0.90
- The model generalizes well across datasets
- Slight difficulty in detecting subtle sarcasm
- Performance depends on dataset style and distribution
- Sarcasm is context-dependent
- Limited understanding of real-world tone
- Domain mismatch (news vs conversational text)
- Replace LSTM with Transformer models (BERT)
- Add Streamlit UI for interactive predictions
- Deploy as a web application
- Fine-tune on conversational sarcasm datasets
- Python
- TensorFlow / Keras
- NumPy / Pandas
- Scikit-learn
- Matplotlib
This project demonstrates:
- End-to-end NLP pipeline design
- Deep learning model implementation
- Proper evaluation and validation
- Real-world testing using multiple datasets
Give it a โญ on GitHub!