-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlast.py
86 lines (65 loc) · 2.18 KB
/
last.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
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 = 'Kv72JhMpzpmjTSxmodudSss5X'
consumer_secret = '5a0Bf57RTzYiVDI97iT8OGTve4ZD2bOZOakvaV9xcYYDlZPN3o'
access_token = '519080160-GnrzCFVJZhDSexK8TuT5gvNjBOvxQqcYwKHDp8zV'
access_token_secret = 'f00oJG9QhJetKc7EGvHVleQNubdxos4GsFKolpBgGBswY'
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()