File size: 10,178 Bytes
fccd4a8
 
 
 
 
 
 
 
 
 
 
 
d09b322
 
 
fccd4a8
 
f746f70
d09b322
f746f70
 
 
 
d09b322
 
 
 
 
 
 
 
 
 
 
 
fccd4a8
 
 
 
d09b322
fccd4a8
d09b322
fccd4a8
 
 
d09b322
 
 
 
 
 
 
 
 
 
fccd4a8
 
 
 
d09b322
 
 
 
 
 
 
 
 
fccd4a8
d09b322
fccd4a8
d09b322
 
 
fccd4a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d09b322
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fccd4a8
 
 
 
 
 
 
 
 
 
d09b322
 
 
 
 
ec4f424
 
 
 
d09b322
 
ec4f424
 
 
 
d09b322
 
ec4f424
 
 
 
d09b322
 
ec4f424
 
 
 
d09b322
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
st.set_page_config(
    page_title="Social Media Sentiment Analyzer", page_icon="πŸ“Š", layout="wide"
)

import pandas as pd
import helper_functions as hf
import plotly.express as px
import plotly.io as pio
import plotly

# Whenever the search button is clicked, the search_callback function is called
def search_callback_twitter():
    if platform == "Twitter":
        if len(st.session_state.search_term_twitter) == 0:
            st.error("Please enter a search term")
            return
        try:
            st.session_state.df = hf.get_tweets(st.session_state.search_term_twitter, st.session_state.num_tweets)
            st.session_state.df = hf.get_sentiment(st.session_state.df)
        except:
            st.error("Please enter a valid search term")
            return
        
def search_callback_youtube():
    if platform == "Youtube":
        if len(st.session_state.search_term_youtube) == 0:
            st.error("Please enter a valid url")
            return
        try:
            st.session_state.df = hf.get_youtube_comments(st.session_state.search_term_youtube, st.session_state.num_comments)
            st.session_state.df = hf.get_sentiment_youtube(st.session_state.df)
        except:
            st.error("Please enter a valid url")
            return

def twitter_form():
    with st.form(key="search_form"):
        st.subheader("Search Parameters")
        st.text_input("Enter a User handle (like _@elonmusk_), Hashtag (like _#Bitcoin_) or Topic (like _climate change_)", key="search_term_twitter")
        st.slider("Number of tweets", min_value=100, max_value=500, key="num_tweets")
        st.form_submit_button(label="Search", on_click=search_callback_twitter)
        st.markdown(
            "Note: it may take a while to load the results, especially with large number of tweets"
        )

def youtube_form():
    with st.form(key="search_form"):
        st.subheader("Search Parameters")
        st.text_input("Enter a Video link to analyse comments", key="search_term_youtube")
        st.slider("Number of Comments", min_value=100, max_value=500, key="num_comments")
        st.form_submit_button(label="Search", on_click=search_callback_youtube)
        st.markdown(
            "Note: it may take a while to load the results, especially with large number of comments"
        )
    

with st.sidebar:
    st.title("Social Media Sentiment Analyzer")
    #st.subheader("Choose your platform")
    platform = st.radio(
        "Choose your platform πŸ‘‡",
        ["Twitter", "Youtube"],
        # key="visibility",
        # label_visibility=st.session_state.visibility,
        # disabled=st.session_state.disabled,
        horizontal=True,
    )

    if platform == "Twitter":
        twitter_form()
    
    if platform == "Youtube":
        youtube_form()

    st.markdown(
    "<div style='position: fixed; bottom: 0;'>Created by Taaha Bajwa</div>",
    unsafe_allow_html=True,
    )

