-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconvert_retriever_result.py
187 lines (150 loc) · 6.57 KB
/
convert_retriever_result.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
import argparse
import collections
import json
import os
import sys
import random
### for single sent retrieve
def convert_train(json_in, json_out, topn, max_len = 256):
with open(json_in) as f_in:
data = json.load(f_in)
for each_data in data:
try:
gold_inds = []
cur_len = 0
table_retrieved = each_data["table_retrieved_all"]
text_retrieved = each_data["text_retrieved_all"]
all_retrieved = table_retrieved + text_retrieved
gold_table_inds = each_data["qa"]["table_evidence"]
gold_text_inds = each_data["qa"]["text_evidence"]
for ind in gold_table_inds:
gold_inds.append(ind)
cur_len += len(each_data["table_description"][ind].split())
for ind in gold_text_inds:
gold_inds.append(ind)
try:
cur_len += len(each_data["paragraphs"][ind].split())
except:
continue
false_retrieved = []
for tmp in all_retrieved:
if tmp["ind"] not in gold_inds:
false_retrieved.append(tmp)
sorted_dict = sorted(false_retrieved, key=lambda kv: kv["score"], reverse=True)
res_n = topn - len(gold_inds)
other_cands = []
while res_n > 0 and cur_len < max_len:
next_false_retrieved = sorted_dict.pop(0)
if next_false_retrieved["score"] < 0:
break
if type(next_false_retrieved["ind"]) == int:
cur_len += len(each_data["paragraphs"][next_false_retrieved["ind"]].split())
other_cands.append(next_false_retrieved["ind"])
res_n -= 1
else:
cur_len += len(each_data["table_description"][next_false_retrieved["ind"]].split())
other_cands.append(next_false_retrieved["ind"])
res_n -= 1
# recover the original order in the document
input_inds = gold_inds + other_cands
context = get_context(each_data, input_inds)
each_data["model_input"] = context
del each_data["table_retrieved_all"]
del each_data["text_retrieved_all"]
except:
print(each_data["uid"])
with open(json_out, "w") as f:
json.dump(data, f, indent=4)
def convert_test(retriever_json_in, question_classification_json_in, json_out, topn, max_len = 256):
with open(retriever_json_in) as f_in:
data = json.load(f_in)
with open(question_classification_json_in) as f_in:
qc_data = json.load(f_in)
qc_map = {}
for example in qc_data:
qc_map[example["uid"]] = example["pred"]
for each_data in data:
cur_len = 0
table_retrieved = each_data["table_retrieved_all"]
text_retrieved = each_data["text_retrieved_all"]
all_retrieved = table_retrieved + text_retrieved
cands_retrieved = []
for tmp in all_retrieved:
cands_retrieved.append(tmp)
sorted_dict = sorted(cands_retrieved, key=lambda kv: kv["score"], reverse=True)
res_n = topn
other_cands = []
while res_n > 0 and cur_len < max_len:
next_false_retrieved = sorted_dict.pop(0)
if next_false_retrieved["score"] < 0:
break
if type(next_false_retrieved["ind"]) == int:
cur_len += len(each_data["paragraphs"][next_false_retrieved["ind"]].split())
other_cands.append(next_false_retrieved["ind"])
res_n -= 1
else:
cur_len += len(each_data["table_description"][next_false_retrieved["ind"]].split())
other_cands.append(next_false_retrieved["ind"])
res_n -= 1
# recover the original order in the document
input_inds = other_cands
context = get_context(each_data, input_inds)
each_data["model_input"] = context
each_data["qa"]["predicted_question_type"] = qc_map[each_data["uid"]]
del each_data["table_retrieved_all"]
del each_data["text_retrieved_all"]
with open(json_out, "w") as f:
json.dump(data, f, indent=4)
def get_context(each_data, input_inds):
context = []
table_sent_map = get_table_sent_map(each_data["paragraphs"])
inds_map = {}
for ind in input_inds:
if type(ind) == str:
table_ind = int(ind.split("-")[0])
sent_ind = table_sent_map[table_ind]
if sent_ind not in inds_map:
inds_map[sent_ind] = [ind]
else:
if type(inds_map[sent_ind]) == int:
inds_map[sent_ind] = [ind]
else:
inds_map[sent_ind].append(ind)
else:
if ind not in inds_map:
inds_map[ind] = ind
for sent_ind in sorted(inds_map.keys()):
if type(inds_map[sent_ind]) != list:
context.append(sent_ind)
else:
for table_ind in sorted(inds_map[sent_ind]):
context.append(table_ind)
return context
def get_table_sent_map(paragraphs):
table_index = 0
table_sent_map = {}
for i, sent in enumerate(paragraphs):
if sent.startswith("## Table "):
table_sent_map[table_index] = i
table_index += 1
return table_sent_map
if __name__ == '__main__':
json_dir_in = "output/retriever_output"
question_classification_json_dir_in = "output/question_classification_output"
json_dir_out = "dataset/reasoning_module_input"
os.makedirs(json_dir_out, exist_ok = True)
topn, max_len = 10, 256
mode_names = ["train", "test", "dev"]
for mode in mode_names:
json_in = os.path.join(json_dir_in, f"{mode}.json")
question_classification_json_in = os.path.join(question_classification_json_dir_in, f"{mode}.json")
json_out_train = os.path.join(json_dir_out, mode + "_training.json")
json_out_inference = os.path.join(json_dir_out, mode + "_inference.json")
if mode == "train":
convert_train(json_in, json_out_train, topn, max_len)
if mode == "dev":
convert_train(json_in, json_out_train, topn, max_len)
convert_test(json_in, question_classification_json_in, json_out_inference, topn, max_len)
elif mode == "test":
convert_test(json_in, question_classification_json_in, json_out_inference, topn, max_len)
print(f"Convert {mode} set done")