Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Final Year

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

5

CHAPTER 1
INTRODUCTION

In the Today era, there is a huge role of social media to deliver any information to public. This
information can be in the form of text, photo, video. As well as the hate content or offensive content
are also spreading rapidly by these platforms. So, it is necessary to solve these issues to make
social media as an appropriate place for use.
With the proliferation of social media platforms, which provide anonymity, easy access and online
community training and online discussion, the detection and monitoring of hate speech is a
growing challenge for society, individuals, policymakers, and researchers. Violence in social
media will now be known as hate speech.
In this respect, researchers have produced numerous data sets from multiple sources, which has
contributed to research in this area. A number of these studies also examined hate speech in several
non-English languages and online communities. In the age of social informatics, the interaction
between individuals becomes more striking, particularly through social means.
This resulted in the study and contrast of various treatment pipelines, including the selection of the
set of characteristics and Machine Learning (ML) methods (e.g., supervised, unsupervised and
semi-supervised), classification algorithms (e.g., Naive Bayes, Linear Regression, SVM) and
Natural Language Processing. Within the field of AI, and Natural Language Processing (NLP) in
particular, techniques for tasks related to Sentiment Analysis and Opinion Mining (SA&OM) grew
in relevance over the past decades.
Such techniques are typically motivated by purposes such as extracting users’ opinion on a given
product or polling political stance. Robust and effective approaches are made possible by the rapid
progress in supervised learning technologies and by the huge number of user-generated contents
available online, especially on social media. More recently the NLP community witnesses a
growing interest in tasks related to social and ethical issues, also encouraged by the global
commitment to fighting extremism, violence, fake news, and other plagues affecting the online
environment.
One such phenomenon is hated speech, a toxic discourse which stems from prejudices and
intolerance, and which can lead to episodes, and even structured policies, of violence,
discrimination, and persecution.
6

CHAPTER 2
LITERATURE SURVEY

PAPER 1

Title: Automatic Hate Speech Detection using Machine Learning: A Comparative Study
Authors: Sindhu Abro, Sarang Shaikh, Zafar Ali, Sajid Khan, Ghulam Mujtaba
Journal: (IJACSA) International Journal of Advanced Computer science and Application
Publication Year: 2020
Findings: In the Today era, there is a huge role of social media to deliver any information to
public. This information can be in the form of text, photo, video. As well as the hate content or
offensive content are also spreading rapidly by these platforms. So, it is necessary to solve these
issues to make social media as an appropriate place for use. Text Pre-processing and feature
engineering techniques are used before using classifiers. We will use some machine learning
classifiers (Naïve Bayes, Random Forest, Support Vector Machine, K Nearest Neighbour,
Decision Trees, Adaptive Boosting, Multilayer Perceptron, Logistic Regression) for detection of
hateful content. The accuracy, precision, recall, f-measure will be evaluated for each classifier.
The one which accuracy will be high, choose as the best fit for our model. It showed best accuracy
(79%) with SVM. So, SVM is chosen best Classifier.
Strength:
1. It evaluates each model to find best fit so that our project performs well.
2. The results from this research study hold practical importance that is a baseline study to
compare upcoming research within different automatic text classification method.
Weakness:
1. There is a need to improve the proposed ML model which can be used to predict the
severity of hate speech messages as well, more data instances will be collected to be used
for learning the classification rules efficiently.
2. This model is insufficient in terms of real-time predictions accuracy for the data.
7

PAPER 2

Title: A systematic review of hate speech automatic detection using Natural Language Processing
Authors: Md Saroar Jahan, Mourad Oussalah
Publisher: University of Oulu, CMVS Finland
Publication Year: 2021
Findings: With the proliferation of social media platforms, which provide anonymity, easy access
and online community training and online discussion, the detection and monitoring of hate speech
is a growing challenge for society, individuals, policymakers, and researchers. In this respect,
researchers have produced numerous data sets from multiple sources, which has contributed to
research in this area. A number of these studies also examined hate speech in several non-English
languages and online communities. In the age of social informatics, the interaction between
individuals becomes more striking, particularly through social means. Microblogging apps have
made it possible for people all over the world to express and share their thoughts instantly and
widely. On the one hand, thanks to the easily accessible and anonymous platform. And, on the
other hand, by the user's desire to dominate the debate, to spread/defend views or arguments and
possibly certain commercial incentives. This has provided a fertile environment for aggressive and
harmful dispersal. This resulted in the study and contrast of various treatment pipelines, including
the selection of the set of characteristics and Machine Learning (ML) methods (e.g., supervised,
unsupervised and semi-supervised), classification algorithms (e.g., Naive Bayes, Linear
Regression, Convolution Neural Network (CNN), LSTM, BERT deep learning architectures, and
so on). This document provides a systematic review of literature in this field, with a focus on
natural language processing and deep learning technologies, highlighting the terminology,
processing pipeline, core methods employed, with a focal point of deep learning architecture.
Strength:
1. This analysis also included other hate speech domains like abusive languages,
discrimination, extremism, and radicalization.
2. This model consists of many datasets to improve the accuracy of system.
Weakness:
1. There is an absence of resources in non-English experiments.
2. It is found that lack of comparative studies that evaluate the existing approaches
8

