File size: 6,180 Bytes
a2720a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126a2ed
a2720a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st

import streamlit as st 
import pandas as pd
import script.functions as fn
import plotly.express as px
import matplotlib.pyplot as plt
# import text_proc in script folder
import script.text_proc as tp
from sentence_transformers import SentenceTransformer


st.set_page_config(
    page_title="twitter sentiment analysis",
    page_icon="πŸ‘‹",
)

st.sidebar.markdown("πŸ“š Twitter Sentiment Analysis App")

# Load data
# add tiwtter logo inside title 
st.markdown("<h1 style='text-align: center;'>πŸ“š Twitter Sentiment Analysis App</h1>", unsafe_allow_html=True)
st.write("Aplikasi sederhana untuk melakukan analisis sentimen terhadap tweet yang diinputkan dan mengekstrak topik dari setiap sentimen.")
# streamlit selectbox simple and advanced

sb1,sb2 = st.columns([2,4])
with sb1:
    option = st.selectbox('Pilih Mode Pencarian',('Simple','Advanced'))
with sb2:
    option_model = st.selectbox('Pilih Model',("IndoBERT (Accurate,Slow)",'Naive Bayes','Logistic Regression (Less Accurate,Fast)','XGBoost','Catboost','SVM','Random Forest'))

if option == 'Simple':
# create col1 and col2
    col1, col2 = st.columns([3,2])
    with col1:
        input = st.text_input("Masukkan User/Hastag", "@traveloka")
    with col2:
        length = st.number_input("Jumlah Tweet", 10, 500, 100)
else :
    col1, col2 = st.columns([3,1])
    with col1:
        input = st.text_input("Masukkan Parameter Pencarian", "(to:@traveloka AND @traveloka) -filter:links filter:replies lang:id")
    with col2:
        length = st.number_input("Jumlah Tweet", 10, 500, 100)
    st.caption("anda bisa menggunakan parameter pencarian yang lebih spesifik, parameter ini sama dengan paremeter pencarian di twitter")

submit = st.button("πŸ”Cari Tweet")

st.caption("semakin banyak tweet yang diambil maka semakin lama proses analisis sentimen")

if submit:
    with st.spinner('Mengambil data dari twitter... (1/2)'):
        df = fn.get_tweets(input, length, option)
    with st.spinner('Melakukan Prediksi Sentimen... (2/2)'):
        df = fn.get_sentiment(df,option_model)
        df.to_csv('assets/data.csv',index=False)
    # plot
    st.write("<b>Preview Dataset</b>",unsafe_allow_html=True)
    def color_sentiment(val):
        color_dict = {"positif": "#00cc96", "negatif": "#ef553b","netral": "#636efa"}
        return f'color: {color_dict[val]}'
    st.dataframe(df.style.applymap(color_sentiment, subset=['sentiment']),use_container_width=True,height = 200)
    # st.dataframe(df,use_container_width=True,height = 200)
    st.write ("Jumlah Tweet: ",df.shape[0])
    # download datasets 
    

    st.write("<h3>πŸ“Š Analisis Sentimen</h3>",unsafe_allow_html=True)
    col_fig1, col_fig2 = st.columns([4,3])
    with col_fig1:
         with st.spinner('Sedang Membuat Grafik...'):
            st.write("<b>Jumlah Tweet Tiap Sentiment</b>",unsafe_allow_html=True)
            fig_1 = fn.get_bar_chart(df)
            st.plotly_chart(fig_1,use_container_width=True,theme="streamlit")
    with col_fig2:
        st.write("<b>Wordcloud Tiap Sentiment</b>",unsafe_allow_html=True)
        tab1,tab2,tab3 = st.tabs(["😞 negatif","😐 netral","πŸ˜ƒ positif"])
        with tab1:
            wordcloud_pos = tp.get_wordcloud(df,"negatif")
            fig = plt.figure(figsize=(10, 5))
            plt.imshow(wordcloud_pos, interpolation="bilinear")
            plt.axis("off")
            st.pyplot(fig)
        with tab2:
            wordcloud_neg = tp.get_wordcloud(df,"netral")
            fig = plt.figure(figsize=(10, 5))
            plt.imshow(wordcloud_neg, interpolation="bilinear")
            plt.axis("off")
            st.pyplot(fig)
        with tab3:
            wordcloud_net = tp.get_wordcloud(df,"positif")
            fig = plt.figure(figsize=(10, 5))
            plt.imshow(wordcloud_net, interpolation="bilinear")
            plt.axis("off")
            st.pyplot(fig)
    st.write("<h3>✨ Sentiment Clustering</h3>",unsafe_allow_html=True)
    @st.cache_resource
    def load_sentence_model():
        embedding_model = SentenceTransformer('sentence_bert')
        return embedding_model
    embedding_model = load_sentence_model()
    tab4,tab5,tab6 = st.tabs(["😞 negatif","😐 netral","πŸ˜ƒ positif"])
    with tab4:
        if len(df[df["sentiment"]=="negatif"]) < 11:
            st.write("Tweet Terlalu Sedikit, Tidak dapat melakukan clustering")
            st.write(df[df["sentiment"]=="negatif"])
        else:
            with st.spinner('Sedang Membuat Grafik...(1/2)'):
                text,data,fig = tp.plot_text(df,"negatif",embedding_model)
                st.plotly_chart(fig,use_container_width=True,theme=None)
            with st.spinner('Sedang Mengekstrak Topik... (2/2)'):
                fig,topic_modelling = tp.topic_modelling(text,data)
                st.plotly_chart(fig,use_container_width=True,theme="streamlit")
    with tab5:
        if len(df[df["sentiment"]=="netral"]) < 11:
            st.write("Tweet Terlalu Sedikit, Tidak dapat melakukan clustering")
            st.write(df[df["sentiment"]=="netral"])
        else:
            with st.spinner('Sedang Membuat Grafik... (1/2)'):
                text,data,fig = tp.plot_text(df,"netral",embedding_model)
                st.plotly_chart(fig,use_container_width=True,theme=None)
            with st.spinner('Sedang Mengekstrak Topik... (2/2)'):
                fig,topic_modelling = tp.topic_modelling(text,data)
                st.plotly_chart(fig,use_container_width=True,theme="streamlit")
    with tab6:
        if len(df[df["sentiment"]=="positif"]) < 11:
            st.write("Tweet Terlalu Sedikit, Tidak dapat melakukan clustering")
            st.write(df[df["sentiment"]=="positif"])
        else:
            with st.spinner('Sedang Membuat Grafik...(1/2)'):
                text,data,fig = tp.plot_text(df,"positif",embedding_model)
                st.plotly_chart(fig,use_container_width=True,theme=None)
            with st.spinner('Sedang Mengekstrak Topik... (2/2)'):
                fig,topic_modelling = tp.topic_modelling(text,data)
                st.plotly_chart(fig,use_container_width=True,theme="streamlit")