-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredictor.py
454 lines (395 loc) · 22 KB
/
predictor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
import streamlit as st
session = st.session_state
# import gzip
import pickle
import time
import pandas as pd
import preprocess_predictor as prep
with open('rfc_new.pkl', 'rb') as file:
rfc, tf_idf_vector = pickle.load(file)
def predict_sentiment(sample_review):
if session['opt_casefolding'] == 'Ya':
sample_review1 = prep.casefolding(sample_review)
session['txtcasefolding'] = sample_review1
else:
sample_review1 = sample_review
if session['opt_cleansing'] == 'Ya':
sample_review2 = prep.cleansing(sample_review1)
session['txtcleansing'] = sample_review2
else:
sample_review2 = sample_review1
sample_review3 = sample_review2
if session['opt_stemming'] == 'Ya':
sample_review4 = prep.stemming(sample_review3)
session['txtstemming'] = sample_review4
else:
sample_review4 = sample_review3
if session['opt_convert_slangword'] == 'Ya':
sample_review5 = prep.convert_slangword(sample_review4)
session['txtconvert_slangword'] = sample_review5
else:
sample_review5 = sample_review4
if session['opt_remove_stopword'] == 'Ya':
sample_review6 = prep.remove_stopword(sample_review5)
session['txtremove_stopword'] = sample_review6
else:
sample_review6 = sample_review5
if session['opt_remove_unwanted_words'] == 'Ya':
sample_review7 = prep.remove_unwanted_words(sample_review6)
session['txtremove_unwanted_words'] = sample_review7
else:
sample_review7 = sample_review6
if session['opt_remove_short_words'] == 'Ya':
sample_review8 = prep.remove_short_words(sample_review7)
session['txtremove_short_words'] = sample_review8
else:
sample_review8 = sample_review7
sample_review9 = prep.tokenizing(sample_review8)
predictor = sample_review9
return predictor
def predictor_page():
st.title("Sentiment Predictor", help="Aplikasi prediksi positif atau negatif pada suatu sentiment.")
st.write("")
# st.divider()
with st.expander("", expanded=True):
st.markdown(f'''
<span style="text-decoration: none;
font-family: 'Open Sans'; font-size: 13px; font-weight: bold;
color: white; background-color: #00386b;
border-radius: 5px; padding: 9px 22px;">
Input Sentiment :</span>
''', unsafe_allow_html = True)
sample_review = st.text_area("Input Sentiment:", height=200, placeholder='''
"Sangat membantu dalam pembelian tiket, namun masih harus diperbaiki kecepatannya."
''', label_visibility="collapsed")
session['sample_review'] = sample_review
#SIDEBAR
with st.sidebar.expander("Opsi Preprocessing ", expanded=True):
st.write("")
# Casefolding
st.markdown(f'''
<span style="text-decoration: none;
font-family: 'Open Sans'; font-size: 12px;
color: white; background-color: #3b5998;
border-radius: 20px; padding: 7px 13px;">
<b>Casefolding?</b></span>
''', unsafe_allow_html = True, help="_Proses mengkonverikan teks (mis: huruf é menjadi e, huruf E menjadi e)_")
opt_casefolding = st.radio("Casefolding?", options=('Tidak', 'Ya'), index=1, horizontal=True, label_visibility="collapsed")
if opt_casefolding:
session['opt_casefolding'] = opt_casefolding
else:
session['opt_casefolding'] = opt_casefolding
st.write("")
# Cleansing
st.markdown(f'''
<span style="text-decoration: none;
font-family: 'Open Sans'; font-size: 12px;
color: white; background-color: #317256;
border-radius: 20px; padding: 7px 13px;">
<b>Cleansing?</b></span>
''', unsafe_allow_html = True, help="_Proses pembersihan dokumen pada kata atau karakter_")
opt_cleansing = st.radio("Cleansing?", options=('Tidak', 'Ya'), index=1, horizontal=True, label_visibility="collapsed")
if opt_cleansing:
session['opt_cleansing'] = opt_cleansing
else:
session['opt_cleansing'] = opt_cleansing
st.write("")
# Stemming
st.markdown(f'''
<span style="text-decoration: none;
font-family: 'Open Sans'; font-size: 12px;
color: white; background-color: #e0474c;
border-radius: 20px; padding: 7px 13px;">
<b>Stemming?</b></span>
''', unsafe_allow_html = True, help="_Memotong/menghapus kata imbuhan menjadi kata dasar tanpa menggunakan akar dasar kamus bahasa_")
opt_stemming = st.radio("Stemming?", options=('Tidak', 'Ya'), index=1, horizontal=True,label_visibility="collapsed")
if opt_stemming:
session['opt_stemming'] = opt_stemming
else:
session['opt_stemming'] = opt_stemming
st.write("")
# Convert Slangword
st.markdown(f'''
<span style="text-decoration: none;
font-family: 'Open Sans'; font-size: 12px;
color: black; background-color: #ffe303;
border-radius: 20px; padding: 7px 13px;">
<b>Convert Slangword?</b></span>
''', unsafe_allow_html = True, help="_Mengubah kata non-formal menjadi kata formal_")
opt_convert_slangword = st.radio("Convert Slangword?", options=('Tidak', 'Ya'), index=1, horizontal=True, label_visibility="collapsed")
if opt_convert_slangword:
session['opt_convert_slangword'] = opt_convert_slangword
else:
session['opt_convert_slangword'] = opt_convert_slangword
st.write("")
# Remove Stopword
st.markdown(f'''
<span style="text-decoration: none;
font-family: 'Open Sans'; font-size: 12px;
color: white; background-color: #8250e5;
border-radius: 20px; padding: 7px 13px;">
<b>Remove Stopword?</b></span>
''', unsafe_allow_html = True, help="_Menghapus seluruh kata yang dianggap tidak memberikan kontribusi seperti kata hubung 'yang', 'di', 'dan', 'dari'_")
opt_remove_stopword = st.radio("Remove Stopword?", options=('Tidak', 'Ya'), index=1, horizontal=True, label_visibility="collapsed")
if opt_remove_stopword:
session['opt_remove_stopword'] = opt_remove_stopword
else:
session['opt_remove_stopword'] = opt_remove_stopword
st.write("")
# Remove Unwanted Words
st.markdown(f'''
<span style="text-decoration: none;
font-family: 'Open Sans'; font-size: 12px;
color: white; background-color: #76a633;
border-radius: 20px; padding: 7px 13px;">
<b>Remove Unwanted Words?</b></span>
''', unsafe_allow_html = True, help="_Membuat dictionary kata-kata yang kurang dianggap membawa hasil signifikan untuk analisis sentimen, dimana kata-kata yang sama dalam ulasan akan dihapus dari ulasan_")
opt_remove_unwanted_words = st.radio("Remove Unwanted Words?", options=('Tidak', 'Ya'), index=1, horizontal=True,label_visibility="collapsed")
if opt_remove_unwanted_words:
session['opt_remove_unwanted_words'] = opt_remove_unwanted_words
else:
session['opt_remove_unwanted_words'] = opt_remove_unwanted_words
st.write("")
# Remove Short Words
st.markdown(f'''
<span style="text-decoration: none;
font-family: 'Open Sans'; font-size: 12px;
color: white; background-color: #046cd4;
border-radius: 20px; padding: 7px 13px;">
<b>Remove Short Words?</b></span>
''', unsafe_allow_html = True, help="_Mempertahankan kata-kata lebih dari 2 karakter atau menghapus yang kurang dari 3 karakter_")
opt_remove_short_words = st.radio("Remove Short Words?", options=('Tidak', 'Ya'), index=1, horizontal=True,label_visibility="collapsed")
if opt_remove_short_words:
session['opt_remove_short_words'] = opt_remove_short_words
else:
session['opt_remove_short_words'] = opt_remove_short_words
with st.sidebar:
st.write("")
st.write("")
st.write("")
st.write("")
if st.button("Prediksi"):
if sample_review:
st.write("")
with st.spinner('Memprediksi...'):
st.empty()
time.sleep(0.3)
with st.chat_message("user"):
st.write("Sentiment:")
st.write(f'''
<p style="text-decoration: none;
font-family: 'Open Sans'; font-size: 13px;
color: white; background-color: #00386b;
border-radius: 5px; padding: 9px 22px;">
<b> {session['sample_review']} </b></p>
''', unsafe_allow_html = True)
with st.chat_message("assistant"):
st.write("Berdasarkan pola yang saya pelajari, sentiment dianggap:")
result_predict = predict_sentiment(sample_review)
polar = {
"kata positive": [],
"kata negative": [],
}
polar_neutral = []
for x in result_predict:
tf = tf_idf_vector.transform([x])
temp = rfc.predict(tf)
if (temp[0] == "negative"):
polar["kata negative"].append(x)
elif (temp[0] == "positive"):
polar["kata positive"].append(x)
else:
polar_neutral.append(x)
positive_count = len(polar["kata positive"])
negative_count = len(polar["kata negative"])
# with st.expander("Hasil Polar"):
# st.write(polar_neutral)
# st.write(polar)
# Negative Result
if (negative_count > positive_count):
sentiment_analysis = " Negative"
# st.write("")
st.warning(f'{sentiment_analysis}', icon="😟")
st.write(f"Jumlah: ({positive_count} kata positif, {negative_count} kata negatif)")
df_polar = pd.DataFrame.from_dict(polar, orient="index").transpose()
st.dataframe(df_polar.fillna(''), hide_index=True, use_container_width=True)
st.write("")
if (opt_casefolding == 'Ya' or opt_cleansing =='Ya' or opt_stemming =='Ya'
or opt_convert_slangword =='Ya' or opt_remove_stopword =='Ya'
or opt_remove_unwanted_words =='Ya' or opt_remove_short_words =='Ya'
# or opt_tokenizing =='Ya'
) :
with st.expander("Tahap Preprocessing", expanded=True):
if opt_casefolding == 'Ya':
st.write(f'''
<p style="text-decoration: none;
font-family: 'Open Sans'; font-size: 13px;
color: white; background-color: #3b5998;
border-radius: 5px; padding: 9px 22px;">
Casefolding : <b>{session['txtcasefolding']}</b></p>
''', unsafe_allow_html = True)
else:
st.empty()
if opt_cleansing == 'Ya':
st.write(f'''
<p style="text-decoration: none;
font-family: 'Open Sans'; font-size: 13px;
color: white; background-color: #317256;
border-radius: 5px; padding: 9px 22px;">
Cleansing : <b>{session['txtcleansing']}</b></p>
''', unsafe_allow_html = True)
else:
st.empty()
if opt_stemming == 'Ya':
st.write(f'''
<p style="text-decoration: none;
font-family: 'Open Sans'; font-size: 13px;
color: white; background-color: #e0474c;
border-radius: 5px; padding: 9px 22px;">
Stemming : <b>{session['txtstemming']}</b></p>
''', unsafe_allow_html = True)
else:
st.empty()
if opt_convert_slangword == 'Ya':
st.write(f'''
<p style="text-decoration: none;
font-family: 'Open Sans'; font-size: 13px;
color: black; background-color: #ffe303;
border-radius: 5px; padding: 9px 22px;">
Convert Slangword : <b>{session['txtconvert_slangword']}</b></p>
''', unsafe_allow_html = True)
else:
st.empty()
if opt_remove_stopword == 'Ya':
st.write(f'''
<p style="text-decoration: none;
font-family: 'Open Sans'; font-size: 13px;
color: white; background-color: #8250e5;
border-radius: 5px; padding: 9px 22px;">
Remove Stopword : <b>{session['txtremove_stopword']}</b></p>
''', unsafe_allow_html = True)
else:
st.empty()
if opt_remove_unwanted_words == 'Ya':
st.write(f'''
<p style="text-decoration: none;
font-family: 'Open Sans'; font-size: 13px;
color: white; background-color: #76a633;
border-radius: 5px; padding: 9px 22px;">
Remove Unwanted Words : <b>{session['txtremove_unwanted_words']}</b></p>
''', unsafe_allow_html = True)
else:
st.empty()
if opt_remove_short_words == 'Ya':
st.write(f'''
<p style="text-decoration: none;
font-family: 'Open Sans'; font-size: 13px;
color: white; background-color: #046cd4;
border-radius: 5px; padding: 9px 22px;">
Remove Short Words : <b>{session['txtremove_short_words']}</b></p>
''', unsafe_allow_html = True)
else:
st.empty()
# Positive Result
else:
sentiment_analysis = " Positive"
st.write("")
st.success(f'{sentiment_analysis}', icon="😄")
st.write(f"Jumlah: ({positive_count} kata positif, {negative_count} kata negatif)")
df_polar = pd.DataFrame.from_dict(polar, orient='index').transpose()
st.dataframe(df_polar.fillna(''), hide_index=True, use_container_width=True)
st.write("")
if (opt_casefolding == 'Ya' or opt_cleansing =='Ya' or opt_stemming =='Ya'
or opt_convert_slangword =='Ya' or opt_remove_stopword =='Ya'
or opt_remove_unwanted_words =='Ya' or opt_remove_short_words =='Ya'
# or opt_tokenizing =='Ya'
) :
with st.expander("Tahap Preprocessing", expanded=True):
if opt_casefolding == 'Ya':
st.write(f'''
<p style="text-decoration: none;
font-family: 'Open Sans'; font-size: 13px;
color: white; background-color: #3b5998;
border-radius: 5px; padding: 9px 22px;">
Casefolding : <b>{session['txtcasefolding']}</b></p>
''', unsafe_allow_html = True)
else:
st.empty()
if opt_cleansing == 'Ya':
st.write(f'''
<p style="text-decoration: none;
font-family: 'Open Sans'; font-size: 13px;
color: white; background-color: #317256;
border-radius: 5px; padding: 9px 22px;">
Cleansing : <b>{session['txtcleansing']}</b></p>
''', unsafe_allow_html = True)
else:
st.empty()
# if opt_lemmatize == 'Ya':
# st.write(f'''
# <p style="text-decoration: none;
# font-family: 'Open Sans'; font-size: 13px;
# color: white; background-color: #7b7d7b;
# border-radius: 5px; padding: 9px 22px;">
# Lemmatize : <b>{session['txtlemmatize']}</b></p>
# ''', unsafe_allow_html = True)
# else:
# st.empty()
if opt_stemming == 'Ya':
st.write(f'''
<p style="text-decoration: none;
font-family: 'Open Sans'; font-size: 13px;
color: white; background-color: #e0474c;
border-radius: 5px; padding: 9px 22px;">
Stemming : <b>{session['txtstemming']}</b></p>
''', unsafe_allow_html = True)
else:
st.empty()
if opt_convert_slangword == 'Ya':
st.write(f'''
<p style="text-decoration: none;
font-family: 'Open Sans'; font-size: 13px;
color: black; background-color: #ffe303;
border-radius: 5px; padding: 9px 22px;">
Convert Slangword : <b>{session['txtconvert_slangword']}</b></p>
''', unsafe_allow_html = True)
else:
st.empty()
if opt_remove_stopword == 'Ya':
st.write(f'''
<p style="text-decoration: none;
font-family: 'Open Sans'; font-size: 13px;
color: white; background-color: #8250e5;
border-radius: 5px; padding: 9px 22px;">
Remove Stopword : <b>{session['txtremove_stopword']}</b></p>
''', unsafe_allow_html = True)
else:
st.empty()
if opt_remove_unwanted_words == 'Ya':
st.write(f'''
<p style="text-decoration: none;
font-family: 'Open Sans'; font-size: 13px;
color: white; background-color: #76a633;
border-radius: 5px; padding: 9px 22px;">
Remove Unwanted Words : <b>{session['txtremove_unwanted_words']}</b></p>
''', unsafe_allow_html = True)
else:
st.empty()
if opt_remove_short_words == 'Ya':
st.write(f'''
<p style="text-decoration: none;
font-family: 'Open Sans'; font-size: 13px;
color: white; background-color: #046cd4;
border-radius: 5px; padding: 9px 22px;">
Remove Short Words : <b>{session['txtremove_short_words']}</b></p>
''', unsafe_allow_html = True)
else:
st.empty()
# Kolom Kosong
else:
st.write("")
with st.error("Mohon input sentiment terlebih dahulu."):
time.sleep(2)
st.empty()
hide_streamlit = """ <style> footer {visibility: hidden;} </style> """
st.markdown(hide_streamlit, unsafe_allow_html=True)