-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile_tweets.py
72 lines (62 loc) · 2.68 KB
/
profile_tweets.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
import sys
import ast
###########################################################
# profile_tweets.py
#
# Purpose:
# count among tweets processed:
# (1) How many have not-none 'place' attribute at root level?
# (2) How many have in US 'place' attribute at root level?
# (3) How many have not-none 'place' attribute inside the 'quoted_status' nested tweet?
# (4) How many have in US 'place' attribute inside the 'quoted_status' nested tweet?
# (5) How many have not-none 'place' attribute inside the 'retweeted_status' nested tweet?
# (6) How many have in US 'place' attribute inside the 'retweeted_status' nested tweet?
#
# Example:
# gunzip -c coronavirus_12-27-2020.gz | python3 profile_tweets.py
#
# Author:
# Qiushi Bai ([email protected])
###########################################################
def count_place(dict_obj):
if 'place' in dict_obj.keys():
if dict_obj['place'] is not None:
return 1
return 0
def count_place_us(dict_obj):
if 'place' in dict_obj.keys():
if dict_obj['place'] is not None:
if 'country_code' in dict_obj['place'].keys():
if dict_obj['place']['country_code'].lower() == 'us':
return 1
return 0
if __name__ == '__main__':
countries = {}
count = 0
count_root_place = 0
count_quoted_status_place = 0
count_retweeted_status_place = 0
count_root_us = 0
count_quoted_status_us = 0
count_retweeted_status_us = 0
for line in sys.stdin:
tweet = ast.literal_eval(line)
count += 1
count_root_place += count_place(tweet)
count_root_us += count_place_us(tweet)
if 'quoted_status' in tweet.keys():
if tweet['quoted_status'] is not None:
count_quoted_status_place += count_place(tweet['quoted_status'])
count_quoted_status_us += count_place_us(tweet['quoted_status'])
if 'retweeted_status' in tweet.keys():
if tweet['retweeted_status'] is not None:
count_quoted_status_place += count_place(tweet['retweeted_status'])
count_quoted_status_us += count_place_us(tweet['retweeted_status'])
print('--------------------------------------------')
print(str(count) + ' tweets has been processed.')
print(str(count_root_place) + " has place.")
print(str(count_root_us) + " has place in US.")
print(str(count_quoted_status_place) + " has place in quoted_status.")
print(str(count_quoted_status_us) + " has place in US in quoted_status.")
print(str(count_retweeted_status_place) + " has place in retweeted_status.")
print(str(count_retweeted_status_us) + " has place in US in retweeted_status.")