-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter_sentimate.py
84 lines (69 loc) · 2.61 KB
/
twitter_sentimate.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
# Tweeter Sentimant Analysis..
import tweepy
import re
import pickle
from tweepy import OAuthHandler
# Initializing the key
consumer_key = 'PNoNsQgYYhZ5PVRkworfYXXX'
consumer_secret ='QvXgCNEQMz3HKdIuhpL8bj1FexyBrmHQBWz2osXXXXXXX'
access_token= '2919754765-xMs4E3FG4ABwrNsisbQmknoGxxxxxxx'
access_secret='dFiOFGVOj4rLzOKqTJY1yc7jDAUq7KNNYXXXXXXX'
auth = OAuthHandler(consumer_key , consumer_secret)
auth.set_access_token(access_token, access_secret)
args = ['india']
api = tweepy.API(auth, timeout= 10)
list_tweets = []
query = args[0]
if len(args) == 1:
for status in tweepy.Cursor(api.search, q=query+" -filter:retweets", lang='en',result_type='recent').items(100):
list_tweets.append(status.text)
with open('tfidfmodel.pickle', 'rb') as f:
vectorizer = pickle.load(f)
with open('classifier.pickle', 'rb') as f:
clf = pickle.load(f)
total_pos = 0
total_neg = 0
for tweet in list_tweets:
tweet = re.sub(r"^https://t.co/[a-zA-Z0-9]*\s"," ",tweet)
tweet = re.sub(r"\s+https://t.co/[a-zA-Z0-9]*\s"," ",tweet)
tweet = re.sub(r"\s+https://t.co/[a-zA-Z0-9]*$"," ",tweet)
tweet = tweet.lower()
tweet = re.sub(r"that's","that is", tweet)
tweet = re.sub(r"there's","there is", tweet)
tweet = re.sub(r"what's","what is", tweet)
tweet = re.sub(r"where's","where is", tweet)
tweet = re.sub(r"it's","it is", tweet)
tweet = re.sub(r"who's","who is", tweet)
tweet = re.sub(r"i'm","i am", tweet)
tweet = re.sub(r"she's","she is", tweet)
tweet = re.sub(r"he's","he is", tweet)
tweet = re.sub(r"they're", "they are", tweet)
tweet = re.sub(r"who're","who are", tweet)
tweet = re.sub(r"ain't","am not", tweet)
tweet = re.sub(r"wouldn't","would not", tweet)
tweet = re.sub(r"shouldn't","should not", tweet)
tweet = re.sub(r"can't","can not", tweet)
tweet = re.sub(r"couldn't","could not", tweet)
tweet = re.sub(r"won't","won not", tweet)
tweet = re.sub(r"\W"," ", tweet)
tweet = re.sub(r"\d"," ",tweet)
tweet = re.sub(r'\s+[a-z]\s+',' ',tweet)
tweet = re.sub(r'^[a-z]\s+',' ', tweet)
tweet = re.sub(r'\s+[a-z]$',' ',tweet)
tweet = re.sub(r"\s+"," ", tweet)
sent = clf.predict(vectorizer.transform([tweet]).toarray())
#print(tweet," : ", sent)
if sent[0] == 1:
total_pos +=1
else:
total_neg +=1
# Ploating the bar chart..
import matplotlib.pyplot as plt
import numpy as np
objects = ['Positive', 'Negative']
y_pos = np.arange(len(objects))
plt.bar(y_pos,[total_pos, total_neg], alpha =0.5)
plt.xticks(y_pos,objects)
plt.ylabel('Number')
plt.title('Number of Positive and Negative Tweets')
plt.show()