PAPER 3
Title: Advances in Machine Learning Algorithms for Hate Speech Detection in social media

Authors: NANLIR SALLAU MULLAH 1,2, (Member, IEEE), AND WAN MOHD NAZMEE
WAN ZAINON 1

Journal: IEEEAccess
Publisher: 1. School of Computer Sciences, University Sains Malaysia, Penang 11800,
Malaysia

2. Federal College of Education Pankshin, PMB1027 Pankshin, Plateau State, Nigeria


Publication Year: 2021
Findings: The purpose of this article is to review algorithms and machine learning methods for
detecting hateful speech on social network. The hateful speech issue is normally a text
classification task model. In this study, we examined the core components of the hate speech
categorisation through ML algorithms. There are five primary components – data collection and
exploration, feature extraction, dimensionality reduction, classifier selection and training, and
model evaluation, were tested. Enhancements have been made to the ML algorithms that have
been used for the detection of hate speech. New datasets and different performance measurement
have been proposed in the literature. Keeping researchers up to date on these trends in the
automatic detection of hate speech requires a comprehensive and updated state of the art. This
study comprises of three steps. First, provide readers with the information they need about the
critical steps in detecting hate speech using ML algorithms. Second, the weaknesses and strengths
of each method are seriously evaluated to guide researchers through the difficulty of choosing the
algorithm. Finally, gaps in research as well as open challenges have been identified. The various
variants of ML techniques were examined, including standard ML, the overall approach and deep
learning methods. Researchers and experts equally will benefit extremely from this study.

Strength:
1. Random Forest proven to be the most accurate Machine Learning algorithm.
2. Training speed is faster and easy to make a parallel method.
3. Helps to process bias or under-integration in the dataset
Weakness:
1. The first fundamental issue is the availability of a data set on hate speech in various parts of the
world.
2. The second issue has to do with the lack of data.
9

PAPER 4

Title: Comparative Analysis of Machine Learning and Deep Learning Algorithms for Detection
of Online Hate Speech.
Authors: Tashvik Dhamija, Anjum, Rahul Katarya
Publisher: Delhi Technological University, New Delhi, India – 110042
Publication Year: 2020
Findings: The identification of Hate messages on social media has become a burning issue over
the past few years. To eliminate disrespectful content, reliable identification of hate messages is
necessary. Number of attempts have been made to categorize hateful messages through machine
learning. To categorize hate messages, we will use the NLP features and machine learning
techniques to identify them. There are three primary components – data collection (Regex, Stop
words removal), feature engineering (TF-IDF, Bag of Words and Doc2Vec) and classifier model
(Logistic Regression, Decision Tree, Random Forest, Naive Bayes, and Support Vector Machine).
In the paper, we compare different machine learning patterns on a tweet dataset using various
feature engineering techniques and try to figure out which algorithm is best suited to this problem
instruction.
Strength:
1. It finds the best model from all the algorithms to show the best result.
2. In future we can expanding the variety of sequences of words in the dataset by increasing the
number of samples available and by sampling different social media networks to make the
classifiers more robust and accurate.
Weakness:
1. The problem of unbalanced class distribution of the dataset in detecting hate speech is common
because it occurs naturally in most real-life problems.
2. The limitation of this work is that no experiment was conducted with a given dataset.
10

PAPER 5

Title: Hate Speech Detection in English and Non-English Languages: A Review of Techniques
and Challenges.
Authors: Tian Xiang May, Mafas Raheem, Rajasvaran Logeswaram.
Publisher: Research Gate
Publication Year: 2021
Findings: The growth in the use of social media is spreading thanks to the richness of information
available on the Internet. Along with the possibility of connecting with peers on various public
personalities social media channels. A lot of social media has been put out to public figures.
Violence in social media will now be known as hate speech. Hate speech negatively impacts the
physical and mental well-being of people. We must put an end to this heinous language. NLP
(Natural language Processing) is used in this project for feature engineering purpose. The NLP
includes a variety of tasks, including classification, structure, forecasting, matching, sequencing
decision-making, and translation tasks. Logistic Regression is used for modelling the likelihood of
a discrete result given to an input variable. SVM is a straight-line model for categorization and
regression problems and solves both linear and nonlinear problems and works well with numerous
practical problems. Bayes theorem is used as a predictor of the class. It operates on conditional
independent functionalities. CNN can take a picture as input, assign importance to the various
aspects/objects of the picture and be able to distinguish from one another.
Strength:
1. This paper put forth consideration for multilingual hate speech detection.
2. This model allows for construction of classifier when data for target language is scarce.
Weakness:
1. Literature on detecting hate speech should be devoted to language.
2. Best practices in the construction and design of data bodies and data sets should be
identified and standardized.
11

