Commit
·
5f30172
1
Parent(s):
0592565
add other datasets
Browse files
mrqa.py
CHANGED
|
@@ -95,6 +95,18 @@ class MRQA(datasets.GeneratorBasedBuilder):
|
|
| 95 |
VERSION = datasets.Version("1.1.0")
|
| 96 |
|
| 97 |
BUILDER_CONFIGS = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
MRQAConfig(
|
| 99 |
name="newsqa",
|
| 100 |
data_url={"validation": _URLs["validation+NewsQA"],
|
|
@@ -113,6 +125,12 @@ class MRQA(datasets.GeneratorBasedBuilder):
|
|
| 113 |
"train": _URLs["train+HotpotQA"],
|
| 114 |
"test": _URLs["validation+HotpotQA"]}
|
| 115 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
]
|
| 117 |
|
| 118 |
def _info(self):
|
|
@@ -123,38 +141,9 @@ class MRQA(datasets.GeneratorBasedBuilder):
|
|
| 123 |
{
|
| 124 |
"subset": datasets.Value("string"),
|
| 125 |
"context": datasets.Value("string"),
|
| 126 |
-
# "context_tokens": datasets.Sequence(
|
| 127 |
-
# {
|
| 128 |
-
# "tokens": datasets.Value("string"),
|
| 129 |
-
# "offsets": datasets.Value("int32"),
|
| 130 |
-
# }
|
| 131 |
-
# ),
|
| 132 |
"qid": datasets.Value("string"),
|
| 133 |
"idx": datasets.Value("int32"),
|
| 134 |
"question": datasets.Value("string"),
|
| 135 |
-
# "question_tokens": datasets.Sequence(
|
| 136 |
-
# {
|
| 137 |
-
# "tokens": datasets.Value("string"),
|
| 138 |
-
# "offsets": datasets.Value("int32"),
|
| 139 |
-
# }
|
| 140 |
-
# ),
|
| 141 |
-
# "detected_answers": datasets.Sequence(
|
| 142 |
-
# {
|
| 143 |
-
# "text": datasets.Value("string"),
|
| 144 |
-
# "char_spans": datasets.Sequence(
|
| 145 |
-
# {
|
| 146 |
-
# "start": datasets.Value("int32"),
|
| 147 |
-
# "end": datasets.Value("int32"),
|
| 148 |
-
# }
|
| 149 |
-
# ),
|
| 150 |
-
# "token_spans": datasets.Sequence(
|
| 151 |
-
# {
|
| 152 |
-
# "start": datasets.Value("int32"),
|
| 153 |
-
# "end": datasets.Value("int32"),
|
| 154 |
-
# }
|
| 155 |
-
# ),
|
| 156 |
-
# }
|
| 157 |
-
# ),
|
| 158 |
"answers": datasets.Sequence(datasets.Value("string")),
|
| 159 |
"answer": datasets.Value("string"),
|
| 160 |
}
|
|
@@ -204,35 +193,19 @@ class MRQA(datasets.GeneratorBasedBuilder):
|
|
| 204 |
idx = 0
|
| 205 |
for row in f:
|
| 206 |
paragraph = json.loads(row)
|
| 207 |
-
context = paragraph["context"]
|
| 208 |
-
if subset == "HotpotQA":
|
| 209 |
-
context = context.replace("[PAR] ", "\n\n")
|
| 210 |
-
context = context.replace("[TLE]", "Title:")
|
| 211 |
-
context = context.replace("[SEP]", "\nPassage:").strip()
|
| 212 |
-
# context_tokens = [{"tokens": t[0], "offsets": t[1]} for t in paragraph["context_tokens"]]
|
| 213 |
for qa in paragraph["qas"]:
|
| 214 |
qid = qa["qid"]
|
| 215 |
question = qa["question"].strip()
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
# detected_answers.append(
|
| 220 |
-
# {
|
| 221 |
-
# "text": detect_ans["text"].strip(),
|
| 222 |
-
# "char_spans": [{"start": t[0], "end": t[1]} for t in detect_ans["char_spans"]],
|
| 223 |
-
# "token_spans": [{"start": t[0], "end": t[1]} for t in detect_ans["token_spans"]],
|
| 224 |
-
# }
|
| 225 |
-
# )
|
| 226 |
-
answers = qa["answers"]
|
| 227 |
final_row = {
|
| 228 |
"subset": subset,
|
| 229 |
-
"context": context,
|
| 230 |
-
# "context_tokens": context_tokens,
|
| 231 |
"qid": qid,
|
| 232 |
"idx": idx,
|
| 233 |
-
"question": question,
|
| 234 |
-
# "question_tokens": question_tokens,
|
| 235 |
-
# "detected_answers": detected_answers,
|
| 236 |
"answers": answers,
|
| 237 |
"answer": answers[0]
|
| 238 |
}
|
|
@@ -240,7 +213,53 @@ class MRQA(datasets.GeneratorBasedBuilder):
|
|
| 240 |
yield f"{source}_{qid}", final_row
|
| 241 |
|
| 242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
if __name__ == '__main__':
|
| 244 |
from datasets import load_dataset
|
| 245 |
-
|
|
|
|
| 246 |
x = 5
|
|
|
|
| 95 |
VERSION = datasets.Version("1.1.0")
|
| 96 |
|
| 97 |
BUILDER_CONFIGS = [
|
| 98 |
+
MRQAConfig(
|
| 99 |
+
name="searchqa",
|
| 100 |
+
data_url={"validation": _URLs["validation+SearchQA"],
|
| 101 |
+
"train": _URLs["train+SearchQA"],
|
| 102 |
+
"test": _URLs["validation+SearchQA"]}
|
| 103 |
+
),
|
| 104 |
+
MRQAConfig(
|
| 105 |
+
name="squad",
|
| 106 |
+
data_url={"validation": _URLs["validation+SQuAD"],
|
| 107 |
+
"train": _URLs["train+SQuAD"],
|
| 108 |
+
"test": _URLs["validation+SQuAD"]}
|
| 109 |
+
),
|
| 110 |
MRQAConfig(
|
| 111 |
name="newsqa",
|
| 112 |
data_url={"validation": _URLs["validation+NewsQA"],
|
|
|
|
| 125 |
"train": _URLs["train+HotpotQA"],
|
| 126 |
"test": _URLs["validation+HotpotQA"]}
|
| 127 |
),
|
| 128 |
+
MRQAConfig(
|
| 129 |
+
name="triviaqa",
|
| 130 |
+
data_url={"validation": _URLs["validation+TriviaQA"],
|
| 131 |
+
"train": _URLs["train+TriviaQA"],
|
| 132 |
+
"test": _URLs["validation+TriviaQA"]}
|
| 133 |
+
),
|
| 134 |
]
|
| 135 |
|
| 136 |
def _info(self):
|
|
|
|
| 141 |
{
|
| 142 |
"subset": datasets.Value("string"),
|
| 143 |
"context": datasets.Value("string"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
"qid": datasets.Value("string"),
|
| 145 |
"idx": datasets.Value("int32"),
|
| 146 |
"question": datasets.Value("string"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
"answers": datasets.Sequence(datasets.Value("string")),
|
| 148 |
"answer": datasets.Value("string"),
|
| 149 |
}
|
|
|
|
| 193 |
idx = 0
|
| 194 |
for row in f:
|
| 195 |
paragraph = json.loads(row)
|
| 196 |
+
context = clean_context(paragraph["context"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
for qa in paragraph["qas"]:
|
| 198 |
qid = qa["qid"]
|
| 199 |
question = qa["question"].strip()
|
| 200 |
+
if question[-1] != "?":
|
| 201 |
+
question += "?"
|
| 202 |
+
answers = [clean_up_spaces(a) for a in qa["answers"]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
final_row = {
|
| 204 |
"subset": subset,
|
| 205 |
+
"context": clean_up_spaces(context),
|
|
|
|
| 206 |
"qid": qid,
|
| 207 |
"idx": idx,
|
| 208 |
+
"question": clean_up_spaces(question),
|
|
|
|
|
|
|
| 209 |
"answers": answers,
|
| 210 |
"answer": answers[0]
|
| 211 |
}
|
|
|
|
| 213 |
yield f"{source}_{qid}", final_row
|
| 214 |
|
| 215 |
|
| 216 |
+
def clean_context(context):
|
| 217 |
+
return (
|
| 218 |
+
context.replace("[PAR] ", "\n\n")
|
| 219 |
+
.replace("[TLE]", "Title:")
|
| 220 |
+
.replace("[SEP]", "\nPassage:").strip()
|
| 221 |
+
.replace("<Li>", "")
|
| 222 |
+
.replace("</Li>", "")
|
| 223 |
+
.replace("<OI>", "")
|
| 224 |
+
.replace("</OI>", "")
|
| 225 |
+
.replace("<Ol>", "")
|
| 226 |
+
.replace("</Ol>", "")
|
| 227 |
+
.replace("<Dd>", "")
|
| 228 |
+
.replace("</Dd>", "")
|
| 229 |
+
.replace("<UI>", "")
|
| 230 |
+
.replace("</UI>", "")
|
| 231 |
+
.replace("<Ul>", "")
|
| 232 |
+
.replace("</Ul>", "")
|
| 233 |
+
.replace("<P>", "")
|
| 234 |
+
.replace("</P>", "")
|
| 235 |
+
.replace("[DOC]", "")
|
| 236 |
+
).strip()
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
def clean_up_spaces(s):
|
| 240 |
+
out_string = s
|
| 241 |
+
return (
|
| 242 |
+
out_string.replace(" .", ".")
|
| 243 |
+
.replace(" ?", "?")
|
| 244 |
+
.replace(" !", "!")
|
| 245 |
+
.replace(" ,", ",")
|
| 246 |
+
.replace(" ' ", "'")
|
| 247 |
+
.replace(" n't", "n't")
|
| 248 |
+
.replace(" 'm", "'m")
|
| 249 |
+
.replace(" 's", "'s")
|
| 250 |
+
.replace(" 've", "'ve")
|
| 251 |
+
.replace(" 're", "'re")
|
| 252 |
+
.replace("( ", "(")
|
| 253 |
+
.replace(" )", ")")
|
| 254 |
+
.replace(" %", "%")
|
| 255 |
+
.replace("`` ", "\"")
|
| 256 |
+
.replace(" ''", "\"")
|
| 257 |
+
.replace(" :", ":")
|
| 258 |
+
)
|
| 259 |
+
|
| 260 |
+
|
| 261 |
if __name__ == '__main__':
|
| 262 |
from datasets import load_dataset
|
| 263 |
+
|
| 264 |
+
ssfd_debug = load_dataset("mrqa.py", name="squad")
|
| 265 |
x = 5
|