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

TOXIC COMMENT ANALYSER

By

ASHISH KUMAR 678


AMARJIT HORE 669
ROSHAN KUMAR 729

Bachelor/Master Thesis submitted to

Indian Institute of Information Technology Kalyani

for the partial fulfilment of the degree of

Bachelor of Technology
in
Computer Science and Engineering/Information Technology
November, 2023

1
CERTIFICATE

This is to certify that the project entitled "Toxic Comment Classifier" is


being submitted by Ashish Kumar, Amarjit Hore, and Roshan Kumar
undergraduate students in the Department of Computer Science and
Engineering, Indian Institute of Information Technology Kalyani, West
Bengal 741235, India.

For the award of Bachelor of Technology in Computer Science and


Engineering, this project represents original research work carried out by
them under my supervision and guidance.

The project has fulfilled all the requirements as per the regulations of
the Indian Institute of Information Technology Kalyani. In my opinion, it has
reached the standards needed for submission. The work, techniques, and
the results presented have not been submitted to any other university or
institute for the award of any other degree or diploma.

……………………….
Dr. Anirban Lakshman
Assistant Professor,
Department of Computer Science and Engineering
Indian Institute of Information Technology Kalyani
West Bengal 741235, India
10/11/2023

2
DECLARATION

We hereby affirm that the research presented in this report, titled "Toxic
Comment Analyser" has been submitted to the Indian Institute of
Information Technology Kalyani in partial fulfilment for the degree of
Bachelor of Technology in Computer Science and Engineering.
The work was conducted from July 2023 to Nov 2023 under the
guidance of Dr. Anirban Lakshman, Department of Computer Science and
Engineering, Indian Institute of Information Technology Kalyani, West
Bengal - 741235, India. We declare that the report does not contain any
classified information.

Candidates:
- Ashish Kumar 678
- Amarjit Hore 669
- Roshan Kumar 729

Department: Computer Science and Engineering


Institute Name: Indian Institute of Information Technology Kalyani

3
ACKNOWLEDGEMENT

First and foremost, we would like to thank our guide, Dr Anirban


Lakshman, for having suggested the topic of our project and for his
constant support and guidance, without which we would not have
been able to attempt this project.We also thank other resources which
we have mentioned in our references

Candidates:
Ashish Kumar 678
Amarjit Hore 669
Roshan Kumar 729

IIIT Kalyani
26/11/2023

4
ABSTRACT

In the ever-expanding digital landscape, online platforms provide


spaces for diverse interactions, yet the prevalence of toxic comments
poses significant challenges to maintaining a healthy online environment.
This project addresses the critical issue of identifying and classifying toxic
comments using advanced machine learning techniques. The Toxic
Comment Classifier employs a state-of-the-art deep learning model,
incorporating a bidirectional LSTM network and embedding layers for
effective feature extraction from textual data.

The project adheres to the rigorous standards set by the Indian Institute
of Information Technology Kalyani, fulfilling all regulatory requirements.
Under the expert supervision and guidance of their mentor, the students
demonstrate a comprehensive understanding of natural language
processing and deep learning techniques.

The results showcase the efficacy of the Toxic Comment Classifier in


distinguishing toxic comments, with precision, recall, and accuracy metrics
reflecting the model's robust performance. The work presented in this
project is an original contribution, and the findings have not been
previously submitted for any other degree or diploma.

This research not only contributes to the field of natural language


processing but also addresses the pressing need for effective tools to
maintain a positive and inclusive online environment. The Toxic Comment
Classifier stands as a testament to the commitment of the students and
the academic community at the Indian Institute of Information Technology
Kalyani towards fostering responsible and respectful online
communication.

5
CONTENTS

Chapters

1. Problem Statement 7

2. Objective of Problem 7

3. Literature Survey 9

4. Proposed System 11

5. Methodology 12

6. Output Interface 22

7. Observations 23

8. Conclusion 23

9. Future Scope of Work 24

10. References 25

6
Problem Statement

To build a prototype of online hate and abuse comment classifier


which can used to classify hate and offensive comments so that it can
be controlled and restricted from spreading hatred and cyberbullying.

Objective
Automated Detection of Toxic Comments:
- Develop a machine learning model capable of automatically
detecting toxic comments within digital content.
- Implement natural language processing techniques to analyze
and understand the linguistic features associated with toxicity.

