Permalink
Switch branches/tags
Nothing to show
Find file Copy path
07c7756 Apr 17, 2018
Fidel-Willis Add files via upload
0 contributors

Users who have contributed to this file

87 lines (65 sloc) 2.07 KB
import tweepy
from Tkinter import *
from time import sleep
from datetime import datetime
from textblob import TextBlob
import matplotlib.pyplot as plt
#Authentication
consumer_key = 'consumer_key'
consumer_secret = 'consumer_secret'
access_token = 'access_token'
access_token_secret = 'access_token_secret'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
#GUI
root = Tk()
label1 = Label(root, text="Search")
E1 = Entry(root, bd =5)
label2 = Label(root, text="Sample Size")
E2 = Entry(root, bd =5)
def getE1():
return E1.get()
def getE2():
return E2.get()
def getData():
getE1()
keyword = getE1()
getE2()
numberOfTweets = getE2()
numberOfTweets = int(numberOfTweets)
#Where the tweets are stored to be plotted
polarity_list = []
numbers_list = []
number = 1
for tweet in tweepy.Cursor(api.search, keyword, lang="en").items(numberOfTweets):
try:
analysis = TextBlob(tweet.text)
analysis = analysis.sentiment
polarity = analysis.polarity
polarity_list.append(polarity)
numbers_list.append(number)
number = number + 1
except tweepy.TweepError as e:
print(e.reason)
except StopIteration:
break
#Plotting
axes = plt.gca()
axes.set_ylim([-1, 2])
plt.scatter(numbers_list, polarity_list)
averagePolarity = (sum(polarity_list))/(len(polarity_list))
averagePolarity = "{0:.0f}%".format(averagePolarity * 100)
time = datetime.now().strftime("At: %H:%M\nOn: %m-%d-%y")
plt.text(0, 1.25, "Average Sentiment: " + str(averagePolarity) + "\n" + time, fontsize=12, bbox = dict(facecolor='none', edgecolor='black', boxstyle='square, pad = 1'))
plt.title("Sentiment of " + keyword + " on Twitter")
plt.xlabel("Number of Tweets")
plt.ylabel("Sentiment")
plt.show()
submit = Button(root, text ="Submit", command = getData)
label1.pack()
E1.pack()
label2.pack()
E2.pack()
submit.pack(side =BOTTOM)
root.mainloop()