Commit
·
0292810
1
Parent(s):
12d151f
Upload app1.py
Browse files
app1.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from altair.vegalite.v4.schema.core import Header
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import plotly.express as px
|
| 6 |
+
from wordcloud import WordCloud, STOPWORDS
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
|
| 9 |
+
st.set_option('deprecation.showPyplotGlobalUse', False)
|
| 10 |
+
|
| 11 |
+
DATA_ = pd.read_csv("Tweets.csv")
|
| 12 |
+
st.title("Sentiment Analysis of Tweets about US Airlines")
|
| 13 |
+
st.sidebar.title("Sentiment Analysis of Tweets about US Airlines")
|
| 14 |
+
st.markdown("This application is a streamlit dashboard to analyze the sentiment of Tweets")
|
| 15 |
+
st.sidebar.markdown("This application is a streamlit dashboard to analyze the sentiment of Tweets")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def run():
|
| 19 |
+
|
| 20 |
+
@st.cache(persist=True)
|
| 21 |
+
def load_data():
|
| 22 |
+
DATA_['tweet_created'] = pd.to_datetime(DATA_['tweet_created'])
|
| 23 |
+
return DATA_
|
| 24 |
+
data = load_data()
|
| 25 |
+
|
| 26 |
+
st.sidebar.subheader("Show random tweet")
|
| 27 |
+
random_tweet = st.sidebar.radio('Sentiment', ('positive', 'neutral', 'negative'))
|
| 28 |
+
st.sidebar.markdown(data.query('airline_sentiment == @random_tweet')[["text"]].sample(n=1).iat[0,0])
|
| 29 |
+
|
| 30 |
+
st.sidebar.markdown("### Number of tweets by sentiment")
|
| 31 |
+
select = st.sidebar.selectbox('Visualization type', ['Histogram', 'Pie chart'])
|
| 32 |
+
sentiment_count = data['airline_sentiment'].value_counts()
|
| 33 |
+
sentiment_count = pd.DataFrame({'Sentiment':sentiment_count.index, 'Tweets':sentiment_count.values})
|
| 34 |
+
|
| 35 |
+
if not st.sidebar.checkbox("Hide", True):
|
| 36 |
+
st.markdown("### Number of tweets by sentiment")
|
| 37 |
+
if select == "Histogram":
|
| 38 |
+
fig = px.bar(sentiment_count, x='Sentiment', y='Tweets', color='Tweets', height=500)
|
| 39 |
+
st.plotly_chart(fig)
|
| 40 |
+
else:
|
| 41 |
+
fig = px.pie(sentiment_count, values='Tweets', names='Sentiment')
|
| 42 |
+
st.plotly_chart(fig)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
st.sidebar.subheader("When and Where are users tweeting from?")
|
| 46 |
+
hour = st.sidebar.slider("Hour of day", 0,23)
|
| 47 |
+
modified_data = data[data['tweet_created'].dt.hour == hour]
|
| 48 |
+
if not st.sidebar.checkbox("Close", True, key='1'):
|
| 49 |
+
st.markdown("### Tweets locations based on the time of date")
|
| 50 |
+
st.markdown("%i tweets between %i:00 and %i:00" % (len(modified_data), hour, (hour+1)%24))
|
| 51 |
+
st.map(modified_data)
|
| 52 |
+
if st.sidebar.checkbox("Show Raw Data", False):
|
| 53 |
+
st.write(modified_data)
|
| 54 |
+
st.sidebar.subheader("Breakdown airline tweets by sentiment")
|
| 55 |
+
choice = st.sidebar.multiselect('Pick airline', ('US Airways', 'United', 'American', 'Southwest', 'Delta', 'Virgin America'), key='0')
|
| 56 |
+
|
| 57 |
+
if len(choice) > 0:
|
| 58 |
+
choice_data = data[data.airline.isin(choice)]
|
| 59 |
+
fig_choice = px.histogram(choice_data, x='airline',
|
| 60 |
+
y='airline_sentiment',
|
| 61 |
+
histfunc = 'count', color = 'airline_sentiment',
|
| 62 |
+
facet_col='airline_sentiment',
|
| 63 |
+
labels={'airline_sentiment':'tweets'}, height=600, width=800)
|
| 64 |
+
st.plotly_chart(fig_choice)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
st.sidebar.header("Word Cloud")
|
| 68 |
+
word_sentiment = st.sidebar.radio('Display word cloud for what sentiment?',('positive', 'neutral','negative'))
|
| 69 |
+
|
| 70 |
+
if not st.sidebar.checkbox("Close", True, key='3'):
|
| 71 |
+
st.header('Word cloud for %s sentiment' % (word_sentiment))
|
| 72 |
+
df = data[data['airline_sentiment']==word_sentiment]
|
| 73 |
+
words = ' '.join(df['text'])
|
| 74 |
+
processed_words = ' '.join([word for word in words.split() if 'http' not in word and not word.startswith('@') and word !='RT'])
|
| 75 |
+
wordcloud = WordCloud(stopwords=STOPWORDS,
|
| 76 |
+
background_color='white', height=640, width=800).generate(processed_words)
|
| 77 |
+
plt.imshow(wordcloud)
|
| 78 |
+
plt.xticks([])
|
| 79 |
+
plt.yticks([])
|
| 80 |
+
st.pyplot()
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
if __name__ == '__main__':
|
| 84 |
+
run()
|
| 85 |
+
|