-
Notifications
You must be signed in to change notification settings - Fork 0
/
airline_tweets.py
152 lines (132 loc) · 6.29 KB
/
airline_tweets.py
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
import streamlit as st
import pandas as pd
import numpy as np
import plotly.express as px
from plotly.subplots import make_subplots
import plotly as go
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
DATA_URL = (
"Tweets.csv"
)
st.title("Sentiment Analysis of Tweets about US Airlines")
st.sidebar.title("Sentiment Analysis of Tweets")
st.markdown("This application is a Streamlit dashboard used to analyze sentiments of tweets 🦩")
st.sidebar.markdown("This application is a Streamlit dashboard used to analyze sentiments of tweets 🦩")
@st.cache_data(persist=True)
def load_data():
data = pd.read_csv(DATA_URL)
data['tweet_created'] = pd.to_datetime(data['tweet_created'])
return data
data = load_data()
st.sidebar.subheader("Show random tweet")
random_tweet = st.sidebar.radio('Sentiment', ('positive', 'neutral', 'negative'))
st.sidebar.markdown(data.query("airline_sentiment == @random_tweet")[["text"]].sample(n=5).iat[0, 0])
st.markdown("""
<html>
<head>
<style>
::-webkit-scrollbar {
width: 12px;
}
/* Track */
::-webkit-scrollbar-track {
background: #cfd1d1;
}
/* Handle */
::-webkit-scrollbar-thumb {
background:#696b6b;
}
/* Handle on hover */
::-webkit-scrollbar-thumb:hover {
background: #0a0a0a;
}
</style>
</head>
<body>
</body>
</html>
""", unsafe_allow_html=True)
st.sidebar.markdown("### Number of tweets by sentiment")
select = st.sidebar.selectbox('Visualization type', ['Bar plot', 'Pie chart'], key='1')
sentiment_count = data['airline_sentiment'].value_counts()
sentiment_count = pd.DataFrame({'Sentiment':sentiment_count.index, 'Tweets':sentiment_count.values})
if not st.sidebar.checkbox("Hide", True):
st.markdown("### Number of tweets by sentiment")
if select == 'Bar plot':
fig = px.bar(sentiment_count, x='Sentiment', y='Tweets', color='Tweets', height=500)
st.plotly_chart(fig)
else:
fig = px.pie(sentiment_count, values='Tweets', names='Sentiment')
st.plotly_chart(fig)
st.sidebar.subheader("When and where are users tweeting from?")
hour = st.sidebar.slider("Hour to look at", 0, 23)
modified_data = data[data['tweet_created'].dt.hour == hour]
if not st.sidebar.checkbox("Close", True, key='2'):
st.markdown("### Tweet locations based on time of day")
st.markdown("%i tweets between %i:00 and %i:00" % (len(modified_data), hour, (hour + 1) % 24))
st.map(modified_data)
if st.sidebar.checkbox("Show raw data", False):
st.write(modified_data)
st.sidebar.subheader("Total number of tweets for each airline")
each_airline = st.sidebar.selectbox('Visualization type', ['Bar plot', 'Pie chart'], key='3')
airline_sentiment_count = data.groupby('airline')['airline_sentiment'].count().sort_values(ascending=False)
airline_sentiment_count = pd.DataFrame({'Airline':airline_sentiment_count.index,
'Tweets':airline_sentiment_count.values.flatten()})
if not st.sidebar.checkbox("Close", True, key='4'):
if each_airline == 'Bar plot':
st.subheader("Total number of tweets for each airline")
fig_1 = px.bar(airline_sentiment_count, x='Airline', y='Tweets', color='Tweets', height=500)
st.plotly_chart(fig_1)
if each_airline == 'Pie chart':
st.subheader("Total number of tweets for each airline")
fig_2 = px.pie(airline_sentiment_count, values='Tweets', names='Airline')
st.plotly_chart(fig_2)
@st.cache_data(persist=True)
def plot_sentiment(airline):
df = data[data['airline']==airline]
count = df['airline_sentiment'].value_counts()
count = pd.DataFrame({'Sentiment':count.index, 'Tweets':count.values.flatten()})
return count
st.sidebar.subheader("Breakdown airline by sentiment")
choice = st.sidebar.multiselect('Pick airlines', ('American','Delta','Southwest','United','US Airways','Virgin America'))
if len(choice) > 0:
st.subheader("Breakdown airline by sentiment")
breakdown_type = st.sidebar.selectbox('Visualization type', ['Bar plot','Pie chart' ], key='5')
fig_3 = make_subplots(rows=1, cols=len(choice), subplot_titles=choice)
if breakdown_type == 'Bar plot':
for i in range(1):
for j in range(len(choice)):
fig_3.add_trace(
go.Bar(x=plot_sentiment(choice[j]).Sentiment, y=plot_sentiment(choice[j]).Tweets, showlegend=False),
row=i+1, col=j+1
)
fig_3.update_layout(height=600, width=800)
st.plotly_chart(fig_3)
else:
fig_3 = make_subplots(rows=1, cols=len(choice), specs=[[{'type':'domain'}]*len(choice)], subplot_titles=choice)
for i in range(1):
for j in range(len(choice)):
fig_3.add_trace(
go.Pie(labels=plot_sentiment(choice[j]).Sentiment, values=plot_sentiment(choice[j]).Tweets, showlegend=True),
i+1, j+1
)
fig_3.update_layout(height=600, width=800)
st.plotly_chart(fig_3)
st.set_option('deprecation.showPyplotGlobalUse', False)
# fig, ax = plt.subplots()
# ax.scatter([1, 2, 3], [1, 2, 3])
# st.pyplot(fig)
# non-depreacted example
st.sidebar.header("Word Cloud")
word_sentiment = st.sidebar.radio('Display word cloud for what sentiment?', ('positive', 'neutral', 'negative'))
if not st.sidebar.checkbox("Close", True, key='6'):
st.subheader('Word cloud for %s sentiment' % (word_sentiment))
df = data[data['airline_sentiment']==word_sentiment]
words = ' '.join(df['text'])
processed_words = ' '.join([word for word in words.split() if 'http' not in word and not word.startswith('@') and word != 'RT'])
wordcloud = WordCloud(stopwords=STOPWORDS, background_color='white', width=800, height=640).generate(processed_words)
plt.imshow(wordcloud)
plt.xticks([])
plt.yticks([])
st.pyplot()