Abstract

The following project devises a method of detecting hate speech on Twitter, using deep-learning algorithms. In this regards, the project proposes the Bidirectional Encoder Representations from Transformers (BERT), which is the state-of-the-art method for the majority of NLP-associated tasks. We contrast the results of our proposed BERT architecture with the more traditional Support Vector Machines approach, trying to highlight the improved accuracy offered by deep-learning algorithms. Additionally, we try to optimise the SVM by using complex pre-processing methods, which lead to accuracy levels similar to that of the unoptimised BERT algorithm. This allows us to ultimately see whether BERT’s improved accuracy remains significant even after the pre-processing.

Introduction

Background

Twitter is considered by people to be one of the most popular social-media platforms used currently in the English-speaking world, and it has significantly improved the capacity in which normal people communicate and share opinions, ideas, and sources of information. Nevertheless, given the low levels of active moderation, the very high number of users and the democratic nature of speech transmission, Twitter has come to be exploited by malintended individuals. In this sense, the dissemination of offensive, aggressive and even hateful content has become a staple of Twitter, with policymakers trying to combat this wave of digital aggression.

This untamed distribution of hateful content is indeed one of the biggest problems concerning the content present in the digital world. It has the potential of negatively affecting disenfranchised groups, who already suffer due to the stereotypes in society. However, regardless of the policies that need to be adopted to prevent these development, and regardless of the ethical discussions on the limits of free speech, existing hate speech needs to be detected on Twitter, in order for any measure to be functional. Given the scale of the phenomenon on social media, the best solution remains the automatic detection of hate speech.

Once again, this process is not straightforward .The automatic detection of hate speech is a convoluted and challenging task due to disagreements existing over the different hate speech definitions. Therefore, some content published on Twitter might be hateful to some individuals and not to others, based on the definition of hate speech they employ. Several academic papers and other research projects that rely on traditional machine learning approaches (such as bag of words, and word and character n-grams) have been published in the last decade. Recently however, the proffered methodological setups involved deep-learning algorithms, such as LSTM-based RNNs, or BERT.

Research scope

In this project we deploy deep-learning methods for detecting hate-speech on Twitter, focusing on the English language. We make use of existing implemen tation of BERT, an architecture based on the Transformers model. To precisely characterise the efficiency and effectiveness of our implementation of BERT, we use numerous automatic metrics that allow us to see both if the model identifies hate-speech, and if it avoid conflating hate-speech with non-hateful speech. In this sense, our project is interested in both false positives and false negatives, which is an approach rarely seen in the field.

In addition to our implementation of BERT, we also develop a more traditional “shallow” approach, in the form of SVMs. In this regard, we are able to actually see the improvement of using the state-of-the-art in the field of NLP.

One novel approach brought by our project is that in the case of the SVM, we use complex and comprehensive pre-processing methods that improve as much as possible the prediction power of the model. As such, we are able not only to compare SVMs with BERT, but to compare the best possible SVMs with the latter deep-learning architecture.

Proposed Method

SVMs

In terms of a baseline method, we chose to use Support Vector Machines (SVMs). Given a training dataset, each observation labeled as to belong to one of two general categories, an SVM algorithm builds a model that assigns all the observations from a testing dataset to one category or another, making it, therefore, a non-probabilistic binary linear classifier.

BERT

Bidirectional Encoder Representations from Transformers (BERT) is a Transformer-based machine learning technique for natural language processing developed by Google in 2019. In turn, Transformers are encoder-decoder architectures developed in 2017 The encoder consists of a set of encoding layers that processes the input iteratively one layer after another and the decoder consists of a set of decoding layers that does the same thing to the output of the encoder. The introduction of Transformers, and specifically BERT, has led to the replacement of previous models such as LSTM-based RNNs.

As opposed to the majority of the architectures that have preceded BERT, this algorithm does not make use of directional models, which read the text input sequentially, either left-to-right or right-to-left. BERTs encoder reads the entire sequence of words at once, making it bi-directional—simultaneously left-to-right and right-to-left.This particular construction feature allows the BERT model to learn the context of a word based on all of its surroundings, regardless of position in relation to the word.

Experiments

Data: Given the scope of our study, we made use of dataset provided by Davidson et al. (2017), which contains tweets that are characterized as being either hateful, offensive or neither of these. As hate speech is a contested term, the authors define it as language that is used to expresses hatred towards a targeted group or is intended to be derogatory, to humiliate, or to insult the members of the group. This definition avoids a narrow focus on only the most extreme usage of the English language, as accepting the existence of instances of offensive language where people use terms that are highly offensive in a qualitatively different manner.

