/
TweetsQuery.py
252 lines (212 loc) · 6.38 KB
/
TweetsQuery.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# _*_ coding: utf-8 _*_
import sys
import time
import random
from pymongo import MongoClient
from nltk.tokenize import TweetTokenizer
from progressbar import AnimatedMarker, Bar, BouncingBar, Counter, ETA, \
AdaptiveETA, FileTransferSpeed, FormatLabel, Percentage, \
ProgressBar, ReverseBar, RotatingMarker, \
SimpleProgress, Timer
class TweetsQuery:
"""docstring for TweetsQuery"""
_collection = MongoClient()
_querystr = ""
_ip = ""
_port = 0
_db = ""
_text = []
_queryCount = 0
_tagList = []
# def __init__(self, arg):
# super(TweetsQuery, self).__init__()
# self.arg = arg
def initDB(self, ip = 'localhost', port = 27017, db = 'twitter', collec = 'geo_tweets'):
self._ip = ip
self._port = port
self._db = db
client = MongoClient(self._ip, self._port)
self._collection = client[self._db][collec]
def tweetsCount(self):
return self._collection.count()
def setQuery(self, query = {'lang':'en'}):
self._querystr = query
def getQuery(self):
return self._querystr
def setQueryCount(self, num):
self._queryCount = num
def getQueryCount(self):
return self._queryCount
def setTagList(self, tags): # the tags in the list must contain "#"
self._tagList = tags
def getTagList(self):
return self._tagList
# get text tweets list.
def getText(self):
return self._text
def getTagTweets(self):
if len(self._tagList) <= 0:
print "No tags provided! Please provide tag(s) first!"
return
self.getTweets()
def getTweets(self):
if self._queryCount <= 0:
pbar = ProgressBar(widgets=[Percentage(), Bar()], maxval = self._collection.count()).start()
else:
pbar = ProgressBar(widgets=[Percentage(), Bar()], maxval = self._queryCount).start()
print "Begin to collect tweets..."
index = 0
self._text = []
if len(self._tagList) <= 0:
if self._queryCount > 0:
for tweet in self._collection.find(self._querystr):
try:
text = tweet['text']
self._text.append(str(text))
# time.sleep(0.01)
pbar.update(index+1)
index += 1
if index >= self._queryCount:
break
except Exception, e:
continue
else:
pass
pbar.finish()
else:
for tweet in self._collection.find(self._querystr):
try:
text = tweet['text']
self._text.append(text)
pbar.update(index+1)
index += 1
except Exception, e:
continue
else:
pass
# Get tagged tweets
else:
tknzr = TweetTokenizer()
if self._queryCount > 0:
for tweet in self._collection.find(self._querystr):
try:
text = tweet['text']
tokens = set(tknzr.tokenize(text))
if len(tokens & set(self._tagList)) > 0: # If the tweet contains any tag that listed in the list
self._text.append(str(text))
pbar.update(index+1)
index += 1
if index >= self._queryCount:
break
except Exception, e:
continue
else:
pass
pbar.finish()
else:
for tweet in self._collection.find(self._querystr):
try:
text = tweet['text']
tokens = set(tknzr.tokenize(text))
if len(tokens & set(self._tagList)) > 0: # If the tweet contains any tag that listed in the list
self._text.append(str(text))
pbar.update(index+1)
index += 1
except Exception, e:
continue
else:
pass
print "Finishing collecting tweets!"
print str(len(self._text)) + ' tweets have been queried.'
# Get all the ids of the collection and then randomly to get tweets
def getRandomTweets(self):
if self._queryCount <= 0:
print "ERROR: Randomly quering tweets must specify the query count!"
return
# get all items' id
print "Begin to collect all ids of tweets..."
id_list = []
index = 0
pbar = ProgressBar(widgets=[Percentage(), Bar()], maxval = self._collection.count()).start()
for tweet in self._collection.find(self._querystr):
id_list.append(tweet['_id'])
pbar.update(index+1)
index += 1
pbar.finish()
print "Finishing collecting " + str(len(id_list)) + " ids of tweets."
print "Sampling the ids and get randomly selected tweets..."
randomNums = random.sample(xrange(len(id_list)), self._queryCount)
index = 0
self._text = []
pbar = ProgressBar(widgets=[Percentage(), Bar()], maxval = len(randomNums)).start()
if len(self._tagList) <= 0:
for i in randomNums:
try:
query = {'_id' : id_list[i]}
tweet = self._collection.find(query)
text = tweet[0]['text']
text = text.encode('utf-8')
self._text.append(text)
pbar.update(index+1)
index += 1
except Exception, e:
print e
else:
pass
pbar.finish()
else:
tknzr = TweetTokenizer()
for i in randomNums:
try:
query = {'_id' : id_list[i]}
tweet = self._collection.find(query)
text = tweet[0]['text']
text = text.encode('utf-8')
tokens = set(tknzr.tokenize(text))
if len(tokens & set(self._tagList)) > 0: # If the tweet contains any tag that listed in the list
self._text.append(str(text))
pbar.update(index+1)
index += 1
except Exception, e:
continue
else:
pass
pbar.finish()
print "Finishing collecting tweets!"
print str(len(self._text)) + ' tweets have been randomly queried.'
def writeTweets2file(self, filePath):
if len(filePath) <= 0:
print "No tweets are collected!"
return
tweet_list = self.getText()
pbar = ProgressBar(widgets=[Percentage(), Bar()], maxval = len(tweet_list)).start()
print "Begin to write tweets into file..."
index = 0
filewrite = open(filePath, 'w')
for tweet in tweet_list:
text = tweet.replace('\n', ' ') # 替換掉所有換行符
text = text.replace(';', '.') # 所有分號替換成句號
text = text.replace('\t', ' ') # 替換掉所有tab
filewrite.write(text)
filewrite.write('\t' + 'surprise' + '\t' + 'sadness') # 每行後面添加一個制表符,為了符合JAVA的simpleClassifier代碼
filewrite.write('\r\n')
pbar.update(index+1)
index += 1
pbar.finish()
filewrite.close()
print "Finishing writing tweets!"
print str(len(self._text)) + ' tweets have been written into the file: ' + filePath
'''
以下代碼用完需要及時註釋掉
'''
# query = {'lang':'en'}
# crawler = TweetsQuery()
# crawler.initDB()
# crawler.setQuery(query)
# crawler.setQueryCount(30000)
# crawler.getTweets()
# crawler.writeTweets2file("/home/pan/Dropbox/UserTweets")
# tweet_list = crawler.getText()
'''
以上代碼用完需要及時註釋掉
'''