Hate Speech Detection — Automated Classification of Harmful Text

Machine Learning
Type
NLP text-classification task

What does it do?

Hate speech detection is a natural-language-processing task that automatically identifies hateful, offensive, or harmful text on social media and online platforms.

Davidson et al. (2017) identified separating hate speech from other offensive language as the key challenge, noting that earlier supervised approaches had failed to distinguish the two categories.

The intuition behind the method

Online platforms generate far more content than any human moderator can read, and harmful posts hide among ordinary ones. The aim of hate speech detection is to teach a model to flag text that targets or attacks people, while distinguishing it from language that is merely rude or offensive. The model learns from examples humans have already labelled, then applies those learned patterns to new posts so that harmful content can be surfaced at scale.

History

Davidson et al. (2017) framed automated hate speech detection on social media as a multi-class problem and presented the separation of hate speech from other offensive language as its central challenge. Fortuna and Nunes (2018) later surveyed the field, which they described as recent in computer science, organising its core algorithms, methods and features and discussing the difficulty of defining hate speech across platforms.

When to use it

Use hate speech detection when you have text from social media or online platforms and a labelled dataset that is large enough to train or fine-tune a classifier. It suits classification goals where the categories (hate speech, offensive language only, neither) can be defined clearly enough for annotators to apply them consistently. Plan for the skewed class distribution from the start, and complete an ethics and privacy review before working with user-generated content. Do not use it when labels cannot be obtained reliably, because a classifier trained on inconsistent annotations will not reflect any stable definition of hate speech.

Limitations and considerations

  • Labels depend on human annotation of text, which is costly and can be inconsistent; Davidson et al. (2017) relied on crowd-sourced labels.
  • Davidson et al. (2017) collected tweets using a crowd-sourced hate-speech lexicon, and tweets without explicit hate keywords were harder to classify, so performance on implicit hate may be lower than headline figures suggest.
  • Hate speech is defined in many different ways across platforms and contexts, as Fortuna and Nunes (2018) discuss, so a model's categories may not match the definition a new user needs.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Surfaces potentially harmful posts across large volumes of text that manual review cannot cover.
  • Separates hate speech from offensive language as distinct classes, following the three-category design Davidson et al. (2017) used, rather than treating all abusive text as one category.
  • Accepts different representations, from engineered features for feature-based classifiers to contextual embeddings from transformer models, so the same task can be pursued with simpler or more context-sensitive models.

Applications

  • Content moderation on social media and online community platforms.
  • Flagging offensive or harmful comments for human review at scale.
  • Research into the prevalence and dynamics of online hate and abuse.
  • Trust-and-safety monitoring for organisations hosting user-generated content.

How it works

How it works

1

A labelled dataset is the starting point. Davidson et al. (2017) crowd-sourced labels for a sample of tweets collected with hate-speech keywords, assigning each to one of three categories: hate speech, offensive language only, or neither. The class distribution is typically skewed, since genuine hate speech is usually a small share of posts, and this imbalance shapes every later choice.

2

The raw posts are cleaned and standardised (normalising casing, stripping noise such as URLs and handles, and tokenising the text into analysable units) so that features stay comparable across documents drawn from messy social-media sources.

3

The cleaned text is turned into features a classifier can read, either as engineered features for a machine-learning model or as contextual embeddings produced by a transformer. This representation is what carries the linguistic signal the model will learn from.

4

A classifier assigns each document to one of the classes. Davidson et al. (2017) trained a multi-class classifier over the three categories so that hate speech is kept separate from offensive language. Practical approaches include feature-based machine-learning models and fine-tuned transformer models, which draw on the surrounding text rather than isolated terms.

5

Predictions are scored against held-out labelled data, not the data used for training, and the split must keep the hate-speech class represented. Build a confusion matrix for the three classes. For a given class, precision = TP / (TP + FP) and recall = TP / (TP + FN), where TP, FP and FN are the true positives, false positives and false negatives for that class. Their harmonic mean, F1 = 2 × precision × recall / (precision + recall), summarises the trade-off in one figure. Because the classes are imbalanced, report these metrics per class, with hate speech given its own row rather than pooled with offensive language, and treat overall accuracy as a secondary measure. Inspect misclassifications before the model is deployed.

Common pitfalls

  • Optimising overall accuracy on imbalanced labels hides poor recall on the rare hate-speech class. Report precision and recall for the minority class instead.
  • Training only on posts selected by hate keywords may teach a model to rely on those keywords. This is a plausible risk rather than a result reported by Davidson et al. (2017). What they did observe is that tweets without explicit hate keywords were more difficult to classify, so implicit hateful posts are a known weak point.
  • Collapsing hate speech and offensive language into one class removes the distinction that Davidson et al. (2017) identified as the key challenge, so the model cannot separate the two categories.
  • Applying a model trained on one platform or language to another without re-labelling risks degraded performance, because the definition and expression of hate speech vary across platforms and contexts (Fortuna & Nunes, 2018).
  • Skipping an ethics and privacy review when processing user-generated content risks both harm to users and non-compliance.

Frequently asked questions

What is the difference between hate speech and offensive language?

They are distinct classes in the three-category scheme Davidson et al. (2017) used: hate speech, offensive language only, and neither. Davidson et al. found that racist and homophobic tweets were more likely to be classified as hate speech, while sexist tweets were generally classified as offensive. Conflating the two classes blurs the distinction that Davidson et al. identified as the key challenge, which is why they treated the task as a multi-class problem.

How much labelled data do I need?

There is no fixed number that guarantees good performance. You need enough labelled documents to train or fine-tune a classifier, and because hate speech is usually the minority class, you need enough examples of it for the model to learn the category rather than ignore it. Check performance on held-out labelled data before relying on any model.

Why is class imbalance such a concern?

Hate speech is usually uncommon, so most posts are neither hateful nor offensive. A model can score high overall accuracy by predicting the majority class while missing much of the harmful content. Evaluate precision and recall on the rare class, not accuracy alone.

Do I need to consider ethics and privacy?

Yes. The task processes user-generated content and produces decisions about people's speech, so an ethics and privacy review should be carried out before deployment. Annotator subjectivity and the effect of the model's errors on users should be considered as part of that review.

Related methods

Sources

  1. Davidson, T., Warmsley, D., Macy, M. & Weber, I. (2017). Automated Hate Speech Detection and the Problem of Offensive Language. ICWSM, 11(1), 512-515.

    Open DOI reference
  2. Fortuna, P. & Nunes, S. (2018). A Survey on Automatic Detection of Hate Speech in Text. ACM Computing Surveys, 51(4), 1-30.

    Open DOI reference