Editor’s introduction
The study focused on identifying and categorizing offensive language in user comments across Stack Overflow, GitHub, Gitter, and Slack. The best-performing models achieved an average F1-score of 0.85, with some reaching up to 0.94. The system also included paraphrasing capabilities to generate non-offensive alternatives while preserving original sentiment. Participants noted the value of categorization and sentiment preservation, though one suggested expanding the system’s ability to address nuanced abuse types.
Introduction
RQ1: What is the nature and prevalence of offensive language in SE communities? RQ2: How can an Offensive Language Reduction System for Software Engineering (OLRS-SE) be developed? RQ2.1: How can we develop approaches to accurately detect and classify offensive language in online SE communities, using machine learning techniques? RQ2.2: How can we develop approaches to explain offensive language to its poster and also rephrase offensive ones into less offensive ones using language models?
Data collection
Of the 311073 comments with five classes, the SO dataset contained two offence classes which we consider in this work: Rude/Offensive (RO) and Unfriendly/Unwelcoming (UN). RO comments (108473, 34.87%) were flagged by users or the SO automatic flagging system (a bot) and have also been judged rude or offensive by human moderators. Those comments were subsequently removed from the site as part of the moderation, as these comments are highly offensive or toxic in nature. The UN comments (28030, 9.01%) are comments with less offensive content than RO, but through the flagging and moderation process, they were also removed from the site because they were deemed unfriendly. Hence, these two classes of comments contain offensive language, as they elicited sanctions for breaching the CoC of the site (i.e., they were deleted from the site). We treat both RO and UN labels as identifying unwelcoming comments (i.e., offensive and unfriendly comments, respectively).
Results of user evaluation
In order to solicit descriptive suggestions from the participants, we used two questions at the end of the survey on aspects they liked in the system and what they would like to be improved. Regarding the functionalities they found useful, one wrote “The ability to adjust swearing without removing good sentiment was nice”. This supports our attempts to maintain the overall tone of the statement (e.g., keeping user’s sentiments while rephrasing highly offensive phrases). Another noted “I find categorizing the offensive comments significant as it gives an idea on the sort of areas an offensive comment target [sic]”. One suggestion for future work was to create a plugin that has the features of the OLRS-SE as expressed by a user who noted “The OLRS-SE system is useful to detect offensive language and also very useful to get paraphrased comments. I would like to see and use when this [is] integrated to any other online platforms that are currently used or even to get as a google extension”. A few opined that the system needs improvement, especially for improving the speed of providing paraphrased suggestions. We believe deploying the models in faster servers can address this issue.
While expressing feedback on other ways of reducing offensive language in SE communities that are not yet a part of the tool, making ML models to continuously learn from user feedback was suggested. The feedback noted “If there is a way you can add continuous online learning and improvement such that if the user is not satisfied with the feedback (rephrased comments), he can make suggestions and the ML model will improve from user feedback as well”. This is a useful suggestion for future work where human-in-the-loop is incorporated as a part of continuous learning, and this can be achieved when the tool is integrated as a part of real-life systems such as Stack Overflow. In the next section we provide a discussion of our results and the threats to validity.
Discussion
The reported cross-platform analysis was done as a pilot study on other datasets. However, we observed significant vocabulary differences and varying length in the comments of other platforms. For example, GitHub comments found to be long and slack comments were the short in general. Gitter comments were of similar length as that of SO, which produced similar result metrics to SO comments. Given that we limited our comment length to 250 characters, this might have affected the performance of ML models on various classes of offence detection in GitHub comments.
Offence explanation and paraphrasing: In addressing RQ2.2, we developed solutions to reduce offensive content in textual comments using paraphrasing approaches. There were two main pathways for paraphrasing an offensive comment: a modified GPS pipeline to paraphrase SE-related comments and a counter-narrative generation approach for non-SE comments. The paraphrasing module consumes input from the LIME-based explainer module to locate the most offensive tokens in a comment. We used LIME to locate the most offensive five terms in an input comment. 1% of the 200 comments we evaluated contained more than five offensive terms3434 34 Our limit of five was based on pragmatism. However, we acknowledge that we are unable to rule out sentences that have more than five offensive terms, and future work can investigate a higher limit (e.g., 10 or 15), or even finding all offensive terms..
While analysing the results of the modified GPS pipeline to paraphrase offensive comments, overall, the model provided good results (an average J score of 0.8) indicating high quality paraphrased suggestions that can replace offensive comments. We believe that the high quality is due to the stringent measures to compute the J score. When the suggestions produced were unable to meet the J score threshold, the seq2seq model was used, and this ensured that there was a paraphrased output for every offensive comment. The provided suggestions would make a writer think twice before posting an offensive comment. Thus, readers of these comments are less likely to be offended and this approach provides a pathway for the community to be free from toxic contents.
Future work: For some of the labels, the performance of the best model is less than desirable. For example, the F1-score for the target-detection model (Level-2 UN) was only 0.56. A key future work is to improve the performance of these models by collecting more training data (for deep learning models) and identifying effective sets of characteristics (for traditional models) to precisely detect such offences. In addition, we identified that the length of the comments varies between the various SE platforms included in this study. For example, GitHub comments were found to be lengthier than the other three, and Slack comments were observed to be shorter than the rest. This also might have contributed towards reduced ML result metrics. Important directions for future work include a systematic sensitivity analysis of setting thresholds for various metrics (e.g., LIME and J score) and quality evaluation of output suggestions from the seq2seq model.
Another future work is to develop the tool as an extension to web browsers (e.g., as plug-ins) so that offensive language can be detected across the internet spaces where rephrased suggestions can be provided, in order to make those places welcoming. Prompt-based offensive language reduction is another potential strand to be considered as an extension to the system so that when an offensive comment is entered, the system would be able to provide some prompting messages such as Are you sure?, along with the opportunity to rewrite the comment. A similar approach has been implemented in Tinder.
Threats to validity
Internal validity - The foremost threat to internal validity is the trustworthiness of the various sub-systems that we used, for example, Regex and PAPI. We collected the complete set of Regex from SO, but not sure whether there can be anymore, or would catch all obfuscated offensive terms in English language. Similarly, the vulnerability of PAPI score is also a threat, that have questioned the integrity of PAPI score using adversarial examples.
Another threat is the linguistic barrier: we considered the comments generated in English only. However, users can post offensive posts in any language. Our proposed system is not able to detect such offence. Moreover, we have considered comments in textual form only, not in the form of emojis or other visual ways (e.g., GIFs) to represent or express offence. Also, while considering comments in English alone, the research is not able to detect offence with 100% accuracy (as shown in our results) due to the nuanced nature of expressing language. Even though the ML models are trained on a reasonable amount of data, human creativity to use context-specific offensive language can fool systems put in place to reduce offence. In particular, this can be an important issue in detecting mild offence (i.e., UN offence) which can be subtle and nuanced.
Another internal threat is the upper limit of 250 characters in the length of comments considered by this research. If someone expresses longer offensive contents, the system may fail to detect and classify the offence. This is due to the way our ML models are trained (which target short comments), even though the approach can be scaled in future work by considering longer text. However, we observed that only around 6% of the total RO and UN comments were beyond that limit. Another threat is the complexity of the OLRS-SE pipeline.
Another threat is that we did not consider the context around the comment (e.g., whether the comment provided for a question or answer, the type of question and what other users have commented). Considering the context of commenting could be useful in accurately determining whether a comment is offensive.
External validity - The developed OLRS-SE has been found to provide promising results for comments from all the four platforms. However, the scalability of the system beyond these platforms needs to be evaluated before deploying the system as a decision support system for users of other SE platforms. Moreover, since the ML models for offence detection and classification are trained on SE-related datasets, the OLRS-SE pipeline cannot be directly used on other domains such as general social media platforms. Fine-tuning using relevant datasets and testing the system with comments from targeted social media platforms will be required.
Related work
This section compares the contributions of our current work with prior work in the SE domain on offensive language, focusing on the four phases of OLRS-SE framework (offence detection, classification, explanation and rephrasing). Detection refers to the presence or absence of offensive language within a text snippet. Offence classification is the process of detecting fine-grained attributes (such as the targets of abuse) in an offensive text. A fine-grained topology of abuse was proposed by Waseem et al., that includes the type of abuse (explicit or implicit), possible targets of abuse (e.g., individual or group) and fine-grained classes of abuse (e.g., racism or sexism). Zampieri et al. also extended the offence detection to target detection (individual or group targeted) in social media posts. These works include attributes in the above-mentioned fine-grained typology for the classification of offensive language. We make three remarks pertaining to prior work in this arena.
First, while there has been a large body of work focusing on offensive language detection on the internet, limited work investigated the nature and prevalence of such language in SE communities. In this work, we bridge this gap by investigating the prevalence and nature of offensive language in online SE communities. The presence and patterns of toxic interactions in GitHub issues were investigated by Raman et al., and the impacts of such discussions are evaluated against code quality to correlate these. An assessment of Gitter comments using existing toxicity detectors also suggests that offence is present in these communities, however, none of the existing tools perform adequately to detect offence in SE specific discussions. However, ML models using language models such as BERT have been proposed as potential candidate models. This work thus adds to the growing body of literature showing that offensive language is present in all the four SE communities considered in this work.
Second, there has been some prior work on automated offence classification in SE communities. A taxonomy was proposed in to classify offence in SO comments, and a rule-based system was developed to detect swearing and racial comments in SO discussions. Following that, Sultana et al. developed ML models to detect sexist and misogynistic discussions in GitHub pull requests. Our work extends the classification scheme of by considering additional attributes in the presented taxonomy (type of abuse and target of abuse) and develops approaches to classify offensive language with the ultimate goal of reducing them. A recent work has proposed a taxonomy of 11 attributes present in offensive language by manually analysing comments, and this is a fertile avenue for pursuing automatic classification of attributes in the future.
Thirdly, this is the first work that has attempted offence explanation. We explain to the poster why a comment is offensive and also propose non-offensive alternative suggestions, which is a novel proposition in the domain. Offence rephrasing has been considered by prior works, even though none of them are in SE context. The widely adopted strand of paraphrasing was generating counter narratives, which has the limitation of needing an annotated dataset. So, we used language models to generate paraphrases for offensive text, which do not require annotated dataset.
A.2.1 Modified GPS pipeline
First, we are not producing counter hate speech sentences for offensive comments since counter hate speech is a response system (in the form template sentences) that helps to reduce or avoid offensive language by providing non-negative argumentative feedback. Moreover, counter hate speech sentences might not retain the original non-offensive contents that are present within the offensive comment. In our modified GPS system, we retain the non-offensive contents of the original offensive comment. In other words, we keep the generated sentences that are close to the original comment that do not contain offensive language.
To generate a set of candidate paraphrases, we used RoBERTa, a Masked Language Model (MLM), which is able to generate alternate tokens for a special class of token called [mask]. The MLMs are originally trained on unsupervised data to generate contextual suggestions to fill the masked tokens. We use a multi-token RoBERTa model, which is a robust version of the BERT model, to generate multiple token suggestions for the [mask] tokens in the masked comment (Step 4). For the MLM model to generate sets of token suggestions, we fine-tuned the pretrained RoBERTa model using UN comments in an unsupervised mode so that the negative tone of the comment could be preserved. The outcome of the RoBERTa model is a set of token suggestions for each masked token. While RoBERTa is able to generate a large number of alternatives for a masked word, we limit our results to the top five token suggestions.
Second, we prune not only the ungrammatical candidate sentences from the generator pool, but also the set of candidate paraphrases which do not retain the style or context of the original comment. For the original GPS pipeline, there is only one evaluation metric, which is grammatical accuracy. However, since grammatical accuracy cannot be considered as the sole metric to evaluate the quality of the generated paraphrases, we used a metric called J score which incorporates three aspects: style, context and grammatical accuracy for the evaluation of the generated paraphrases.
Third, for the selection phase, the original GPS pipeline uses a linear mapping of sentences into a vector space and then uses cosine similarity to determine the cosine distance from the original comment. Since semantic similarity alone cannot construct a valuable metric for detoxification evaluation, we used the J score as the metric to evaluate the paraphrased comments. We set a threshold for the J score (0.8 out of 1) to select the final set of suggestions and present them as recommendations to the writer.
The modified version of the GPS pipeline primarily includes similar components to those of the GPS pipeline: a generator that generates a set of candidate paraphrased suggestions for an input comment (Steps 4 to 6), a pruning system that prunes some unwanted suggestions (Steps 7) and finally a selection system to choose the best suggestions as recommendations to the user (Step 8).