PAPER 6

Title: Hate Speech operationalization: a preliminary examination of hate speech indicators and
their structure
Authors: Jana Papcunova, Marcel Martocik, Denisa Fedakova, Michael Kentos, Miroslava
Bozoganova, Ivan Srba, Robert Moro, Matus Pikuliak, Marian Simko, Matus Adamkovic.
Publisher: Springer
Publication Year: 2021
Findings: 75% of those who watch or participate in online debates have witnessed or been
subjected to abuse, threats or hate speech. Effective action against hate speech requires an adequate
multi-disciplinary understanding of the phenomenon. Intuitively, people usually have some self-
understanding of what hate speech is and when they cross the line. But the degree to which they
evaluate the content and shape may vary. Hate speech should be addressed and continued,
depending on whether it is operationalized. To deal with this problem and overcome the existing
ambiguity, and the absence of a unified and operational theoretical definition, we adopt a slightly
different approach. We provide an empirical definition of hate speech by providing a list of hate
speech indicators. The document is therefore intended to operationalize hate speech using
indicators and to explore their use and impact. Online hate speech is an area of research open to
various knowledge-based domains, and interdisciplinary approaches are both essential and often
recommended. There are automated hate speech detection methods, employing advanced NLP and
ML methods. It could be used to assist interested sociologists to undertake large-scale studies and
extensive research into hate speech.
Strength:
1. The proposed indicators may prove beneficial in the manual detection of hate speech in the
aforementioned scenarios, their greatest potential resides in the automatic detection of hate
speech.
2. Some types of ad hominem attacks are relatively easy to detect, particularly attacks that
include other indicators, such as insults and stereotyped language.
Weakness:
1. The list of indicators is probably not complete, and one can expect it to change as more
ideas or empirical evidence come to light.
2. This is not surprising given that even relatively well-established concepts in diagnostic
manuals are continually evolving.
12

PAPER 7

Title: Hate Speech Detection on Twitter: Feature Engineering vs Feature Selection


Authors: David Robinson, Ziqi Zhang and Jonathan Tepper
Publisher: Nottingham Trent University, Nottingham, U.K.
and University of Sheffield, Sheffield, U.K.
Year: 2018
Findings:
The increasing presence of hate speech on social media has drawn significant investment
from governments, companies. Existing methods typically use a supervised text
classification approach that depends on carefully engineered features. We conduct a
feature engineering analysis in such a task using Twitter as a case study and show findings
that challenge the conventional perception of the importance of manual feature
engineering. Automatic feature selection can drastically reduce the carefully engineered
features by over 90% and selects predominantly generic features often used by many other
language related tasks.
The pressing situation has attracted increasing research using semantic content analysis
techniques based on NLP and machine learning to develop scalable, automated methods
of hate speech detection. Feature engineering has always been an important task for this.
Feature engineering is the process of analysing and designing predictive features for
classifying hate speech. These can conclude n-grams, word clusters, linguistic features
such as Parts of Speech (PoS).
Our method uses a number of different types of features which are unigrams, bigrams and
trigrams each weighted by TF-IDF, and filtered by a minimum frequency of 5. We use
SVM to denote the model using the original feature set, and SVM+ to denote that using
both the original and extended feature sets.

Strength:
1. Importance of feature engineering is shown
2. Important features like n-grams are given.

Weaknesses:
1. Not much importance to modelling is given.
2. Not much details are given about data.
13

CHAPTER 3
PROBLEM STATEMENT

