-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_quantifier_tables.py
executable file
·176 lines (153 loc) · 5.38 KB
/
generate_quantifier_tables.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
"""Script to generate quantifier tables for the paper, based on word importances
Usage:
$python generate_tables.py --path ~/word_importances --name SQuAD
"""
import argparse
import copy
import pickle as pkl
import string
from word2number import w2n
import nltk
from nltk.corpus import stopwords
import pandas as pd
nltk.download("stopwords")
nltk.download("averaged_perceptron_tagger")
parser = argparse.ArgumentParser(
prog="generate_tables.py",
description="Generate Importance Tables across layers for word importances.",
)
parser.add_argument(
"--path",
type=str,
action="store",
help="The path for word importances binary file.",
required=True,
)
parser.add_argument(
"--name",
type=str,
action="store",
help="The name of the dataset to be used while storing table.",
required=True,
)
parser.add_argument(
"--topk",
type=int,
action="store",
help="The topk to be used for the table.",
required=True,
)
args = parser.parse_args()
with open(args.path, "rb") as f:
word_importances = pkl.load(f)
stopwords = stopwords.words("english")
def mark_categories(word_list, category_list):
"""Mark in categories whether a word is a query or contextual word.
Args:
word_list (list): List of words.
category_list (list): List of word categories (question, context, answer).
Returns:
list,int: list of modified categories, number of question words
"""
question_words = []
answer_indices = []
category_list = copy.deepcopy(category_list)
i = 0
while i < len(word_list) and category_list[i] == "question":
i += 1
question_words.append(word_list[i].lower())
for j in range(i, len(category_list)):
if (
category_list[j] == "context"
and word_list[j].lower() in question_words
and word_list[j] not in stopwords
):
category_list[j] = "query_words"
if category_list[j] == "answer":
answer_indices.append(j)
if answer_indices:
for k in range(
answer_indices[0] - 1,
min(answer_indices[0] - (args.topk + 1), len(question_words) - 1),
-1,
):
if category_list[k] == "query_words":
category_list[k] = "contextual_and_query"
# MARK contextual query if query words
else:
category_list[k] = "contextual_words"
for l in range(
answer_indices[-1] + 1,
min(answer_indices[-1] + (args.topk + 1), len(word_list)),
):
if category_list[l] == "query_words":
category_list[l] = "contextual_and_query"
# MARK contextual query if query words
else:
category_list[l] = "contextual_words"
return category_list, len(question_words)
num_layers = len(word_importances[0])
num_percentages = [
{
f"% numerical/top-{args.topk}": 0,
"% numerical/all_numerical": 0,
}
for i in range(num_layers)
]
for sample_idx, sample in enumerate(word_importances):
if sample is None:
continue
for layer_idx, layers in enumerate(sample):
words = layers[0]
importances = layers[1]
categories = layers[2]
new_categories, new_index = mark_categories(words, categories)
words = words[new_index:]
importances = importances[new_index:]
categories = categories[new_index:]
word_pos_tags = nltk.pos_tag(words)
total_numerical_words = 0
for word_pos_tag in word_pos_tags:
if word_pos_tag[1] == "CD":
total_numerical_words += 1
else:
try:
num = w2n.word_to_num(word_pos_tag[0])
total_numerical_words += 1
except ValueError:
try:
num = w2n.word_to_num(
word_pos_tag[0][:-1]
) ##thousands, hundreds
total_numerical_words += 1
except ValueError:
continue
top_k_indices = importances.argsort()[-args.topk :]
numerical_top_count = 0
for index in top_k_indices:
if words[index] != "":
pos_tag = nltk.pos_tag([words[index]])[0][1]
if pos_tag.startswith("CD"):
numerical_top_count += 1
else:
try:
num = w2n.word_to_num(words[index])
numerical_top_count += 1
except ValueError:
try:
num = w2n.word_to_num(words[index][:-1])
numerical_top_count += 1
except ValueError:
continue
num_percentages[layer_idx][f"% numerical/top-{args.topk}"] += (
numerical_top_count / args.topk
)
if total_numerical_words != 0:
num_percentages[layer_idx]["% numerical/all_numerical"] += (
numerical_top_count / total_numerical_words
)
for num_percentage in num_percentages:
for key in num_percentage.keys():
num_percentage[key] *= 100 / len(word_importances)
with open(f"{args.name} {args.topk} Quantifier Table.txt", "w") as f:
pd.DataFrame(num_percentages).to_latex(f, index=False)