-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestPie.py
99 lines (75 loc) · 2.68 KB
/
testPie.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
87
88
89
90
91
92
93
94
95
96
97
98
99
import sys
import csv
import tweepy
import matplotlib.pyplot as plt
from collections import Counter
from aylienapiclient import textapi
if sys.version_info[0] < 3:
input = raw_input
consumer_key = 'Kv72JhMpzpmjTSxmodudSss5X'
consumer_secret = '5a0Bf57RTzYiVDI97iT8OGTve4ZD2bOZOakvaV9xcYYDlZPN3o'
access_token = '519080160-GnrzCFVJZhDSexK8TuT5gvNjBOvxQqcYwKHDp8zV'
access_token_secret = 'f00oJG9QhJetKc7EGvHVleQNubdxos4GsFKolpBgGBswY'
application_id = '20874377'
application_key = 'b56699543d175da217273f217dfc453c'
## set up an instance of Tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
## set up an instance of the AYLIEN Text API
client = textapi.Client(application_id, application_key)
## search Twitter for something that interests you
query = input("What subject do you want to analyze for this example? \n")
number = input("How many Tweets do you want to analyze? \n")
results = api.search(
lang="en",
q=query + " -rt",
count=number,
result_type="recent"
)
print("--- Gathered Tweets \n")
## open a csv file to store the Tweets and their sentiment
file_name = 'Sentiment_Analysis_of_{}_Tweets_About_{}.csv'.format(
number, query)
with open(file_name, 'w', newline='') as csvfile:
csv_writer = csv.DictWriter(
f=csvfile,
fieldnames=["Tweet", "Sentiment"]
)
csv_writer.writeheader()
print("--- Opened a CSV file to store the results of your sentiment analysis... \n")
## tidy up the Tweets and send each to the AYLIEN Text API
for c, result in enumerate(results, start=1):
tweet = result.text
tidy_tweet = tweet.strip().encode('ascii', 'ignore')
if len(tweet) == 0:
print('Empty Tweet')
continue
response = client.Sentiment({'text': tidy_tweet})
csv_writer.writerow({
'Tweet': response['text'],
'Sentiment': response['polarity']
})
print("Analyzed Tweet {}".format(c))
## count the data in the Sentiment column of the CSV file
with open(file_name, 'r') as data:
counter = Counter()
for row in csv.DictReader(data):
counter[row['Sentiment']] += 1
positive = counter['positive']
negative = counter['negative']
neutral = counter['neutral']
## declare the variables for the pie chart, using the Counter variables for "sizes"
colors = ['green', 'red', 'grey']
sizes = [positive, negative, neutral]
labels = 'Positive', 'Negative', 'Neutral'
## use matplotlib to plot the chart
plt.pie(
x=sizes,
shadow=True,
colors=colors,
labels=labels,
startangle=90
)
plt.title("Sentiment of {} Tweets about {}".format(number, query))
plt.show()