File size: 3,775 Bytes
02a7dcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dbd544a
02a7dcf
 
 
36374f8
02a7dcf
 
 
 
 
 
36374f8
02a7dcf
 
 
 
 
 
 
 
 
 
0304dc4
02a7dcf
 
 
36374f8
1986c70
02a7dcf
 
 
 
 
36374f8
02a7dcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
364f083
 
 
02a7dcf
 
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
from variables import *
import plotly_express as px
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
import matplotlib.pyplot as plt
import streamlit as st
import numpy as np
import pandas as pd
import textwrap

st.set_option('deprecation.showPyplotGlobalUse', False)

#st.set_page_config(page_title="Earnings Sentiment Analysis", page_icon="πŸ“ˆ")
st.sidebar.header("Sentiment Analysis Visualization")
st.markdown("## Sentiment Analysis and Density Graphs")

max_word = st.sidebar.slider(label= "WordCloud Max Words", min_value=20, max_value=500, value=50)
max_font = st.sidebar.slider(label = "WordCloud Max Font", min_value=50, max_value=350, value=50)


stopwords = set(STOPWORDS)
stopwords.update(['us', 'one', 'will', 'said', 'now', 'well', 'man', 'may',
    'little', 'say', 'must', 'way', 'long', 'yet', 'mean',
    'put', 'seem', 'asked', 'made', 'half', 'much',
    'certainly', 'might', 'came','RT','amp'])

def cloud(text, max_word, max_font, random):
    '''Generate Word Cloud'''
    
    wc = WordCloud(background_color="white", colormap="hot", max_words=max_word,
    stopwords=stopwords, max_font_size=max_font, random_state=random).generate(text)

    return wc
    
try:

    if 'tdf' in st.session_state:
        
        df = st.session_state['tdf']
        # df['creation_date'] = pd.to_datetime(df['creation_date'], 
        #                                format='%Y-%m-%d %H:%M:%S-%Z', 
        #                                errors='coerce').dt.date

        with st.container():
            st.subheader('Sentiment Scatter Plot')
            ## Display negative sentence locations
            ht = df.tweet.apply(lambda txt: '<br>'.join(textwrap.wrap(txt, width=70)))
            fig = px.scatter(df, y='sentiment', x='creation_time', color='sentiment', size='sentiment_confidence', hover_data=[ht,'topic','username'], \
                             color_discrete_map={"Bearish":"firebrick","Neutral":"navajowhite","Bullish":"darkgreen"}, \
                             title='Sentiment Score Distribution')
        
            fig.update_layout(
            	showlegend=False,
                autosize=True,
                width=900,
                height=500,
                margin=dict(
                    b=5,
                    t=50,
                    pad=2
                )
            )
        
            st.plotly_chart(fig)

        with st.container():
            st.subheader('Topic Distribution Scatter Plot')
            ## Display negative sentence locations
            ht = df.tweet.apply(lambda txt: '<br>'.join(textwrap.wrap(txt, width=70)))
            fig = px.scatter(df, y='topic', x='creation_time', color='sentiment', size='topic_confidence', hover_data=[ht,'topic','username'],\
                             color_discrete_map={"Bearish":"firebrick","Neutral":"navajowhite","Bullish":"darkgreen"},\
                             title='Topic Score Distribution')
        
            fig.update_layout(
            	showlegend=False,
                autosize=True,
                width=900,
                height=500,
                margin=dict(
                    b=5,
                    t=50,
                    pad=2
                )
            )
        
            st.plotly_chart(fig)
    
        with st.container():
            st.subheader('Sentiment WordCloud')
            cleaned_tweets = "".join(df['tweet'].tolist())
            wc = cloud(cleaned_tweets, max_word, max_font, 35)
            plt.imshow(wc, interpolation='bilinear')
            plt.axis("off")
            plt.show()
            st.pyplot()
        
except (AttributeError, KeyError) as e:

    print(e)

    st.error('Tweets Error, please navigate to Home page and refresh tweet stream', icon="🚨")