-
Notifications
You must be signed in to change notification settings - Fork 1
/
make_qa_data.py
214 lines (184 loc) · 6.05 KB
/
make_qa_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
"""
To download TydiQA, do:
>> mkdir -p data/tydiqa && cd data/tydiqa
>> wget https://storage.googleapis.com/tydiqa/v1.1/tydiqa-goldp-v1.1-train.json
>> wget https://storage.googleapis.com/tydiqa/v1.1/tydiqa-goldp-v1.1-dev.tgz
>> tar -xf tydiqa-goldp-v1.1-dev.tgz
>> python make_qa_data.py (execute this file)
"""
import os
import json
import random
import numpy as np
import unicodedata as u
from collections import defaultdict
from shutil import copyfile
random.seed(42)
save_dir = "./data/tydiqa/all"
test_path = "./data/tydiqa/tydiqa-goldp-v1.1-dev/tydiqa-goldp-dev-{}.json"
def _clean_text(text):
output = []
for char in text:
cp = ord(char)
if cp == 0 or cp == 0xFFFD or _is_control(char):
continue
if _is_whitespace(char):
output.append(" ")
else:
output.append(char)
return "".join(output)
def _is_whitespace(char):
if char == " " or char == "\t" or char == "\n" or char == "\r":
return True
cat = u.category(char)
if cat == "Zs":
return True
return False
def _is_control(char):
if char == "\t" or char == "\n" or char == "\r":
return False
cat = u.category(char)
if cat in ("Cc", "Cf"):
return True
return False
def convert_to_unicode(text):
if isinstance(text, str):
return text
elif isinstance(text, bytes):
return text.decode("utf-8", "ignore")
else:
raise ValueError("Unsupported string type: %s" % (type(text)))
def count(data):
qs = 0
qlens, clens = [], []
for dp in data:
for p in dp["paragraphs"]:
clens.append(len(p["context"]))
qs += len(p["qas"])
for qa in p["qas"]:
qlens.append(len(qa["question"]))
return (
qs,
np.mean(clens),
np.std(clens),
np.max(clens),
np.mean(qlens),
np.std(qlens),
np.max(qlens),
)
def clean_and_normalize(dp):
dp["title"] = _clean_text(convert_to_unicode(dp["title"]))
for p in dp["paragraphs"]:
p["context"] = _clean_text(convert_to_unicode(p["context"]))
for qa in p["qas"]:
qa["question"] = _clean_text(convert_to_unicode(qa["question"]))
for a in qa["answers"]:
a["text"] = _clean_text(convert_to_unicode(a["text"]))
return dp
def normalize(dp):
dp["title"] = u.normalize("NFKC", dp["title"]).replace("\xa0", " ")
for p in dp["paragraphs"]:
p["context"] = u.normalize("NFKC", p["context"]).replace("\xa0", " ")
for qa in p["qas"]:
qa["question"] = u.normalize("NFKC", qa["question"]).replace("\xa0", " ")
for a in qa["answers"]:
a["text"] = u.normalize("NFKC", a["text"]).replace("\xa0", " ")
return dp
def print_stats():
train_paths = [
os.path.join(save_dir, fname)
for fname in os.listdir(save_dir)
if fname.endswith(".train")
]
dev_paths = [
os.path.join(save_dir, fname)
for fname in os.listdir(save_dir)
if fname.endswith(".dev")
]
test_paths = [
os.path.join(save_dir, fname)
for fname in os.listdir(save_dir)
if fname.endswith(".test")
]
fmt_str = (
"{0:>10}: {1:>7.0f} {2:>7.2f} {3:>7.2f} {4:>7.0f} {5:>7.2f} {6:>7.2f} {7:>7.0f}"
)
def loop(paths):
for fpath in paths:
with open(fpath, "r") as f:
data = json.load(f)["data"]
lang = fpath.split("/")[-1].split(".")[0]
print(fmt_str.format(lang, *count(data)))
print(
"\n{0:>10} {1:>7} {2:>7} {3:>7} {4:>7} {5:>7} {6:>7} {7:>7}".format(
"", "# of Qs", "C mean", "C std", "C max", "Q mean", "Q std", "Q max"
)
)
print("Train lang")
loop(train_paths)
print("\nDev lang")
loop(dev_paths)
print("\nTest lang")
loop(test_paths)
def main():
with open("./data/tydiqa/tydiqa-goldp-v1.1-train.json", "r") as f:
data = json.load(f)
version = data["version"]
data = data["data"]
langs = [
"arabic",
"bengali",
"finnish",
"indonesian",
"swahili",
"korean",
"russian",
"telugu",
]
random.shuffle(langs)
train_langs, test_langs = langs[:4], langs[4:]
train_langs += ["english"]
print(f"Train langs: {train_langs}")
print(f"Test langs: {test_langs}")
datasets = defaultdict(list)
for dp in data:
lang = dp["paragraphs"][0]["qas"][0]["id"].split("-")[0]
if lang not in langs + ["english"]:
raise ValueError(
"Datapoint does not have a valid id: {}".format(
dp["paragraphs"][0]["qas"][0]["id"]
)
)
elif lang in ["russian", "korean"]:
datasets[lang].append(clean_and_normalize(dp))
else:
datasets[lang].append(normalize(dp))
assert len(data) == sum(len(ds) for ds in datasets.values())
for ds in datasets.values():
random.shuffle(ds)
# write train and dev files
os.makedirs(save_dir, exist_ok=True)
for lang in train_langs:
d = datasets[lang]
split_idx = round(len(d) * 0.1)
with open(os.path.join(save_dir, f"{lang}.dev"), "w") as f:
json.dump({"version": version, "data": d[:split_idx]}, f)
with open(os.path.join(save_dir, f"{lang}.train"), "w") as f:
json.dump({"version": version, "data": d[split_idx:]}, f)
# copy real dev files as test files
for lang in test_langs:
with open(test_path.format(lang), "r") as f:
data = json.load(f)
version = data["version"]
data = data["data"]
clean_data = []
for dp in data:
if lang in ["russian", "korean"]:
clean_data.append(clean_and_normalize(dp))
else:
clean_data.append(normalize(dp))
with open(os.path.join(save_dir, f"{lang}.test"), "w") as f:
json.dump({"version": version, "data": clean_data}, f)
print_stats()
if __name__ == "__main__":
main()