Hate Speech Detection on X Using K-Nearest Neighbor with TF–IDF and Cosine Similarity
Deteksi Ujaran Kebencian pada X Menggunakan K-Nearest Neighbor dengan TF–IDF dan Kesamaan Kosinus
DOI:
https://doi.org/10.26714/jodi.v4i1.1149Abstract
The rapid growth of social media has increased online interactions but has also accelerated the spread of hate speech content that may negatively impact individuals and communities. X (formerly Twitter), as one of the largest social networking platforms, enables users to share opinions publicly, making automatic hate speech detection increasingly important. This research proposes a hate speech classification approach using the K-Nearest Neighbor (KNN) algorithm combined with Term Frequency–Inverse Document Frequency (TF–IDF) weighting and Cosine Similarity. The dataset consists of 900 social media posts collected through the platform API and manually labeled into hate speech and non-hate speech categories, consisting of 675 training data and 225 testing data. Prior to classification, text preprocessing techniques including tokenization, stopword removal, and stemming were applied to improve text quality. Model evaluation was conducted using 10-fold cross validation to assess classification performance. Experimental results showed that the KNN algorithm with Cosine Similarity distance measurement and K=3 parameter achieved an accuracy of 78.22% in hate speech detection tasks. The findings indicate that KNN combined with TF–IDF and Cosine Similarity provides a reliable approach for social media text classification and can support automated hate speech detection systems.
Downloads
Downloads
Published
How to Cite
Issue
Section
License
Copyright (c) 2026 Faiq Madani

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.










