-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanUserTweet.py
45 lines (40 loc) · 1.36 KB
/
cleanUserTweet.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
import json
import langid
import sys
reload(sys)
sys.setdefaultencoding('utf8')
def clean(fileName):
brandList = []
listFile = open(fileName, 'r')
for line in listFile:
if not line.startswith('#'):
brandList.append(line.strip())
listFile.close()
for brand in brandList:
print(brand)
userTweets = {}
tweetFile = open('data/userTweets2/'+brand+'.json', 'r')
for line in tweetFile:
try:
data = json.loads(line.strip())
except:
continue
userID = data['user']
if userID not in userTweets:
userTweets[userID] = []
tweets = data['statuses']
for tweet in tweets:
if len(tweet['text']) > 5:
if langid.classify(tweet['text'])[0] == 'en':
userTweets[userID].append(tweet)
if len(userTweets[userID]) > 20:
break
tweetFile.close()
outputFile = open('data/userTweets2/clean2/' + brand + '.json', 'w')
for userID, tweets in userTweets.items():
if len(tweets) > 19:
output = {'user_id': userID, 'statuses': tweets}
outputFile.write(json.dumps(output)+'\n')
outputFile.close()
if __name__ == '__main__':
clean('lists/popularAccount4.list')