File size: 4,309 Bytes
fccd4a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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():
    if twitter_agree:
        if len(st.session_state.search_term) == 0:
            st.error("Please enter a search term")
            return
        st.session_state.df = hf.get_tweets(st.session_state.search_term, st.session_state.num_tweets)
        st.session_state.df = hf.get_sentiment(st.session_state.df)

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")
        st.slider("Number of tweets", min_value=100, max_value=500, key="num_tweets")
        st.form_submit_button(label="Search", on_click=search_callback)
        st.markdown(
            "Note: it may take a while to load the results, especially with large number of tweets"
        )
    

with st.sidebar:
    st.title("Social Media Sentiment Analyzer")
    st.subheader("Choose your platform")
    twitter_agree = st.checkbox('Twitter')

    if twitter_agree:
        twitter_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)

    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)

    tab1, tab2, tab3, tab4 = st.tabs(["All", "Positive 😊", "Negative ☹️", "Neutral 😐"])
    with tab1:
        tweet_df = st.session_state.df
        make_dashboard(tweet_df, bar_color="#1F77B4", wc_color="Blues")
    with tab2:
        tweet_df = st.session_state.df.query("Sentiment == 'Positive'")
        make_dashboard(tweet_df, bar_color="#54A24B", wc_color="Greens")
    with tab3:
        tweet_df = st.session_state.df.query("Sentiment == 'Negative'")
        make_dashboard(tweet_df, bar_color="#FF7F0E", wc_color="Oranges")
    with tab4:
        tweet_df = st.session_state.df.query("Sentiment == 'Neutral'")
        make_dashboard(tweet_df, bar_color="#1F77B4", wc_color="Blues")