Spaces:
Build error
Build error
Create pages/1_Tweets_Visualization_π_.py
Browse files
pages/1_Tweets_Visualization_π_.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from variables import *
|
2 |
+
import plotly_express as px
|
3 |
+
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import streamlit as st
|
6 |
+
import numpy as np
|
7 |
+
import pandas as pd
|
8 |
+
import textwrap
|
9 |
+
|
10 |
+
st.set_option('deprecation.showPyplotGlobalUse', False)
|
11 |
+
|
12 |
+
#st.set_page_config(page_title="Earnings Sentiment Analysis", page_icon="π")
|
13 |
+
st.sidebar.header("Sentiment Analysis Visualization")
|
14 |
+
st.markdown("## Sentiment Analysis and Density Graphs")
|
15 |
+
|
16 |
+
max_word = st.sidebar.slider(label= "WordCloud Max Words", min_value=20, max_value=500, value=50)
|
17 |
+
max_font = st.sidebar.slider(label = "WordCloud Max Font", min_value=50, max_value=350, value=50)
|
18 |
+
|
19 |
+
|
20 |
+
stopwords = set(STOPWORDS)
|
21 |
+
stopwords.update(['us', 'one', 'will', 'said', 'now', 'well', 'man', 'may',
|
22 |
+
'little', 'say', 'must', 'way', 'long', 'yet', 'mean',
|
23 |
+
'put', 'seem', 'asked', 'made', 'half', 'much',
|
24 |
+
'certainly', 'might', 'came','RT','amp'])
|
25 |
+
|
26 |
+
def cloud(text, max_word, max_font, random):
|
27 |
+
'''Generate Word Cloud'''
|
28 |
+
|
29 |
+
wc = WordCloud(background_color="white", colormap="hot", max_words=max_word,
|
30 |
+
stopwords=stopwords, max_font_size=max_font, random_state=random).generate(text)
|
31 |
+
|
32 |
+
return wc
|
33 |
+
|
34 |
+
try:
|
35 |
+
|
36 |
+
if 'tdf' in st.session_state:
|
37 |
+
|
38 |
+
df = st.session_state['tdf']
|
39 |
+
# df['creation_date'] = pd.to_datetime(df['creation_date'],
|
40 |
+
# format='%Y-%m-%d %H:%M:%S-%Z',
|
41 |
+
# errors='coerce').dt.date
|
42 |
+
|
43 |
+
with st.comtainer():
|
44 |
+
st.subheader('Sentiment Scatter Plot')
|
45 |
+
## Display negative sentence locations
|
46 |
+
ht = df.tweet.apply(lambda txt: '<br>'.join(textwrap.wrap(txt, width=70)))
|
47 |
+
fig = px.scatter(df, y='sentiment', x='creation_time', color='topic', size='sentiment_confidence', hover_data=[ht,'topic'], \
|
48 |
+
color_discrete_map={"Bearish":"firebrick","Neutral":"navajowhite","Bullish":"darkgreen"}, \
|
49 |
+
title='Sentiment Score Distribution')
|
50 |
+
|
51 |
+
fig.update_layout(
|
52 |
+
showlegend=False,
|
53 |
+
autosize=True,
|
54 |
+
width=1000,
|
55 |
+
height=500,
|
56 |
+
margin=dict(
|
57 |
+
b=5,
|
58 |
+
t=50,
|
59 |
+
pad=2
|
60 |
+
)
|
61 |
+
)
|
62 |
+
|
63 |
+
st.plotly_chart(fig)
|
64 |
+
|
65 |
+
with st.comtainer():
|
66 |
+
st.subheader('Topic Distribution Scatter Plot')
|
67 |
+
## Display negative sentence locations
|
68 |
+
ht = df.tweet.apply(lambda txt: '<br>'.join(textwrap.wrap(txt, width=70)))
|
69 |
+
fig = px.scatter(df, y='topic', x='creation_time', color='sentiment', size='topic_confidence', hover_data=[ht,'sentiment'],\
|
70 |
+
title='Topic Score Distribution')
|
71 |
+
|
72 |
+
fig.update_layout(
|
73 |
+
showlegend=False,
|
74 |
+
autosize=True,
|
75 |
+
width=1000,
|
76 |
+
height=500,
|
77 |
+
margin=dict(
|
78 |
+
b=5,
|
79 |
+
t=50,
|
80 |
+
pad=2
|
81 |
+
)
|
82 |
+
)
|
83 |
+
|
84 |
+
st.plotly_chart(fig)
|
85 |
+
|
86 |
+
with st.container():
|
87 |
+
st.subheader('Topic Density Heatmap')
|
88 |
+
fig = px.density_heatmap(df, x='creation_time', y='topic')
|
89 |
+
st.plotly_chart(fig)
|
90 |
+
|
91 |
+
with st.container():
|
92 |
+
st.subheader('Sentiment WordCloud')
|
93 |
+
cleaned_tweets = "".join(df['tweet'].tolist())
|
94 |
+
wc = cloud(cleaned_tweets, max_word, max_font, 35)
|
95 |
+
plt.imshow(wc, interpolation='bilinear')
|
96 |
+
plt.axis("off")
|
97 |
+
plt.show()
|
98 |
+
st.pyplot()
|
99 |
+
|
100 |
+
else:
|
101 |
+
|
102 |
+
st.warning("No Tweets detected, please navigate to Home page and refresh tweet stream",icon="β οΈ")
|
103 |
+
|
104 |
+
except (AttributeError, KeyError):
|
105 |
+
|
106 |
+
st.error('Tweets Error, please navigate to Home page and refresh tweet stream', icon="π¨")
|