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; Arvanida Feizal Permana; Abdul Karim; Riyagung Nuryusufa Tranggono Adi Prasetya; Wendy Sarasjati (Department of Sains Data UNIMUS Universitas Muhammadiyah Semarang, 2026)
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.