-
Notifications
You must be signed in to change notification settings - Fork 0
/
predictor_user.py
90 lines (73 loc) · 3.71 KB
/
predictor_user.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
import streamlit as st
import pickle
import time
import pandas as pd
import numpy as np
import preprocess_predictor_user as prep
with open('data/rfc_new.pkl', 'rb') as file:
rfc, tf_idf_vector = pickle.load(file)
def predictor_page():
st.title("Sentiment Predictor", help="Aplikasi prediksi positif atau negatif pada suatu sentiment.")
st.write("")
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")
st.write("")
if st.button("Prediksi"):
if sample_review:
st.write("")
with st.spinner('Memprediksi...'):
st.empty()
time.sleep(1)
with st.chat_message("assistant"):
st.write("Berdasarkan pola yang saya pelajari, sentiment dianggap:")
# memproses sample_review
sample_review1 = prep.casefolding(sample_review)
sample_review2 = prep.cleansing(sample_review1)
sample_review3 = prep.stemming(sample_review2)
sample_review4 = prep.convert_slangword(sample_review3)
sample_review5 = prep.remove_stopword(sample_review4)
sample_review6 = prep.remove_unwanted_words(sample_review5)
sample_review7 = prep.remove_short_words(sample_review6)
sample_review8 = prep.tokenizing(sample_review7)
# st.write(sample_review8)
# temp = tf_idf_vector.transform(sample_review8)
# st.write(temp)
# predict = rfc.predict(temp)
if not sample_review8:
# Hasil Positive karena tidak terdapat kata pada sample
sentiment_analysis = " Positive"
st.success(f'{sentiment_analysis}', icon="😄")
else:
# Transformasi TF-IDF dan Prediksi RFC
temp = tf_idf_vector.transform(sample_review8)
# st.write(temp)
predict = rfc.predict(temp)
# menghitung jumlah kemunculan kata yang diprediksi
jumlah_negative = np.count_nonzero(predict == 'Negative')
jumlah_positive = np.count_nonzero(predict == 'Positive')
# st.write("Negative :", jumlah_negative, " // ", "Positive :", jumlah_positive)
# Negative Result
if jumlah_negative >= jumlah_positive:
sentiment_analysis = " Negative"
st.warning(f'{sentiment_analysis}', icon="😟")
# Positive Result
else:
sentiment_analysis = " Positive"
st.success(f'{sentiment_analysis}', icon="😄")
# Text Area Kosong
else:
st.write("")
with st.error("Maaf, input sentiment dahulu."):
time.sleep(2)
st.empty()
hide_streamlit = """ <style> footer {visibility: hidden;} </style> """
st.markdown(hide_streamlit, unsafe_allow_html=True)