naufalnashif
commited on
Commit
•
222b841
1
Parent(s):
207cbc3
Update app.py
Browse files
app.py
CHANGED
@@ -12,14 +12,8 @@ import matplotlib.pyplot as plt
|
|
12 |
import seaborn as sns
|
13 |
from wordcloud import WordCloud
|
14 |
|
15 |
-
|
16 |
-
# Set judul situs web
|
17 |
-
st.set_page_config(page_title="naufalnashif-ML")
|
18 |
-
|
19 |
-
# Judul
|
20 |
-
st.title("Aplikasi ML Analisis Sentimen based on data Biskita Transpakuan")
|
21 |
-
|
22 |
# Fungsi untuk membersihkan teks dengan ekspresi reguler
|
|
|
23 |
def clean_text(text):
|
24 |
# Tahap-1: Menghapus karakter non-ASCII
|
25 |
text = re.sub(r'[^\x00-\x7F]+', '', text)
|
@@ -75,12 +69,14 @@ kamus_gaul_baru = json.loads(kamus_sendiri)
|
|
75 |
lookp_dict.update(kamus_gaul_baru)
|
76 |
|
77 |
# Fungsi untuk normalisasi kata gaul
|
|
|
78 |
def normalize_slang(text, slang_dict):
|
79 |
words = text.split()
|
80 |
normalized_words = [slang_dict.get(word, word) for word in words]
|
81 |
return ' '.join(normalized_words)
|
82 |
|
83 |
# Fungsi untuk ekstraksi fitur TF-IDF
|
|
|
84 |
def extract_tfidf_features(texts, tfidf_vectorizer):
|
85 |
tfidf_matrix = tfidf_vectorizer.transform(texts)
|
86 |
return tfidf_matrix
|
@@ -112,8 +108,8 @@ def select_sentiment_model(selected_model):
|
|
112 |
# Memilih model sentimen berdasarkan pilihan pengguna
|
113 |
sentiment_model = select_sentiment_model(selected_model)
|
114 |
|
115 |
-
|
116 |
# Fungsi untuk prediksi sentimen
|
|
|
117 |
def predict_sentiment(text, model, tfidf_vectorizer, slang_dict):
|
118 |
# Tahap-1: Membersihkan dan normalisasi teks
|
119 |
cleaned_text = clean_text(text)
|
@@ -150,6 +146,12 @@ def get_table_download_link(df, download_format):
|
|
150 |
csv = df.to_csv(index=False)
|
151 |
return f'<a href="data:file/csv;base64,{b64encode(csv.encode()).decode()}" download="hasil_sentimen.csv">Unduh File CSV</a>'
|
152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
# Pilihan input teks manual atau berkas XLSX
|
154 |
input_option = st.radio("Pilih metode input:", ("Teks Manual", "Unggah Berkas XLSX"))
|
155 |
|
@@ -171,7 +173,7 @@ else:
|
|
171 |
|
172 |
# Analisis sentimen
|
173 |
results = []
|
174 |
-
|
175 |
if input_option == "Teks Manual" and user_input:
|
176 |
# Pisahkan teks yang dimasukkan pengguna menjadi baris-baris terpisah
|
177 |
user_texts = user_input.split('\n')
|
@@ -181,6 +183,7 @@ if input_option == "Teks Manual" and user_input:
|
|
181 |
cleaned_text = clean_text(text)
|
182 |
norm_slang_text = normalize_slang(cleaned_text, lookp_dict)
|
183 |
results.append((text, cleaned_text, norm_slang_text, sentiment_label, emoticon))
|
|
|
184 |
|
185 |
elif input_option == "Unggah Berkas XLSX" and uploaded_file is not None:
|
186 |
if 'Text' in df.columns:
|
@@ -190,81 +193,83 @@ elif input_option == "Unggah Berkas XLSX" and uploaded_file is not None:
|
|
190 |
cleaned_text = clean_text(text)
|
191 |
norm_slang_text = normalize_slang(cleaned_text, lookp_dict)
|
192 |
results.append((text, cleaned_text, norm_slang_text, sentiment_label, emoticon))
|
|
|
|
|
193 |
else:
|
194 |
st.warning("Berkas XLSX harus memiliki kolom bernama 'Text' untuk analisis sentimen.")
|
195 |
-
|
196 |
-
|
197 |
-
# Membagi tampilan menjadi dua kolom
|
198 |
-
columns = st.columns(2)
|
199 |
-
|
200 |
-
# Kolom pertama untuk Word Cloud
|
201 |
-
with columns[0]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
202 |
if results:
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
st.
|
218 |
-
|
219 |
-
|
220 |
-
with columns[1]:
|
221 |
-
st.subheader("Chart")
|
222 |
-
if results:
|
223 |
-
df_results = pd.DataFrame(results, columns=["Teks", "Cleaned Text", "Norm Text", "Hasil Analisis Sentimen", "Emotikon"])
|
224 |
-
sns.set_style("whitegrid")
|
225 |
-
|
226 |
-
# Menyiapkan label kelas
|
227 |
-
class_labels = ["Negatif", "Netral", "Positif"]
|
228 |
-
|
229 |
-
# Menghitung nilai hitungan per label
|
230 |
-
value_counts = df_results["Hasil Analisis Sentimen"].value_counts()
|
231 |
-
|
232 |
-
# Mengurutkan nilai hitungan berdasarkan label
|
233 |
-
value_counts = value_counts.reindex(class_labels)
|
234 |
-
|
235 |
-
fig, ax = plt.subplots() # Buat objek Figure
|
236 |
-
sns.barplot(x=value_counts.index, y=value_counts.values, ax=ax) # Gunakan ax= untuk plot
|
237 |
-
plt.xticks(rotation=45)
|
238 |
-
|
239 |
-
st.pyplot(fig) # Tampilkan plot menggunakan st.pyplot(fig)
|
240 |
-
|
241 |
-
# Menampilkan hasil analisis sentimen dalam kotak yang dapat diperluas
|
242 |
-
with st.expander("Hasil Analisis Sentimen"):
|
243 |
-
# Tampilkan tabel hasil analisis sentimen
|
244 |
-
st.write(pd.DataFrame(results, columns=["Teks", "Cleaned Text", "Norm Text", "Hasil Analisis Sentimen", "Emotikon"]))
|
245 |
-
|
246 |
-
|
247 |
-
# Tautan untuk mengunduh hasil dalam format XLSX atau CSV
|
248 |
-
st.subheader("Unduh Hasil")
|
249 |
-
download_format = st.selectbox("Pilih format unduhan:", ["XLSX", "CSV"])
|
250 |
-
if results:
|
251 |
-
if download_format == "XLSX":
|
252 |
-
# Simpan DataFrame ke dalam file XLSX
|
253 |
-
df = pd.DataFrame(results, columns=["Teks", "Cleaned Text", "Norm Text", "Hasil Analisis Sentimen", "Emotikon"])
|
254 |
-
df.to_excel("hasil_sentimen.xlsx", index=False)
|
255 |
-
|
256 |
-
# Tampilkan tombol unduh XLSX
|
257 |
-
st.download_button(label="Unduh XLSX", data=open("hasil_sentimen.xlsx", "rb").read(), key="xlsx_download", file_name="hasil_sentimen.xlsx")
|
258 |
-
|
259 |
-
else: # Jika CSV
|
260 |
-
# Simpan DataFrame ke dalam file CSV
|
261 |
-
df = pd.DataFrame(results, columns=["Teks", "Cleaned Text", "Norm Text", "Hasil Analisis Sentimen", "Emotikon"])
|
262 |
-
csv = df.to_csv(index=False)
|
263 |
-
|
264 |
-
# Tampilkan tombol unduh CSV
|
265 |
-
st.download_button(label="Unduh CSV", data=csv, key="csv_download", file_name="hasil_sentimen.csv")
|
266 |
-
else:
|
267 |
-
st.write("Tidak ada data untuk diunduh.")
|
268 |
|
269 |
|
270 |
# Garis pemisah
|
|
|
12 |
import seaborn as sns
|
13 |
from wordcloud import WordCloud
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
# Fungsi untuk membersihkan teks dengan ekspresi reguler
|
16 |
+
@st.cache_data
|
17 |
def clean_text(text):
|
18 |
# Tahap-1: Menghapus karakter non-ASCII
|
19 |
text = re.sub(r'[^\x00-\x7F]+', '', text)
|
|
|
69 |
lookp_dict.update(kamus_gaul_baru)
|
70 |
|
71 |
# Fungsi untuk normalisasi kata gaul
|
72 |
+
@st.cache_data
|
73 |
def normalize_slang(text, slang_dict):
|
74 |
words = text.split()
|
75 |
normalized_words = [slang_dict.get(word, word) for word in words]
|
76 |
return ' '.join(normalized_words)
|
77 |
|
78 |
# Fungsi untuk ekstraksi fitur TF-IDF
|
79 |
+
@st.cache_data
|
80 |
def extract_tfidf_features(texts, tfidf_vectorizer):
|
81 |
tfidf_matrix = tfidf_vectorizer.transform(texts)
|
82 |
return tfidf_matrix
|
|
|
108 |
# Memilih model sentimen berdasarkan pilihan pengguna
|
109 |
sentiment_model = select_sentiment_model(selected_model)
|
110 |
|
|
|
111 |
# Fungsi untuk prediksi sentimen
|
112 |
+
@st.cache_data
|
113 |
def predict_sentiment(text, model, tfidf_vectorizer, slang_dict):
|
114 |
# Tahap-1: Membersihkan dan normalisasi teks
|
115 |
cleaned_text = clean_text(text)
|
|
|
146 |
csv = df.to_csv(index=False)
|
147 |
return f'<a href="data:file/csv;base64,{b64encode(csv.encode()).decode()}" download="hasil_sentimen.csv">Unduh File CSV</a>'
|
148 |
|
149 |
+
# Set judul situs web
|
150 |
+
st.set_page_config(page_title="naufalnashif-ML")
|
151 |
+
|
152 |
+
# Judul
|
153 |
+
st.title("Analisis Sentimen Based on Tweets Biskita Transpakuan")
|
154 |
+
|
155 |
# Pilihan input teks manual atau berkas XLSX
|
156 |
input_option = st.radio("Pilih metode input:", ("Teks Manual", "Unggah Berkas XLSX"))
|
157 |
|
|
|
173 |
|
174 |
# Analisis sentimen
|
175 |
results = []
|
176 |
+
analisis = False
|
177 |
if input_option == "Teks Manual" and user_input:
|
178 |
# Pisahkan teks yang dimasukkan pengguna menjadi baris-baris terpisah
|
179 |
user_texts = user_input.split('\n')
|
|
|
183 |
cleaned_text = clean_text(text)
|
184 |
norm_slang_text = normalize_slang(cleaned_text, lookp_dict)
|
185 |
results.append((text, cleaned_text, norm_slang_text, sentiment_label, emoticon))
|
186 |
+
analisis = True
|
187 |
|
188 |
elif input_option == "Unggah Berkas XLSX" and uploaded_file is not None:
|
189 |
if 'Text' in df.columns:
|
|
|
193 |
cleaned_text = clean_text(text)
|
194 |
norm_slang_text = normalize_slang(cleaned_text, lookp_dict)
|
195 |
results.append((text, cleaned_text, norm_slang_text, sentiment_label, emoticon))
|
196 |
+
analisis = True
|
197 |
+
|
198 |
else:
|
199 |
st.warning("Berkas XLSX harus memiliki kolom bernama 'Text' untuk analisis sentimen.")
|
200 |
+
st.info('Tekan "Analysis" kemabli jika tampilan menghilang', icon = 'ⓘ')
|
201 |
+
if results and analisis == True and st.button("Analysis"):
|
202 |
+
# Membagi tampilan menjadi dua kolom
|
203 |
+
columns = st.columns(2)
|
204 |
+
|
205 |
+
# Kolom pertama untuk Word Cloud
|
206 |
+
with columns[0]:
|
207 |
+
if results:
|
208 |
+
all_texts = [result[2] for result in results if result[2] is not None and not pd.isna(result[2])]
|
209 |
+
all_texts = " ".join(all_texts)
|
210 |
+
|
211 |
+
st.subheader("Word Cloud")
|
212 |
+
|
213 |
+
if all_texts:
|
214 |
+
wordcloud = WordCloud(width=800, height=660, background_color='white',
|
215 |
+
colormap='Purples', # Warna huruf
|
216 |
+
contour_color='black', # Warna kontur
|
217 |
+
contour_width=2, # Lebar kontur
|
218 |
+
mask=None, # Gunakan mask untuk bentuk kustom
|
219 |
+
).generate(all_texts)
|
220 |
+
st.image(wordcloud.to_array())
|
221 |
+
else:
|
222 |
+
st.write("Tidak ada data untuk ditampilkan dalam Word Cloud.")
|
223 |
+
|
224 |
+
# Kolom kedua untuk Bar Chart
|
225 |
+
with columns[1]:
|
226 |
+
st.subheader("Chart")
|
227 |
+
if results:
|
228 |
+
df_results = pd.DataFrame(results, columns=["Teks", "Cleaned Text", "Norm Text", "Hasil Analisis Sentimen", "Emotikon"])
|
229 |
+
sns.set_style("whitegrid")
|
230 |
+
|
231 |
+
# Menyiapkan label kelas
|
232 |
+
class_labels = ["Negatif", "Netral", "Positif"]
|
233 |
+
|
234 |
+
# Menghitung nilai hitungan per label
|
235 |
+
value_counts = df_results["Hasil Analisis Sentimen"].value_counts()
|
236 |
+
|
237 |
+
# Mengurutkan nilai hitungan berdasarkan label
|
238 |
+
value_counts = value_counts.reindex(class_labels)
|
239 |
+
|
240 |
+
fig, ax = plt.subplots() # Buat objek Figure
|
241 |
+
sns.barplot(x=value_counts.index, y=value_counts.values, ax=ax) # Gunakan ax= untuk plot
|
242 |
+
plt.xticks(rotation=45)
|
243 |
+
|
244 |
+
st.pyplot(fig) # Tampilkan plot menggunakan st.pyplot(fig)
|
245 |
+
|
246 |
+
# Menampilkan hasil analisis sentimen dalam kotak yang dapat diperluas
|
247 |
+
with st.expander("Hasil Analisis Sentimen"):
|
248 |
+
# Tampilkan tabel hasil analisis sentimen
|
249 |
+
st.write(pd.DataFrame(results, columns=["Teks", "Cleaned Text", "Norm Text", "Hasil Analisis Sentimen", "Emotikon"]))
|
250 |
+
|
251 |
+
|
252 |
+
# Tautan untuk mengunduh hasil dalam format XLSX atau CSV
|
253 |
+
st.subheader("Unduh Hasil")
|
254 |
+
download_format = st.selectbox("Pilih format unduhan:", ["XLSX", "CSV"])
|
255 |
if results:
|
256 |
+
if download_format == "XLSX":
|
257 |
+
# Simpan DataFrame ke dalam file XLSX
|
258 |
+
df = pd.DataFrame(results, columns=["Teks", "Cleaned Text", "Norm Text", "Hasil Analisis Sentimen", "Emotikon"])
|
259 |
+
df.to_excel("hasil_sentimen.xlsx", index=False)
|
260 |
+
|
261 |
+
# Tampilkan tombol unduh XLSX
|
262 |
+
st.download_button(label="Unduh XLSX", data=open("hasil_sentimen.xlsx", "rb").read(), key="xlsx_download", file_name="hasil_sentimen.xlsx")
|
263 |
+
|
264 |
+
else: # Jika CSV
|
265 |
+
# Simpan DataFrame ke dalam file CSV
|
266 |
+
df = pd.DataFrame(results, columns=["Teks", "Cleaned Text", "Norm Text", "Hasil Analisis Sentimen", "Emotikon"])
|
267 |
+
csv = df.to_csv(index=False)
|
268 |
+
|
269 |
+
# Tampilkan tombol unduh CSV
|
270 |
+
st.download_button(label="Unduh CSV", data=csv, key="csv_download", file_name="hasil_sentimen.csv")
|
271 |
+
else:
|
272 |
+
st.write("Tidak ada data untuk diunduh.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
273 |
|
274 |
|
275 |
# Garis pemisah
|