Multi-Class Categorization
- Enable the classifier to categorize toxic comments into specific
classes, such as "toxic," "severe toxic," "obscene," "threat," "insult,"
and "identity hate."
- Provide a nuanced classification system to better understand
and address different forms of toxic language.

Model Robustness and Accuracy


- Train the model to achieve high accuracy in identifying and
categorising toxic comments.
- Ensure the robustness of the classifier to effectively handle
variations in language and context.

Scalability and Efficiency


- Design the Toxic Comment Classifier to scale efficiently,
allowing it to process a large volume of comments in real-time.

7
- Optimise the model for computational efficiency without
compromising accuracy.

User-Friendly Integration
- Develop an interface or integration method that allows users to
easily incorporate the Toxic Comment Classifier into various online
platforms.

8
Literature Survey

​ Early Approaches

Early efforts in toxic comment classification primarily relied on


rule-based methods and keyword filtering. These methods, though
straightforward, often lacked the ability to capture the nuances and
context-dependent nature of toxic language. As a result, researchers
began to shift towards machine learning-based approaches.

​ Machine Learning Approaches

a. Supervised Learning:

The majority of research in toxic comment classification has adopted


supervised learning techniques. Various algorithms, including Support
Vector Machines (SVM), Naive Bayes, and decision trees, have been
employed to train models on labelled datasets. These models leverage
features such as bag-of-words, TF-IDF, and word embeddings to identify
patterns associated with toxic language.

b. Deep Learning:

The advent of deep learning has significantly impacted the field, with
recurrent neural networks (RNNs), long short-term memory networks
(LSTMs), and more recently, transformer-based models such as BERT and
GPT, achieving state-of-the-art performance. These models excel in
capturing contextual information and semantic relationships, enabling
them to effectively identify subtle instances of toxicity.

​ Challenges and Open Problems

Despite the progress made in toxic comment classification, several


challenges persist. The dynamic nature of online language, evolving

9
patterns of toxicity, and the existence of cultural and contextual variations
pose difficulties for model generalisation. Additionally, issues related to
bias and fairness in models, especially those trained on biassed datasets,
need careful consideration.

10
PROPOSED SYSTEM
In the dynamic realm of online communication, the unrestricted
exchange of ideas on digital platforms has empowered diverse
voices. However, this openness has also given rise to the persistent
challenge of toxic comments, which can undermine the constructive
nature of online discussions. Recognizing the gravity of this issue, our
project, the Toxic Comment Classifier, spearheaded by
undergraduate students Ashish Kumar, Amarjit Hore, and Roshan
Kumar from the Department of Computer Science and Engineering at
the Indian Institute of Information Technology Kalyani, delves into the
intricate domain of natural language processing and machine
learning.

As we navigate through the intricacies of the Toxic Comment


Classifier, we not only showcase the technical prowess of our
students but also emphasize the importance of responsible and
respectful online communication. The outcomes of this project stand
not only as a testament to academic excellence but also as a
proactive step toward fostering a digital landscape where constructive
dialogue can thrive, free from the shadows of toxicity.

11
METHODOLOGY
Dataset and Training

Dataset Description:
-Taken from Kaggle.In Total 15971 comments in dataset.
-The Toxic Comment Classification project utilizes a dataset
containing comments from online platforms labeled for various forms
of toxicity.
-The dataset, in a CSV format.
-The comments were manually classified into following categories:
1. Toxic
2. Severe toxic
3. Obscene
4. Threat
5. Insult
6. Identity hate

Software and Hardware Requirements

12
1. Data Preprocessing
● Dataset Loading: Load the toxic comment dataset
(e.g., 'train.csv') containing comments and corresponding toxicity
labels.
● Text Vectorization: Utilise the TextVectorization layer to convert
raw text into numerical vectors, allowing for efficient processing by
the model.
● Label Preparation: Extract the target labels (toxicity categories)
and format them for model training.

import pandas as pd
from tensorflow.keras.layers
import TextVectorization

# Load dataset
df = pd.read_csv('train.csv/train.csv')

# Text Vectorization
X = df['comment_text']
y = df[df.columns[2:]].values
vectorizer = TextVectorization(max_tokens=MAX_FEATUR

output_sequence_length=1800,
output_mode='int')
vectorizer.adapt(X.values)
vectorized_text = vectorizer(X.values)

