-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsent.py
More file actions
99 lines (76 loc) · 2.84 KB
/
sent.py
File metadata and controls
99 lines (76 loc) · 2.84 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
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 = eval(input())
## Twitter credentials
consumer_key = "yl8HQmny2J6cp5ZjNde9sKpto"
consumer_secret = "ca4b6d0KhuwN11hvPcsrx97f6SGptyOXBClY0WTWf6cAnGsHgo"
access_token = "2170850031-j1ecgOcPLl5KmTWekb7ekTJE5HTazm34QKgHTn1"
access_token_secret = "CpxUPwTZhH7ihcGVRqwD05HJzveaSeXisA2v3uiI1Wldu"
## AYLIEN credentials
application_id = "1e102392"
application_key = "0f1b341ab3502755c8c0b0805f498292"
## 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()