Hate crimes are unfortunately nothing new in society. However, social media and other means of
online communication have begun playing a larger role in hate crimes. For instance, suspects in
several recent hate-related terror attacks had an extensive social media history of hate-related
posts, suggesting that social media contributes to their radicalization.
Vast online communication forums, including social media, enable users to express themselves
freely, at times, anonymously. While the ability to freely express oneself is a human right that
should be cherished, inducing, and spreading hate towards another group is an abuse of this liberty.
As such, many online forums such as Facebook, YouTube, and Twitter consider hate speech
harmful, and have policies to remove hate speech content. Due to the societal concern and how
widespread hate speech is becoming on the Internet, there is strong motivation to study automatic
detection of hate speech. By automating its detection, the spread of hateful content can be reduced.
Microblogging apps have made it possible for people all over the world to express and share their
thoughts instantly and widely. On the one hand, thanks to the easily accessible and anonymous
platform. And, on the other hand, by the user's desire to dominate the debate, to spread/defend
views or arguments and possibly certain commercial incentives. This has provided a fertile
environment for aggressive and harmful dispersal.
The identification of Hate messages on social media has become a burning issue over the past few
years. To eliminate disrespectful content, reliable identification of hate messages is necessary.
Number of attempts have been made to categorize hateful messages through machine learning. To
categorize hate messages, we will use the NLP features and machine learning techniques to
identify them.
Social media networks are the primary media for perpetrating hate speeches nowadays. In line
with this, cyber-hate crime has grown significantly in the last few decades. More research is being
conducted to curb with the rising cases of hate speeches in social media. Different calls have been
made to social media providers to filter each comment before allowing it into the public domain.
The impacts of hate crimes are already overwhelming due to widespread adoption of social media
and the anonymity enjoyed by the online users. In this era of big data, it is time-consuming and
difficult to manually process and classify massive quantities of text data.
14

CHAPTER 4
SOCIETY BENEFITTED

Hate speech detection plays a major role to detect all hate words on the social platforms (Twitter,
Facebook). So that it will be a proper platform to use it.
As hate speech continues to be a societal problem, the need for automatic hate speech detection
systems becomes more apparent. We presented the current approaches for this task as well as a
new system that achieves reasonable accuracy. We also proposed a new approach that can
outperform existing systems at this task, with the added benefit of improved interpretability. Given
all the challenges that remain, there is a need for more research on this problem, including both
technical and practical matters.
Hate speech negatively impacts the physical and mental well-being of people. We must put an end
to this heinous language. Effective action against hate speech requires an adequate multi-
disciplinary understanding of the phenomenon. Intuitively, people usually have some self-
understanding of what hate speech is and when they cross the line. But the degree to which they
evaluate the content and shape may vary. Hate speech should be addressed and continued,
depending on whether it is operationalized.
Most social media platforms have established user rules that prohibit hate speech, enforcing these
rules, however, requires copious manual labor to review every report. Some platforms, such as
Facebook, recently increased the number of content moderators.
This analysis also included other hate speech domains like abusive languages, discrimination,
extremism, and radicalization. This model consists of datasets to improve the accuracy of system.
Some types of ad hominem attacks are relatively easy to detect, particularly attacks that include
other indicators, such as insults and stereotyped language.
15

CHAPTER 5
PROPOSED SOLUTION

5.1 PRESENTLY AVAILABLE SOLUTION


Most social media platforms have established user rules that prohibit hate speech; enforcing these
rules, however, requires copious manual labor to review every report. Some platforms, such as
Facebook, recently increased the number of content moderators.
A basic approach for identifying hate speech is using a keyword-based approach. By using an
ontology or dictionary, text that contain potentially hateful keywords are identified.
Keyword-based approaches are fast and straightforward to understand. However, they have severe
limitations. Detecting only racial slurs would result in a highly precise system but with low recall
where precision is the percentage of relevant from the set detected and recall is the percent of
relevant from within the global population. In other words, a system that relies chiefly on keywords
would not identify hateful content that does not use these terms. In contrast, including terms that
could but are not always hateful (e.g., “trash”, “swine”, etc.) would create too many false alarms,
increasing recall at the expense of precision.

5.2 SHORT COMINGS OF PRESENTLY AVAILABLE SOLUTION


keyword-based approaches cannot identify hate speech that does not have any hateful keywords
(e.g., figurative, or nuanced language). Slang such as “build that wall” literally means constructing
a physical barrier (wall). However, with the political context, some interpret this is a condemnation
of some immigrates in the United States. Additional information from social media can help further
understand the characteristics of the posts and potentially lead to a better identification approach.
However, this information is not often readily available to external researchers as publishing data
with sensitive user information raises privacy issues. Using user information potentially raises
some ethical issues. Models or systems might be biased against certain users and frequently flag
their posts as hateful even if some of them are not.

5.3 IDEA OF PROPOSED SOLUTION


In Automated Hate Speech detection, it takes samples of labeled text to produce a classifier that
can detect the hate speech based on labels annotated by content reviewers. Various models were
proposed and proved successful in the past. We describe a selection of open-sourced systems
presented in the recent research.
16

Fig 5.1: Proposed Solution

5.4 DOMAIN AREA


