Detection of Hate Speech in Social Networks using Transformers and Natural Language Processing

Violentómetro Online explores Spanish-language abuse detection using BETO, data augmentation and local explanations of model predictions.

A learning project shared by the community. The article documents its team, methods and results; it does not establish that the prototype is ready for real-world use.

Detection of Hate Speech in Social Networks using Transformers and Natural Language Processing

La Paz. Deep Learning. 2021

The United Nations strategy describes hate speech as communication that attacks people or groups, or uses derogatory or discriminatory language about them, because of identity factors such as religion, ethnicity or gender. This is a working definition, not a universal legal definition.

In 2021, several events highlighted the spread of hate speech. For example, the Euro 2020 final was followed by a wave of racist attacks on social media against England players who had missed penalties. Anonymity, distance between participants and moderation challenges can help such abuse spread.

Platforms use automated systems to detect this content. The following chart shows posts removed by Facebook during the pandemic. These figures measure moderation actions; on their own, they do not establish how much hate speech increased.

Figure 1: Hate-speech posts removed by Facebook. Source: https://es.statista.com/grafico/21710/publicaciones-de-discurso-del-odio-eliminadas-por-facebook/

Platform moderation tools are not always available to individuals who want to analyse content themselves. We therefore set out to develop an accessible method that could support tools for detecting these comments and limiting their spread.

In this article, we describe how we apply Deep Learning and Natural Language Processing (NLP) methods for the detection of hate speech in Spanish-language Twitter comments. This work is a continuation of the project Violentómetro Online. In this project we had a first approach to the problem of detecting hate speech against women through the use of classic Machine Learning techniques.

Solution

In this section, we describe the methodology (Transformers and Data augmentation), as well as the data we used during the completion of this project. We also detail the model parameters and the interpretation techniques we use to understand its predictions.

Data Set

MEX-A3T: Fake News and Aggressiveness Analysis is an event organized by the NLP community in Mexico to detect fake news and texts with hate speech. The organizers shared with the team the training data set consisting of Tweets in the Spanish language. The set has the following characteristics:

  • 7,332 records
  • 2 columns:
  • Text: Tweet text, with user handles removed.
  • Category:
  • 1: Contains general hatred (2110 records)
  • 0: Does not contain hate (5222 records)

Figure 2: Distribution of the MEX-A3T data set

Data augmentation

The dataset distribution (Figure 2) shows fewer records in category 1 than in category 0. To reduce this imbalance, we generated synthetic examples of the minority class.

The operations of data augmentation that were applied to 50% of hate speech messages in the dataset to obtain more examples are the following:

  • Synonym Replacement: Replacing some words with their synonym.
  • Random Deletion: Deleting some words randomly with probability p.
  • Random Swap: Random word exchange.
  • Random Insertion: Inserting a synonym in a random position n.

Model

To create the model we use the library Transformers of Hugging Face which provides pretrained models for tasks such as text classification, information extraction and translation. We used BETO, which is a model with the architecture BERT trained on a Spanish-language corpus, to obtain vector representations of the text (embeddings). We then added two components: a multi-layer bi-directional GRU and another linear one that obtains the predictions. It is possible to use other architectures instead of multi-layer bi-directional GRU, but for this project we decided to use this one since it is more computationally efficient than LSTM.

Figure 3: Transformers Architecture. Source: https://lilianweng.github.io/lil-log/2018/06/24/attention-attention.html#transformer

Note: The complete code can be consulted in the project repository violentometro-online.

Best Parameters

We tested different variations of BETO to obtain the best training parameters for the final model. We evaluate each model using the F1 metric which is widely used in text classification. We compared the following settings:

  • Model: Variation of BETO (cased and uncased).
  • Epochs: Number of complete passes through the training data.
  • Preprocessed: Text preprocessing that includes operations such as removing emojis, digits, stopwords, among others.
  • Sample frac: Proportion of synthetic examples in the data set.

The following table shows the models with which we obtained the best results:

Table 1: Results of the best model parameters

The best model in our tests, BETO-Uncased, achieved an F1 score of 0.842 without the preprocessing described above and with substantial synthetic data augmentation. This exceeded our previous result with Random Forest.

Explaining the predictions

We use the API Lime to estimate which parts of a text influenced an individual prediction. We created a function returning a NumPy array of class probabilities. LIME estimates words’ local contributions; it does not provide a complete account of the model’s internal workings. Figure 4 shows an example:

Figure 4: Example of explanation of the prediction of a text with hate speech

It can be seen that in the model prediction, more weight was given to the word that appears first in the list in addition to the text representation of the emoji.

Web Application

We developed the prototype of a web application with the model that obtained the best results. This web application can be consulted here. The prototype was developed with the framework Streamlit and we used GitHub Actions for continuous integration and deployment to AWS. The following image shows the prototype:

Figure 5: Violentómetro Online web application

Users can enter any text in the application. When users press the Ctrl+Enter keys, the application (model) returns the following categories:

  • 1 = Contains hate speech
  • 0 = Does not contain hate speech

Conclusions

The goal of our project is to develop an effective method to automatically detect Spanish-language verbal violence that occurs in online discourse. With this project we were able to create a method that is quite reasonably effective using advanced techniques such as Deep Learning and data augmentation, in addition to being built with free and open source tools. An API was also used that allowed us to understand the model predictions.

Among the next steps of our project we can highlight the following:

  • Use other variants of the Spanish language.
  • Collect examples targeting different groups, for instance on grounds of gender, religion or origin, to evaluate and improve model robustness.
  • Incorporate a feedback mechanism for users of the web application.

Acknowledgments

We would like to thank María José Díaz-Torres, Paulina Alejandra Morán-Méndez, Luis Villasenor-Pineda, Manuel Montes-y-Gómez, Juan Aguilera, Luis Meneses-Lerín, authors of the MEX-A3T Dataset and the article Automatic Detection of Offensive Language in Social Media: Defining Linguistic Criteria to build a Mexican Spanish Dataset. We also want to thank the team that made Saturdays.AI La Paz possible for all their work and dedication in organizing the program.

Sources

Members

SophieB

Project presentation: DemoDay

More artificial intelligence!

Saturdays.AI makes AI more accessible through shared learning and projects with a social purpose. Our motto is AI4ALL IS AI4GOOD.

Explore the available courses

Meet the Saturdays.AI community

FROM CURIOSITY TO PRACTICE

Your next step starts here.

Put an idea into practice or find people to continue learning with.

Receive new stories in your RSS reader ↗

A story from the Saturdays.AI community. Originally published on Oct 4, 2021.

← More community stories