This repository has been archived by the owner on Oct 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_data.py
executable file
·395 lines (319 loc) · 12.5 KB
/
filter_data.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
#! /usr/bin/env python3
# coding: utf-8
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input", type= str, help="Input File", required=False)
parser.add_argument("-fl", "--filter_by_list", action='store_true', help="Create a version of file with tokens from \'-l\' removed", required=False)
parser.add_argument("-l", "--list_file", type=str, help="File with the list of tokens to filter", required=False)
parser.add_argument("--combine", help="combine the files (*.most_common) to a single file, with updated values", nargs="*", required=False)
parser.add_argument("-ds", "--get_default_seeds", action='store_true', help="Get the default seeds from the file with true_gender data. Requires \'-i\' argument", required=False)
parser.add_argument("--bootstrap", action='store_true', help="Complete contextual bootstraping step for sample.bootstrap file", required=False)
parser.add_argument("--conllu", nargs="*", help="CONLLU files for bootstraping algorithm", required=False)
parser.add_argument("--morphology", action='store_true', help="Create a trie and analyze the words as in Section 2.4 of the paper", required=False)
parser.add_argument("--get_accuracy", action='store_true', help="Get coverage and the accuracy for the generated data", required=False)
args = parser.parse_args()
# wrapper method for extracting `token : value` format into a dict, with (token, value) as (key, value) pair.
def get_dict(all_contents):
values = dict()
for line in all_contents:
key, value = line.split(" : ")
values[key] = int(value.strip("\n"))
return values
# wrapper method for extracting `token : value` format into a dict, with (token, value) as (key, value) pair.
# differs from get_dict() in only adding a key if value is >=5.
# called while using `--bootstrap` and `--morphology`
def get_dict2(all_contents, min_value):
values = dict()
for line in all_contents:
key, value = line.split(" : ")
if int(value.strip("\n")) >= min_value:
values[key] = int(value.strip("\n"))
return values
# function used for `--filter_by_list` switch.
# takes the second file contents, and if element is found in the dict, removes it.
def filter_dict(a_dict, all_contents):
dict_copy = a_dict
for line in all_contents:
# for files with only the list, eg- remove_list
if len(line.split("\t")) == 1:
if line.strip("\n") in dict_copy:
del dict_copy[line.strip("\n")]
# for files with tsv format, eg- seeds.LIST
else:
if line.split("\t")[0] in dict_copy:
del dict_copy[line.split("\t")[0]]
return dict_copy
# function to read conllu, and send the line of the token to get_gender().
# called while using `--bootstrap`
def process_conllu(score_dict):
for i in range(len(args.conllu)):
with open(args.conllu[i], "r") as conllu:
contents = conllu.readlines()
for line in contents:
if line != "\n":
if line[:2] != "ID":
token = ""
if line.split("\t")[1].lower() in score_dict:
token = line.split("\t")[1].lower()
if get_gender(line) == "Fem":
score_dict[token][1] += 1
elif get_gender(line) == "Masc":
score_dict[token][0] += 1
else:
score_dict[token][2] += 1
return score_dict
# updates the seeds, by using contextual bootstrapping.
# works with confidence values of 0.33 and higher.
# called when using `--bootstrap`
def update_seeds(dest_file, score_dict):
# for every token in the score_dict, we iterate this
for token in score_dict:
male, female, quest = score_dict[token]
total = male + female + quest
confidence = 1 - quest / total
# base confidence score = 0.33
if confidence > 0.33:
if male == 0 and female != 0:
dest_file.write(token + "\tF\n")
elif male != 0 and female == 0:
dest_file.write(token + "\tM\n")
# if none of the individual values = 0, work with higher confidence score
elif confidence > 0.70:
if male <= 0.85 * female:
dest_file.write(token + "\tF\n")
elif female <= 0.85 * male:
dest_file.write(token + "\tM\n")
# returns the values of the process_conllu() in the form of probability score
# called when using `--morphology`
def get_probab(scoredict):
for key in scoredict:
val_list = scoredict[key]
total = 0
for a in val_list:
total += a
for i in range(len(val_list)):
val_list[i] = val_list[i] / total
return scoredict
# A wrapper function to reverse a string.
# Used when input-ing values into the trie
def reverse(stringval):
return stringval[::-1]
# returns P(gender_j | l_n l_n-1 ... l_i) as per formula in the paper
# called when using `--morphology`
def P(fullword, word, gen):
if word in t.keys(shallow=False):
if gen == "M":
return t[word][0]
elif gen == "F":
return t[word][1]
else:
if gen == "M":
return t[reverse(fullword)][0]
elif gen == "F":
return t[reverse(fullword)][1]
# returns P_node(quest) as per formula in the paper
# called when using `--morphology`
def P_quest(fullword, word):
if word in t.keys(shallow=False):
return t[word][2]
else:
return t[fullword][2]
# returns P^ (gender_j | l_n l_n-1 ... l_i l_i+1) as per formula in the paper
# recursive call
# called when using `--morphology`
def p_cap(word, i, gender):
if i == len(word):
if gender == "F":
return t[reverse(word)][1]
elif gender == "M":
return t[reverse(word)][0]
return P(word, reverse(word)[:i], gender) + P_quest(reverse(word), reverse(word)[:i]) * p_cap(word, i+1, gender)
# function call to calculate the coefficient smoothing for a given word
# calls p_cap(), P_quest() and P()
# called when using `--morphology`
def process_word(word, gender):
i = 0
return p_cap(word, i, gender)
if __name__ == "__main__":
if args.filter_by_list:
# check if other necessary arguments are provided for.
if not args.list_file:
print("MISSING ARGUMENT\n"
"-l: File with the list of tokens to filter")
exit(0)
# get contents of the input file into a dict.
items = dict()
with open(args.input, "r") as infile:
contents = infile.readlines()
items = get_dict(contents)
# get contents of the second file, and send them to filter_dict() for processing.
with open(args.list_file, "r") as listfile:
contents = listfile.readlines()
items = filter_dict(items, contents)
# output the results into the file with `.list_filtered` as a suffix.
with open(args.input+".list_filtered", "w") as outfile:
for a in items:
outfile.write(a + " : " + str(items[a]) + "\n")
if args.combine:
from collections import Counter
counter = Counter()
outfile = open("sample/sample.most_common", "w")
# combine the files, opening each file and updating the counter
for i in range(len(args.combine)):
with open(args.combine[i], "r") as infile:
contents = infile.readlines()
for line in contents:
word = line.split(" : ")[0].lower()
value = int(line.split(" : ")[1])
if word not in counter:
counter[word] = value
else:
counter[word] += value
counter = counter.most_common()
# write the results in output file.
for vals in counter:
outfile.write(vals[0] + " : " + str(vals[1]) + "\n")
outfile.close()
if args.get_default_seeds:
# check if other necessary arguments are provided for.
if not args.input:
print("MISSING ARGUMENT\n"
"-i: Input File")
exit(0)
# just strip second and third column from the file to get the list of default seeds.
with open(args.input) as tsvfile:
contents = tsvfile.readlines()
with open("seeds.LIST", "w") as outfile:
for lines in contents:
if lines.split("\t")[1] != "-":
outfile.write(lines.split("\t")[1] + "\t" + lines.split("\t")[2].strip("\n") + "\n")
if args.bootstrap:
# check if other necessary arguments are provided for.
if not args.input:
print("MISSING ARGUMENT\n"
"-i: Input File")
exit(0)
if not args.conllu:
print("MISSING ARGUMENT\n"
"--conllu: CONLLU files for bootstraping algorithm")
exit(0)
# import from sample.process_sample file
from sample.process_sample import get_gender
from collections import defaultdict
# call update_seeds and update the seeds.LIST file
seeds_file = open("seeds.LIST", "a")
with open(args.input) as bootfile:
contents = bootfile.readlines()
bootfile_dict = get_dict2(contents, 4)
scores = defaultdict(list)
for items in bootfile_dict:
scores[items] = [0, 0, 0]
scores = process_conllu(scores)
update_seeds(seeds_file, scores)
if args.morphology:
# check if other necessary arguments are provided for.
if not args.input:
print("MISSING ARGUMENT\n"
"-i: Input File")
exit(0)
# import from sample.process_sample file
from collections import defaultdict
from sample.process_sample import get_gender
import pygtrie as trie
t = trie.StringTrie()
# first, add all the seeds
with open("seeds.LIST", "r") as seeds_file:
contents = seeds_file.readlines()
for line in contents:
word, gender = line.strip("\n").split("\t")
if gender == "M":
t[reverse(word)] = [1, 0, 0]
elif gender == "F":
t[reverse(word)] = [0, 1, 0]
# now, input entries from the input file
with open(args.input, "r") as morpho:
contents = morpho.readlines()
# since the value at the end of string is supposed to be probability of a gender as per contextual bootstraping
# we bootstrap the values with count >= 4 and input the value as the released value of the process_conllu() function, sent to get_probab() function
# for count <= 4, we simply input them as [0, 0, 1] making them all questionable, and relying on just smoothing to get the value.
if args.conllu:
allscores_dict = get_dict2(contents, 4)
# add the words with count <= 3 with the corresponding [0, 0, 1] into the trie.
for line in contents[::-1]:
word, count = line.strip("\n").split(" : ")
if word not in allscores_dict:
t[reverse(word)] = [0.25, 0.25, .50]
allscores_dict[word] = [0, 0, 1]
else:
break
# process_conllu() with elements of count >= 4
scores = defaultdict(list)
for items in allscores_dict:
scores[items] = [0, 0, 0]
scores = process_conllu(scores)
scores = get_probab(scores)
# having received the probability scores, add the reversed string with the scores to the trie.
for key in scores:
t[reverse(key)] = scores[key]
# for case when not using context bootstraping, just use all the values as such
# and let the relevant information be received from the seeds input.
else:
allscores_dict = get_dict(contents)
for word in allscores_dict:
t[reverse(word)] = [0.25, 0.25, 0.50]
# now that we have added all the data, it's now time for recursive algorithm.
for word in allscores_dict:
M = process_word(word, "M")
F = process_word(word, "F")
t[reverse(word)] = [M, F, 1-M-F]
# Get final output values in an output file
with open("{}.output".format(args.input), "w") as outfile:
for a in t.keys(shallow=False):
if reverse(a) in allscores_dict:
if t[a][0] > 0.55:
outfile.write(reverse(a) + "\tM\n")
elif t[a][1] > 0.55:
outfile.write(reverse(a) + "\tF\n")
else:
print(reverse(a) + "\t" + str(t[a]))
if args.get_accuracy:
# check if other necessary arguments are provided for.
if not args.input:
print("MISSING ARGUMENT\n"
"-i: Input File for predictions")
exit(0)
if not args.conllu:
print("MISSING ARGUMENT\n"
"--conllu: CONLLU files for bootstraping algorithm")
exit(0)
# get predictions in a dictionary
predict = dict()
with open(args.input, "r") as predict_file:
contents = predict_file
for line in contents:
word, gender = line.strip("\n").split("\t")
predict[word] = gender
# get truth values using context bootstraping with exact values, and append them to a dictionary
from collections import defaultdict
from sample.process_sample import get_gender
scores = defaultdict(list)
true = dict()
for word in predict:
scores[word] = [0, 0, 0]
scores = process_conllu(scores)
for items in scores:
male, female, quest = scores[items]
if male == 0 and quest == 0 and female != 0:
true[items] = "F"
elif female == 0 and quest == 0 and male != 0:
true[items] = "M"
# get accuracy
i = 1
right = 0
for word in true:
if word in predict:
if predict[word] == true[word]:
right += 1
i += 1
# print accuracy
print("Accuracy: " + str(right) + " / " + str(i) + " = " + str(right*100/i) + " %")