-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
489 lines (427 loc) · 19 KB
/
app.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import streamlit as st
from streamlit_option_menu import option_menu
import os
import re
import string
import pandas as pd
from Sastrawi.Stemmer.StemmerFactory import StemmerFactory
import matplotlib.pyplot as plt
import seaborn as sns
from wordcloud import WordCloud
# Default Wide Mode Appearance Streamlit
st.set_page_config(layout='wide')
# Default Dataset
text_data = "Tolong Cleansing data berikut: \n1. Emoji : 😃😃😃 \n2. UPPERCASE \n3. Siaaaap \n4. Kamus alay : 3x, aer, t3tapjokowi \n5. Stemming : mencintai membacakan \n6. Stopwords : aku dan kamu"
csv_data = pd.read_csv("data.csv", encoding = 'ISO-8859-1')
# Import kamus alay
df_kamusalay = pd.read_csv("new_kamusalay.csv", encoding = 'ISO-8859-1', header=None,index_col=0,squeeze=True)
dict_kamusalay = df_kamusalay.to_dict()
#Import Stopwords Indonesia
df_stopwords = pd.read_csv("stopwordbahasa.csv", encoding = 'ISO-8859-1')
list_stopwords = df_stopwords["stopwordbahasa"].tolist()
# Sidebar
with st.sidebar:
selected = option_menu ("Text Cleaner App",
["Input Text",
"Input Text from CSV",
"Input Multiple Text from CSV",
"Update Kamus Alay",
"Update Stopwords"])
# Checkbox
st.header("Select Option :")
all_option_check = st.checkbox ("EXECUTE ALL OPTION")
remove_emoji_check = st.checkbox ("Remove Emoji")
lowercase_check = st.checkbox ("Lowercase")
threeormore_check = st.checkbox ("Three or More")
stemming_check = st.checkbox ("Stemming")
tokenization_check = st.checkbox ("Tokenization")
normalization_check = st.checkbox ("Normalization (using Kamus Alay)")
remove_number_check = st.checkbox ("Remove Number")
remove_punctuation_check = st.checkbox ("Remove Punctuation")
stopwords_check = st.checkbox ("Stopwords")
to_string_check = st.checkbox ("List to String")
# FUNCTION
# Remove emoji
def remove_emoji (data):
# Remove UTF-8 format emoji
data = re.sub(r'\\x[0-9A-Fa-f]{2}', '', data)
#return data
# Remove unicode format emoji
st.success("Success to Run 'Remove Emoji'", icon="✅")
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F" # emoticons
u"\U0001F300-\U0001F5FF" # symbols & pictographs
u"\U0001F680-\U0001F6FF" # transport & map symbols
u"\U0001F1E0-\U0001F1FF" # flags (iOS)
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
"]+", flags=re.UNICODE)
return emoji_pattern.sub(r'', data)
# Lowercase
def lowercase (data):
data = data.lower()
st.success("Success to Run 'Lowercase'", icon="✅")
return data
# Three or More
def threeormore (data):
pattern = re.compile(r"(.)\1{1,}", re.DOTALL)
data = pattern.sub(r"\1", data)
st.success("Success to Run 'Three or More'", icon="✅")
return data
# Stemming
def stemming (data) :
factory = StemmerFactory()
stemmer = factory.create_stemmer()
data = stemmer.stem(data)
st.success("Success to Run 'Stemming'", icon="✅")
return data
# Tokenization
def tokenization (data) :
data = data.split()
st.success("Success to Run 'Tokenization'", icon="✅")
return data
# Normalization (Kamus Alay)
def normalization (data) :
for i in data :
for key, value in dict_kamusalay.items():
if key not in data:
continue
index = data.index(key)
data[index] = value
st.success("Success to Run 'Normalization'", icon="✅")
return data
# Remove Number
def remove_number (data):
data = str(data)
data = re.sub(r'\d+','',data)
data = data.split()
st.success("Success to Run 'Remove Number'", icon="✅")
return data
# Remove Punctuation
def remove_punctuation (data) :
data = str(data)
data = re.sub('-', ' ', data)
data = re.sub(r'[^\w\s]', ' ', data)
data = data.split()
st.success("Success to Run 'Remove Punctuation'", icon="✅")
return data
# Stopwords
def stopwords (data) :
for i in reversed (data) :
if i in list_stopwords :
data.remove(i)
st.success("Success to Run 'Stopwords'", icon="✅")
return data
# List to String
def to_string (data) :
data = ' '.join(map(str,data))
st.success("Success to Run 'List to String'", icon="✅")
return data
# Update file CSV text cleansing
def update_csv (uploadedfile):
with open (os.path.join("data.csv"),"wb") as f:
f.write (uploadedfile.getbuffer())
return st.success ("Saved file:{} ".format(uploadedfile.name))
# Update File Kamus Alay
def update_kamusalay (uploadedfile):
with open (os.path.join("new_kamusalay.csv"),"wb") as f:
f.write (uploadedfile.getbuffer())
return st.success ("Saved file:{} ".format(uploadedfile.name))
# Update Stopwords
def update_stopwords (uploadedfile):
with open (os.path.join("stopwordbahasa.csv"),"wb") as f:
f.write (uploadedfile.getbuffer())
return st.success ("Saved file:{} ".format(uploadedfile.name))
# Main Page
st.title ("Text Cleaner App")
# Input Text
if selected == "Input Text":
col1, col2 = st.columns(2)
with col1:
st.subheader ("Input text :")
text_data = st.text_area ("Input text :",text_data, height = 200,label_visibility="collapsed")
st.write (f"Data Type of Input Text : {type(text_data)}")
running_cleansing_text = st.button ("Click to Run Text Cleansing")
if running_cleansing_text:
# EXECUTE ALL OPTION
if all_option_check :
if stemming_check or lowercase_check or remove_number_check or remove_punctuation_check or tokenization_check or normalization_check or stopwords_check == True :
st.error('"Failed to Run "Execute All Option". Please choose "Execute All Option" ONLY !', icon="🚨")
else :
text_data = remove_emoji (text_data)
text_data = lowercase (text_data)
text_data = threeormore (text_data)
text_data = stemming (text_data)
text_data = tokenization (text_data)
text_data = normalization (text_data)
text_data = remove_number (text_data)
text_data = remove_punctuation (text_data)
text_data = stopwords (text_data)
text_data = to_string (text_data)
st.success("Success to Run 'Execute All Option'", icon="✅")
# Remove Emoji
if remove_emoji_check :
text_data = remove_emoji (text_data)
# Lowercase
if lowercase_check :
text_data = lowercase (text_data)
else:
pass
# Three or More
if threeormore_check :
text_data = threeormore (text_data)
# Stemming
if stemming_check :
text_data = stemming (text_data)
# Tokenization
if tokenization_check :
text_data = tokenization (text_data)
# Normalization (Kamus Alay)
if normalization_check :
if tokenization_check == False :
st.error('"Failed to Run "Normalization". Normalization need Tokenization. Please choose Tokenization first !', icon="🚨")
#st.write ("Normalization need Tokenization. Please choose Tokenization first !")
else :
pass
text_data = normalization (text_data)
# Remove Number
if remove_number_check :
text_data = remove_number (text_data)
# Remove Punctuation
if remove_punctuation_check :
text_data = remove_punctuation (text_data)
# Stopwords
if stopwords_check :
if tokenization_check == False :
st.error('"Failed to Run "Stopwords". Stopwords need Tokenization. Please choose Tokenization first !', icon="🚨")
#st.write ("Stopwords need Tokenization. Please choose Tokenization first !")
else :
text_data = stopwords (text_data)
# List to String
if to_string_check :
text_data = to_string (text_data)
with col2:
st.subheader ("Output text :")
st.text_area ("Output text :", value = text_data, height = 200,label_visibility="collapsed")
st.write (f"Data Type of Output Text : {type(text_data)}")
# Input CSV
if selected == "Input Text from CSV":
col1, col2, col3, col4 = st.columns(4)
with col1:
st.subheader ("Update File CSV")
upload_file1 = st.file_uploader ("Update File CSV", type = "csv", label_visibility="collapsed")
if upload_file1 is not None :
csv_data_uploaded = pd.read_csv(upload_file1, encoding = 'ISO-8859-1')
st.dataframe (csv_data_uploaded)
clicked = st.button ("Click to Update Database")
if clicked :
update_csv (upload_file1)
csv_data = csv_data_uploaded
st.subheader ("List File CSV After Update")
st.dataframe (csv_data)
with col2:
# Default Data CSV
st.subheader ("Data CSV Used:")
st.dataframe (csv_data, height = 200)
# Choose Row and Coloumn
st.subheader ("Write the Row :")
choose_row = st.text_input ("Write the Row :", value = "Tweet", label_visibility="collapsed" )
st.subheader ("Pick the Column :")
choose_column = st.number_input ("Pick the Number of Column :", value = 1, key = int, step = 1, min_value=None, max_value=None, label_visibility="collapsed" )
st.write (f"Data yang dipilih adalah kolom '{choose_row}' baris ke-'{choose_column}'")
text_data = csv_data[choose_row][choose_column]
with col3:
# Input Text Used
st.subheader ("Input Text Used :")
st.text_area ("Input Text Used :", value = text_data, height =200, label_visibility ="collapsed" )
st.write (f"Data Type : {type(text_data)}")
running_cleansing_text_2 = st.button ("Click to Run Text Cleansing")
if running_cleansing_text_2:
# EXECUTE ALL OPTION
if all_option_check :
if stemming_check or lowercase_check or remove_number_check or remove_punctuation_check or tokenization_check or normalization_check or stopwords_check == True :
st.error('"Failed to Run "Execute All Option". Please choose "Execute All Option" ONLY !', icon="🚨")
else :
text_data = remove_emoji (text_data)
text_data = lowercase (text_data)
text_data = threeormore (text_data)
text_data = stemming (text_data)
text_data = tokenization (text_data)
text_data = normalization (text_data)
text_data = remove_number (text_data)
text_data = remove_punctuation (text_data)
text_data = stopwords (text_data)
text_data = to_string (text_data)
st.success("Success to Run 'Execute All Option'", icon="✅")
# Remove Emoji
if remove_emoji_check :
text_data = remove_emoji (text_data)
# Lowercase
if lowercase_check :
text_data = lowercase (text_data)
else:
pass
# Three or More
if threeormore_check :
text_data = threeormore (text_data)
# Stemming
if stemming_check :
text_data = stemming (text_data)
# Tokenization
if tokenization_check :
text_data = tokenization (text_data)
# Normalization (Kamus Alay)
if normalization_check :
if tokenization_check == False :
st.error('"Failed to Run "Normalization". Normalization need Tokenization. Please choose Tokenization first !', icon="🚨")
#st.write ("Normalization need Tokenization. Please choose Tokenization first !")
else :
pass
text_data = normalization (text_data)
# Remove Number
if remove_number_check :
text_data = remove_number (text_data)
# Remove Punctuation
if remove_punctuation_check :
text_data = remove_punctuation (text_data)
# Stopwords
if stopwords_check :
if tokenization_check == False :
st.error('"Failed to Run "Stopwords". Stopwords need Tokenization. Please choose Tokenization first !', icon="🚨")
#st.write ("Stopwords need Tokenization. Please choose Tokenization first !")
else :
text_data = stopwords (text_data)
# List to String
if to_string_check :
text_data = to_string (text_data)
with col4:
st.subheader ("Output text :")
st.text_area ("Output text :", value = text_data, height = 200,label_visibility="collapsed")
st.write (f"Data Type of Output Text : {type(text_data)}")
# Input Multiple Text from CSV
if selected == "Input Multiple Text from CSV":
col1, col2, col3 = st.columns(3)
with col1:
st.subheader ("Update File CSV")
upload_file1 = st.file_uploader ("Update File CSV", type = "csv", label_visibility="collapsed")
if upload_file1 is not None :
csv_data_uploaded = pd.read_csv(upload_file1, encoding = 'ISO-8859-1')
st.dataframe (csv_data_uploaded)
clicked = st.button ("Click to Update Database")
if clicked :
update_csv (upload_file1)
csv_data = csv_data_uploaded
st.subheader ("List File CSV After Update")
st.dataframe (csv_data)
# Default Data CSV
st.subheader ("Data CSV Used:")
st.dataframe (csv_data, height = 200)
# Choose Row and Coloumn
st.subheader ("Write the Row :")
choose_row = st.text_input ("Write the Row :", value = "Tweet", label_visibility="collapsed" )
st.subheader ("Pick the Column :")
values = st.slider('Select a range of values', 0, 500, (0, 1))
st.write('Values:', values)
a, b = values
choose_row_series = csv_data[choose_row]
selected_row = choose_row_series[a:(b+1)].tolist()
text_data = ' '.join(selected_row)
with col2:
# Input Text Used
st.subheader ("Input Text Used :")
st.text_area ("Input Text Used :", value = text_data, height =200, label_visibility ="collapsed" )
word_counts = ()
#if running_word_counter:
text_data = remove_emoji (text_data)
text_data = lowercase (text_data)
text_data = threeormore (text_data)
text_data = stemming (text_data)
text_data = tokenization (text_data)
text_data = normalization (text_data)
text_data = remove_number (text_data)
text_data = remove_punctuation (text_data)
text_data = stopwords (text_data)
text_data = to_string (text_data)
words = pd.Series(text_data.split())
word_count = words.value_counts()
st.success("Success to Run 'Execute Word Counter'", icon="✅")
with col3:
st.subheader ("Output text :")
st.text_area ("Output text :", value = text_data, height = 200,label_visibility="collapsed")
# Bar Chart
st.subheader ("Bar Chart Top 5 Word :")
top_5 = word_count[:5].reset_index()
fig = plt.figure(figsize=(10,5))
sns.barplot(x='index', y=0, data = top_5)
st.pyplot(fig)
# Word Cloud
st.subheader ("Word Cloud :")
st.set_option('deprecation.showPyplotGlobalUse', False)
wordcloud = WordCloud(background_color = 'white').generate(text_data)
plt.imshow(wordcloud)
plt.axis('off')
st.pyplot()
# Update Kamus Alay
if selected == "Update Kamus Alay":
col1, col2, col3 = st.columns(3)
with col1 :
st.subheader ("List Kamus Alay")
st.dataframe (df_kamusalay)
# Download Kamus Alay
st.subheader ("Download Kamus Alay")
@st.cache
def convert_df(df):
return df.to_csv(index_label=False).encode('utf-8')
csv = convert_df(df_kamusalay)
st.download_button(
label="Download data as CSV",
data=csv,
file_name='new_kamusalay.csv',
mime='text/csv',)
with col2 :
# Upload Kamus Alay
st.subheader ("Update List Kamus Alay")
upload_file2 = st.file_uploader ("Update List Kamus Alay", type = "csv", label_visibility="collapsed")
if upload_file2 is not None :
csv_data_uploaded = pd.read_csv(upload_file2, encoding = 'ISO-8859-1')
st.dataframe (csv_data_uploaded)
clicked = st.button ("Click to Update Database")
if clicked :
update_kamusalay (upload_file2)
df_kamusalay = csv_data_uploaded
with col3 :
if clicked :
st.subheader ("List Kamus Alay After Update")
st.dataframe (df_kamusalay)
# Update Stopwords
if selected == "Update Stopwords":
col1, col2, col3 = st.columns(3)
with col1 :
st.subheader ("List Stopwords")
st.dataframe (df_stopwords)
# Download Stopwords
st.subheader ("Download Stopwords")
@st.cache
def convert_df(df):
return df.to_csv(index=False,index_label=False).encode('utf-8')
csv = convert_df(df_stopwords)
st.download_button(
label="Download data as CSV",
data=csv,
file_name='stopwordbahasa.csv',
mime='text/csv',)
with col2 :
# Upload File CSV
st.subheader ("Update List Stopwords")
upload_file3 = st.file_uploader ("Update List Stopwords", type = "csv", label_visibility="collapsed")
if upload_file3 is not None :
stopwords_uploaded = pd.read_csv(upload_file3, encoding = 'ISO-8859-1')
st.dataframe (stopwords_uploaded)
clicked = st.button ("Click to Update Database")
if clicked :
update_stopwords (upload_file3)
df_stopwords = stopwords_uploaded
with col3 :
if clicked :
st.subheader ("List Stopwords After Update")
st.dataframe (df_stopwords)