forked from christian1741/Twitter-Sentiment-Analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StreamingTwitter.py
215 lines (167 loc) · 5.92 KB
/
StreamingTwitter.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import sys
import tweepy
import re
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
class CustomStreamListener(tweepy.StreamListener):
def __init__(self, maxnum, api=None):
self.n = maxnum
self.api = api
def on_status(self, status):
try:
tweets = []
name = status.author.screen_name
textTwitter = status.text
Tweet = textTwitter.lower()
Tweet = re.sub('((www\.[\s]+)|(https?://[^\s]+))','URL',Tweet)
Tweet = re.sub('@[^\s]+','TWITTER_USER',Tweet)
Tweet = re.sub('[\s]+', ' ', Tweet)
#Replace #word with word Handling hashtags
Tweet = re.sub(r'#([^\s]+)', r'\1', Tweet)
#trim
Tweet = Tweet.strip('\'"')
a = ':)'
b = ':('
Tweet = Tweet.replace(a,'')
Tweet = Tweet.replace(b,'')
tag = 'TWITTER_USER'
rt = 'rt'
Tweet = Tweet.replace(tag,'')
Tweet = Tweet.replace(rt,'')
final_tweet = '|positive|, |%s| ' % (Tweet)
f = open('finalpositive.csv', 'a+')
old = f.read()
f.seek(0,2)
print final_tweet
f.write(final_tweet+'\n')
f.close()
self.n = self.n+1
if self.n < 3000:
return True
else:
print 'maxnum = '+str(self.n)
return False
except Exception as e:
print (e)
def on_error(self, status_code):
return True # Don't kill the stream
def on_timeout(self):
return True
class CustomStreamListenerNEG(tweepy.StreamListener):
def __init__(self, maxnum, api=None):
self.n = maxnum
self.api = api
def on_status(self, status):
try:
tweets = []
name = status.author.screen_name
textTwitter = status.text
Tweet = textTwitter.lower()
Tweet = re.sub('((www\.[\s]+)|(https?://[^\s]+))','URL',Tweet)
Tweet = re.sub('@[^\s]+','TWITTER_USER',Tweet)
Tweet = re.sub('[\s]+', ' ', Tweet)
Tweet = re.sub(r'#([^\s]+)', r'\1', Tweet)
Tweet = Tweet.strip('\'"')
a = ':)'
b = ':('
Tweet = Tweet.replace(a,'')
Tweet = Tweet.replace(b,'')
tag = 'TWITTER_USER'
rt = 'rt'
Tweet = Tweet.replace(tag,'')
Tweet = Tweet.replace(rt,'')
final_tweet = '|negative|, |%s| ' % (Tweet)
print final_tweet
f = open('finalneg.csv', 'r+')
old = f.read()
f.seek(0, 2)
f.write(final_tweet + '\n')
f.close()
self.n = self.n+1
if self.n < 3000:
return True
else:
print 'maxnum = '+str(self.n)
return False
except Exception as e:
print (e)
def on_error(self, status_code):
return True # Don't kill the stream
def on_timeout(self):
return True # Don't kill the stream
#initialize stopWords
stopWords = []
#starting the function
def replaceTwoOrMore(s):
#look for 2 or more repetitions of character and replace with the character itself
pattern = re.compile(r"(.)\1{1,}", re.DOTALL)
return pattern.sub(r"\1\1", s)
#end
#starting the function
def getStopWordList(stopWordListFileName):
#read the stopwords file and build a list
stopWords = []
stopWords.append('TWITTER_USER')
stopWords.append('URL')
fp = open(stopWordListFileName, 'r')
line = fp.readline()
while line:
word = line.strip()
stopWords.append(word)
line = fp.readline()
fp.close()
return stopWords
#end
#starting the function
def getFeatureVector(tweet):
featureVector = []
#split tweet into words
words = tweet.split()
for w in words:
#replace two or more with two occurrences
w = replaceTwoOrMore(w)
#strip punctuation
w = w.strip('\'"?,.')
#check if the word stats with an alphabet
val = re.search(r"^[a-zA-Z][a-zA-Z0-9]*$", w)
#ignore if it is a stop word
if(w in stopWords or val is None):
continue
else:
featureVector.append(w.lower())
return featureVector
def main():
consumer_key="****"
consumer_secret="****"
access_token="****"
access_token_secret="****"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
print "Establishing stream...\n"
sapi = tweepy.streaming.Stream(auth, CustomStreamListener(maxnum=0))
setTerms = [':)']
sapi.filter(None, setTerms, languages=["en"])
def mainNEG():
consumer_key = 'TVeJNu6TEiURi2UAVMYIXs9Dp'
consumer_secret = 'uTkfTzT2k9gB33XYJPBCmgFqaGcBzjzrS0wZUbaxve7H8o4ec4'
access_token = '877381454-Mo0egDzpYMvnizoWkYZcRYeqdogA88MY9uwNbG6Z'
access_token_secret = 'P06bOHjI02MKybb2enEqKZRqEnsFnuH69sGOeDgnHIuJn'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
NegTweets = tweepy.streaming.Stream(auth, CustomStreamListenerNEG(maxnum=0))
setTermsNEG = [':(']
NegTweets.filter(None, setTermsNEG, languages=["en"])
def Positive():
fp = open('finalneg', 'r')
line = fp.readline()
st = open('StopWords.txt', 'r')
stopWords = getStopWordList('StopWords.txt')
while line:
featureVector = getFeatureVector(line)
line = fp.readline()
if __name__ == '__main__':
main()
mainNEG()
Positive()