-
Notifications
You must be signed in to change notification settings - Fork 8
/
raw_data_to_official_format.py
190 lines (150 loc) · 6.31 KB
/
raw_data_to_official_format.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
from typing import List, Dict
import argparse
import json
import os
from tqdm import tqdm
from utils.common import read_jsonl, write_jsonl, translate_id, step_placeholder
def filter_answers_in_context(answer_texts: List[str], contexts: List[Dict]) -> List[str]:
context_text = " ".join([
context["wikipedia_title"] + " " + context["paragraph_text"]
for context in contexts
]).lower()
answer_texts_in_context = [
answer_text for answer_text in answer_texts if answer_text.lower().strip() in context_text
]
return answer_texts_in_context
def raw_instance_to_official_format(instance: Dict, id_to_answer_aliases: Dict) -> Dict:
"""
Output Data Format.
{
"id":
"question": "...",
"question_decomposition": [
],
"answerable": True/False,
"answer": "...",
"answer_aliases": ["...", "..."]
"paragraphs": [
]
}
Prediction Format. It should be jsonl in the same order as the input.
{
"id": <id>,
"predicted_support_idxs": [...],
"predicted_answer": "...",
"predicted_answerable": True/False
}
"""
def get_decomposed_question_texts(decomposed_question_text: str):
relation_separator = ">>"
assert relation_separator not in decomposed_question_text
decomposed_question_text = decomposed_question_text.replace("[SEP]", relation_separator)
unlike_str = "<--UNLIKELY_BREAK-->"
for index in range(5):
if "who is #1 most listened to on spotify" not in decomposed_question_text:
assert f"#{index+1}" not in decomposed_question_text
decomposed_question_text = decomposed_question_text.replace(
step_placeholder(index, is_prefix=True, strip=True),
unlike_str
)
decomposed_question_text = decomposed_question_text.replace(
step_placeholder(index, is_prefix=False, strip=True),
f"#{index+1}"
)
decomposed_question_texts = [
e.strip() for e in decomposed_question_text.split(unlike_str) if e.strip()
]
return decomposed_question_texts
paragraph2idx = {}
updated_paragraphs = []
for idx, context in enumerate(instance["contexts"]):
updated_paragraphs.append({
"idx": idx,
"title": context["wikipedia_title"],
"paragraph_text": context["paragraph_text"],
"is_supporting": context["is_supporting"]
})
paragraph2idx[context["paragraph_text"]] = idx
decomposed_question_texts = get_decomposed_question_texts(instance['question_text'])
question_decomposition = []
for decomposed_question_text, decomposed_instance in zip(
decomposed_question_texts,
instance["decomposed_instances"]
):
sub_instance_id = decomposed_instance["id"]
sub_answer_text = decomposed_instance["answer_text"]
paragraph_support_idx = (
paragraph2idx[decomposed_instance["contexts"][0]["paragraph_text"]]
if decomposed_instance["contexts"][0]["paragraph_text"] in paragraph2idx else None
)
if instance["answerable"]:
assert paragraph_support_idx is not None
question_decomposition.append({
"id": sub_instance_id,
"question": decomposed_question_text,
"answer": sub_answer_text,
"paragraph_support_idx": paragraph_support_idx
})
def id2chain(chain_id: str) -> Dict:
shape, iids = chain_id.strip().split("__")
iids = [int(id_) for id_ in iids.split("_")]
return {"shape": shape, "iids": iids}
end_question_id = str(id2chain(instance['id'])['iids'][-1])
answer_aliases = id_to_answer_aliases.get(end_question_id, [])
answer_aliases = filter_answers_in_context(answer_aliases, instance["contexts"])
if instance["answer_text"] in answer_aliases:
answer_aliases.remove(instance["answer_text"])
answer_aliases = list(set(answer_aliases))
translated_instance = {
"id": translate_id(instance["id"]),
"paragraphs": updated_paragraphs,
"question": instance['composed_question_text'],
"question_decomposition": question_decomposition,
"answer": instance["answer_text"],
"answer_aliases": answer_aliases,
"answerable": instance["answerable"],
}
return translated_instance
def raw_dataset_to_official_format(
source_filepath: str,
target_filepath: str,
hide_labels: bool = False
):
aliases_path = ".answer_aliases.json" # only applicable for musique.
id_to_answer_aliases = {}
with open(aliases_path, "r") as file:
for key, value in tqdm(json.load(file).items()):
if key in id_to_answer_aliases and sorted(value) != sorted(id_to_answer_aliases[key]):
value = list(set(id_to_answer_aliases[key] + value))
id_to_answer_aliases[key] = value
source_instances = read_jsonl(source_filepath)
translated_instances = []
for instance in source_instances:
translated_instance = raw_instance_to_official_format(
instance, id_to_answer_aliases
)
translated_instances.append(translated_instance)
if hide_labels:
for translated_instance in translated_instances:
translated_instance.pop("answer")
translated_instance.pop("answer_aliases")
translated_instance.pop("answerable")
translated_instance.pop("question_composed_by", None)
translated_instance.pop("question_decomposition")
for paragraph in translated_instance["paragraphs"]:
paragraph.pop("is_supporting")
write_jsonl(translated_instances, target_filepath)
def main():
parser = argparse.ArgumentParser(description='Convert raw dataset file to official format.')
parser.add_argument(
'input_filepath',
type=str,
help='filepath to raw dataset file.'
)
args = parser.parse_args()
if not os.path.exists(args.input_filepath):
exit(f"Filepath {args.input_filepath} not found.")
output_filepath = "_official_format".join(os.path.splitext(args.input_filepath))
raw_dataset_to_official_format(args.input_filepath, output_filepath)
if __name__ == '__main__':
main()