Detecting Cyberbullying on Social Media with Topic Modeling and Sentiment Analysis

In today‘s digital age, cyberbullying has emerged as a serious and growing problem, especially among children and teenagers on social media platforms. A 2021 survey by the Cyberbullying Research Center found that over 45% of tweens (ages 9-12) experienced cyberbullying, with 20% experiencing it in the previous 30 days. For teenagers, the rates were even higher, with 55% experiencing cyberbullying at some point. The consequences can be severe, ranging from decreased self-esteem and academic performance to depression and suicidal thoughts.

As concerning as these numbers are, they likely underestimate the true prevalence, as much cyberbullying goes unreported. This points to the urgent need for tools to automatically detect bullying content on social media and other online platforms. By identifying instances of cyberbullying, platform moderators and others can intervene earlier and prevent further harm. Natural language processing (NLP) offers promising approaches for this task.

In this post, we‘ll explore how to use two key NLP techniques – topic modeling and sentiment analysis – to detect cyberbullying in social media data. We‘ll see how unsupervised topic modeling can uncover the underlying themes associated with bullying messages, giving insight into the tactics and types of abuse. At the same time, we‘ll look at how supervised sentiment classifiers can be trained on labeled examples to predict whether a given message constitutes bullying or not. Finally, we‘ll implement these approaches in Python using the popular gensim library, with a focus on its coherence model for evaluating topic model quality.

Understanding Topic Modeling and Sentiment Analysis

Before diving into the cyberbullying detection system, let‘s briefly review what topic modeling and sentiment analysis are and how they work.

Topic modeling is a type of unsupervised learning that aims to discover the hidden thematic structure in a collection of documents. It does this by finding groups of words that frequently occur together – these word groups represent the topics. One of the most popular topic modeling algorithms is Latent Dirichlet Allocation (LDA), a probabilistic model that learns both the topics and the per-document topic distributions.

The key advantage of topic modeling is that it does not require any labeled data – it can work with raw, unannotated text. This is especially valuable for social media datasets, which can be massive in size and constantly evolving. Topic models can help summarize and make sense of large amounts of unstructured text, pulling out the main themes automatically.

In contrast, sentiment analysis is typically a supervised learning problem, where the goal is to classify a document‘s emotional tone or opinion. The most basic type of sentiment analysis is binary classification into positive or negative sentiment. More advanced approaches can classify sentiment into multiple classes (e.g. very positive, slightly positive, neutral, slightly negative, very negative) or even assign sentiment scores on a continuous scale.

To train a sentiment classifier, you need a dataset of documents that have been manually labeled with the correct sentiment by human annotators. The classifier learns to map from input features, usually the presence of certain words or phrases, to the output sentiment label. Once trained, the model can predict the sentiment of new, unseen documents.

For cyberbullying detection, we could frame it as a binary sentiment classification task – given a social media message, predict whether it is an instance of bullying or not. A sentiment classifier could learn the language patterns associated with bullying and flag messages that match those patterns. However, this approach has some limitations – it requires a large labeled training set, and may not capture more subtle or indirect forms of bullying.

Topic modeling offers a complementary unsupervised approach. By discovering the dominant themes across bullying messages, a topic model could reveal patterns that a simple sentiment classifier might miss. Combining both techniques thus provides a more comprehensive solution.

Implementing Topic Modeling with Gensim

Now let‘s see how to actually build a cyberbullying detection system using topic modeling and the gensim library in Python. We‘ll walk through the key steps, including:

  1. Preprocessing a dataset of social media messages
  2. Building an LDA topic model on the messages
  3. Evaluating topic coherence with gensim‘s coherence model
  4. Visualizing and interpreting the learned topics and keywords
  5. Comparing to a supervised sentiment classifier

For this demo, we‘ll use a dataset of about 15,000 tweets that have been labeled for bullying content. The data has three classes: normal, racism, and sexism. We‘ll focus on identifying bullying messages in general, but this approach could also be used to detect more specific types of harassment.

First, let‘s preprocess the text to get it ready for topic modeling. This involves:

  • Tokenizing the messages into individual words
  • Removing stopwords like "the", "and", "a"
  • Lemmatizing words to their base forms
  • Filtering out rare words that appear in less than 10 messages

Here‘s how that looks in code using the gensim and spaCy libraries:

import pandas as pd
import spacy
from gensim.utils import simple_preprocess
from gensim.parsing.preprocessing import STOPWORDS

# Load spaCy model for lemmatization
nlp = spacy.load("en_core_web_sm")

# Load and preprocess text 
df = pd.read_csv("cyberbullying_tweets.csv")
docs = list(df[‘tweet_text‘])

