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

La Paz. Deep Learning. 2021 According to the United Nations, hate speech is defined as “All forms of expression that share, encourage, justify or promote humiliation, contempt,…

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

This archive includes automatic translations. Audio, video and text inside images remain in their original language.

La Paz. Deep Learning. 2021

According to the United Nations, hate speech is defined as “All forms of expression that share, encourage, justify or promote humiliation, contempt, stigmatization or threat against a person or group such as women and girls.”

Currently, we are experiencing a wave of hate speech in various areas and towards different minorities. For example, after the Euro 2020 final, a wave of racist attacks on social networks against players from the English section after having missed penalties. In particular, the use of the Internet for the propagation of this type of attacks stands out thanks to the fact that it provides anonymity, distancing, absence of content regulations, among others.

Social networks and other Internet platforms have some automatic mechanisms to detect hate speech. These tools have become more relevant in the face of various events that have triggered the proliferation of content with hate speech. The following graph shows how hate speech increased during the pandemic year, based on hate messages removed by Facebook:

Figure 1:Number of posts with hate speech deleted on Twitter. Source: https://es.statista.com/grafico/21710/publicaciones-de-discurso-del-odio-eliminadas-por-facebook/

However, these tools are not available to any citizen who would like to analyze content to determine the existence of hate speech. It is for this reason that we decided to build a method that would serve as a tool or basis for the construction of technologies that could help in the detection of this type of comments and thus be able to stop its 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 Online Violentometer. 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 thousand 332 records
  • 2 columns:
  • Text: Tweet text (does not contain handlers).
  • 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 distribution of the data set (Figure 2) shows that there are fewer records of category 1 (hate speech). This problem particularly affects Deep Learning models, so it was necessary to apply techniques that would allow us to generate new (synthetic) examples to have a number of records close to that of category 2.

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 (Figure 4) containing pre-trained Deep Learning models for various purposes such as text classification, information extraction, translation, among others. BETO, which is a model with the architecture BERT trained with a corpus in Spanish, to obtain the vector representation of the text (embeddings). In addition, two additional layers were used: 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 violentometer-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 since this is commonly used in text classification problems in addition to taking into account the following variations:

  • Model: Variation of BETO (cased and uncased).
  • Epochs: Number of iterations in training.
  • Preprocessed: Text preprocessing that includes operations such as removing emojis, digits, stopwords, among others.
  • Sample tailcoat: 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

As we can see, the best model (BETO-Uncased) did not require text preprocessing in addition to the fact that it was necessary to generate a significant amount of synthetic data. This model obtained the best value (0.842) of the F1 metric. We want to highlight that this result is much better than what we had previously obtained using the Random Forest model.

Predictions Explanation

We use the API Lime for a detailed explanation of the model predictions. Lime is capable of explaining any classification model that makes predictions of one or more classes. In order to use Lime it is necessary to create a function that returns a Numpy array with the probabilities of each of the classes. Lime shows the weights of each of the text words in the prediction. Figure 4 contains the explanation of the prediction of a text:

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 used GitHub Actions to deploy it (continuous integration) on AWS. The following image shows the prototype:

Figure 5: Online Violentometer 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 more examples of hate speech that are directed at different minorities (women, religions, opinions, among others) to obtain a more robust model.
  • 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!

The mission of Saturdays.ai is to make artificial intelligence more accessible (#ai4all) while carrying out projects for good (#ai4good). The workshops we carry out are part of the program AI 4 Schools so that anyone “learn by doing” AI regardless of their background or starting point.

Explore our AI master’s programme at https://saturdays.ai/master-ia-online/

If you want to learn more about artificial intelligence join our community through this link or visit us on our website www.saturdays.ai Join us!

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 history of the Saturdays.AI community. Originally published on Oct 4, 2021.

← More community stories