Machine Learning
Machine learning is an application of Artificial intelligence (AI) that provides a system the ability
to automatically learn and improve from the experience without being explicitly programmed.
Machine learning mainly focus on the development of a computer logical programs that can access
any data and use it learn for themselves.
In this project we use Machine Learning Classifiers.
Machine learning classifiers
Machine learning models take samples of labeled text to produce a classifier that is able to detect
the hate speech based on labels annotated by content reviewers. Various models were proposed
and proved successful in the past. We describe a selection of open-sourced systems presented in
the recent research.
Content preprocessing and feature selection.
To identify or classify user-generated content, text features indicating hate must be extracted.
Obvious features are individual words or phrases (n-grams, i.e., sequence of n consecutive words).
17

The bag-of-words assumption is commonly used in text categorization. Under this assumption, a
post is represented simply as a set of words or n-grams without any ordering.
Hate speech detection approaches and baselines.
Naïve Bayes, Support Vector Machine and Logistic Regression. These models are commonly used
in text categorization. Naïve Bayes models label probabilities directly with the assumption that the
features do not interact with one another. Support Vector Machines (SVM) and Logistic
Regression are linear classifiers that predict classes based on a combination of scores for each
feature. Open-source implementations of these models exist, for instance in the well-known
Python machine learning package sci-kit learn.

5.5 MODULE WISE DESCRIPTION: The following modules are used in this Project
5.5.1 - Data cleaning
Data cleaning is the process of preparing data for analysis by removing or modifying data that is
incorrect, incomplete, irrelevant, duplicated, or improperly formatted.
This data is usually not necessary or helpful when it comes to analyzing data because it may hinder
the process or provide inaccurate results. There are several methods for cleaning data depending
on how it is stored along with the answers being sought.
Data cleaning is not simply about erasing information to make space for new data, but rather
finding a way to maximize a data set’s accuracy without necessarily deleting information.
For one, data cleaning includes more actions than removing data, such as fixing spelling and syntax
errors, standardizing data sets, and correcting mistakes such as empty fields, missing codes, and
identifying duplicate data points. Data cleaning is considered a foundational element of the data
science basics, as it plays an important role in the analytical process and uncovering reliable
answers.

5.5.2 – Feature Engineering


I. Natural Language Processing (NLP) - This includes tokenizing, removing stop words,
vectorizing, etc.
Tokenizing Before Removing Stop Words: -When working with text data, one of the first
steps is to remove stop words from the corpus. Although text would be grammatically
incorrect without these stop words, they provide little value to models and typically
hinder performance.

Removing Stop Words in Tokenization: -We can use NLTK's built-in library of stop
words to remove them in a tokenizing function.
Stop words are like- 'a', 'to', 'the'.... etc.

Creating Bigrams: - Knowing individual word frequencies is somewhat informative, but


in practice, some of these tokens are parts of larger phrases that should be treated as a
18

single unit. Let's create some bigrams and see which combinations of words most are
telling.

II. Doc to Vec- This is the Doc2Vec model analogous to Skip-gram model in Word2Vec.
Here we can see that training a Doc2Vec model is much more straight forward in Genism.
However, it's important to note that the Doc2Vec method may be performing worse than
the TF-IDF method. We can try grid searching to improve this.

5.5.3 Machine Learning Models


I. Support Vector Machine (SVM)
We iterate through an SVM baseline, trying different class imbalance remedy methods. We
also grid search to try and optimize the baseline's hyperparameters.
The idea behind SVMs is that you perform classification by finding the separation line or
(in higher dimensions) 'hyperplane' that maximizes the distance between two classes.

Baseline with SMOTE: -


Used to over-sample the minority class (hate speech).
Baseline with Tomek Links
Used to under-sample the majority class (not hate speech).

II. Logistic Regression


We iterate through a logistic regression baseline, trying different class imbalance remedy
methods. We also grid search to try and optimize the baseline's hyperparameters.
After running a baseline, we can try oversampling the minority class (hate speech) with
SMOTE and under sampling the majority class with Tomek Links.

5.5.4 Model Evolution


• Precision = TP/ (TP+FP)
• Recall = TP/ (TP + FN)
• F-Measure = 2 x (Precision x Recall)/ (Precision + Recall)
• Accuracy = (TP + TN)/ (TP + FP + TN + FN)
Where: TN = True Negative TP = True Positive FP = False Positive FN = False Negative
19

CHAPTER 6
PRESENT STATUS OF DEVELOPMENT

Source Code
I. Cleaning Raw Data

# Import libraries
import pandas as pd pd.options.display.max_columns = 50
import numpy as np import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
sns.set()
plt.style.use('bmh')
# Packages for data cleaning function
import re import string import pickle
df = pd.read_csv('../src/twitter_data.csv', index_col=0) [Fig 6.1]
df.head()

Fig 6.1: Load in data

df['class']. unique ()
sns.countplot(df['class'], palette='Set3').set_title('Distribution of Class')
plt.show('../visualizations/orig_class_imbalance.png', bbox_inches = "tight", pad_inches=.5) [Fig 6.2]
# df['class'].sum() / len(df['class'])
20