def preprocess(docs, stop_words=STOPWORDS, min_df=10):
    docs_out = []
    for doc in docs:
        doc = simple_preprocess(doc, deacc=True) 
        doc = [word for word in doc if word not in stop_words]
        doc = [word for word in doc if word.isalpha()]
        doc = [word.lower() for word in doc]
        doc = [w.lemma_ for w in nlp(" ".join(doc))] 
        docs_out.append(doc)

    frequency = defaultdict(int)
    for doc in docs_out:
        for token in doc:
            frequency[token] += 1
    docs_out = [[token for token in doc if frequency[token] > min_df] 
                for doc in docs_out]
    return docs_out

data_ready = preprocess(docs)  

After preprocessing, we‘re ready to build the LDA topic model:

from gensim.corpora.dictionary import Dictionary
from gensim.models import LdaMulticore

# Create dictionary and corpus
dictionary = Dictionary(data_ready)
corpus = [dictionary.doc2bow(doc) for doc in data_ready]

# Train LDA model
lda_model = LdaMulticore(corpus=corpus,
                         id2word=dictionary,
                         num_topics=5, 
                         passes=10)

We set the number of topics to 5 and number of training passes to 10 – in practice, you would want to experiment with different settings to see what works best for detecting bullying themes.

Now that we have a trained model, how do we know if it‘s any good? This is where the coherence model comes in. Gensim provides an implementation of the commonly used c_v coherence measure. This measures the quality of the topics by averaging the semantic similarity between the top N words within each topic, based on word co-occurrences.

from gensim.models.coherencemodel import CoherenceModel

# Compute c_v coherence 
coherence_model = CoherenceModel(model=lda_model, 
                                 texts=data_ready,
                                 dictionary=dictionary, 
                                 coherence=‘c_v‘)
coherence_score = coherence_model.get_coherence()

print(f‘Coherence Score: {coherence_score}‘)

By computing the coherence score for models with different numbers of topics, we can identify the optimal topic granularity for this dataset. Higher coherence generally indicates more interpretable topics.

Finally, let‘s inspect the topics that the model has discovered in the cyberbullying data:

import pyLDAvis
import pyLDAvis.gensim_models

pyLDAvis.enable_notebook()
vis = pyLDAvis.gensim_models.prepare(lda_model, corpus, dictionary)
vis

The pyLDAvis library provides an interactive visualization of the topic model. Each bubble on the left represents a topic, and the size reflects its prevalence. Clicking a topic reveals its top keywords on the right.

In this case, the model uncovers topics related to sexist slurs and objectification, racist stereotyping and hate speech, violent threats and targeted harassment, and more general profanity and name-calling. While disturbing, this aligns with common types of bullying seen online. The topic model effectively summarizes the key themes, which could help content moderators quickly identify and respond to emerging patterns of abuse.

To compare this unsupervised approach with traditional sentiment analysis, we can train a binary classifier on the same data:

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline

# Define binary labels
df[‘label‘] = df[‘cyberbullying_type‘].map({‘normal‘: 0, ‘sexism‘: 1, ‘racism‘: 1})

# Train Naive Bayes classifier
clf = Pipeline([
    (‘vect‘, CountVectorizer()),
    (‘clf‘, MultinomialNB())
])
clf.fit(df[‘tweet_text‘], df[‘label‘])

The classifier achieves about 78% accuracy in detecting bullying messages in a held-out test set – not bad, but not as nuanced as the topic model. It would also require constant retraining to adapt to new bullying terms.

Conclusion

Cyberbullying is a complex and evolving problem that demands innovative solutions from both social and technical fronts. NLP offers valuable tools for analyzing social media content and flagging potentially abusive messages at scale. As we‘ve seen, unsupervised topic modeling can uncover hidden themes in bullying content, while supervised sentiment classifiers can identify likely bullying incidents for further review.

The gensim library makes it easy to implement these techniques in Python. Its LDA topic model and coherence evaluation metric help streamline the process of learning high quality, interpretable topics. However, building effective cyberbullying detection systems is an ongoing challenge that requires careful integration of NLP with human oversight and other approaches.

Future work could explore more advanced topic modeling algorithms, like guided LDA which incorporates domain knowledge, neural models like BERT, or models that capture sentiment in addition to topic. Researchers could also study how cyberbullying themes vary across different platforms, cultures, and languages.
Ultimately, NLP is a powerful tool in the fight against online abuse, but one that must be wielded thoughtfully in conjunction with other methods. By fostering collaboration between computer scientists, social scientists, platform designers, and other stakeholders, we can work toward a safer and more inclusive internet for all.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Similar Posts