├── README.md ├── Tweets.csv └── app.py /README.md: -------------------------------------------------------------------------------- 1 | # Twitter-sentiment-Streamlit-dashboard 2 | This is a guided cousera project in which we build a twitter sentiment dashbord using streamlit. 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | import streamlit as st 2 | import pandas as pd 3 | import numpy as np 4 | import plotly.express as px 5 | from wordcloud import WordCloud, STOPWORDS 6 | import matplotlib.pyplot as plt 7 | 8 | 9 | st.title("Sentiment analysis of Tweets about US Airlines") 10 | st.sidebar.title("Sentiment analysis of Tweets about US Airlines") 11 | 12 | st.markdown("This application is a Streamlit dashboard to analyze the sentiment of Tweets🐦") 13 | st.sidebar.markdown("This application is a Streamlit dashboard to analyze the sentiment of Tweets🐦") 14 | 15 | PATH = ("/home/rhyme/Desktop/Project/Tweets.csv") 16 | 17 | @st.cache(persist=True) 18 | def load_data(): 19 | data = pd.read_csv(PATH) 20 | data['tweet_created'] = pd.to_datetime(data['tweet_created']) 21 | return data 22 | 23 | data = load_data() 24 | 25 | #st.write(data) 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'], key='1') 32 | sentiment_count = data['airline_sentiment'].value_counts() 33 | #st.write(sentiment_count) 34 | sentiment_count = pd.DataFrame({'Sentiment':sentiment_count.index, 'Tweets':sentiment_count.values}) 35 | 36 | if st.sidebar.checkbox("Hide", True): 37 | st.markdown("### Number of tweets by sentiment") 38 | if select == "Histogram": 39 | fig = px.bar(sentiment_count,x='Sentiment', y='Tweets', color='Tweets', height=500) 40 | st.plotly_chart(fig) 41 | else: 42 | fig = px.pie(sentiment_count, values='Tweets', names='Sentiment') 43 | st.plotly_chart(fig) 44 | 45 | #st.map(data) 46 | 47 | st.sidebar.subheader("When and where are users tweeting ?") 48 | hour = st.sidebar.slider("Hour of the day", 0 ,23)#alternatively:hour = st.sidebar.number_input("Hour of the day", min_value=1 ,max_value=24) 49 | modified_data = data[data['tweet_created'].dt.hour == hour] 50 | if not st.sidebar.checkbox("Close", True, key='1'): 51 | st.markdown("##Tweets locations based on the time of day") 52 | st.markdown("%i tweets %i:00 and %i:00" %(len(modified_data), hour, (hour+1)%24)) 53 | st.map(modified_data) 54 | if st.sidebar.checkbox("Show raw data",False): 55 | st.write(modified_data) 56 | 57 | st.sidebar.subheader("Breakdown Airline tweets by sentiment") 58 | choice = st.sidebar.multiselect('Pick airlines', ('US airway', 'United', 'American', 'Southwest', 'Delta', 'Virgin America'),key='0') 59 | 60 | if len(choice) > 0: 61 | choice_data = data[data.airline.isin(choice)] 62 | fig_choice = px.histogram(choice_data, x='airline', y='airline_sentiment',histfunc='count', color='airline_sentiment', 63 | facet_col='airline_sentiment', labels={'airline_sentiment':'tweets'}, height=600, width=800) 64 | st.plotly_chart(fig_choice) 65 | 66 | st.sidebar.header("Word Cloud") 67 | word_sentiment = st.sidebar.radio('Which sentiment type would you like to display a word cloud?', ('positive', 'neutral', 'negative')) 68 | 69 | if not st.sidebar.checkbox("Close", True, key='3'): 70 | st.subheader('Word cloud for %s sentiment' %(word_sentiment)) 71 | df = data[data['airline_sentiment']==word_sentiment] 72 | words = ' '.join(df['text']) 73 | processed_words = ' '.join([word for word in words.split() if 'http' not in word and not word.startswith('@') and word != 'RT']) 74 | wordcloud = WordCloud(stopwords=STOPWORDS, background_color='white', height=640, width=800).generate(processed_words) 75 | plt.imshow(wordcloud) 76 | plt.xticks([]) 77 | plt.yticks([]) 78 | st.pyplot() 79 | --------------------------------------------------------------------------------