naufalnashif
commited on
Commit
Β·
e5b0dd1
1
Parent(s):
7cbf2e4
Update app.py
Browse filesupdate (function predict_sentiment) : emoticon logic
app.py
CHANGED
@@ -131,6 +131,29 @@ def select_sentiment_model(selected_model, model_enesmble, model_nb, model_lr):
|
|
131 |
|
132 |
# Fungsi untuk prediksi sentimen
|
133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
def predict_sentiment(text, _sentiment_model, _tfidf_vectorizer, slang_dict):
|
135 |
# Tahap-1: Membersihkan dan normalisasi teks
|
136 |
cleaned_text = clean_text(text)
|
@@ -140,18 +163,16 @@ def predict_sentiment(text, _sentiment_model, _tfidf_vectorizer, slang_dict):
|
|
140 |
tfidf_matrix = _tfidf_vectorizer.transform([norm_slang_text])
|
141 |
|
142 |
# Tahap-3: Lakukan prediksi sentimen
|
143 |
-
sentiment = _sentiment_model.predict(tfidf_matrix)
|
144 |
|
145 |
# Tahap-4: Menggantikan indeks dengan label sentimen
|
146 |
labels = {0: "Negatif", 1: "Netral", 2: "Positif"}
|
147 |
-
sentiment_label = labels[
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
else:
|
154 |
-
emoticon = "π" # Emotikon untuk sentimen netral
|
155 |
return sentiment_label, emoticon
|
156 |
|
157 |
|
|
|
131 |
|
132 |
# Fungsi untuk prediksi sentimen
|
133 |
|
134 |
+
# def predict_sentiment(text, _sentiment_model, _tfidf_vectorizer, slang_dict):
|
135 |
+
# # Tahap-1: Membersihkan dan normalisasi teks
|
136 |
+
# cleaned_text = clean_text(text)
|
137 |
+
# norm_slang_text = normalize_slang(cleaned_text, slang_dict)
|
138 |
+
|
139 |
+
# # Tahap-2: Ekstraksi fitur TF-IDF
|
140 |
+
# tfidf_matrix = _tfidf_vectorizer.transform([norm_slang_text])
|
141 |
+
|
142 |
+
# # Tahap-3: Lakukan prediksi sentimen
|
143 |
+
# sentiment = _sentiment_model.predict(tfidf_matrix)
|
144 |
+
|
145 |
+
# # Tahap-4: Menggantikan indeks dengan label sentimen
|
146 |
+
# labels = {0: "Negatif", 1: "Netral", 2: "Positif"}
|
147 |
+
# sentiment_label = labels[int(sentiment)]
|
148 |
+
|
149 |
+
# if sentiment == "Positif":
|
150 |
+
# emoticon = "π" # Emotikon untuk sentimen positif
|
151 |
+
# elif sentiment == "Negatif":
|
152 |
+
# emoticon = "π" # Emotikon untuk sentimen negatif
|
153 |
+
# else:
|
154 |
+
# emoticon = "π" # Emotikon untuk sentimen netral
|
155 |
+
# return sentiment_label, emoticon
|
156 |
+
|
157 |
def predict_sentiment(text, _sentiment_model, _tfidf_vectorizer, slang_dict):
|
158 |
# Tahap-1: Membersihkan dan normalisasi teks
|
159 |
cleaned_text = clean_text(text)
|
|
|
163 |
tfidf_matrix = _tfidf_vectorizer.transform([norm_slang_text])
|
164 |
|
165 |
# Tahap-3: Lakukan prediksi sentimen
|
166 |
+
sentiment = _sentiment_model.predict(tfidf_matrix)[0] # Ambil elemen pertama dari hasil prediksi
|
167 |
|
168 |
# Tahap-4: Menggantikan indeks dengan label sentimen
|
169 |
labels = {0: "Negatif", 1: "Netral", 2: "Positif"}
|
170 |
+
sentiment_label = labels[sentiment]
|
171 |
+
|
172 |
+
# Tahap-5: Tentukan emoticon berdasarkan label sentimen
|
173 |
+
emoticons = {"Negatif": "π", "Netral": "π", "Positif": "π"}
|
174 |
+
emoticon = emoticons.get(sentiment_label, "π") # Default emoticon untuk label tidak dikenal
|
175 |
+
|
|
|
|
|
176 |
return sentiment_label, emoticon
|
177 |
|
178 |
|