if "df" in st.session_state:
    def make_dashboard(tweet_df, bar_color, wc_color):
        # first row
        col1, col2, col3 = st.columns([28, 34, 38])
        with col1:
            sentiment_plot = hf.plot_sentiment(tweet_df)
            sentiment_plot.update_layout(height=350, title_x=0.5)
            st.plotly_chart(sentiment_plot, theme=None, use_container_width=True)
        with col2:
            top_unigram = hf.get_top_n_gram(tweet_df, ngram_range=(1, 1), n=10)
            unigram_plot = hf.plot_n_gram(
                top_unigram, title="Top 10 Occuring Words", color=bar_color
            )
            unigram_plot.update_layout(height=350)
            st.plotly_chart(unigram_plot, theme=None, use_container_width=True)
        with col3:
            top_bigram = hf.get_top_n_gram(tweet_df, ngram_range=(2, 2), n=10)
            bigram_plot = hf.plot_n_gram(
                top_bigram, title="Top 10 Occuring Bigrams", color=bar_color
            )
            bigram_plot.update_layout(height=350)
            st.plotly_chart(bigram_plot, theme=None, use_container_width=True)

        # second row
        col1, col2 = st.columns([60, 40])
        with col1:

            def sentiment_color(sentiment):
                if sentiment == "Positive":
                    return "background-color: #54A24B; color: white"
                elif sentiment == "Negative":
                    return "background-color: #FF7F0E"
                else:
                    return "background-color: #1F77B4"

            st.dataframe(
                tweet_df[["Sentiment", "Tweet"]].style.applymap(
                    sentiment_color, subset=["Sentiment"]
                ),
                height=350,
            )
        with col2:
            wordcloud = hf.plot_wordcloud(tweet_df, colormap=wc_color)
            st.pyplot(wordcloud)


    def make_dashboard_youtube(tweet_df, bar_color, wc_color):
        tweet_df = tweet_df.rename(columns={"Comment": "Tweet"})
        # first row
        col1, col2, col3 = st.columns([28, 34, 38])
        with col1:
            sentiment_plot = hf.plot_sentiment(tweet_df)
            sentiment_plot.update_layout(height=350, title_x=0.5)
            st.plotly_chart(sentiment_plot, theme=None, use_container_width=True)
        with col2:
            top_unigram = hf.get_top_n_gram(tweet_df, ngram_range=(1, 1), n=10)
            unigram_plot = hf.plot_n_gram(
                top_unigram, title="Top 10 Occuring Words", color=bar_color
            )
            unigram_plot.update_layout(height=350)
            st.plotly_chart(unigram_plot, theme=None, use_container_width=True)
        with col3:
            top_bigram = hf.get_top_n_gram(tweet_df, ngram_range=(2, 2), n=10)
            bigram_plot = hf.plot_n_gram(
                top_bigram, title="Top 10 Occuring Bigrams", color=bar_color
            )
            bigram_plot.update_layout(height=350)
            st.plotly_chart(bigram_plot, theme=None, use_container_width=True)

        # second row
        col1, col2 = st.columns([60, 40])
        with col1:

            def sentiment_color(sentiment):
                if sentiment == "Positive":
                    return "background-color: #54A24B; color: white"
                elif sentiment == "Negative":
                    return "background-color: #FF7F0E"
                else:
                    return "background-color: #1F77B4"
            tweet_df_temp = tweet_df[["Sentiment", "Tweet"]]
            tweet_df_temp = tweet_df_temp.rename(columns={"Tweet": "Comment"})
            st.dataframe(
                tweet_df_temp[["Sentiment", "Comment"]].style.applymap(
                    sentiment_color, subset=["Sentiment"]
                ),
                height=350,
            )
        with col2:
            wordcloud = hf.plot_wordcloud(tweet_df, colormap=wc_color, mask_url='static/yt_mask.png')
            try:
                st.pyplot(wordcloud)
            except:
                st.write("Wordcloud not available for this search term")

    adjust_tab_font = """
    <style>
    button[data-baseweb="tab"] > div[data-testid="stMarkdownContainer"] > p {
        font-size: 20px;
    }
    </style>
    """

    st.write(adjust_tab_font, unsafe_allow_html=True)

    if platform == "Twitter" and st.session_state.search_term_twitter != "":
        try:
            tab1, tab2, tab3, tab4 = st.tabs(["All", "Positive 😊", "Negative ☹️", "Neutral 😐"])
            with tab1:
                tweet_df = st.session_state.df
                if tweet_df.shape[0] > 0:
                    make_dashboard(tweet_df, bar_color="#1F77B4", wc_color="Blues")
                else:
                    st.write("No tweets to display.")
            with tab2:
                tweet_df = st.session_state.df.query("Sentiment == 'Positive'")
                if tweet_df.shape[0] > 0:
                    make_dashboard(tweet_df, bar_color="#54A24B", wc_color="Greens")
                else:
                    st.write("No tweets to display.")
            with tab3:
                tweet_df = st.session_state.df.query("Sentiment == 'Negative'")
                if tweet_df.shape[0] > 0:
                    make_dashboard(tweet_df, bar_color="#FF7F0E", wc_color="Oranges")
                else:
                    st.write("No tweets to display.")
            with tab4:
                tweet_df = st.session_state.df.query("Sentiment == 'Neutral'")
                if tweet_df.shape[0] > 0:
                    make_dashboard(tweet_df, bar_color="#1F77B4", wc_color="Blues")
                else:
                    st.write("No tweets to display.")
        except:
            st.error("No plots to display.")
    
    elif platform == "Youtube" and st.session_state.search_term_youtube != "":
        try:
            tab1, tab2, tab3, tab4 = st.tabs(["All", "Positive 😊", "Negative ☹️", "Neutral 😐"])
            with tab1:
                tweet_df = st.session_state.df
                if tweet_df.shape[0] > 0:
                    make_dashboard_youtube(tweet_df, bar_color="#1F77B4", wc_color="Blues")
                else:
                    st.write("No comments found.")
            with tab2:
                tweet_df = st.session_state.df.query("Sentiment == 'Positive'")
                if tweet_df.shape[0] > 0:
                    make_dashboard_youtube(tweet_df, bar_color="#54A24B", wc_color="Greens")
                else:
                    st.write("No positive comments found.")
            with tab3:
                tweet_df = st.session_state.df.query("Sentiment == 'Negative'")
                if tweet_df.shape[0] > 0:
                    make_dashboard_youtube(tweet_df, bar_color="#FF7F0E", wc_color="Oranges")
                else:
                    st.write("No negative comments found.")
            with tab4:
                tweet_df = st.session_state.df.query("Sentiment == 'Neutral'")
                if tweet_df.shape[0] > 0:
                    make_dashboard_youtube(tweet_df, bar_color="#1F77B4", wc_color="Blues")
                else:
                    st.write("No neutral comments found.")
        except:
            st.error("No plots to display.")