-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnti-Semitics_data_extract.py
145 lines (135 loc) · 6.27 KB
/
Anti-Semitics_data_extract.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
import json
import re
import zstandard as zstd
import lzma
import datetime
def convert_sectodate(sec):
ndate = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=(sec))
return ndate.isoformat().replace('T',' ')
sample_dict = {'all_awardings': [],
# 'author': 'ragenukem',
'author_created_utc': 1343369985,
'author_flair_background_color': None,
'author_flair_css_class': None,
'author_flair_richtext': [],
'author_flair_template_id': None,
'author_flair_text': None,
'author_flair_text_color': None,
'author_flair_type': 'text',
'author_fullname': 't2_8gva8',
'author_patreon_flair': False,
# 'body': 'I get to do the thing! /r/beetlejuicing',
'can_gild': True,
'can_mod_post': False,
'collapsed': False,
'collapsed_reason': None,
'controversiality': 0,
'created_utc': 1559347203,
'distinguished': None,
'edited': False,
'gilded': 0,
'gildings': {},
'id': 'epom0fx',
'is_submitter': False,
'link_id': 't3_bv9v53',
'locked': False,
'no_follow': True,
'parent_id': 't1_epobwr9',
'permalink': '/r/Wellthatsucks/comments/bv9v53/ball_boy_meet_wall_boy/epom0fx/',
'quarantined': False,
'removal_reason': None,
'retrieved_on': 1568677948,
# 'score': 2,
# 'send_replies': True,
'steward_reports': [],
'stickied': False,
# 'subreddit': 'Wellthatsucks',
'subreddit_id': 't5_2xcv7',
'subreddit_name_prefixed': 'r/Wellthatsucks',
'subreddit_type': 'public',
'total_awards_received': 0}
def remove_key(target_dict, source_dict):
for key in source_dict:
try:
del target_dict[key]
except KeyError:
pass
antisemitics_words = ['libel', 'clannish', 'conspiracy', 'cowardice', 'goyim', 'globalist', 'greed', 'holocaust', 'jew', 'jewish', 'illuminati', 'khazars', 'kosher', 'zionish', 'scapegoat', 'silencing', 'smirking', 'merchant']
antisemitics_set = set(antisemitics_words)
filenames = ["/l/research/social-media-mining/reddit/comments/RC_2018-01.xz",
"/l/research/social-media-mining/reddit/comments/RC_2018-02.xz",
"/l/research/social-media-mining/reddit/comments/RC_2018-03.xz",
"/l/research/social-media-mining/reddit/comments/RC_2018-04.xz",
"/l/research/social-media-mining/reddit/comments/RC_2018-05.xz",
"/l/research/social-media-mining/reddit/comments/RC_2018-06.xz",
"/l/research/social-media-mining/reddit/comments/RC_2018-07.xz",
"/l/research/social-media-mining/reddit/comments/RC_2018-08.xz",
"/l/research/social-media-mining/reddit/comments/RC_2018-09.xz",
"/l/research/social-media-mining/reddit/comments/RC_2018-10.xz",
"/l/research/social-media-mining/reddit/comments/RC_2018-11.zst",
"/l/research/social-media-mining/reddit/comments/RC_2018-12.zst",
"/l/research/social-media-mining/reddit/comments/RC_2019-01.zst",
"/l/research/social-media-mining/reddit/comments/RC_2019-02.zst",
"/l/research/social-media-mining/reddit/comments/RC_2019-03.zst",
"/l/research/social-media-mining/reddit/comments/RC_2019-04.zst",
"/l/research/social-media-mining/reddit/comments/RC_2019-05.zst",
"/l/research/social-media-mining/reddit/comments/RC_2019-06.zst"
]
for filename in filenames:
date = filename.replace('.xz','').replace('.zst','')
if filename.split('.',1)[1] == 'xz':
with lzma.open(filename, mode='rt', encoding='utf-8') as redditfile:
for line in redditfile:
line = json.loads(line)
words = re.findall(r'\w+', line['body'].lower())
words_set = set(words)
common_elements = words_set.intersection(antisemitics_set)
score = len(common_elements)
antisemitics_list = []
if score >= 4:
for w in words:
for a in antisemitics_words:
if w == a:
antisemitics_list.append(w)
ndate = convert_sectodate(line['created_utc'])
line['date_year_month'] = date[-7:]
line['created_utc_converted'] = ndate
line['score_overall'] = len(antisemitics_list)
line['words'] = ' '.join([str(elem) for elem in antisemitics_list])
line['shared_words'] = ' '.join([str(elem) for elem in list(dict.fromkeys(antisemitics_list))])
line['socre_distint'] = score
remove_key(line, sample_dict)
with open('antisemitics2018-01to10.json', 'a') as outfile:
json.dump(line, outfile)
outfile.write('\n')
outfile.close()
elif filename.split('.',1)[1] == 'zst':
with open(filename, 'rb') as fh:
dctx = zstd.ZstdDecompressor()
stream_reader = dctx.stream_reader(fh)
text_stream = io.TextIOWrapper(stream_reader, encoding='utf-8')
z = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)
for line in text_stream:
line = json.loads(line.translate(z))
words = re.findall(r'\w+', line['body'].lower())
words_set = set(words)
common_elements = words_set.intersection(antisemitics_set)
score = len(common_elements)
antisemitics_list = []
if score >= 4:
for w in words:
for a in antisemitics_words:
if w == a:
antisemitics_list.append(w)
ndate = convert_sectodate(line['created_utc'])
line['date_year_month'] = date[-7:]
line['created_utc_converted'] = ndate
line['score_overall'] = len(antisemitics_list)
line['words'] = ' '.join([str(elem) for elem in antisemitics_list])
line['shared_words'] = ' '.join([str(elem) for elem in list(dict.fromkeys(antisemitics_list))])
line['socre_distint'] = score
remove_key(line, sample_dict)
with open('antisemitics_{date[-7:]}.json', 'a') as outfile:
json.dump(line, outfile)
outfile.write('\n')
outfile.close()