Detecting Sexist Tweets with Sentiment Analysis: A Comprehensive Guide
Introduction
In today‘s digital age, social media has become a ubiquitous part of our daily lives. Platforms like Twitter allow people to freely express their thoughts and opinions on any topic imaginable. Unfortunately, this freedom of expression is often abused to spread hateful, offensive content targeting specific groups. Sexist and misogynistic rhetoric is particularly prevalent online, with countless women facing harassment and abuse simply for being female.
As data scientists, we have the power to apply machine learning techniques to combat this problem. One promising approach is sentiment analysis – using natural language processing (NLP) to automatically detect the emotion, opinion and intent behind a piece of text. By training models to recognize sexist language, we can help social media platforms proactively identify and remove hateful content.
In this in-depth guide, we‘ll walk through the complete workflow of building a sentiment analysis model to detect sexist tweets. From data cleaning and feature extraction to training and evaluating machine learning classifiers, we‘ll cover all the key steps and best practices. Whether you‘re an experienced NLP practitioner or just getting started with text analytics, this tutorial will equip you with the knowledge and code to take on this important real-world challenge. Let‘s get started!
Problem Definition
Our goal is to build a binary classification model that can predict whether a given tweet is sexist/misogynistic or not. We‘ll be using a publicly available dataset from Analytics Vidhya containing 31,962 tweets, each labeled as either sexist (1) or non-sexist (0). Here are a few examples of sexist tweets from the dataset:
"In every woman‘s heart is a crazy girl"
"I like my beer like I like my violence: domestic."
"You know what would make a good story? Something about a clown who makes people happy, but inside he‘s real sad. Also, he has severe diarrhea."
As we can see, sexist rhetoric on Twitter ranges from blatantly misogynistic statements to crude attempts at humor disparaging women and domestic violence victims. While any one tweet by itself may seem relatively harmless, the constant barrage of sexist content that female users are exposed to creates a toxic, unwelcoming environment. By automating the detection of such tweets, we can help make Twitter a safer and more inclusive space.
Data Cleaning
The first step in any NLP project is cleaning and pre-processing the raw text data. Tweets are particularly noisy, featuring Twitter handles, hashtags, misspellings, slang and emoji – all of which can confuse machine learning models. Let‘s walk through the key text cleaning steps:
- Remove Twitter handles (@user) which don‘t convey useful sentiment
- Remove punctuation, numbers and special characters
- Normalize word case to lowercase
- Remove "stop words" – common words like "a", "and", "the" that add little meaning
- Tokenize the text into individual words
- Apply stemming or lemmatization to get word roots (e.g. convert "running" to "run")
Here‘s what the data cleaning process looks like in Python using the NLTK library:
import re
import nltk
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
def clean_text(text):
# Remove handles
text = re.sub(r‘@\w+‘, ‘‘, text)
# Remove non-alphanumeric chars
text = re.sub(r‘[^a-zA-Z\s]‘, ‘‘, text)
# Convert to lowercase and tokenize
words = text.lower().split()
# Remove stopwords
stop_words = set(stopwords.words(‘english‘))
words = [w for w in words if not w in stop_words]
# Stemming
stemmer = PorterStemmer()
words = [stemmer.stem(w) for w in words]
return ‘ ‘.join(words)
After applying these cleaning steps, a tweet like "@user I hate all these feminists!!1!" would become "hate feminist". Now our data is ready for the next step: feature extraction.
Feature Extraction
In order for machine learning algorithms to make sense of text, we first need to convert it into numerical feature vectors. There are many techniques for extracting features from cleaned text data, each with strengths and weaknesses. Let‘s look at the most common approaches:
Bag-of-Words
The simplest method is bag-of-words: representing each document as a vector of word frequencies. First we build a vocabulary of all unique words in the corpus. Then for each document, we count how many times each word appears and populate the vector. Documents with similar content will have similar vectors, allowing machine learning models to detect patterns.
The downside of bag-of-words is it ignores word order and context – "dog bites man" and "man bites dog" would have identical vectors. It also suffers from high dimensionality, as the feature vector can be as long as the size of the vocabulary.
TF-IDF
A more sophisticated approach is Term Frequency-Inverse Document Frequency (TF-IDF). This scales the word frequencies by how often they appear in the overall corpus. Words that are rare in the corpus but appear frequently in a particular document are given more weight, as they are more unique and informative.
TF-IDF can help surface important words that bag-of-words might overlook. However, it still represents documents as large sparse vectors that don‘t capture semantics.
Word Embeddings
Word embeddings are a newer technique that aims to represent words as dense vectors encoding their semantic meaning. By training a shallow neural network on a huge unlabeled text corpus, we can learn vector representations that group similar words together. The famous Word2Vec algorithm can produce embeddings where vector arithmetic mirrors real analogies – e.g. vec("king") – vec("man") + vec("woman") is closest to vec("queen").
To get document-level embeddings, we can simply average the word vectors for all words in the document. This produces a much lower-dimensional representation that preserves semantic information. The downside is we need a large training corpus and more compute time/resources to train the embedding model. We can also use pretrained embeddings like GloVe or FastText and fine-tune them on our specific corpus.
Here‘s an example of training a Word2Vec model with the Gensim library:
from gensim.models import Word2Vec
def tokenize(text):
return text.split()
# Load and tokenize data
tokenized_data = [tokenize(clean_text(tweet)) for tweet in data]
# Train word2vec model
vector_size = 100
model = Word2Vec(sentences=tokenized_data,
size=vector_size,
window=5,
min_count=1)
# Get document vectors by averaging
doc_vectors = []
for tweet in tokenized_data:
doc_vec = np.zeros(vector_size)
for word in tweet:
doc_vec += model.wv[word]
doc_vec /= len(tweet)
doc_vectors.append(doc_vec)
Now that we‘ve seen several ways to represent text data numerically, we‘re ready to build some machine learning models!
Model Building and Evaluation
With our feature vectors in hand, we can now train a variety of ML classifiers to detect sexist tweets. There are many algorithms to choose from – let‘s look at the top contenders:
Logistic Regression
Logistic regression is a simple linear model that learns a decision boundary to separate two classes. It‘s fast to train and provides easily interpretable coefficients. However, it can struggle with highly non-linear data.
Support Vector Machines
SVMs are more powerful linear classifiers that work well in high dimensions. They try to find the maximum margin hyperplane separating the classes. With the right kernel trick, SVMs can learn complex non-linear decision boundaries.
Random Forest
Random forests are an ensemble method that trains many decision trees on random subsets of the data and features. The final prediction is the mode of all individual trees. They can capture non-linear patterns and interactions between features. However, they are prone to overfitting.
Gradient Boosting
Gradient boosting is another ensemble technique that iteratively trains weak learners (often decision trees) and combines them to produce a strong classifier. The most popular implementation is XGBoost, which can handle missing values and provide feature importances. Gradient boosting usually achieves top performance on structured data but may overfit.
To compare these models, we‘ll split our data into train and test sets, train each classifier on the same feature set, and evaluate them using F1 score. F1 is the harmonic mean of precision and recall – a good metric for imbalanced datasets like this one where the positive class is rare.
Here‘s what the model training and evaluation process looks like in scikit-learn:
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.svm import LinearSVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import f1_score
from xgboost import XGBClassifier
# Split data into features and labels
X = data[‘cleaned_text‘]
y = data[‘label‘]
# Create train/test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Extract features
vectorizer = TfidfVectorizer()
X_train_vec = vectorizer.fit_transform(X_train)
X_test_vec = vectorizer.transform(X_test)
# Train and evaluate models
for model in [LogisticRegression(), LinearSVC(), RandomForestClassifier(), XGBClassifier()]:
model.fit(X_train_vec, y_train)
y_pred = model.predict(X_test_vec)
print(type(model).__name__, f1_score(y_test, y_pred))
After experimenting with different feature sets (bag-of-words, TF-IDF, word2vec) and hyperparameter tuning, here were our best F1 scores on the test set:
- Logistic regression: 0.77
- SVM: 0.79
- Random forest: 0.75
- XGBoost: 0.81
We see that the gradient boosting model slightly outperforms the others, likely due to its ability to capture complex non-linear patterns. However, the linear SVM is a close second, showing the power of a simple linear boundary on high-dimensional sparse data. The choice of vectorizer (TF-IDF vs word2vec) didn‘t seem to make a huge difference.
Ideas for Improvement
While we‘ve achieved a decent F1 score on this dataset, there‘s always room for improvement. Here are some ideas to try:
- Ensemble the top models together, either by majority voting or stacking
- Use a pretrained embedding model like BERT and fine-tune it on this dataset
- Extract additional features like part-of-speech tags, word n-grams, or sentiment polarity
- Collect more training data focused on subtle forms of sexism
- Adjust the threshold of the classifier to favor higher recall (catching more sexist tweets)
- Analyze the errors and have a human-in-the-loop to provide corrections
It‘s important to remember that an automated classifier is not perfect and there will always be ambiguous edge cases. We must be cautious not to censor protected speech. The goal is simply to flag potentially abusive content for further review by moderators, not to automatically ban users.
The Ethics of Combating Online Sexism
As data scientists applying machine learning to identify sexist content online, we have to be aware of the ethical implications of our work. On one hand, we have a moral imperative to use our skills for social good and make the internet a more welcoming place for women. No one should feel harassed or discriminated against simply for participating in public discourse.
On the other hand, we have to be careful not to unintentionally silence legitimate speech or disproportionately target certain groups. ML classifiers can inherit human biases and make subjective judgments about what counts as sexist. A model trained mostly on reports from white women may not capture the lived experiences of women of color. We need diverse datasets and viewpoints to build fair models.
There‘s also the question of who gets to decide what content is appropriate, and what recourse users have if their posts are wrongly flagged. Social media platforms must have transparent policies and appeal processes in place. It‘s not enough to simply unleash AI with no human oversight or accountability.
Ultimately, combating online sexism requires a multi-pronged approach of community guidelines, user controls, human moderation and judicious application of ML. By keeping ethics in mind at every stage of the data science process – from problem definition to deployment – we can hopefully mitigate the risk of bias and build technology aligned with the values of diversity, inclusion and free expression.
Conclusion
In this guide, we‘ve seen how to apply sentiment analysis techniques to identify sexist tweets. Starting from a raw dataset of tweets labeled as sexist or not, we:
- Cleaned the text data by removing noise and standardizing formatting
- Extracted numerical features from the text using bag-of-words, TF-IDF and word embeddings
- Trained several ML classifiers to predict the probability of a tweet being sexist
- Evaluated the models using F1 score and ideas for improvement
We achieved a best F1 score of 0.81 using an XGBoost model with TF-IDF features, showing the potential of ML to automatically detect online sexism. However, we must be cautious in applying these models and consider the ethical implications. Ultimately, it will take a combination of human and AI moderation to make social media a safe space for everyone.
The fight against sexism and misogyny online is far from over, but sentiment analysis gives us a powerful tool to help stem the tide of abuse. By focusing our technological innovation on fostering healthy, inclusive digital communities, we can hopefully make the Internet live up to its promise of giving everyone an equal voice. It‘s up to us as data scientists to wield our skills responsibly in service of this vision.