13
2. Model Architecture

​ Logistic Regression Implementation

Logistic Regression is a statistical method for analysing a dataset in


which there are one or more independent variables that predict an
outcome. It is commonly used for binary classification problems.

​ Advantages:
● Interpretability: Results are easily interpretable, providing
probabilities for class membership.
● Efficiency: Computationally efficient and does not require
high computational resources.
● Less Prone to Overfitting: Less susceptible to overfitting
compared to more complex models when the feature space
is small.
​ Disadvantages:
● Linear Decision Boundary: Limited to linear decision
boundaries, which might be a drawback for complex
datasets.
● Assumption of Linearity: Assumes a linear relationship
between independent variables and the log-odds of the
dependent variable.
● Sensitivity to Outliers: Sensitive to outliers, which can
impact the model's performance.
​ Use Cases:
● Binary Classification: Well-suited for problems with two
classes, such as spam detection or disease diagnosis.
● Probabilistic Predictions: Useful when probability estimates
for class membership are required.
​ Implementation:
● Algorithm: Uses the logistic function to model the
probability of a particular outcome.

14
● Optimization: Typically optimised using techniques like
gradient descent.
​ Scalability:
● Scalability: Scales well with the number of features but may
not be the best choice for large and highly complex
datasets.

Bidirectional LSTM Implementation

Embedding Layer: Create an embedding layer to convert


integer-encoded vocabulary into dense vectors.
Bidirectional LSTM: Implement a Bidirectional LSTM layer to
capture contextual information from both directions in the sequence.

Bidirectional LSTM Layer*: A bidirectional LSTM layer is added to


capture contextual information from the comment text. The LSTM
layer has 32 units and uses the hyperbolic tangent (tanh) activation
function.

15
Fully Connected Layers: Three fully connected layers with ReLU
activation functions are added as feature extractors. The layer sizes
are 128, 256, and 128, respectively.

Output Layer: Include a dense output layer with sigmoid activation


for multi-label classification.

The model is compiled with the Binary Cross Entropy loss function
and the Adam optimizer.

16
Understanding How LSTM Works:

Long Short-Term Memory networks, or LSTMs, are a type of


recurrent neural network (RNN) architecture designed to overcome
the challenges of learning long-term dependencies in sequential data.
Unlike traditional RNNs, LSTMs possess a more sophisticated
memory cell that allows them to capture and retain information over
extended sequences, making them particularly effective in natural
language processing tasks, time series analysis, and other
applications involving sequential data.

Here's a breakdown of the key components and mechanisms that


make LSTMs work effectively:

1. Memory Cell:
- The core of an LSTM is its memory cell, which serves as a
storage unit capable of retaining information over long periods. This
memory cell is responsible for keeping track of relevant information
from earlier parts of the sequence.

2. Three Gates:
- LSTMs employ three gates to regulate the flow of information:
the input gate, forget gate, and output gate

- The input gate determines which values from the input should
be stored in the memory cell.
- The forget gate decides what information to discard from the
memory cell.
- The output gate regulates the information that should be output
based on the current input and the memory cell content.

17
3. Cell State:
- The memory cell maintains a continuous 'cell state' that runs
through the entire sequence. This state is modified by the gates,
allowing the LSTM to selectively update, add, or remove information
from the cell state.

4. Hidden State:
- The hidden state is the LSTM's way of capturing and storing
information from previous time steps. It acts as a summary or
representation of the relevant information learned from the entire
sequence.

5. Training and Backpropagation:


- During training, LSTMs use a variant of backpropagation called
**Backpropagation Through Time (BPTT)**. This involves unfolding
the network across time steps and calculating gradients to update the
model's parameters.

6. Advantages of LSTMs:
- Long-Term Dependencies: LSTMs excel at capturing and
learning dependencies over extended sequences, making them
suitable for tasks requiring an understanding of context over time.
- Gradient Flow: The gating mechanisms help in mitigating the
vanishing and exploding gradient problems that often hinder the
training of traditional RNNs.
- Versatility: LSTMs can be applied to a wide range of sequential
data tasks, including natural language processing, speech
recognition, and time series prediction.

In summary, LSTMs address the limitations of traditional RNNs by


introducing memory cells and gating mechanisms, enabling them to
effectively capture long-term dependencies in sequential data. This

