new_terms = pickle.load(result_data_file) result_data_file.close() result_data_file = open(rw_filename, 'rb') removed_words = pickle.load(result_data_file) result_data_file.close() result_data_file = open(stopwords_filename, 'rb') stopwords = pickle.load(result_data_file) result_data_file.close() weight = Weighting(new_cleaned_data, new_terms) tfidf = weight.get_tf_idf_weighting() idf = weight.get_idf() nb = NBMultinomial() nb.fit(new_cleaned_data, new_terms, data_train[i]["target"], stopwords, idf, tfidf) def predict(input_tweet): prediction, negatif, netral, positif, used_terms = nb.predict(input_tweet) result_dict = {} result_dict["success"] = True result_dict["type"] = "TBRS" result_dict["data"] = { "prediction": prediction, "negatif": negatif, "netral": netral, "positif": positif, "stopwords": stopwords,
# TAHAP PEMBUATAN STOPWORD prepro = Preprocessing() cleaned_data, terms = prepro.preprocessing(data_tweet) print("FIRST PREPRO DONE") tbrs = TermBasedRandomSampling(X=10, Y=50, L=40) stopwords = tbrs.create_stopwords(cleaned_data, terms) # TAHAP PELATIHAN prepro2 = Preprocessing() new_cleaned_data, new_terms = prepro2.remove_stopword(cleaned_data, stopwords) # print("new_terms") # print(new_terms) weight = Weighting(new_cleaned_data, new_terms) tfidf = weight.get_tf_idf_weighting() idf = weight.get_idf() nb = NBMultinomial() nb.fit(new_cleaned_data, new_terms, data_target, stopwords, idf, tfidf) # print("stopwords") print(stopwords) # TAHAP PENGUJIAN tes = pd.read_excel(r'Skripsi.xlsx', "Fold 2 Test") tes_tweet = tes['Tweet'] tes_target = tes['Label'] y_test = tes_target y_pred = [] for i in range(len(tes_tweet)): pred, negatif, netral, positif, pangka, pterm, lneg, lnet, lpos, termnya = nb.predict( tes_tweet[i], tes_target[i]) y_pred.append(pred)
print(time.time() - start) start = time.time() prepro2 = Preprocessing() new_cleaned_data, new_terms, removed_words = prepro2.remove_stopword( cleaned_data, stopwords) print("create stopword") print(time.time() - start) start = time.time() weight = Weighting(new_cleaned_data, new_terms) tfidf = weight.get_tf_idf_weighting() idf = weight.get_idf() nb = NBMultinomial() nb.fit(new_cleaned_data, new_terms, data_train[i]["target"], stopwords, idf, tfidf) print("nb fit") print(time.time() - start) start = time.time() y_test = [] y_pred = [] for j in range(len(data_test[i]["tweet"])): prediction = nb.predict(data_test[i]["tweet"][j], data_test[i]["target"][j]) y_test.append(data_test[i]["target"][j]) y_pred.append(prediction)
'materi', 'ada', 'ini', 'kelas', 'cara', 'sama', 'jadi', 'kasih', 'tapi', 'lebih', 'kalau', 'dosen', 'beberapa', 'kenapa', 'new', 'normal', 'zona', 'lagi', 'sendiri', 'dengan', 'masih', 'jujur', 'corona', 'paham', 'sekali', 'nilai', 'bisa', 'ya', 'selesai', 'jelas', 'mohon', 'kaji' ] # text_file = open('stopword_tala.txt', 'r') # stopword_tala = text_file.read().split('\n') # text_file.close() # new_cleaned_data, new_terms = prepro.remove_stopword(cleaned_data,stopwords) new_cleaned_data, new_terms = prepro.preprocessing(data_tweet, stopwords_tbrs_manualisasi) weight = Weighting(new_cleaned_data, new_terms) tfidf = weight.get_tf_idf_weighting() nb = NBMultinomial() # nb.fit(new_cleaned_data,new_terms,data_target,stopwords_tbrs_manualisasi) nb.fit(new_cleaned_data, new_terms, data_target, stopwords_tbrs_manualisasi, tfidf) nb.predict( "Apa saya saja yang merasa kalau selama kuliah daring nyaman banget sampai saya tidak ingin masuk kuliah karena takut panik", "Positif") # nb.predict("Aku merasa lebih leluasa dengan kuliah daring, tidak capek harus siap-siap berangkat. Hanya tinggal makan, beres didepan komputer sudah siap nyimak. Buat materi, selama online emang tidak pernah mengandalkan dosen atau temen. Jadi lebih banyak waktu buat searching sama buka textbook","Positif") # nb.predict("Jujur tidak ada senang-senangnya kuliah daring. Aku butuh praktik lapangan. Apalagi semester depan magang. Apa magang online juga? Bisa stres gara-gara banyak deadline","Negatif") # nb.predict("Tatap langsung aja kadang tidak paham, apalagi kuliah daring, belum lagi jaringan lambat ditambah beberapa dosen yang jarang memberi kuliah online, atau cuma memberi tugas saja... Fix kampus ku belum siap menerapkan kuliah daring! pic.twitter.com/UHdReyLgh8","Negatif") # nb.predict("Orang lain pada ribut sama keadaan kosan yang sudah ditinggal berbulan-bulan terus ribut gimana caranya balik ke kosan. Aku anteng-anteng saja jadi penghuni kos dari awal pemerintah nyuruh dirumah saja dan kuliah jadi daring","Netral") # nb.predict(data_tweet[4],"Netral") # nb.predict(data_tweet[7],"Positif") # nb.predict("Saya bisa paham tapi kadang suka lupa","Netral")