Fig 6.2: Class Imbalance

df['label'] = df['class'].replace(1, 2)
df.label.unique()
# changing hate speech to 1 and not hate speech to 0
df['label'] = df['label'].replace(0, 1)
df['label'] = df['label'].replace(2, 0)
df.label.unique()
plt.figure(figsize = (10,5))
ax = sns.countplot(df['label'], palette='Set2')
ax.set_title('Amount of Tweets Per Label',fontsize = 20)
ax.set_xlabel('Type of Tweet',fontsize = 15)
ax.set_ylabel('Count',fontsize = 15)
ax.set_xticklabels(['Not Hate Speech','Hate Speech'],fontsize = 13)
total = float(len(df)) # one person per row
for p in ax.patches:
height = p.get_height()
ax.text(p.get_x()+p.get_width()/2.,
height + 3,
'{:1.2f}'.format(height/total * 100) + '%',
ha="center")
plt.show('../visualizations/cleaned_class_imbalance.png', bbox_inches = "tight", pad_inches=.5) [Fig 6.3]
21

Fig 6.3: Cleaned class imbalance

# we can see that only 5.7% of the data is actual hate speech
df['label'].sum() / len(df['label'])
# creating new dataframe `clean_df`
raw_df = df[['count', 'hate_speech', 'offensive_language', 'neither', 'tweet', 'label']].copy()
# renaming those columns
raw_df.rename(columns={'count':'total_votes', 'hate_speech': 'hate_speech_votes', }, inplace=True)
# creating column for vote count for not hate speech tweets
raw_df['other_votes'] = raw_df['offensive_language'] + raw_df['neither']
# reordering the columns and dropping the old 'offensive_language' and 'neither' columns
raw_df = raw_df.reindex(columns=['total_votes', 'hate_speech_votes', 'other_votes','label', 'tweet']) [Fig
6.4]
# checking that it all worked
raw_df.head()

Fig 6.4: New clean data

# copying new `clean_df` and cleaning up the tweets


clean_df = raw_df.copy()
clean_df.head()
22

# function to clean all data


def clean_text_round1(text):
text = text.lower()
text = re.sub('\[.*?\]', '', text)
text = re.sub('\(.*?\)', '', text)
text = re.sub('\w*\d\w*', '', text)
text = re.sub('\s+', ' ', text)
text = re.sub('\n', ' ', text)
text = re.sub('\"+', '', text)
text = re.sub('(\&amp\;)', '', text)
text = re.sub('(@[^\s]+)', '', text)
text = re.sub('(#[^\s]+)', '', text)
text = re.sub('(rt)', '', text)
text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
text = re.sub('(httptco)', '', text)
return text
round1 = lambda x: clean_text_round1(x)
clean_df['clean_tweets'] = clean_df['tweet'].apply(round1)
clean_df.head() [Fig 6.5]

Fig 6.5: Cleaning tweet column

# checking for missing values


clean_df.isnull().sum()

Pickling clean_df for later use clean_df.to_pickle("../pickle/clean_df.pkl")

II. Sentiment Analysis with VADER

import numpy as np import pandas as pd import matplotlib.pyplot as plt


%matplotlib inline
plt.style.use('bmh')
import seaborn as sns
# pip install vaderSentiment
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
analyser = SentimentIntensityAnalyzer()
clean_df = pd.read_pickle('../pickle/clean_df.pkl')
# function to calculate polarity scores
pol = lambda x: analyser.polarity_scores(x)
# creating new column 'polarity' in clean_df
23

clean_df['polarity'] = clean_df['clean_tweets'].apply(pol)
# checking that worked
clean_df.head(2) [Fig 6.6]

Fig 6.6: Sentiment of Tweets


df = pd.concat([clean_df.drop(['total_votes', 'hate_speech_votes', 'other_votes', 'tweet', 'polarity'], axis=1),
clean_df['polarity'].apply(pd.Series)], axis=1)

df.head() [Fig 6.7]

Fig 6.7: Parsing Out Polarity Score

# new dataframe with average polarity score for each label


scores_df = df.groupby('label')['pos'].mean().reset_index(name='avg_positive')
scores_df['avg_neutral'] = df.groupby('label')['neu'].mean()
scores_df['avg_negative'] = df.groupby('label')['neg'].mean()
scores_df['avg_compound'] = df.groupby('label')['compound'].mean()
scores_df.head() [Fig 6.8]

Fig 6.8: Polarity Scores for Each Label

# creating dnsity plot for each label's polarity scores


