-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProject-2_Twitter.py
More file actions
105 lines (80 loc) · 2.85 KB
/
Project-2_Twitter.py
File metadata and controls
105 lines (80 loc) · 2.85 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
100
101
102
103
104
105
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 14 21:13:17 2019
@author: Usman
"""
import tweepy
import re
import pickle
from tweepy import OAuthHandler
# initializing the keys
consumer_key=''
consumer_secret=''
access_token=''
access_secret = ''
#setting up the OAuth Handler
#this authenticates the twitter app
auth = OAuthHandler(consumer_key,consumer_secret)
#this authenticates the rights to fetch the tweets
auth.set_access_token(access_token, access_secret)
#this is the search keyword
args = ['facebook']
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:
vect = pickle.load(f)
with open('classifier.pickle','rb') as f:
clf = pickle.load(f)
#for plotting afterwards
sentpos = 0
sentneg = 0
#filtering/ Preprocessing the tweets
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"i'm","i am",tweet)
tweet = re.sub(r"who's","who is",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","are not",tweet)
tweet = re.sub(r"wouldn't","would not",tweet)
tweet = re.sub(r"shouldn't","should not",tweet)
tweet = re.sub(r"couldn't","could not",tweet)
tweet = re.sub(r"can't","can not",tweet)
tweet = re.sub(r"won't","will not",tweet)
tweet = re.sub(r"\W"," ",tweet)
tweet = re.sub(r"\d"," ",tweet)
tweet = re.sub(r"^[a-z]\s+"," ",tweet)
tweet = re.sub(r"\s+[a-z]\s+"," ",tweet)
tweet = re.sub(r"\s+[a-z]$"," ",tweet)
tweet = re.sub(r"\s+"," ",tweet)
sentiment = clf.predict(vect.transform(tweet).toarray())
#print(tweet,":",sentiment)
#adding total pos , neg tweets
if sentiment[0] == 1:
sentpos = sentpos + 1
else:
sentneg = sentneg + 1
#plotting
import matplotlib.pyplot as plt
import numpy as np
objects = ['Positive','Negative']
y_pos = np.arange(len(objects))
plt.bar(y_pos,[sentpos,sentneg],alpha = 0.5)
plt.xticks(y_pos,objects)
plt.ylabel('Number')
plt.title("Number of Positive & Negative Tweets")
plt.show()