-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patha1_extractFeatures.py
438 lines (304 loc) · 12.5 KB
/
a1_extractFeatures.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import argparse
import csv
import functools
import json
import logging
import re
import numpy as np
prefix_wordlist = '/u/cs401/'
wordlists_dir = prefix_wordlist + 'Wordlists/'
prefix_feats = '/u/cs401/A1/'
feats_dir = prefix_feats + 'feats/'
filename_norm_bristol_gilhooly_logie = wordlists_dir + "BristolNorms+GilhoolyLogie.csv"
filename_norm_warringer = wordlists_dir + "Ratings_Warriner_et_al.csv"
regex_tokenizer = re.compile(r"[^\S\n]*(\n)+[^\S\n]*|[^\S\n]+")
def read_bgl_norms():
with open(filename_norm_bristol_gilhooly_logie, newline='') as csv_file:
csv_file = csv.reader(csv_file)
norm_dict = dict()
for idx, row in enumerate(csv_file):
if idx == 0:
# Skipping the header
continue
word = row[1]
if not word:
logging.warning("the word field was missing, skipping...")
continue
norm_dict[word] = [int(row[3]), int(row[4]), int(row[5])]
return norm_dict
def read_warringer_norms():
with open(filename_norm_warringer, newline='') as csv_file:
csv_file = csv.reader(csv_file)
norm_dict = dict()
for idx, row in enumerate(csv_file):
if idx == 0:
# Skipping the header
continue
word = row[1]
if not word:
logging.warning("the word field was missing, skipping...")
continue
norm_dict[word] = [float(row[2]), float(row[5]), float(row[8])]
return norm_dict
bgl_norms = read_bgl_norms()
warringer_norms = read_warringer_norms()
def read_files_by_line(directory, files):
lines = list()
for file in files:
with open(directory + file) as f:
for line in f:
lines.append(line.strip())
return lines
def read_first_person_pronouns():
files = ["First-person"]
return set(read_files_by_line(wordlists_dir, files))
def read_second_person_pronouns():
files = ["Second-person"]
return set(read_files_by_line(wordlists_dir, files))
def read_third_person_pronouns():
files = ["Third-person"]
return set(read_files_by_line(wordlists_dir, files))
def read_slang_acronyms():
files = ["Slang"]
return set(read_files_by_line(wordlists_dir, files))
def read_receptiviti_id_file(filename):
return read_files_by_line(feats_dir, [filename])
first_person_pronouns = read_first_person_pronouns()
second_person_pronouns = read_second_person_pronouns()
third_person_pronouns = read_third_person_pronouns()
slang_acronyms = set(filter(None, read_slang_acronyms()))
ids_center = read_receptiviti_id_file('Center_IDs.txt')
ids_right = read_receptiviti_id_file('Right_IDs.txt')
ids_left = read_receptiviti_id_file('Left_IDs.txt')
ids_alt = read_receptiviti_id_file('Alt_IDs.txt')
ids_center_dict = {comment_id: idx for idx, comment_id in enumerate(ids_center)}
ids_right_dict = {comment_id: idx for idx, comment_id in enumerate(ids_right)}
ids_left_dict = {comment_id: idx for idx, comment_id in enumerate(ids_left)}
ids_alt_dict = {comment_id: idx for idx, comment_id in enumerate(ids_alt)}
receptiviti_feat_center = np.load(feats_dir + 'Center_feats.dat.npy')
receptiviti_feat_right = np.load(feats_dir + 'Right_feats.dat.npy')
receptiviti_feat_left = np.load(feats_dir + 'Left_feats.dat.npy')
receptiviti_feat_alt = np.load(feats_dir + 'Alt_feats.dat.npy')
# First person pronouns
fpp_alternation = "|".join(first_person_pronouns)
fpp_group = r"(?:{})".format(fpp_alternation)
regex_fpp = re.compile(r"^{0}/[^\s/]+$".format(fpp_group), re.IGNORECASE)
# Second person pronouns
spp_alternation = "|".join(second_person_pronouns)
spp_group = r"(?:{})".format(spp_alternation)
regex_spp = re.compile(r"^{0}/[^\s/]+$".format(spp_group), re.IGNORECASE)
# Third person pronouns
tpp_alternation = "|".join(third_person_pronouns)
tpp_group = r"(?:{})".format(tpp_alternation)
regex_tpp = re.compile(r"^{0}/[^\s/]+$".format(tpp_group), re.IGNORECASE)
# Coordinating conjunctions
regex_cc = re.compile(r"^[^\s/]+/CC$")
# Past tense verbs
regex_ptv = re.compile(r"^[^\s/]+/VBD$")
# Future tense verbs
regex_ftv = re.compile(r"(?:\s+|^)(?:will|'ll)/[^\s/]+(?:\s+|$)", re.IGNORECASE)
regex_gonna_vb = re.compile(r"(?:\s+|^)gonna/[^\s/]+\s+\S+/VB[DGNPZ]?(?:\s+|$)", re.IGNORECASE)
regex_going_to_vb = re.compile(r"(?:\s+|^)going/[^\s/]+\s+to/[^\s/]+\s+\S+/VB[DGNPZ]?(?:\s+|$)", re.IGNORECASE)
# Single comma
regex_single_comma = re.compile(r"^,/[^\s/]+$")
# Multi-character punctuation
regex_mc_punctuation = re.compile(r'^[!"#$%&()*+,\-./:;<=>?@\[\\\]^_{|}~]{2,}/[^\s/]+$')
# Common nouns
regex_noun_common = re.compile(r'^\S+/NNS?$')
# Proper nouns
regex_noun_proper = re.compile(r'^\S+/NN(?:PS|P)?$')
# Multi-character punctuation, without PoS tags
regex_mc_punctuation_no_tag = re.compile(r'^[!"#$%&()*+,\-./:;<=>?@\[\\\]^_{|}~]+$')
# PoS tags
regex_pos = re.compile(r"(\S+)/[^\s/]+", re.IGNORECASE)
regex_newline = re.compile(r"\n+")
# Adverbs
regex_adverbs = re.compile(r'^\S+/(?:RB[RS]?|RP)$')
# wh-words
regex_wh_words = re.compile(r'^\S+/(?:WP\$?|WRB|WDT)$')
# Words in uppercase (3 or more letters long)
regex_uppercase_words_3_chars = re.compile(r'^[A-Z]{3,}/[^\s/]+$')
# Slang acronyms
slang_acronyms_alternation = "(?:" + "|".join(slang_acronyms) + ")"
regex_slang_acronyms = re.compile(r'^{}/[^\s/]+$'.format(slang_acronyms_alternation), re.IGNORECASE)
def extract_features_30_through_173(comment_id, cat, features):
if cat == "Center":
features[29:] = receptiviti_feat_center[ids_center_dict[comment_id]]
elif cat == "Right":
features[29:] = receptiviti_feat_right[ids_right_dict[comment_id]]
elif cat == "Left":
features[29:] = receptiviti_feat_left[ids_left_dict[comment_id]]
elif cat == "Alt":
features[29:] = receptiviti_feat_alt[ids_alt_dict[comment_id]]
else:
logging.warning("Unrecognized category: {}".format(cat))
def extract_features(comment):
features = np.zeros((173,))
extract_features_15_through_29(comment, features)
comment = extract_feature_6(comment, features)
tokens = regex_tokenizer.split(comment)
for idx, token in enumerate(tokens):
if not token or token == "\n":
continue
token = token.strip()
extract_features_1_through_5(features, token)
extract_features_7_through_10(features, token)
extract_features_11_through_14(features, token)
return features
def extract_features_7_through_10(features, token):
# Feature 7: Number of commas
match_single_comma = regex_single_comma.match(token)
if match_single_comma:
features[6] += 1
# Feature 8: Number of multi-character punctuation tokens
match_mc_punctuation = regex_mc_punctuation.match(token)
if match_mc_punctuation:
features[7] += 1
# Feature 9: Number of common nouns
match_noun_common = regex_noun_common.match(token)
if match_noun_common:
features[8] += 1
# Feature 10: Number of proper nouns
match_noun_proper = regex_noun_proper.match(token)
if match_noun_proper:
features[9] += 1
def extract_feature_6(comment, features):
# Feature 6: Number of future tense verbs
# It's better to extract feature 6 before tokenizing the comment down below...
comment, count = regex_ftv.subn(" ", comment)
features[5] += count
comment, count = regex_gonna_vb.subn(" ", comment)
features[5] += count
comment, count = regex_going_to_vb.subn(" ", comment)
features[5] += count
return comment
def extract_features_11_through_14(features, token):
# Feature 11: Number of adverbs
if regex_adverbs.match(token):
features[10] += 1
# Feature 12: Number of wh-words
if regex_wh_words.match(token):
features[11] += 1
# Feature 13: Slang acronyms
if regex_slang_acronyms.match(token):
features[12] += 1
# Feature 14: Number of words in uppercase (>= 3 letters long)
if regex_uppercase_words_3_chars.match(token):
features[13] += 1
def extract_features_15_through_29(comment, features):
# Feature 17: Number of sentences
no_tag_comment = regex_pos.sub(r"\1", comment).strip()
num_sentences = len(regex_newline.split(no_tag_comment))
features[16] = num_sentences
# Feature 15: Average length of sentences, in tokens
no_tag_no_newline_comment = no_tag_comment.replace("\n", " ")
tokens = list(filter(lambda x: x and x != "\n", regex_tokenizer.split(no_tag_no_newline_comment)))
num_tokens = len(tokens)
features[14] = num_tokens / num_sentences
# Feature 16: Average length of tokens, excluding punctuation-only tokens, in characters
tokens_exc_multicharacter_punct = filter(lambda x: not regex_mc_punctuation_no_tag.match(x), tokens)
sum_tokens_length = functools.reduce(lambda acc, x: acc + len(x), tokens_exc_multicharacter_punct, 0)
features[15] = sum_tokens_length / num_tokens
extract_features_18_through_29(tokens, features)
def extract_features_18_through_29(tokens, features):
# Features 18 - 29
bgl_aoa = []
bgl_img = []
bgl_fam = []
warringer_v = []
warringer_a = []
warringer_d = []
for token in tokens:
if not token:
continue
bgl_data = bgl_norms.get(token.lower())
if bgl_data:
bgl_aoa.append(bgl_data[0])
bgl_img.append(bgl_data[1])
bgl_fam.append(bgl_data[2])
warringer_data = warringer_norms.get(token.lower())
if warringer_data:
warringer_v.append(warringer_data[0])
warringer_a.append(warringer_data[1])
warringer_d.append(warringer_data[2])
if bgl_aoa:
features[17] = np.mean(bgl_aoa)
features[20] = np.std(bgl_aoa)
if bgl_img:
features[18] = np.mean(bgl_img)
features[21] = np.std(bgl_img)
if bgl_fam:
features[19] = np.mean(bgl_fam)
features[22] = np.std(bgl_fam)
if warringer_v:
features[23] = np.mean(warringer_v)
features[26] = np.std(warringer_v)
if warringer_a:
features[24] = np.mean(warringer_a)
features[27] = np.std(warringer_a)
if warringer_d:
features[25] = np.mean(warringer_d)
features[28] = np.std(warringer_d)
def extract_features_1_through_5(features, token):
# Feature 1: Number of first-person pronouns
match_fpp = regex_fpp.match(token)
if match_fpp:
features[0] += 1
# Feature 2: Number of second-person pronouns
match_spp = regex_spp.match(token)
if match_spp:
features[1] += 1
# Feature 3: Number of third-person pronouns
match_tpp = regex_tpp.match(token)
if match_tpp:
features[2] += 1
# Feature 4: Number of coordinating conjunctions
match_cc = regex_cc.match(token)
if match_cc:
features[3] += 1
# Feature 5: Number of past tense verbs
match_ptv = regex_ptv.match(token)
if match_ptv:
features[4] += 1
def extract1(comment):
''' This function extracts features from a single comment
Parameters:
comment : string, the body of a comment (after preprocessing)
Returns:
feats : numpy Array, a 173-length vector of floating point features (only the first 29 are expected to be filled, here)
'''
# This shouldn't be necessary, but for sanity...
comment = comment.strip()
if not comment:
# logging.warning("Ignoring empty comment...")
return np.zeros((173,))
features = extract_features(comment)
return features
def encode_label(label):
encoding = {
"Left": 0,
"Center": 1,
"Right": 2,
"Alt": 3
}
return encoding[label]
def main(args):
data = json.load(open(args.input))
feats = np.zeros((len(data), 173 + 1))
for i, datum in enumerate(data):
body = datum['body']
cat = datum['cat']
encoded_cat = encode_label(cat)
features = extract1(body)
comment_id = datum['id']
extract_features_30_through_173(comment_id, cat, features)
features = np.append(features, [encoded_cat], axis=0)
feats[i] = features
np.savez_compressed(args.output, feats)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Process each .')
parser.add_argument("-o", "--output", help="Directs the output to a filename of your choice", required=True)
parser.add_argument("-i", "--input", help="The input JSON file, preprocessed as in Task 1", required=True)
args = parser.parse_args()
main(args)
print("Feature extraction finished. Exiting...")