Journal of Data Insights
Vol 4 No 1 (2026): Journal of Data Insights

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

Faiq Madani (Universitas Muhammadiyah Semarang)
Arvanida Feizal Permana (National Chin Yi University of Technology)
Abdul Karim (Hallym University)
Riyagung Nuryusufa Tranggono Adi Prasetya (Kun Shan University)
Wendy Sarasjati (Universitas Muhammadiyah Semarang)



Article Info

Publish Date
30 Jun 2026

Abstract

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.

Copyrights © 2026






Journal Info

Abbrev

jodi

Publisher

Subject

Computer Science & IT Mathematics

Description

The Journal of Data Insights is an open access publication for peer-reviewed scholarly journals. The Journal of Data Insights focuses on the processing, analysis and interpretation of data for data-driven decisions and solutions in industry, hospitals, government and universities. All articles ...