-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
247 lines (194 loc) · 9.68 KB
/
app.py
File metadata and controls
247 lines (194 loc) · 9.68 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import streamlit as st
import warnings
warnings.filterwarnings("ignore")
# EDA Pkgs
import pandas as pd
import numpy as np
import pandas as pd
import tweepy
import json
from tweepy import OAuthHandler
import re
import os
import textblob
from textblob import TextBlob
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
import openpyxl
import time
import tqdm
#To Hide Warnings
st.set_option('deprecation.showfileUploaderEncoding', False)
st.set_option('deprecation.showPyplotGlobalUse', False)
# Viz Pkgs
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('Agg')
import seaborn as sns
#sns.set_style('darkgrid')
STYLE = """
<style>
img {
max-width: 100%;
}
</style> """
def main():
""" Common ML Dataset Explorer """
st.title("SENTIMENTO")
#st.subheader("Select a topic which you'd like to get the sentiment analysis on :")
html_temp = """
<div style="background-color:tomato;"><p style="color:white;font-size:40px;padding:9px">Live twitter Sentiment analysis</p></div>
"""
st.markdown(html_temp, unsafe_allow_html=True)
st.subheader("Select a topic which you'd like to get the sentiment analysis on :")
################# Twitter API Connection #######################
# Tokens
consumer_key = os.getenv('consumer_key')
consumer_secret = os.getenv('consumer_secret')
access_token = os.getenv('access_token')
access_token_secret = os.getenv('access_token_secret')
# Use the above credentials to authenticate the API.
auth = tweepy.OAuthHandler( consumer_key , consumer_secret )
auth.set_access_token( access_token , access_token_secret )
api = tweepy.API(auth)
################################################################
df = pd.DataFrame(columns=["Date","User","IsVerified","Tweet","Likes","RT",'User_location'])
# Write a Function to extract tweets:
def get_tweets(Topic,Count):
i=0
#my_bar = st.progress(100) # To track progress of Extracted tweets
for tweet in tweepy.Cursor(api.search, q=Topic,count=100, lang="en",exclude='retweets').items():
#time.sleep(0.1)
#my_bar.progress(i)
df.loc[i,"Date"] = tweet.created_at
df.loc[i,"User"] = tweet.user.name
df.loc[i,"IsVerified"] = tweet.user.verified
df.loc[i,"Tweet"] = tweet.text
df.loc[i,"Likes"] = tweet.favorite_count
df.loc[i,"RT"] = tweet.retweet_count
df.loc[i,"User_location"] = tweet.user.location
#df.to_csv("TweetDataset.csv",index=False)
#df.to_excel('{}.xlsx'.format("TweetDataset"),index=False) ## Save as Excel
i=i+1
if i>Count:
break
else:
pass
# Function to Clean the Tweet.
def clean_tweet(tweet):
return ' '.join(re.sub(r'(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)|([RT])', ' ', tweet.lower()).split())
# Funciton to analyze Sentiment
def analyze_sentiment(tweet):
analysis = TextBlob(tweet)
if analysis.sentiment.polarity > 0:
return 'Positive'
elif analysis.sentiment.polarity == 0:
return 'Neutral'
else:
return 'Negative'
#Function to Pre-process data for Worlcloud
def prepCloud(Topic_text,Topic):
Topic = str(Topic).lower()
Topic=' '.join(re.sub('([^0-9A-Za-z \t])', ' ', Topic).split())
Topic = re.split(r"\s+",str(Topic))
stopwords = set(STOPWORDS)
stopwords.update(Topic) ### Add our topic in Stopwords, so it doesnt appear in wordClous
###
text_new = " ".join([txt for txt in Topic_text.split() if txt not in stopwords])
return text_new
from PIL import Image
image = Image.open('Logo2.jpg')
st.image(image, caption='Twitter for Analytics',use_column_width=True)
# Collect Input from user :
Topic = str()
Topic = str(st.text_input("Analyze a trend!"))
if len(Topic) > 0 :
# Call the function to extract the data. pass the topic and filename you want the data to be stored in.
with st.spinner("Please wait, Tweets are being extracted"):
get_tweets(Topic , Count=200)
st.success('Tweets have been Extracted !!!!')
# Call function to get Clean tweets
df['clean_tweet'] = df['Tweet'].apply(lambda x : clean_tweet(x))
# Call function to get the Sentiments
df["Sentiment"] = df["Tweet"].apply(lambda x : analyze_sentiment(x))
# Write Summary of the Tweets
st.write("Total Tweets Extracted for Topic '{}' are : {}".format(Topic,len(df.Tweet)))
st.write("Total Positive Tweets are : {}".format(len(df[df["Sentiment"]=="Positive"])))
st.write("Total Negative Tweets are : {}".format(len(df[df["Sentiment"]=="Negative"])))
st.write("Total Neutral Tweets are : {}".format(len(df[df["Sentiment"]=="Neutral"])))
# See the Extracted Data :
if st.button("See the Extracted Data"):
#st.markdown(html_temp, unsafe_allow_html=True)
st.success("Below is the Extracted Data :")
st.write(df.head(50))
# get the countPlot
if st.button("Get Count Plot for Different Sentiments"):
st.success("Generating A Count Plot")
st.subheader(" Count Plot for Different Sentiments")
st.write(sns.countplot(df["Sentiment"]))
st.pyplot()
# Piechart
if st.button("Get Pie Chart for Different Sentiments"):
st.success("Generating A Pie Chart")
a=len(df[df["Sentiment"]=="Positive"])
b=len(df[df["Sentiment"]=="Negative"])
c=len(df[df["Sentiment"]=="Neutral"])
d=np.array([a,b,c])
explode = (0.1, 0.0, 0.1)
st.write(plt.pie(d,shadow=True,explode=explode,labels=["Positive","Negative","Neutral"],autopct='%1.2f%%'))
st.pyplot()
# get the countPlot Based on Verified and unverified Users
if st.button("Get Count Plot Based on Verified and unverified Users"):
st.success("Generating A Count Plot (Verified and unverified Users)")
st.subheader(" Count Plot for Different Sentiments for Verified and unverified Users")
st.write(sns.countplot(df["Sentiment"],hue=df.IsVerified))
st.pyplot()
## Points to add 1. Make Backgroud Clear for Wordcloud 2. Remove keywords from Wordcloud
# Create a Worlcloud
if st.button("Get WordCloud for all things said about {}".format(Topic)):
st.success("Generating A WordCloud for all things said about {}".format(Topic))
text = " ".join(review for review in df.clean_tweet)
stopwords = set(STOPWORDS)
text_newALL = prepCloud(text,Topic)
wordcloud = WordCloud(stopwords=stopwords,max_words=800,max_font_size=70).generate(text_newALL)
st.write(plt.imshow(wordcloud, interpolation='bilinear'))
plt.axis("off")
st.pyplot()
#Wordcloud for Positive tweets only
if st.button("Get WordCloud for all Positive Tweets about {}".format(Topic)):
st.success("Generating A WordCloud for all Positive Tweets about {}".format(Topic))
text_positive = " ".join(review for review in df[df["Sentiment"]=="Positive"].clean_tweet)
stopwords = set(STOPWORDS)
text_new_positive = prepCloud(text_positive,Topic)
#text_positive=" ".join([word for word in text_positive.split() if word not in stopwords])
wordcloud = WordCloud(stopwords=stopwords,max_words=800,max_font_size=70).generate(text_new_positive)
st.write(plt.imshow(wordcloud, interpolation='bilinear'))
plt.axis("off")
st.pyplot()
#Wordcloud for Negative tweets only
if st.button("Get WordCloud for all Negative Tweets about {}".format(Topic)):
st.success("Generating A WordCloud for all Positive Tweets about {}".format(Topic))
text_negative = " ".join(review for review in df[df["Sentiment"]=="Negative"].clean_tweet)
stopwords = set(STOPWORDS)
text_new_negative = prepCloud(text_negative,Topic)
#text_negative=" ".join([word for word in text_negative.split() if word not in stopwords])
wordcloud = WordCloud(stopwords=stopwords,max_words=800,max_font_size=70).generate(text_new_negative)
st.write(plt.imshow(wordcloud, interpolation='bilinear'))
plt.axis("off")
st.pyplot()
st.sidebar.header("About App")
st.sidebar.info("A Twitter Sentiment analysis Project which will scrap twitter for the topic selected by the user. The extracted tweets will then be used to determine the Sentiments of those tweets. \
The different Visualizations will help us get a feel of the overall mood of the people on Twitter regarding the topic we select.")
st.sidebar.text("Built with Streamlit")
st.sidebar.header("For Any Queries/Suggestions Please reach out at :")
st.sidebar.info("subhadeeppaul35@gmail.com")
st.sidebar.text("Project Mentor:")
st.sidebar.info("Mr. Saifuddin Ahmed")
#st.sidebar.subheader("Scatter-plot setup")
#box1 = st.sidebar.selectbox(label= "X axis", options = numeric_columns)
#box2 = st.sidebar.selectbox(label="Y axis", options=numeric_columns)
#sns.jointplot(x=box1, y= box2, data=df, kind = "reg", color= "red")
#st.pyplot()
if st.button("Exit"):
st.balloons()
if __name__ == '__main__':
main()