Spaces:
Sleeping
Sleeping
William14045
commited on
Commit
•
15462a3
1
Parent(s):
9aab2dd
commit1
Browse files- app.py +10 -0
- formalisasi.txt +7 -0
- kamus.txt +0 -0
- predict.py +50 -0
- proses text.txt +18 -0
- requirements.txt +8 -0
- sentiment_PPKM_NLP_Model.h5 +3 -0
app.py
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import eda
|
3 |
+
import predict
|
4 |
+
|
5 |
+
navigation = st.sidebar.selectbox('pilih halaman :', ('EDA',"Prediction"))
|
6 |
+
|
7 |
+
if navigation =='EDA':
|
8 |
+
eda.run()
|
9 |
+
else:
|
10 |
+
predict.run()
|
formalisasi.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def formalize_words(text, word_formalization_dict):
|
2 |
+
pattern = re.compile('\\b(' + '|'.join(kamus.keys()) + ')\\b')
|
3 |
+
|
4 |
+
def replace_words(match):
|
5 |
+
return kamus[match.group(0)]
|
6 |
+
formalized_text = pattern.sub(replace_words, text)
|
7 |
+
return formalized_text
|
kamus.txt
ADDED
Binary file (17.7 kB). View file
|
|
predict.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import tensorflow as tf
|
5 |
+
import pickle
|
6 |
+
import tensorflow_hub as hub
|
7 |
+
|
8 |
+
|
9 |
+
hub_layer = hub.KerasLayer("https://tfhub.dev/google/nnlm-id-dim128-with-normalization/2",
|
10 |
+
input_shape=[], dtype=tf.string,)
|
11 |
+
tf.keras.utils.get_custom_objects()['KerasLayer'] = hub_layer
|
12 |
+
nlp = tf.keras.models.load_model('sentiment_PPKM_NLP_Model.h5')
|
13 |
+
|
14 |
+
with open('formalisasi.txt', 'r') as file:
|
15 |
+
function_text = file.read()
|
16 |
+
with open("proses text.txt",'r') as file1:
|
17 |
+
proc = file1.read()
|
18 |
+
with open('kamus.txt','rb') as file2:
|
19 |
+
kamus = pickle.load(file2)
|
20 |
+
|
21 |
+
def create_function_from_text(function_tex):
|
22 |
+
# Gunakan pustaka exec() untuk mengevaluasi teks menjadi objek fungsi
|
23 |
+
exec(function_tex, globals())
|
24 |
+
# Ambil fungsi yang telah dibuat dan kembalikan sebagai output
|
25 |
+
return list(globals().values())[0]
|
26 |
+
|
27 |
+
formalize_words = create_function_from_text(function_text)
|
28 |
+
text_process = create_function_from_text(proc)
|
29 |
+
|
30 |
+
def run():
|
31 |
+
with st.form('key=sentiment prediction'):
|
32 |
+
sentiment = st.text_input('ketikkan tweet anda:', '')
|
33 |
+
|
34 |
+
submitted = st.form_submit_button('Predict')
|
35 |
+
|
36 |
+
data_inf = {
|
37 |
+
'sentiment': sentiment
|
38 |
+
}
|
39 |
+
data_inf = pd.DataFrame([data_inf])
|
40 |
+
|
41 |
+
# Menerapkan fungsi klasifikasi sentimen menggunakan apply pada DataFrame
|
42 |
+
data_inf['proc_sentiment'] = data_inf['sentiment'].apply(text_process)
|
43 |
+
|
44 |
+
if submitted:
|
45 |
+
y_pred_inf = [np.argmax(pred) for pred in nlp.predict(data_inf['proc_sentiment'])]
|
46 |
+
st.write('sentiment : ', str(y_pred_inf))
|
47 |
+
|
48 |
+
|
49 |
+
if __name__ == '__main__':
|
50 |
+
run()
|
proses text.txt
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def text_proses(teks):
|
2 |
+
teks = teks.lower()
|
3 |
+
teks = re.sub('@[A-Za-z0-9_]+', ' ', teks)
|
4 |
+
teks = re.sub('#[A-Za-z0-9_]+', ' ', teks)
|
5 |
+
teks = re.sub('\\\\n', ' ', teks)
|
6 |
+
teks = teks.strip()
|
7 |
+
teks = re.sub('http\\S+', ' ', teks)
|
8 |
+
teks = re.sub('www.\\S+', ' ', teks)
|
9 |
+
teks = re.sub("[^A-Za-z\\s']", ' ', teks)
|
10 |
+
teks = re.sub('rt', ' ', teks)
|
11 |
+
teks = re.sub('\\b\\w{1,2}\\b', '', teks)
|
12 |
+
teks = re.sub('(\\w)\\1{2,}', '\\1', teks)
|
13 |
+
teks = re.sub('\\b(wk+|aok+|ha+h[aeiou]+|hi+h[aeiou]+|ho+h[aeiou]+)\\b', 'haha', teks)
|
14 |
+
tokens = word_tokenize(teks)
|
15 |
+
teks = ' '.join([word for word in tokens if word not in stpwds_id])
|
16 |
+
teks = formalize_words(teks, formalize_words)
|
17 |
+
teks = stemmer.stem(teks)
|
18 |
+
return teks
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
pandas
|
3 |
+
seaborn
|
4 |
+
matplotlib
|
5 |
+
plotly
|
6 |
+
Pillow
|
7 |
+
numpy
|
8 |
+
scikit-learn==1.2.2
|
sentiment_PPKM_NLP_Model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1575ff10a1ae6f5e060d4913bc0819fdcb452aa67db9b86c22f26a26bc13cfe2
|
3 |
+
size 450240560
|