Views
No views yet
1from huggingface_hub import hf_hub_download
2import pickle
3import nltk
4from nltk.tokenize import word_tokenize
5from nltk.corpus import stopwords
6import string
7
8# Unduh model dari Hugging Face
9model_path = hf_hub_download(repo_id="aditdwi123/CyberBullyingDetector", filename="naive_bayes_model.pkl")
10
11# Muat model dari file
12with open(model_path, "rb") as model_file:
13 model = pickle.load(model_file)
14
15# Fungsi untuk preprocessing teks
16def preprocess(text):
17 text = text.lower() # Konversi ke huruf kecil
18 tokens = word_tokenize(text) # Tokenisasi
19 tokens = [t for t in tokens if t not in stopwords.words('indonesian') and t not in string.punctuation] # Hapus stopwords dan tanda baca
20 return tokens
21
22# Fungsi untuk mengekstrak fitur dari setiap kata
23def extract_features(words):
24 return {word: True for word in words}
25
26# Fungsi untuk mengklasifikasikan kalimat
27def classify_sentence(sentence):
28 features = extract_features(preprocess(sentence))
29 return model.classify(features)
30
31# Uji model dengan kalimat baru
32test_sentence = "saya sangat senang dengan pelayanan yang baik"
33predicted_sentiment = classify_sentence(test_sentence)
34print(f"Kalimat: {test_sentence}")
35print(f"Prediksi Sentimen: {predicted_sentiment}")