The dataset provided already annotated data, process which was fulfilled manually by CrowdFlower workers. Three or more people coded each tweet where the intercoder-agreement score provided by CF is 92%. Davidson et al. (2017) decided to assign labels to each tweet based on the majority decision, which given the high intercoder-agreement score was easily achieved. The dataset we used is composed of 24,802 labeled tweets. Given the focus of our study, specifically the automatic detection of hate speech, and not offensive language, we dropped tweets that were labelled as being offensive and not hateful. This action generates a final dataset consisting of 1,430 tweets labeled as hate speech and 4,163 tweets labeled as neither hateful or offensive.

The large number of unnecessary features, as well as the typical way in which language is used to generate tweets implies a need for a serious and comprehensive process of pre-processing. The pre-processing leads to clean textual data that is easily transformed to numeric arrays. Corollary text pre-processing offers the opportunity for the model’s optimality to be tested. Additionally, this will further allow us to improve the accuracy of “shallow” machine-learning algorithms.

Evaluation method: The evaluation of our models, both the SVMs and the BERT, has been achieve through a diverse set of automatic metrics. First and foremost, in both cases we have computed the accuracy of the model, namely the number of tweets in the testing dataset that are correctly labelled by our proposed models. This is the primary metric of evaluation for this type of NLP task, and allowed us to compare our results with the ones already existing in the literature. However, given the additional task we have performed, of designing and optimal “shallow” model in order to see how well they perform contrasted with deep-learning architectures, we also uses additional metrics focused on the SVMs.

Results:

The table above shows the results of our multiple models. As such, we describe the overall accuracy of the SVMs (both with and without the pre-processing mechanisms that we have described in the steps above), and the BERT. One general observation is that BERT achieves results that are close to the best results in the literature, while the versions of SVM that have used pre-processing achieve an accuracy above similar “shallow” architectures we have identified.

Comment on quantitative results: First and foremost, we can infer that using deep-learning methods such as BERT outperforms even the best attempts made using “shallow” machine learning algorithms. The differences in accuracy are large, and when translated into policy, would imply significant differences regarding the ease of implementation.

Secondly, the results of the SVMs using complex pre-processing methods are significantly higher than expected. This also implies that the increase in accuracy when contrasting BERT and the pre-processed SVMs is significantly smaller than when comparing BERT and basic SVMs. While the results of the SVMs are nowhere near to the state-of-the-art results of BERT, this is the case for the English language. When trying to automatically predict hateful content in other languages, in the absence of more complex pre-trained Transformer models or in the absence of significant GPU resources, it might be sufficient initially to use basic SVMs, as long as the pre-processing step has been thorough.

Thirdly, the results of the BERT model are rather static and independent of the pre-processing methods used. We have tried multiple experiments using differently pre-processed versions of our original dataset, and the differences in accuracy have not been significant. This is to be expected, but it’s important to acknowledge this in practice, as it can prevent people from wasting time on tasks that do not improve the process of automatic detection of hateful speech on social media.

Analysis

In order to analyse the efficiency and effectiveness of our approach to automatic hate speech detection on Twitter, we need to properly understand the baseline model and how the results of the baseline model appear based on our expectations. Looking at the figure below, we see that our baseline method correctly predicted about 77 percent of tweets that related to hate speech. On the other hand, 98 percent of tweets that did not relate to hate speech (classified as neutral) were predicted correctly. In this light, about 2 percent of tweets that were related to hate speech were classified as neutral, while about 23 percent of neutral tweets were classified as hate speech. Ultimately, this means that the problem with the SVMs (both with and without pre-processing) was that it had serious trouble correctly identifying and labelling neutral tweets. While this is not absurd, and was to be expected based on the literature review, it’s still problematic that the the prediction power of the baseline model lacks balance.

This is even more of a reason for using BERT. In the case of BERT, having such a high accuracy implies that we have, regardless of our optimisation strategy, statistically less chances of reaching an unbalanced prediction pattern. Therefore, by using the latest versions of BERT, and building on previous results, we are able to achieve an accuracy of 91.6% distributed uniformly across the categories studied. This was achieved both by balancing the original dataset, and by leaving the dataset unbalanced, proving once again that the BERT architecture is remarkably flexible and not pretentious at all.

The figure above show that the accuracy cannot increase constantly, and after a number of epochs it’s a matter of very small improvements, if any. This means that we would need, for better performance, a more expansive dataset, or another pre-trained version of the model that is more used with dealing with Twitter. Both extensions are worht discussing in future endeavours.

Conclusion(s)

Our project proves that while the automatic detection of hateful content on social media remains a complicated tasks, and current models are imperfect, we can confidently tackle this challenge. By using the most advanced deep-learning methods, we are able to design models that reach accuracies above 90%, in line with the best results present in the literature. Additionally, we show that deep-learning is superior to other approaches that might be considered by researchers, such as SVMs. Nevertheless, pre-processing can increase the accuracy of the SVM algorithm, which could be useful for researchers working in languages other than English.