Why social media comments are toxic?
Toxic Comment Classification
To help online discussions become more productive and respectful, this project builds models that are capable of detecting different types of toxicity from a dataset of comments from Wikipedia’s talk page edits that is full of toxic comments. The overall procedures are first data cleaning, then deep research on the data to categorize the comments and find out relevant and important indicators, and build useful models to detect the toxicity of comments. By applying a classification tree model, the result shows that insult comments and toxic/obscene comments can be effectively detected.
Introduction
In the project, we have a dataset of comments from Wikipedia’s talk page edits that is full of toxic comments. Our goal for this project is to build models that are capable of detecting different types of toxicity like threats, obscenity, insults, and identity-based hate. It is important because the toxic comments dataset represents the study of negative online behaviors, and if we can use our model to detect the toxic comments, our project may help online discussions become more productive and respectful. In our project, we would first clean our dataset, then create relevant identifier variables to build models, and try to improve our model for a better result.
Data – Cleaning and EDA
The very first step is to clean our dataset “Jigsaw”. “Jigsaw” has 159571 rows and 8 columns, and there are 8 variables: “id”, “comment_text”, “toxic”, “severe_toxic”, “obscene”, “threat”, “insult”, “identity_hate”. By looking at the dataset, “comment_text” is the text comments that we want to focus on, and we can clearly see that “comment_text” are full of words, punctuations, and numbers. Numbers and punctuations are hard to read when using R, so the overall goal for data cleaning is to separate the comments into words, remove any punctuation or numbers, and any unnecessary words. We first remove all the punctuation marks and numbers in the “comment_text”, then we can extract our responsible text. The next step is to tokenize a text and clean the tokenized comments by checking stop words inside. Therefore, we can come up with a cleaned version dataset.
The second step is to visualize the data.
We can check the variables and gain more useful information to fit our model. Overall speaking, as we want to check the comments’ toxicity, there are 6 different possible classifications of toxicity of comments.
In our cleaned data, there are six useful variables: toxic, severe toxic, obscene, threat, insult, identity hate. In the dataset, if the comment satisfies the label of the variable, it will appear “1” in the corresponding variable, otherwise it will appear “0”. Obviously, these 6 variables are all binary. By checking the total number of comments that are labeled in each of the categories. From our data, 15294 comments are labeled as toxic, 1595 comments are labeled as severe toxic, 8449 comments are labeled as obscene, 478 comments are labeled as threat, 7877 comments are labeled as insult, and 1405 comments are labeled as identity hate.
To have a simpler visualization and understanding, we then create a new binary classifier called “isNasty” into our dataset.
The variable “isNasty” takes on the value of 1 if the comment falls into at least one of the 6 categories of toxicity, and 0 otherwise. Then we can call a comment “nasty” if “isNasty” has a value of 1, and “neutral” if “isNasty” has a value of 0. In this way, we can see that our data has 16225 nasty comments and 143346 neutral comments. Apparently, about 10.16% of all the comments are nasty. Then we can divide the whole cleaned dataset into two separate datasets: one holding only the Nasty comments, and the other holding the Neutral comments. As we only focus on the toxic comments, our model would base on the Nasty comments.
Our next step is to create several subcategories for future modeling.
In nasty comments, we have 6 different variables, and we would like to build subcategories that are most representative. The way to build subcategories is to create a new dataset for the chosen variable, then extract the data and unnest its comments. From previous results, we can see that most of nasty comments are labeled as toxic, so we would like to omit this variable into any subcategories, as it is not representative enough. We then decide to combine the severe toxic comments and obscene comments together, as the two variables have highly common nasty comments in between. To make sure our choice, we drew a word cloud plot to figure out the overlaps:
Plot1: Word cloud plot for severe toxic and obscene comments
Eventually, we made our four subcategories to be: hate, threat, insult and severe toxic/obscene. By checking the number of “1” s in each subcategory, we have a “hate” subcategory with 237 comments, an “insult” subcategory with 7877 comments, a “threat” subcategory with 171 comments and a “toxic/obscene” subcategory with 7940 comments.
The next important thing in EDA is to choose our indicators for the model. As now we have 4 subcategories, well-picked indicators can better help us to decide which
comment will fall into a certain subcategory. Our way of choosing indicators is to use bar plots to see the most unique words in each subcategory. For example, in threat subcategory, we can see the bar plot of the word frequency:
Plot2: bar plot for top 10 words appeared in threat comments
Probably because the specialty of threat comments, its common words are not strongly overlapping with other subcategories, and words like “jim” and “wales” are more likely to be people’s names: these words are unique enough to be chosen as indicators.
Similarly, we picked a list of words that are unique for every subcategory and created new columns for every single word. Therefore, if the comment matches the indicator, then the value of a certain indicator would change from 0 to 1, then we can easily implement our model by using these indicators. Our list of the indicators is “di-”, “ki-”, “je-”, “jim”, “su-”, “fu-”, “cu-”, “sh-”, “ni-”, “ga-” and “ba-”.
Methods
Classification tree model:
We would like to apply a classification tree model to our refined dataset. The goal is to create a model that predicts the subcategory of our comment based on several input variables, which are our indicators. A classification tree is a tree in which each internal (non-leaf) node is labeled with an input feature. The arcs coming from a node labeled with an input feature are labeled with each of the possible values of the target feature or the arc leads to a subordinate decision node on a different input feature. Each leaf of the tree is labeled with a class or a probability distribution over the classes, signifying that the data set has been classified by the tree into a specific class.
To realize our tree model, we first separate our data into test data and training data. Our split is 90% training, 10% test. Therefore, we can use the training data to build and train the model and use the test data to test the accuracy of our model. After applying the classification tree model on the training data, our result is:
Plot3: classification tree model on training data
The four subcategories in classes are in the same order as before: hate, threat, insult and severe toxic/obscene.
Results
Classification tree model:
According to the plot of our classification tree, 22% of training comments fall into the insult category. And the remaining 78% of the training comments fall into the toxic/obscene category. Also, we can check the estimating accuracy for the four subcategories. For insult category, estimated accuracy is around 70%, and for toxic/obscene category. Moreover, estimated accuracy is around 54%, but we cannot receive any useful information for the other two subcategories.
Another useful technique is to compute CER (classification error rate) for both training data and test data. CER is the proportion of instances misclassified over the whole set of instances. By computing the CER, we can get the idea of overall accuracy on the dataset. According to the plot, we calculated the CER based on the bottom nodes and got a test CER of 0.4115. This means that around 41.15% percent of test data is not accurate enough. We also generated a prediction table to calculate the training CER:
Table1: prediction table for training data
Then we can calculate the training CER of 0.4198, which means around 41.98% percent of training data is not accurate enough. Therefore, we can conclude that this model is not effective at classifying hate speech. It does not classify any comments into
the hate or threat categories as these categories were missing in our tree model result.
Conclusion and Future Work
From our classification tree model, we can clearly categorize our comments into insult comments and toxic/obscene comments. However, it is still not effective enough as we still miss two other categories. One possible reason for this could be the categories are too small to have comments detected in them. And another possible reason could be our choice of indicators are not helpful enough. In order to fix our model, we would need to reclassify the data into more evenly distributed categories. And lastly find our more useful indicators. An available way is to combine several existing indicators together with new ones. Moreover, use the new indicators in the tree model. As a result, which may lead to a more reasonable result.


