-
Notifications
You must be signed in to change notification settings - Fork 2
/
twitter_tweet_analysis.py
117 lines (83 loc) · 3.83 KB
/
twitter_tweet_analysis.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from TwitterSearch import * #import twittersearch module
tso = TwitterSearchOrder()
ckey = "your customer key"
csecret = "your customer key secret"
atoken = "your access token"
atoksec = "your access token secret"
import sqlite3 as sql #import sqlite module
con = sql.connect('word.db') #connects to database containing words
cur = con.cursor()
def search(keyword):
try:
i = 1
pos_score = 0
neg_score = 0
neutral_count = 0
pos_count = 0
neg_count = 0
pos_rand_var = 0
neg_rand_var = 0
total_pos_score = 0
total_neg_score = 0
""" set TwitterSearch parameters"""
tso.set_keywords([keyword])
tso.set_language('en')
tso.set_include_entities(False)
ts = TwitterSearch(ckey,csecret,atoken,atoksec) #connects to Twitter API
for tweet in ts.search_tweets_iterable(tso):
statement = tweet['text'] #fetches and stores one tweet
for word in statement.split():
try:
con = sql.connect('word.db') #connects to database containing words
cur = con.cursor()
pos_word = cur.execute("SELECT positive_word from Word where positive_word = ?", (word.lower(),))
if pos_word.fetchone()[0] == word: #if word is in database
pos_score +=1
else:
pos_rand_var = 0
con.close()
total_pos_score = pos_rand_var + pos_score
except TypeError: #continue if error
continue
for word in statement.split():
try:
con = sql.connect('word.db') #connects to database containing words
cur = con.cursor()
neg_word = cur.execute("SELECT negative_word from Word where negative_word = ?", (word.lower(),))
if neg_word.fetchone()[0] == word:
neg_score -=1
else:
neg_rand_var = 0
con.close()
total_neg_score = neg_rand_var + neg_score
except TypeError:
continue
if total_pos_score == 0 and total_neg_score == 0:
neutral_count +=1
print statement, "\n------>Statement is neutral."
elif total_pos_score>=1 and (total_neg_score==0 or total_neg_score ==-1) :
pos_count +=1
print statement, "\n------>Statement is positive."
elif total_pos_score >=0 and (total_neg_score==-2 or total_neg_score ==-3 or total_neg_score<=-5):
neg_count+=1
print statement, "\n------>Statement is negative."
else:
neutral_count +=1
print statement, "\n------>Statement is neutral."
i+=1
if i == 11: #enter number of tweets you want
break
total_tweet = neutral_count+pos_count+neg_count
print neutral_count, pos_count, neg_count, total_tweet #prints number of positive, negative, neutral, tweets
pos = arange(3)+1
barh(pos, ( neutral_count, pos_count, neg_count), align = "center", color = '#0099ff')
yticks(pos,("Neutral Tweets", "Positive Tweets", "Negative Tweets"))
xlabel("No. of tweets")
ylabel(keyword.title())
title("Sentiment Analysis")
grid(True)
savefig(keyword.title())
except TwitterSearchException as e:
print "Not connected to internet or error"
search("interstellar")
#search('put your keyword here') #enter keyword