Hate Speech Detection On Social Media Using
LSTM Deep Learning Algorithm
Ali Mohammed Barakat
Mechatronics Engineering Ramy Hossam Saad Sarah Ahmed
October University for Modern Mechatronics Engineering Mechatronics Engineering
Sciences and Arts October University for Modern October University for Modern
Al Qalyobia, Egypt Sciences and Arts Sciences and Arts
ali.mohamed39 @msa.edu.eg Cairo, Egypt Cairo, Egypt
[email protected] [email protected] become necessary for developers and researchers in recent
years to propose models based on machine learning and deep
Abstract learning techniques. However, because much hate speech-
related content is still dynamically changing and floating
The spread of hate speech on social media sites like Twitter across the network, some existing models do not meet the
presents serious societal issues that call for the creation of requirements. This prompted us to create a model that would
effective and precise automated detection systems. This article
presents a unique method for the detection and categorization capture the greatest number of hate speech-related posts
of hate speech on Twitter by using Long Short-Term Memory possible.
(LSTM) networks, a particular kind of recurrent neural
networks. In our dataset, a wide range of potentially damaging For this report, the aim is to create a deep learning model for
material is addressed by tweets classified into three separate classifying social media content as either hateful or normal
classes: 'hate speech' (label 0), 'offensive' (label 1), and inspired by the main paperwork introduced by (Dr. S. Ahmad
'neither' (label 2). and Dr. K. Prasad, (September 2023)). Twitter was chosen as
a platform where detection of hate speech was done. To train
Because of its innate capacity to handle sequential data, the
the models, an open-source dataset was collected and made
LSTM model excels at comprehending the subtle language
patterns and contextual details typical of hate speech. The
publicly available. The models focused are Conventional
LSTM network can successfully capture the semantic linkages Neural Network (CNN) combined with Long Short-Term
and emotional undertones encoded in tweets, which are critical Memory (LSTM) and Bi direction Long Short-Term
for differentiating between hostile, offensive, and innocuous Memory (BiLSTM).
text, by assessing word sequences instead of individual words The CNN model is good for extracting basic features
alone.
through embedding layer convolution layer, identifying
As part of our technique, we preprocess the tweet corpus informative features from text, while the LSTM model is
using vectorization, normalization, and tokenization to get the designed to capture subtle patterns and regularities in
best possible neural network performance. Using this ready- sequences, dealing with the vanishing gradient problem. The
made dataset, the LSTM architecture is then trained and BiLSTM is like the usual LSTM but it can remember the
validated, with an emphasis on fine-tuning model parameters information from both directions.
to maximize detection accuracy and reduce false positives and
negatives. The following sections illustrate the dataset collected for
the research and types of processes go through, the described
Standard measures like accuracy, recall, and F1-score are the methodology used, including the architecture of our
used to systematically assess the performance of the LSTM chosen model, the nuances of the training process, and the
model, with a focus on highlighting its capacity to distinguish
metrics used to evaluate its performance.
minute variations between closely related classes. The model's
resilience and dependability are shown by the findings, which
beat conventional machine learning techniques and provide a
viable option for real-time hate speech identification on social
media platforms.
By giving social media platforms a framework to actively II. RELATED WORKS
monitor and stop the spread of harmful information while
respecting the subtleties of free speech and context-dependent In order to improve future works of such algorithms used for
expressions, our study adds to the continuing efforts to prevent identifying texts that include offensive or hate speech, a
online hate speech. great deal of research has been done on machine learning
Keywords: LSTM, Hate Speech Detection, Twitter, Deep and deep learning methodologies, as well as a comparison of
Learning, NLP, Social Media Monitoring the findings.
I. INTRODUCTION S. Ahmad and K. Prasad (September 2023) shown that the
On last couple of decades people can now express automated machine learning algorithms for hate speech
themselves anytime and anywhere thanks to the rise of social identification are still relatively new, with just a few review
media, but social media platforms are gradually becoming publications on the subject. Numerous machines learning
platforms for spreading hate, even violence, threatening techniques, including TF-IDF, dictionaries, N-gram,
speech, and even a trend of flooding. For these reasons, it has sentiment analysis, template-based approach, part of speech,
Bag of the word, and rule-based approach, have been point. This two-way technique improves the model's textual
studied in earlier research. These studies do have some data analysis capabilities.
limitations, however, since they did not consider more
sophisticated methods like ensemble approaches and deep A research paper by (Kumar et al. (2021)) that focuses on a
learning. classification task involving code-mixed text in Meitei,
Additionally, a suggested method with improved Bangla (Indian variety), Hindi, and English. The task
performance has been shown by Abhijeet Verma et al. involves three levels of classification: aggression, gender
(2022), and it paves the way for more study into enhancing bias, and communal bias. Each level has specific labels or
and perfecting the hate speech recognition algorithm. classifications. The authors explore different approaches to
Finding annotated training sets on Twitter, creating many address the task, including treating it as three separate
classification models, and assessing each model's classification tasks, a multi-label classification task, or a
performance across several platforms are the research's main structured classification task.
achievements. The accuracy is improved by 5% using the
suggested approach. They also suggested a method that There is also provided paperwork outlines a research effort
shows better prediction accuracy by fusing CNN and LSTM focused on hate speech detection on social media,
in a two-layer architecture. An embedding layer, specifically Twitter, utilizing machine learning approaches,
convolutional layers, activation functions, max-pooling, natural language processing (NLP), and fuzzy classification
dropout, LSTM layer, and a dense layer make up the techniques. The review by (Anagha Abraham et al. (2023)) is
architecture of the model. structured into two main sections: Fuzzy Classification and
Fuzzy Hypergraph.
To address this, they developed the use of Long Short-Term
Memory (LSTM) networks for the identification of hate The CNN+LSTM architecture, as presented by (Ziqi Zhang
speech. Recurrent neural networks (RNNs) such as LSTM et al. (2016)), is a comprehensive model designed for text
are capable of handling and forecasting time series data classification tasks. The architecture combines Convolutional
because they solve the vanishing gradient issue that Neural Network (CNN) and Long Short-Term Memory
conventional RNNs have. A cell, input gate, output gate, and (LSTM) layers to process and analyze sequential data, such
forget gate are all parts of the architecture that enable the as text messages. n the experiments, two types of word
network to preserve long-term dependencies and selectively embeddings are tested: one with randomly initialized weights
output pertinent data. that are learned during training (emb-learn) and another
where word embeddings are initialized randomly. Baseline
models, including linear SVM and modified versions of the
CNN+LSTM network, are employed and compared against
the proposed architecture.
The creation of a prediction model to recognize sexism,
aggressiveness, and hate crimes against communities in text
data spanning many languages is the subject of another
research. Interestingly, they address the difficulties caused
by code switching and mixing (Sandip Dutta et al., 2021).
The CNN-LSTM neural network combination used in the
model is well-known for its ability to capture both
sequential and global information. While the LSTM layer is
excellent at detecting minute patterns and regularities in
sequences, the CNN layer is good at recognizing useful
features.
LSTM Architecture III. METHODOLOGY
A reference by C. Paul and P. Bora (2021) indicated some of A. Algorithm
their dataset characteristics; they collected it from Kaggle,
an open-source website for AI algorithms. The pack i. Long Short-Term Memory (LSTM)
included labeled classes for hate speech (1) and non-hate
speech (0). It was important to observe the characteristics of One kind of artificial neural network used in deep
the used dataset samples and the preprocessing applied to it. learning is called Long Short-Term Memory
Stemming was used to address imbalances, and the dataset (LSTM). LSTMs belong to a wider class of neural
underwent processing to eliminate special symbols and networks called recurrent neural networks (RNNs),
convert it to lowercase. In order to provide a fair dataset, the in contrast to typical feedforward neural networks.
authors choose to increase sample the minority class in order They are perfect for applications in time series
to address the imbalance in the dataset. analysis, natural language processing, voice
recognition, and more because they are especially
The same authors also noted that by memorizing data in well-suited to learning from sequences of data.
both directions, Bi-LSTM overcomes the drawback of
conventional LSTMs. Bi-LSTMs perform backpropagation ii. Characteristics of LSTM
in both front and back directions, in contrast to LSTMs, Sequences: Process sequences of data
which only consider information traveled up to a certain efficiently is one of the distinguishing
characteristics of LSTMs. For example, structure, particularly the employment of gates and memory
word order matters when processing cells.
language since meaning may be greatly
influenced by context given by words that
come before and after.
B. Pre-processing
Memory cells: These are the building
blocks of Long Short-Term Memory A minimal pre-processing is done on a given tweet in order
(LSTM) networks. These cells are in to normalize its text as follows:
charge of retaining and transferring • take out the characters | :, ; &!? \
information between stages in a sequence, • Turn hashtags into words by normalizing them, for
which is crucial for comprehending the example, "#refugeesnotwelcome" to "refugees not
context of sequences. welcome." This is a result of the frequent use of these
hashtags in sentence construction. Dictionary-based lookup
Gates: The intricate gating mechanism of is used to divide these hashtags.
LSTMs regulates the information flow. • Using stemming and lowercase to minimize word
There are three kinds of gates in them: inflections
The input gate selects which new data to • eliminating all tokens with fewer than five document
add to the cell state. frequencies.
The forget gate decides which data from Because tweets are written in a colloquial style, the pre-
the cell state should be deleted. process reduces the vocabulary size of the dataset and
Output Gate Determines which data from partially resolves the sparsity in word-based feature
the cell state will be used to calculate the representations. Other options including maintaining word
output. inflections, switching to lemmatization from stemming, and
reducing document frequencies were also examined.
Managing Long-Term Dependencies: When However, our empirical research revealed that the
information from earlier in the sequence is required aforementioned procedure somewhat improves accuracy.
much later, traditional RNNs often struggle with
long-term dependencies. This is because of a Natural language processing (NLP)
phenomenon known as the vanishing gradient Natural Language Processing (NLP) is a field of artificial
issue, in which gradients—which are needed to intelligence (AI) that focuses on the interaction between
train neural networks—become very tiny and computers and human language. It sits at the intersection of
essentially stop the model from learning. Because computer science, artificial intelligence, and linguistics. The
LSTMs are expressly developed to solve this issue, goal of NLP is to enable computers to understand, interpret,
they perform better on jobs where long-term and respond to human language in a valuable way.
contextual information is essential.
i. Tokenization
iii. Applications of LSTM
Natural Language Processing (NLP): used for tasks When preparing textual data for Natural Language
such as text production, sentiment analysis, Processing (NLP) applications, such as hate speech
machine translation, and language modeling. identification, tokenization is an essential first step. It
Speech Recognition: For tasks such as speech-to- entails dividing material into smaller chunks, called
text, they are able to represent the temporal tokens, which are usually words or sentences. This
sequence of sounds. procedure is essential for converting unprocessed text
Time Series Prediction: Long Short-Term Memory into a format that machine learning models can read,
Banks (LSTMs) are useful for anticipating future understand, and interpret.
values of a series, such as stock prices or weather Text Segmentation: Tokenization divides a text
patterns. passage, like a tweet, into discrete components,
iv. Implementation or tokens, such as words or phrases. The
Since LSTMs are implemented in many deep phrase "This is hate speech," for instance,
learning frameworks, such as PyTorch and might be tokenized as ["This", "is", "hate",
TensorFlow, they are usable in a variety of "speech"].
contexts. Simplifying Analysis: Text may be processed
Selecting the number of LSTM layers and units in and analyzed more easily when it is tokenized,
each layer, the duration of the sequence, and the which lowers the text data's complexity. It is
kind of data to be processed are all common steps possible to consider each token as a distinct
in the design of an LSTM network. element and use different NLP algorithms on
it.
Enabling Feature Extraction: Recognizing
In conclusion, LSTMs mark a substantial improvement in certain words or phrases is often essential for
neural networks' capacity to handle sequential input. One of hate speech identification. These
the main challenges in many sequence learning tasks is that characteristics may be extracted from the text
they cannot learn from lengthy sequences without losing the via tokenization, and a machine learning model
context, which is made possible by their distinctive
trained to recognize hate speech can
subsequently be trained with them.
ii. Padding
In Natural Language Processing (NLP), padding is a
preprocessing procedure used to normalize text
sequence lengths. This includes applications such as
hate speech identification. It is often required for all
input sequences to have the same length when working
with textual data, particularly in neural networks, in
order to ensure consistent processing. But the length of
genuine language varies naturally. The technique of
padding involves truncating longer sequences or adding
additional "padding" tokens to shorter sequences in
order to ensure that all of the sequences in a dataset
have the same length.
Adding Padding Tokens: Adding a special
token, often indicated as <PAD>, to the end
(or sometimes the beginning) of sequences is
the standard method of padding them so that
they are all the same length. For instance, three
<PAD> tokens would be inserted to a phrase
that is only seven tokens long if the maximum
sequence length is set to ten.
Truncation: On the other hand, a sequence may Therefore, it’s preferable to make the dataset classes are
be shortened to meet the predefined maximum more balanced. in order to that, some oversampling will be
length if it is greater than that. Depending on applied in the dataset, adding and duplicating the minor
the model and the kind of data, one may classes to have closer numbers.
choose to truncate at the beginning, middle, or
end.
Selection of Maximum Length: The dataset
may be used to inform the maximum sequence
length decision. In order to strike a balance
between information loss and computational
efficiency, it might be set to a predefined
threshold or, more often, the length of the
longest sequence in the dataset.
C. Data set description
The dataset data is sourced from Davidson, Dana Warmsley,
Michael Macy, and Ingmar Weber 2017 which consists of
three columns (id, label, and tweets), has around 49,567
rows of tweets. We have taken the information out of
https://www.kaggle.com/datasets/eldrich/hate-speech-
offensive-tweets-by-davidson-et-al/download?
datasetVersionNumber=1
Table 1.2: Total number of tweets in each class.
CLASSES LABEL TWEETS
HATE_SPEECH 0 2860
Note that this process drastically increase the amount of
OFFENSIVE 1 38380 tweets and slightly consume more memory and time of
training, still effective for training the model for better
NEITHER 2 8326 precision and score.
However, there were some imbalances in the number of
classes in this dataset as it’s shown below, the hate speech
D. Evaluation Metrics
tweets (class 1) was significantly higher than the other
classes.
In statistical modeling and machine learning, evaluation
metrics are vital instruments. They are used to assess a The ratio of accurately anticipated positive
model, algorithm, or classifier's efficacy or performance, observations to all observations made during the
often in comparison to a predefined set of test data with actual class is known as recall, also known as
predetermined labels. The kind of model you are using and sensitivity.
the particular issue you are attempting to address have a
major influence on the assessment metrics you choose. a
These are a few typical measures for evaluation: class 1=
(a+b +c)
i. Confusion Matrix e
class 2=
(d +e +f )
A confusion matrix for a classification problem with i
three classes (hate speech, offensive, neither) expands class 3=
to a 3x3 matrix. This matrix provides a detailed (g+h+i)
breakdown of the predictions versus the actual classes.
ii. How it is structured.
viii. F1-Score
Predicted Predicted Predicted
class 0 class 1 class 2 Precision and Recall's average harmonic is the F1-
Actual a b c Score.
class 0
Actual d e f
class 1 2∗(precision for clas
Actual g h i F 1−Score Class 1=
class 2 ( precision for class
‘a, e, I’ are the number of 2∗( precision for clas
correct predictions for each F 1−Score Class 2=
( precision for class
class (True Positives for
Classes 0, 1, and 2,
respectively). 2∗( precision for class 3∗
F 1−Score Class 3=
‘b, c, d, f, g, h’ are ( precision for class 3+r
misclassifications (False
Positives and False
Negatives).
iii. Accuracy
The ratio of accurately anticipated
observations to total observations is known as
accuracy.
(a+ e+i)
Accuracy=
( a+b+ c+ d+ e+ f + g+h+ i)
IV. RESULT AND ANALYSIS
iv. Precision for each class
i. Model Architecture
The ratio of accurately predicted positive ii.
observations to all anticipated positives for
each class is known as precision.
a
class 1=
(a+ d+ g)
e
class 2=
(b+ e+h)
i
class 3= For the proposed model, each word of the tweets is
(c + f + i)
analyzing in every layer individually then combined to
understand it.
vii. Recall for each class
The model here is sequential so the inputs will be analyzed i. Model performance at epoch= 16
layer by layer, output dimensionality is set to 200, which It
defines the dimensionality of the output space of the Following 16 epochs of training with 300 steps each
embedding layer. Speaking of embedding layers, its job here epoch, we saw the following accuracy:
is to turn positive integers into dense vectors of fixed size,
which was mentioned to be set to 200.
Some hidden layers will be needed as well, here the dense
layers are using activation function “relu” with 128 nodes
and “Softmax” with 3 nodes. This brings the process to one
of 3 classes of outputs in output layer.
The results obtained from our hate speech detection model.
Our analysis primarily focuses on comparing the accuracy of
our model with that reported in the referenced paper. The
model's performance was evaluated at two different training
durations: epoch= 10 and epoch=16, and steps_per_epoch=
200.
The results that were discussed in the
submitted paper that used linear support vector
machine (SVM) algorithm were:
Precision= 0.91
Recall= 0.90
F1 score= 0.90
Following before performing oversampaling, 10
epochs of training with 300 steps each epoch, we saw
the following accuracy:
Confusion matrix for epoch= 16
Confusion matrix for epoch= 10
ii. Model Testing
Testing process and methodology are employed to
evaluate the efficacy of our hate speech detection
model. The model, trained to categorize text into three
classes – hate speech, offensive, or neither – was
subjected to a series of tests to assess its real-world
applicability and reliability. A crucial aspect of these
tests involved user interaction, where users input text,
and the model classifies it into one of the three
categories.
User interaction testing
o Text Input Interface: A simple user
interface (UI) is created that takes a text
sample as input from users. Depending on
the deployment, this user interface (UI)
might be a command-line input or a text
box in a web application.
o Model response: The model receives the
text input and classifies it as hate speech,
offensive, or neither after processing it.
Based on the training it underwent and
the patterns it discovered while
undergoing that training, this
categorization was made.
o Display of results: The user is
subsequently presented with the
categorization result. This immediate
input demonstrates the model's capacity
for real-time analysis.
V. REFERENCES
iii. Abhijeet Verma, A. S. (2022). Identification of Hate Speech on Social Media using LSTM. GMSARN International
Journal 17 .
iv. Anagha Abraham, A. J. (2023). Hate Speech Detection in Twitter Using Different Models. EDP Sciences.
v. Anez, F. Y. (2022). rabic Hate Speech Detection Using Deep Recurrent Neural Networks. Saudi Arabia:
Management Information System Department, Prince Mohammad bin Fahd University.
vi. Bora, C. P. (2021). Detecting Hate Speech using Deep Learning Technique. International Journal of Advanced
Computer Science and Applications (IJACSA).
vii. PRADEEP KUMAR ROY, ,. A.-Z. (2020). A Framework for Hate Speech Detection Using Deep Convolutional
Neural Network. Retrieved from IEEE Access : 0.1109/ACCESS.2020.3037073
viii. Prasad, D. S. (September 2023). PREDICTING HATE SPEECH IN SOCIAL MEDIA USING DEEP LEARNING .
International Journal of Engineering, Science, Technology and Innovation (IJESTI).
ix. Prashant Kapil', A. E. (n.d.). Investigating Deep Learning Approaches for Hate Speech Detection in Social Media.
India : Indian Institute of Technology Patna.
x. Sandip Dutta, U. M. (2021). A CNN-LSTM Model For Hate Detection. Proceedings of the 18th International
Conference on Natural Language Processing (pp. pages 53–57). Kolkata, India: Department of ETCE, ‡Department
of CSE Jadavpur University.
xi. Ali, M.M., Qaseem, M.S., Ahmad, S.S. (2023). Rumour Detection Model for Political Tweets Using ANN. In:
Kumar, A., Ghinea, G., Merugu, S. (eds) Proceedings of the 2nd International Conference on Cognitive and
Intelligent Computing.
xii. Alzubaidi, L., Zhang, J., Humaidi, A. J., Al-Dujaili, A., Duan, Y., Al-Shamma, O., ... & Farhan, L. (2021). Review
of deep learning: Concepts, CNN architectures, challenges, applications, future directions. Journal of big Data (2023)
xiii. Nugroho, K., Noersasongko, E., Fanani, A. Z., & Basuki, R. S. (2019, July). Improving random forest method to
detect hatespeech and offensive word. In 2019 International Conference on Information and Communications
Technology (ICOIACT)
xiv. Ziqi Zhang, David Robinson, and Jonathan Tepper. 2018. Hate Speech Detection Using a Convolution-LSTM Based
Deep Neural Network. In Proceedings of ACM The Web conference (WWW’2018). ACM, New York, NY, USA,
xv. Chikashi Nobata, Joel Tetreault, Achint Thomas, Yashar Mehdad, and Yi Chang. 2016. Abusive language detection
in online user content. In Proceedings of the 25th international conference on world wide web
xvi. Wang, Q. (2023). dvanced Deep Learning Approaches for Hate Speech Detection. Highlights in Science,
Engineering and Technolo.
xvii. Zhou, Y., Yang, Y., Liu, H., Liu, X., & Savage, N. (2020). Deep Learning Based Fusion Approach for Hate Speech
Detection. Retrieved from IEEE Access: 10.1109/ACCESS.2020.3009244