18
makes them a powerful tool for tasks that involve understanding
context and relationships across extended sequences.

Bidirectional LSTM

One shortcoming of conventional RNNs is that they are only able to


make use of previous context. … Bidirectional RNNs (BRNNs) do
this by processing the data in both directions with two separate
hidden layers, which are then fed forwards to the same output layer.
… Combining BRNNs with LSTM gives Bidirectional LSTM, which
can access long-range context in both input directions

3. Model Compilation and Training:

Compilation: Compile the model using the binary cross entropy


loss function and the Adam optimizer.
Training: Train the model on the preprocessed dataset, specifying
the number of epochs and validation data.

model.compile(loss='BinaryCrossentropy', optimizer='Adam')
history = model.fit(train, epochs=6, validation_data=val)

4. Evaluation and Metrics:

Precision, Recall, Accuracy: Utilize precision, recall, and categorical


accuracy metrics to evaluate the model's performance on the test set.

19
from tensorflow.keras.metrics import Precision, Recall,
CategoricalAccuracy

pre = Precision()
re = Recall()
acc = CategoricalAccuracy()

for batch in test.as_numpy_iterator():


X_true, y_true = batch
yhat = model.predict(X_true)

y_true = y_true.flatten()
yhat = yhat.flatten()

pre.update_state(y_true, yhat)
re.update_state(y_true, yhat)
acc.update_state(y_true, yhat)

print(f'Precision: {pre.result().numpy()},
Recall:{re.result().numpy()}, Accuracy:{acc.result().numpy()}')

5. Model Saving and Loading:


model.save('toxicity.h5')

6. Integration with Gradio for Demo:


import gradio as gr

def score_comment(comment):
vectorized_comment = vectorizer([comment])
results = model.predict(vectorized_comment)

text = ''
for idx, col in enumerate(df.columns[2:]):

20
text += '{}: {}\n'.format(col, results[0][idx]>0.5)

return text

interface = gr.Interface(fn=score_comment,
inputs=gr.inputs.Textbox(lines=2,
placeholder='Comment to score'),
outputs='text')
interface.launch(share=True)

21
Output

Toxic Comment as input, Output shows toxic is True

Non-Toxic Comment as input, Output shows toxic is false

22
Observation

Loss Graph

Model Scores

Conclusion
Final Model is giving us an Accuracy score of 92.41% which is
slightly improved compared to earlier Accuracy score of 92.0%.LSTM
classifier is fastest algorithm compared to others.

23
Future Scope Of Work

1. Multilingual Toxic Comment Classifier: The Application can


analyse comments in more languages like French, Spanish,
German, and even Hindi.

2. Sentiment Analysis: The application categorises comments into


positive, negative, and neutral sentiments to determine the nature
of the following that a social media influencer has.

3. Fake Comment Detection: It can identify potentially fake or spam


comments, thereby ensuring the authenticity of the feedback the
influencer receives.

4. Report Generation: The tool provides an easy-to-understand


comprehensive report on an influencer's online following, thereby
assisting them in effectively managing their online reputation

24
References

1. "Detecting Hate Speech in Social Media"


a. Authors: Thomas Davidson, Dana Warmsley, Michael Macy,
Ingmar Weber
b. Published in: Proceedings of the 24th International
Conference on World Wide Web, 2015.
2. "LSTM-based Deep Learning Models for Non-factoid Answer
Selection"
a. Authors: S. Severyn, A. Moschitti
b. Published in: Proceedings of the 54th Annual Meeting of
the Association for Computational Linguistics (ACL), 2016.
3. "Deep Learning for Hate Speech Detection in Tweets"
a. Authors: Pranav Goel, Karan Jindal
b. Published in: Proceedings of the First Workshop on Abusive
Language Online, 2017.
4. "Detecting Offensive Language in Social Media to Protect
Adolescent Online Safety"
a. Authors: Zeerak Waseem, Dirk Hovy
b. Published in: Proceedings of the 2016 Conference of the
North American Chapter of the Association for
Computational Linguistics: Human Language Technologies,
2016.
5. "A Hierarchical Model of Reviews for Aspect-based Sentiment
Analysis with Latent Sentence Representation"
a. Authors: Duyu Tang, Bing Qin, Ting Liu
b. Published in: Proceedings of the 2016 Conference on
Empirical Methods in Natural Language Processing, 2016.

25

You might also like