-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentiment_analyzer_edu.py
77 lines (67 loc) · 1.91 KB
/
sentiment_analyzer_edu.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
'''
Importing modules
'''
import tweepy
from textblob import TextBlob
'''
Declaring variables to store the keys from Twitter API
'''
consumer_key = '<your_key_here>'
consumer_key_secret = '<your_key_here>'
access_token = '<your_key_here>'
access_token_secret = '<your_key_here>'
'''
Creating the connection using the API
'''
auth = tweepy.OAuthHandler(consumer_key, consumer_key_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
'''
Searching for any term
See handling erros to add exception in case the users don't pass the correct argument.
'''
search_term = input('What do you wanna search for? ')
num_of_terms = int(input('How many terms? '))
people_tweets = tweepy.Cursor(api.search,
#since = "2019-11-01",
#until = "2019-11-14",
q = search_term,
lang = "en").items(num_of_terms)
positive = 0
negative = 0
neutral = 0
for tweet in people_tweets:
print(tweet.text)
analysis = TextBlob(tweet.text)
print(analysis.sentiment)
if analysis.sentiment[0] > 0.00:
print('Positive')
positive += 1
elif analysis.sentiment[0] < 0.00:
print('Negative')
negative += 1
else:
print('Neutral')
neutral += 1
print()
print('-=' * 3, 'TOTALS', '-=' * 3)
print('Total Positives:', positive)
print()
print('Total Negatives:', negative)
print()
print('Total Neutrals:', neutral)
print()
'''
Percentages
Formula: (part / whole) * 100
'''
print('-=' * 3, 'PERCENTAGE', '-=' * 3)
print(int((positive / num_of_terms) * 100), '% Positives')
print()
print(int((negative / num_of_terms) * 100), '% Negatives')
print()
print(int((neutral / num_of_terms) * 100), '% Neturals')
print()
'''
Create a function to return percentage charts here in the future.
'''