plt.figure(figsize=(12, 8))
ax = sns.distplot(df['neg'][clean_df['label'] == 0], label='Not Hate Speech', color='green')
ax = sns.distplot(df['neg'][clean_df['label'] == 1], label='Hate Speech', color='red')
# setting label, title and legend
ax.set_title('Negativity of All Tweets', fontsize=20)
ax.set_ylabel('Density', fontsize=16)
ax.set_xlabel('Negativity Score', fontsize=16)
ax.legend(prop=dict(size=14))
24

plt.show('../visualizations/negativity_scores.png', bbox_inches = "tight", pad_inches=.5)[ Fig 6.9]

Fig 6.9: Negativity Scores

# creating dnsity plot for each label's polarity scores


plt.figure(figsize=(12, 8))
ax = sns.distplot(df['neu'][clean_df['label'] == 0], label='Not Hate Speech', color='orange')
ax = sns.distplot(df['neu'][clean_df['label'] == 1], label='Hate Speech', color='blue')
# setting label, title and legend
ax.set_title('Neutrality of All Tweets', fontsize=20)
ax.set_ylabel('Density', fontsize=16)
ax.set_xlabel('Neutrality Score', fontsize=16)
ax.legend(prop=dict(size=14))
plt.show('../visualizations/neutrality_scores.png', bbox_inches = "tight", pad_inches=.5)[Fig 6.10]

Fig 6.10: Neutrality Scores


25

# creating dnsity plot for each label's polarity scores


plt.figure(figsize=(12, 8))
ax = sns.distplot(df['pos'][clean_df['label'] == 0], label='Not Hate Speech', color='green')
ax = sns.distplot(df['pos'][clean_df['label'] == 1], label='Hate Speech', color='red')
# setting label, title and legend
ax.set_title('Positivity of All Tweets', fontsize=20)
ax.set_ylabel('Density', fontsize=16)
ax.set_xlabel('Positivity Score', fontsize=16)
ax.legend(prop=dict(size=14))
plt.show('../visualizations/positivity_scores.png', bbox_inches = "tight", pad_inches=.5)[Fig 6.11]

Fig 6.11: Positivity Scores

# creating dnsity plot for each label's polarity scores


plt.figure(figsize=(12, 8))
ax = sns.distplot(df['compound'][clean_df['label'] == 0], label='Not Hate Speech', color='purple')
ax = sns.distplot(df['compound'][clean_df['label'] == 1], label='Hate Speech', color='blue')
# setting label, title and legend
ax.set_title('Compound Polarity Score of All Tweets', fontsize=20)
ax.set_ylabel('Density', fontsize=16)
ax.set_xlabel('Compound Score', fontsize=16)
ax.legend(prop=dict(size=14))
plt.show('../visualizations/compound_polarity_score.png', bbox_inches = "tight", pad_inches=.5)[Fig 6.12]
26

Fig 6.12: Compound Scores


# function to add percentage above each polarity score

