Cyberbullying Detection: Identifying Hate
Speech using Machine Learning
Dev Kathuria Ishank Nijhawan Prakhar Bhasin
Computer Science Engineering Computer Science Engineering Computer Science Engineering
Shiv Nadar University Shiv Nadar University Shiv Nadar University
email: dk382@[Link] email: in104@[Link] email: pb652@[Link]
Kirtik Singh
Computer Science Engineering
Shiv Nadar University
email: ki426@[Link]
Abstract—Bullying has been prevalent since the be- media and its detrimental effect on the younger genera-
ginning of time, It’s just the ways of bullying which tion. A growing body of work is emerging on automated
have changed over the years, from physical bullying to approaches to cyberbullying detection. These approaches
cyberbullying. According to Williard(2004), there are eight
types of cyberbullying such as harassment, denigration, utilise machine learning and natural language processing
impersonation, etc. It’s been around 2 decades since social techniques to identify the characteristics of a cyberbully-
media sites came into the picture, but there hasn’t been a ing exchange and automatically detect cyberbullying by
lot of effective measures to curb social bullying and it has matching textual data to the identified traits.
become one of the alarming issues in recent times. Natural language processing focusing specifically on
In this paper, we present a systematic review of some
published research on cyberbullying detection approaches
this phenomenon is required since basic word filters do
and examine methods to detect hate speech in social media, not provide a sufficient remedy: What is considered a
while distinguishing this from general profanity. We aim hate speech message might be influenced by aspects such
to establish lexical baselines for this task by applying as the domain of an utterance, its discourse context, as
supervised classification methods using a manually annoted well as context consisting of co-occurring media objects
open source dataset for this purpose. This paper does
a comparative study of various Supervised algorithms,
(e.g. images,videos, audio), the exact time of posting
including standard, as well as ensemble methods. The and world events at this moment, identity of author and
evaluation of the result shows that Ensemble supervised targeted recipient.
methods have the potential to perform better than tra- This paper provides a comprehensive and structured
ditional supervised methods. A number of directions for overview of automatic hate speech detection, and com-
future work are also discussed.
pares few of its current approaches in a systematic man-
Index Terms—Machine Learning, Cyberbullying, Super-
vised, Ensemble, Hate Speech, Natural Language Process- ner, along presenting an insightful review of some pub-
ing lished research on cyberbullying detection approaches.
II. RELATED WORK
I. INTRODUCTION
For Detecting Cyberbullying, numerous approaches
Hate speech refers to words whose intent is to create have been developed, majorly using Natural Language
hatred towards a particular group, that group may be a Processing and Information Retrieval which are then
community, religion or race. This speech may or may used to classify textual data by extracting it’s features
not have meaning, but is likely to result in violence. by using TF-IDF, Sentiment Analysis, Dimensionality
Hate speech online has been linked to a global increase Reduction etc. and they have received commendable
in violence toward minorities, including mass shootings, accuracies.
lynchings, and ethnic cleansing. Sambhagadi et al. [2] tries ways to detect nastiness
Due to the massive rise of user-generated web content, on social media using NLP techniques to detect and
particularly on social media networks, the amount of hate deter cyberbullying eventually. NLP techniques are used
speech is also steadily increasing. Over the past years, in such a way that they can even detect when profanities
research into cyberbullying detection has increased, due in the data are used in an insulting way or in a neutral
in part to the proliferation of cyberbullying across social way. Annotations used in the paper are iteratively revised
using in lab annotations and crowdsourcing. Data was TABLE I
crawled from English posts on social media sites even R ESULTS OF [4]
including semi-anonymous social media sites such as ROC TP
[Link]. A ranked list of profanities along with NLP Bagging 0.700 0.211
helped in crawling in an effective way. To classify the J48 0.628 0.259
SMO 0.703 0.733
data, modified linear SVM was used to distinguish bad Dagging 0.755 0.763
words in a casual way, multiple other features that Naive Bayes 0.695 0.723
could have gone unnoticed were also considered such
as, Question answer posts and Emoticons. In the end,
F1-Score came out to be 0.59 (which although is less Using Reddit’s comment corpus for cyberbullying
than the Kaggle’s winner but considering the fact that detection, Rakib et al. [5] extracted the corpus and
this study didn’t use customized data and a new and cleaned it from the Reddit database, followed by training
better dataset, F1-Score of 0.59 still looks promising). a word embedding model based on word2vec skip-gram
Challenges faced in this study were - model. Then, the features of this model were used to
• In [Link], comments are question-answer pairs train a random forest classifier for classifying cyberbully
which are shorter in other datasets and both comments, This new word embedding model made using
question-answer may contain only one word making domain knowledge performed better than 4 pre-trained
it hard for the algorithm to classify without under- word embedding models, as well as handcrafted feature
standing the full context. extraction methods.
• People use informal language and slang on social Silva et al. [6] proposed a model for cyberbullying
media which are full of misspellings and abbrevia- identification that uses research based on psychology; it
tion, making processing them very difficult describes the design for an app referred as BullyBlocker,
Acknowledging the repetitive nature of cyberbullying which aims to intimate the parents of the user if cy-
on social media i.e. a sequence of aggressive messages berbullying is detected. It uses traditional methods to
sent from bully to a victim with the intent of harm, the analyze social media data of the user by going through
paper by Yao et al. [3] uses sequential hypothesis testing their messages and comments and rank them as warning
formulation to drastically reduce the number of features signs or give them a bullying rank. It is specifically made
used in classification, while still maintaining high accu- for adolescents and uses old methods for detection in
racy. This approach focuses High accuracy, Timeliness, Facebook, but it has the potential to grow by acting as
and scalability. Models are trained using semi-supervised a data-collecting app over which ML classification can
ML algorithms, using an Instagram dataset collected be run.
using snowball sampling, labeled manually(to a small
extent) by a group of experts. The limitation of this III. DATA
approach was the use of a single data set that was only This section contains all the aspects of Data from
valid for Instagram, with no way to check the validity collection to preprocessing and features extraction.
of labels, and the time overhead due to difficulty in
capturing comment based labels. A. Data Collection
Huang et al. [4] focuses on analyzing the social
We have used Dataturks’ Tweet Dataset for Cybertroll
network structure between users and deriving features
Detection obtained from Kaggle [1] for reaching the
such as a number of friends, network embeddedness,
final results. Because of the seriousness of the issue
and relationship centrality, by integrating textual features
we aim to resolve, it was crucial to choose a dataset
with social network features, detection of cyberbullying
that was complete, reliable, relevant, and to the point.
can be achieved. The study claims that past researches
While we considered many other datasets as well, many
haven’t fully utilized social media features, this paper
of them either had missing attributes, were too low
proposes cyberbullying detection beyond textual analysis
in quality, or were found to have irrelevant data after
to also consider the social relationships in which these
manual inspection. Thus, after having tried out of many
bullying messages are exchanged. It uses twitter corpus
other open sourced datasets, we came down to [1] as it
from Dec 2008 to Jan 2009 and uses SMOTE(synthetic
seemed in line with all the parameters required.
minority oversampling technique) approach to create a
Here is the Detailed Description of the dataset:
balanced data, with Naive Bayes, J48, SMO, Bagging
and dagging being applied on it. 1) It is a partially manually labelled dataset.
2) Total Instances: 20001
The dataset has 2 attributes- tweet and label [0 corre-
sponds to No while 1 corresponds to Yes]
B. Data Cleaning TABLE III
T EST AND TRAINING I NSTANCES
The dataset used was set in a json format. Since the
fields of the dataset were relatively simple to interpret, Test Training
Total Instance 4001 16000
the original set of fields in the annotation attribute was
CyberBullying Instances 2429 9750
removed, and filled with the label values to simplify the Non-CyberBullying instances 1572 6250
next step. The number of instances for each class are
mentioned in table 1.
matrix consists of each document (row) and each
TABLE II word (column) and the importance (weight) com-
I NSTANCES
puted by tf * idf (values of the matrix). If a word
Twitter has high tf-idf in a document, it has most of the
Total Instances 20001 times occurred in given documents and must be
CyberBullying instances 7822
Non-CyberBullying instances 12179
absent in the other documents. So the words must
be a signature word.
Attribute evaluation is done manually as can be seen
C. Data Preprocessing where we have printed the top 25 words according to
the calculated tf-idf score. Some Top ranked words for
The preprocessing steps were done as follows using
the dataset were: [hate, fuck, damn, suck, ass, that, lol,
the nltk library along with regex:
im, like, you, it, get, what, no, would, bitch]
1) Word Tokenization: A Token is a single entity that
is building blocks for sentence or paragraph. Word D. Data Resampling
Tokenization converts our text to separate words in As the data was skewed, Resampling had to be per-
a list. formed on the training data, Firstly the data was split
2) Stop words filtering is done using into Training and Test in 80:20 ratio and resampling was
[Link](‘english’) to fetch a performed on the training data.
list of stopwords in the English dictionary, after
• As we had ample data to work with, we used
which they are removed. Stop words are words
oversampling of the minority class. This means
such as “the”, “a”, “an”, “in”, which are not
that if the majority class had 1,000 examples and
significant and do not affect the meaning of the
the minority class had 100, this strategy would
data to be interpreted.
oversampling the minority class so that it has 1,000
3) To remove punctuation, we save only the charac-
examples.
ters that are not punctuation, which can be checked
• For Oversampling, RandomOverSample function
by using [Link] .
is used from imblearn package for all the ”not
4) Stemming: Stemming is a process of linguis-
majority” classes which in our case, was only the
tic normalization, which reduces words to their
1 minority class.
word root word. We stem the tokens using
[Link] to get the stemmed
tokens. For example, connection, connected, con- After resampling, the training data had 9750 CB &
necting word reduce to a common word ”connect”. NON-CB instances.
5) Digit removal: We also filtered out any numeric IV. DESCRIPTION OF METHODS
content as it doesn’t contribute to cyberbullying.
6) Now the next step was to extract features so that A. Gaussian Naive Bayes
it can be used with ML algorithms, for which Naive Bayes classifiers are a collection of classifica-
we used TF-IDF Transform using Python’s sklearn tion algorithms based on Bayes’ Theorem of mathemat-
libary. TF-IDF is a statistical measure to evaluate ics. In simple words, the Bayes’ theorem describes the
the relevance of a word, which is basically cal- probability of an event, based on prior knowledge of
culated by multiplying the number of times that conditions that might be related to the event. It is not a
words appeared in the document by the inverse single algorithm but a family of algorithms where all of
document frequency of the word. TF-IDF uses the them share a common principle, i.e. every pair of features
method diminishing the weight (importance) of being classified is independent of each other. Naive
words appeared in many documents in common, Bayes is a classification algorithm for binary (two-class)
considered them incapable of discerning the doc- and multi-class classification problems. The technique
uments, rather than simply counting the frequency is easiest to understand when described using binary or
of words as CountVectorizer does. The outcome categorical input values. It is called naive Bayes because
the calculation of the probabilities for each hypothesis information gain (IG) (reduction in uncertainty towards
are simplified to make their calculation tractable. the final decision). In an iterative process, we can then
Naive Bayes can be extended to real-valued attributes, repeat this splitting procedure at each child node until
most commonly by assuming a Gaussian distribution. the leaves are pure. This means that the samples at each
This extension of Naive Bayes is called Gaussian Naive leaf node all belong to the same class.
Bayes. Beside the Gaussian Naive Bayes there are also The classifier was implemented using [Link] pack-
existing the Multinomial naive Bayes and the Bernoulli age.
naive Bayes. We picked the Gaussian Naive Bayes
because it is the most popular one and one of the simplest D. Adaboost Classifier
to implement because we only need to estimate the mean AdaBoost is an iterative ensemble method. The gen-
and the standard deviation from the training data. eral idea behind boosting methods is to train predictors
The classifier was implemented using sequentially, each trying to correct its predecessor. Ad-
[Link] bayes package. aBoost classifier builds a strong classifier by combining
multiple poorly performing classifiers so that you will
B. Logistic Regression get high accuracy strong classifier. The basic concept
Regression analysis is a predictive modelling tech- behind Adaboost is to set the weights of classifiers and
nique that analyzes the relation between the target or training the data sample in each iteration such that it
dependent variable and independent variable in a dataset. ensures the accurate predictions of unusual observations.
Regression analysis techniques get used when the target Any machine learning algorithm can be used as base
and independent variables show a linear or non-linear classifier if it accepts weights on the training set.
relationship between each other, and the target variable At a high level, AdaBoost is similar to Random Forest
contains continuous values. Regression analysis involves as they both tally up the predictions made by each
determining the best fit line, which is a line that passes decision trees within the forest to decide on the final
through all the data points in such a way that distance classification. There however, lie some subtle differ-
of the line from each data point is minimized. ences. In AdaBoost, the decision trees have a depth
Logistic regression is one of the types of regression of 1 (i.e. 2 leaves). In addition, the predictions made
analysis technique, which gets used when the dependent by each decision tree have varying impact on the final
variable is discrete. Example: 0 or 1, true or false, etc. prediction made by the model. Rather than taking the
This means the target variable can have only two values, average of the predictions made by each decision tree in
and a sigmoid curve denotes the relation between the the forest (or majority in the case of classification), in
target variable and the independent variable, by mapping the AdaBoost algorithm, every decision tree contributes
any real value to a value between 0 and 1. We chose a varying amount to the final prediction.
Logistic Regression as the size of our data set was large, The classifier was implemented using
and it had almost equal occurrence of values to come [Link] package.
in target variables. Moreover, there was no correlation
between independent variables in the dataset. E. Random Forest Classifier
The classifier was implemented using As its name implies, Random Forest Classifier consists
[Link] model package. of a large number of individual decision trees that
operate as an ensemble. Each individual tree in the
C. Decision Tree Classifier random forest spits out a class prediction and the class
A Decision Tree is constructed by asking a series with the most votes becomes our model’s prediction. The
of questions with respect to the dataset. Each time an low correlation between models is the key as they can
answer is received, a follow-up question is asked until a produce ensemble predictions that are more accurate than
conclusion about the class label of the record. The series any of the individual predictions, as the trees protect
of questions and their possible answers can be organised each other from their individual errors. The process of
in the form of a decision tree, which is a hierarchical Bagging is used to diversify models as each individual
structure consisting of nodes and directed edges. It has tree is allowed to randomly sample from the dataset with
3 types of nodes: Root, Internal, and Leaf nodes. replacement.
In a decision tree, each leaf node is assigned a class The classifier was implemented using [Link]
label. The non-terminal nodes, which include the root package.
and other internal nodes, contain attribute test conditions
to separate records that have different characteristics. V. EXPERIMENT AND RESULTS
Using the decision algorithm, we start at the tree root For our supervised learning technique analysis, we’ve
and split the data on the feature that results in the largest used Naive Bayes(Gaussian), Logistic regression, and
J48 Decision Tree as the standard methods. As Ensemble
TABLE IV
methods, we have used AdaBoost and RandomForest S UPERVISED T RADITIONAL M ETHODS
Classifiers. In our research, we found that the Gaussian
Naive Bayes classifier performed the poorest, whereas NaiveBayes Regression DecisionTree
Accuracy 0.62 0.80 0.85
the Random Forest Classifier gave the best result in terms Precision 0.79 0.81 0.88
of every metric.[fig1 & fig2]. Recall 0.62 0.80 0.85
F1-Score 0.59 0.81 0.85
It wasn’t surprising to see the Random Forest clas- ROCArea 0.68 0.81 0.87
sifier performing the best. The Decision Tree classifier
performed better than Naive Bayes classifier and Logistic Confusion 925 1504 1920 509 1896 533
Matrix 31 1541 274 1298 67 1505
Regression. The Random Forest Classifier came out on
top in all the performance metrics, which was expected
as it is an extension of the Decision Tree classifier,
TABLE V
averaging out results of multiple recursions of the same. S UPERVISED E NSEMBLE M ETHODS
The Metrics used for determining the performance of
models are as follows: AdaBoost Random Forest
Accuracy 0.71 0.92
• Precision 0.74 0.92
Recall 0.71 0.92
(2 × P recison × Recall) F1-Score 0.72 0.92
F − M easure = ROCArea 0.73 0.92
P recision + Recall
Confusion 1616 813 2175 254
• Matrix 332 1240 73 1499
TP + TN
Accuracy =
TP + TN + FP + FN
•
TP
P recision =
TP + FP
•
TP
Recall =
TP + FN
• ROCArea, which denotes the area under the curve
formed by plotting TP rate.
where,
TP = No. of True Positives
TN = No. of True Negatives
FP = No. of False Positives Fig. 1. Precision, Recall, F1-Score, ROCArea
FN= No. of False Negatives
Traditional Supervised Learning used: NaiveBayes,
Logistic Regression and J48 Decision Trees classifier
The Ensemble Learning Methods used: AdaBoost and
Random Forest classifier
Figure 1 and Figure 2 shows a graphical comparison
between the aforementioned algorithms.
Note: Table IV represents the weighted average using
both the classes(hate speech and non hate speech) for
Precision, Recall, and F1 score.
First column and row of the confusion matrices
represents Cyberbullying class whereas the second row Fig. 2. Accuracy
and column represents Non-cyberbullying class.
VI. C ONCLUSION AND F UTURE S COPE [4] Huang, Qianjia, Vivek Kumar Singh, and Pradeep Kumar Atrey.
”Cyberbullying detection using social and textual analysis.” Pro-
In this paper, We did a comparative study between ceedings of the 3rd International Workshop on Socially-Aware
Multimedia. 2014.
various Supervised algorithms, additionally also com- [5] T. Bin Abdur Rakib, L. K. Soon, in Lecture Notes in Computer
paring various Supervised Ensemble methods as well. Science (including subseries Lecture Notes in Artificial Intelli-
The overall best performance was shown by Random gence and Lecture Notes in Bioinformatics) (Springer Verlag,
2018), vol. 10751 LNAI, pp. 180–189.
Forest classifier, giving an accuracy of about 92%. The [6] Y. N. Silva, D. L. Hall, C. Rich, BullyBlocker: toward an inter-
Ensemble methods performed equal to, or better than disciplinary approach to identify cyberbullying. Social Network
the Supervised methods but still, We observed a high Analysis and Mining. 8 (2018), doi:10.1007/s13278-018-0496-z.
[7] E. Raisi, B. Huang, Weakly supervised cyberbullying detection
True positive rate for the cyberbullying class in all the with participant-vocabulary consistency. Social Network Analysis
ensemble methods, which is much more desirable. Naive and Mining. 8 (2018), doi:10.1007/s13278-018-0517-y.
Bayes performed the worst, giving just 61% accuracy. [8] Homa Hosseinmardi, Sabrina Arredondo Mattson, Rahat Ibn
Rafiq, Richard Han, Qin Lv, Shivakant Mishra. (2015). Detection
Through this paper, we evaluated our approach and of Cyberbullying Incidents on the Instagram Social Network. ”
compared it with other papers in the section “Related [9] Dadvar, Maral Eckert, Kai. (2018). Cyberbullying Detection
Work”. We also observed that none of the studied past in Social Networks Using Deep Learning Based Models; A
Reproducibility Study. 10.13140/RG.2.2.16187.87846.
researches used any semi-supervised methods, probably [10] Nandhini, B. Sri, and J. I. Sheeba. ”Cyberbullying detection and
because they are not that popular or effective an didn’t classification using information retrieval algorithm.” Proceedings
give any commendable result in comparison to the su- of the 2015 International Conference on Advanced Research in
Computer Science Engineering Technology (ICARCSET 2015).
pervised methods. 2015.
A very notable fact to be addressed is also the lack
of labelled datasets and non-holistic consideration of
cyberbullying by researchers when developing detection
systems. These are two key challenges facing cyberbul-
lying detection research. Another challenge faced was
the lack of resources, due to which we were not able
to analyze the performance of SVM(Support Vector
Machine) or Multi Layer Perceptron(Neural Networks)
classifiers. They have however been mentioned in our
study for reference.
Future work on cyberbullying can also benefit by us-
ing Dimensionality Reduction as the number of features
in this case can be quite high as seen in our example.
PCA(Principal Component Analysis) and LDA(Linear
Discriminant Analysis) are few common techniques used
for this purpose which have the ability to play a really
important role in machine learning, especially when
working with thousands of features. Principal Com-
ponents Analysis are one of the top dimensionality
reduction algorithms, and in addition to making the
work of feature manipulation easier, it can also help
to improve the results of the classifier. The idea is to
explore advantages and disadvantages of each one and
check its results individually and combined as well.
R EFERENCES
[1] DataTurks. (2018, July 12). Tweets Dataset for Detection
of Cyber-Trolls. Retrieved November 07, 2020, from
[Link]
cybertrolls?select=Dataset+for+Detection+of+[Link]
[2] Samghabadi, Niloofar Safi, et al. ”Detecting nastiness in social
media.” Proceedings of the First Workshop on Abusive Language
Online. 2017.
[3] Yao, Mengfan, Charalampos Chelmis, and Daphney? Stavroula
Zois. ”Cyberbullying ends here: Towards robust detection of cy-
berbullying in social media.” The World Wide Web Conference.
2019.
A PPENDIX A
F URTHER S TUDIES ON C YBERBULLYING D ETECTION
Raisi et. al [7] presents a unique model, referred to
as participant vocabulary consistency (PVC) Model.
This relational model is trained in a weakly supervised
manner, as getting high quality labeled data is difficult,
in this model, human experts only need to provide high
fidelity annotations in the form of key phrases that are
highly indicative of harassment. This algorithm then
uses these annotations, by searching for patterns of
victimization in unlabeled social interaction network-
to find other likely key phrases indicators and specific
instances of bullying.
Hosseinmardi et. al [8] addresses the cyberbullying
incidents on a popular social media platform Instagram
by analysing the top comments on the user’s public
posts. The algorithm used Naive Bayes classification
to separate the data and further differentiates it from
Cyber Aggression. Cyberbullying was studied in the
context of a media based social network, incorporating
both images and comments in the labelling. They
were also able to show that a Linear SVM classifier
can significantly improve the accuracy of identifying
cyberbullying to 87% by incorporating multi-modal
features from text, images, and meta data for the media Fig. 3. Naive Bayes Classifier
session.
Dadvar et. al [9] focuses on using Deep neural
networks to detect instances of cyberbullying. Models
like Convolutional Neural Network (CNN), Long Short-
Term Memory (LSTM), Bidirectional LSTM (BLSTM)
and BLSTM with attention which vary in complexity
in their neural architecture were used on Formspring,
Wikipedia and Twitter datasets.
Nandhini et al. [10] Proposes a naive Bayes based
learning model and used the dataset of [Link],
they achieved a high accuracy of 91%. [8] uses
Formspring data, available at [Link] by Kelly
Reynolds which initially had about 12000 instances, but
after preprocessing, they got a total of 1608 instances
where half of them corresponds to cyberbullying,
basically, they used this small dataset to train a Neural
network and an SVM Classifier.
A PPENDIX B
V ISUALISATION OF O UTCOMES
Fig. 4. Logistic Regression Classifier
Fig. 5. Decision Tree Classifier
Fig. 7. Random Forest Classifier
Fig. 6. Adaboost Classifier