This project builds a machine learning pipeline for detecting hate, offensive, and neutral speech in short-form text. It includes dataset processing, cleaning, balancing, TF-IDF vectorization, model training, and an example inference step.
.
βββ main.py
βββ hate_speech.csv
βββ cleaned_hate_dataset.csv # (generated)
βββ hate_speech_model.pkl # (generated)
- Cleans and normalizes tweet text
- Removes stopwords, URLs, mentions, emojis, and special chars
- Handles class imbalance via oversampling
- Trains a neural network classifier (MLP)
- Uses TF-IDF embeddings with n-grams
- Saves trained model + vectorizer as artifacts
- Contains inference demo for quick testing
Algorithm: MLPClassifier (sklearn)
Key Params
hidden_layer_sizes=(100,)activation="relu"solver="adam"max_iter=200early_stopping=True
Classes:
0β Hate1β Offensive2β Neutral
The script balances the dataset by oversampling under-represented labels before training.
The clean_text() function applies:
- Lowercasing
- URL + username removal
- Hashtag stripping
- Punctuation removal
- Stopword filtering
- Token reconstruction
Example input:
"@user Check out this link! http://site.com #Wow"
Output:
"Neutral"
pip install -r requirements.txt
(If NLTK stopwords aren't installed, script will download them.)
python main.py
This will:
- Load dataset
- Clean + balance
- Train model
- Save artifacts
- Display metrics
After training completes:
cleaned_hate_dataset.csvhate_speech_model.pkl
The .pkl file stores:
{
"vectorizer": TfidfVectorizer,
"model": MLPClassifier
}The script ends by running inference on sample inputs such as:
"I hate you so much!"
"Check out my new blog post"
"Had a great time at the concert!"
Outputs include predicted class labels.
Recommended environment:
- Python 3.8+
- scikit-learn
- imbalanced-learn
- pandas
- nltk
- Designed for academic + research use
- Not production-hardened
- Requires dataset
hate_speech.csvto exist locally