def autolabel(rects):
"""Add a text label above bars to display its perentage of data."""
for rect in rects:
height = rect.get_height()
ax.annotate('{:.3}'.format(height * 100) + '%',
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom')
# setting labls and average scores
labels = ['Hate Speech', 'Not Hate Speech']
all_positivity = df['pos'].groupby(df['label']).mean()
all_neutrality = df['neu'].groupby(df['label']).mean()
all_negativity = df['neg'].groupby(df['label']).mean()
# set width of bars
width = 0.2 # the width of the bars
# set position of bar on x-axis
x = np.arange(len(labels))
r1 = x
r2 = [x + width for x in r1]
r3 = [x + width for x in r2]
# graph figure
fig, ax = plt.subplots(figsize=(8, 6))
# 3 types of grouped bar graphs
ax1 = ax.bar(r1, all_positivity, width, label='Positivity', color='green')
ax2 = ax.bar(r2, all_neutrality, width, label='Neutrality', color='purple')
ax3 = ax.bar(r3, all_negativity, width, label='Negativity', color='firebrick')
# applying percentage display function
autolabel(ax1)
autolabel(ax2)
autolabel(ax3)
# setting axis labels, title and legend location
ax.set_ylabel('Scores')
ax.set_title('Average Polarity Scores by Tweet Type')
27

ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()
plt.show('../visualizations/avg_polarity_by_tweet_type.png', bbox_inches = "tight", pad_inches=.5)[Fig
6.13]

Fig 6.13: Average Polarity Scores

III. Natural Language Processing

import numpy as np import pandas as pd import matplotlib.pyplot as plt


from matplotlib import cm import seaborn as sns; sns.set()
%matplotlib inline
# packages for NLP preprocessing
import nltk from sklearn.feature_extraction import text
from nltk.stem import WordNetLemmatizer from nltk.probability import FreqDist
from nltk.corpus import stopwords from nltk.stem import PorterStemmer
from nltk.collocations import *
import pickle
clean_df = pd.read_pickle('../pickle/clean_df.pkl') clean_df.head()
tweet_df = clean_df[['clean_tweets', 'label']].copy() tweet_df.head()
data = tweet_df['clean_tweets']
target = tweet_df['label']
def unfiltered_tokens(text):
dirty_tokens = nltk.word_tokenize(text)
return dirty_tokens
unfilterd_data = list(map(unfiltered_tokens, data))
flat_unfiltered = [item for sublist in unfilterd_data for item in sublist]
dirty_corpus_freqdist = FreqDist(flat_unfiltered)
dirty_corpus_freqdist.most_common(20)
stop_words = set(stopwords.words('english'))
def process_tweet(text):
tokens = nltk.word_tokenize(text)
stopwords_removed = [token.lower() for token in tokens if token.lower() not in stop_words]
return stopwords_removed
processed_data = list(map(process_tweet, data))
total_vocab = set()
28

for comment in processed_data:


total_vocab.update(comment)
len(total_vocab)
pickle_out = open('../pickle/processed_data.pkl','wb')
pickle.dump(processed_data, pickle_out)
pickle_out.close()
flat_filtered = [item for sublist in processed_data for item in sublist]
clean_corpus_freqdist = FreqDist(flat_filtered)
clean_corpus_freqdist.most_common(20)
lemmatizer = WordNetLemmatizer()
lemmatized_output = []
for listy in processed_data:
lemmed = ' '.join([lemmatizer.lemmatize(w) for w in listy])
lemmatized_output.append(lemmed)
X_lem = lemmatized_output
y_lem = target
pickle_out = open('../pickle/X_lem.pkl','wb')
pickle.dump(X_lem, pickle_out)
pickle_out.close()
y_lem.to_pickle('../pickle/y_lem.pkl')
bigram_measures = nltk.collocations.BigramAssocMeasures()
bigram_finder = BigramCollocationFinder.from_words(flat_filtered)
corpus_scored = bigram_finder.score_ngrams(bigram_measures.raw_freq)
corpus_scored[:20]
pmi_finder = BigramCollocationFinder.from_words(flat_filtered)
pmi_finder.apply_freq_filter(5)
pmi_scored = pmi_finder.score_ngrams(bigram_measures.pmi)
pmi_scored[:30]
bigrams_list = pmi_scored[:30]
bigrams_list
bigrams_series = (pd.Series(nltk.ngrams(flat_filtered, 2)).value_counts())[:20]
bigrams_series.sort_values().plot.barh(color=cm.viridis_r(np.linspace(.4,.8, 30)), width=.9, figsize=(12, 8))
plt.title('20 Most Frequently Occurring Bigrams', fontsize=18)
plt.ylabel('Bigram', fontsize=18)
plt.xlabel('Number of Occurances', fontsize=18)
bigrams_df = pd.DataFrame(bigrams_list,columns=['bigram', 'pmi_score'])
bigrams_df.head()[Fig 6.14]

Fig 6.14: Bigram Scores


29

References

[1]. Abro, Sindhu, ZA Sarang Shaikh, Sajid Khan, Ghulam Mujtaba, and Zahid Hussain Khand.
"Automatic Hate Speech Detection using Machine Learning: A Comparative Study." Machine
Learning 10, no. 6 (2020).
[2]. Moy, Tian Xiang, Mafas Raheem, and Rajasvaran Logeswaran. "Hate Speech Detection in
English and Non-English Languages: A Review of Techniques and Challenges." Technology
(2021).
[3]. Jahan, Md Saroar, and Mourad Oussalah. "A systematic review of Hate Speech automatic
detection using Natural Language Processing." arXiv preprint arXiv:2106.00742 (2021).
[4]. Papcunová, Jana, Marcel Martončik, Denisa Fedáková, Michal Kentoš, Miroslava
Bozogáňová, Ivan Srba, Robert Moro, Matúš Pikuliak, Marián Šimko, and Matúš Adamkovič.
"Hate speech operationalization: a preliminary examination of hate speech indicators and their
structure." Complex & Intelligent Systems (2021): 1-16.
[5]. Dhamija, Tashvik, and Rahul Katarya. "Comparative analysis of machine learning and deep
learning algorithms for detection of online hate speech." In Advances in Mechanical Engineering,
pp. 509-520. Springer, Singapore, 2021.
[6]. Mullah, Nanlir Sallau, and Wan Mohd Nazmee Wan Zainon. "Advances in Machine Learning
Algorithms for Hate Speech Detection in Social Media: A Review." IEEE Access (2021).
[7]. Robinson, David, Ziqi Zhang, and Jonathan Tepper. "Hate speech detection on twitter: Feature
engineering vs feature selection." In European Semantic Web Conference, pp. 46-49. Springer,
Cham, 2018.

You might also like