File size: 10,882 Bytes
76afc16 5f30172 76afc16 5f30172 76afc16 277a6f4 76afc16 ac762f8 76afc16 959d23e 0592565 76afc16 5f30172 76afc16 5f30172 76afc16 5f30172 76afc16 959d23e 5f30172 76afc16 a369e64 76afc16 959d23e 76afc16 5f30172 76afc16 5f30172 76afc16 |
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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# coding=utf-8
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""MRQA 2019 Shared task dataset."""
import json
import datasets
_CITATION = """\
@inproceedings{fisch2019mrqa,
title={{MRQA} 2019 Shared Task: Evaluating Generalization in Reading Comprehension},
author={Adam Fisch and Alon Talmor and Robin Jia and Minjoon Seo and Eunsol Choi and Danqi Chen},
booktitle={Proceedings of 2nd Machine Reading for Reading Comprehension (MRQA) Workshop at EMNLP},
year={2019},
}
"""
_DESCRIPTION = """\
The MRQA 2019 Shared Task focuses on generalization in question answering.
An effective question answering system should do more than merely
interpolate from the training set to answer test examples drawn
from the same distribution: it should also be able to extrapolate
to out-of-distribution examples — a significantly harder challenge.
The dataset is a collection of 18 existing QA dataset (carefully selected
subset of them) and converted to the same format (SQuAD format). Among
these 18 datasets, six datasets were made available for training,
six datasets were made available for development, and the final six
for testing. The dataset is released as part of the MRQA 2019 Shared Task.
"""
_HOMEPAGE = "https://mrqa.github.io/2019/shared.html"
_LICENSE = "Unknwon"
_URLs = {
# Train sub-datasets
"train+SQuAD": "https://s3.us-east-2.amazonaws.com/mrqa/release/v2/train/SQuAD.jsonl.gz",
"train+NewsQA": "https://s3.us-east-2.amazonaws.com/mrqa/release/v2/train/NewsQA.jsonl.gz",
"train+TriviaQA": "https://s3.us-east-2.amazonaws.com/mrqa/release/v2/train/TriviaQA-web.jsonl.gz",
"train+SearchQA": "https://s3.us-east-2.amazonaws.com/mrqa/release/v2/train/SearchQA.jsonl.gz",
"train+HotpotQA": "https://s3.us-east-2.amazonaws.com/mrqa/release/v2/train/HotpotQA.jsonl.gz",
"train+NaturalQuestions": "https://s3.us-east-2.amazonaws.com/mrqa/release/v2/train/NaturalQuestionsShort.jsonl.gz",
# Validation sub-datasets
"validation+SQuAD": "https://s3.us-east-2.amazonaws.com/mrqa/release/v2/dev/SQuAD.jsonl.gz",
"validation+NewsQA": "https://s3.us-east-2.amazonaws.com/mrqa/release/v2/dev/NewsQA.jsonl.gz",
"validation+TriviaQA": "https://s3.us-east-2.amazonaws.com/mrqa/release/v2/dev/TriviaQA-web.jsonl.gz",
"validation+SearchQA": "https://s3.us-east-2.amazonaws.com/mrqa/release/v2/dev/SearchQA.jsonl.gz",
"validation+HotpotQA": "https://s3.us-east-2.amazonaws.com/mrqa/release/v2/dev/HotpotQA.jsonl.gz",
"validation+NaturalQuestions": "https://s3.us-east-2.amazonaws.com/mrqa/release/v2/dev/NaturalQuestionsShort.jsonl.gz",
# Test sub-datasets
"test+BioASQ": "http://participants-area.bioasq.org/MRQA2019/", # BioASQ.jsonl.gz
"test+DROP": "https://s3.us-east-2.amazonaws.com/mrqa/release/v2/dev/DROP.jsonl.gz",
"test+DuoRC": "https://s3.us-east-2.amazonaws.com/mrqa/release/v2/dev/DuoRC.ParaphraseRC.jsonl.gz",
"test+RACE": "https://s3.us-east-2.amazonaws.com/mrqa/release/v2/dev/RACE.jsonl.gz",
"test+RelationExtraction": "https://s3.us-east-2.amazonaws.com/mrqa/release/v2/dev/RelationExtraction.jsonl.gz",
"test+TextbookQA": "https://s3.us-east-2.amazonaws.com/mrqa/release/v2/dev/TextbookQA.jsonl.gz",
}
class MRQAConfig(datasets.BuilderConfig):
"""BuilderConfig for FS."""
def __init__(self, data_url, **kwargs):
"""BuilderConfig for FS.
Args:
additional_features: `list[string]`, list of the features that will appear in the feature dict
additionally to the self.id_key, self.source_key and self.target_key. Should not include "label".
data_url: `string`, url to download the zip file from.
citation: `string`, citation for the data set.
url: `string`, url for information about the data set.
label_classes: `list[string]`, the list of classes for the label if the
label is present as a string. Non-string labels will be cast to either
'False' or 'True'.
**kwargs: keyword arguments forwarded to super.
"""
super(MRQAConfig, self).__init__(version=datasets.Version("1.0.0"), **kwargs)
self.data_url = data_url
class MRQA(datasets.GeneratorBasedBuilder):
"""MRQA 2019 Shared task dataset."""
VERSION = datasets.Version("1.1.0")
BUILDER_CONFIGS = [
MRQAConfig(
name="searchqa",
data_url={"validation": _URLs["validation+SearchQA"],
"train": _URLs["train+SearchQA"],
"test": _URLs["validation+SearchQA"]}
),
MRQAConfig(
name="squad",
data_url={"validation": _URLs["validation+SQuAD"],
"train": _URLs["train+SQuAD"],
"test": _URLs["validation+SQuAD"]}
),
MRQAConfig(
name="newsqa",
data_url={"validation": _URLs["validation+NewsQA"],
"train": _URLs["train+NewsQA"],
"test": _URLs["validation+NewsQA"]}
),
MRQAConfig(
name="natural_questions",
data_url={"validation": _URLs["validation+NaturalQuestions"],
"train": _URLs["train+NaturalQuestions"],
"test": _URLs["validation+NaturalQuestions"]}
),
MRQAConfig(
name="hotpotqa",
data_url={"validation": _URLs["validation+HotpotQA"],
"train": _URLs["train+HotpotQA"],
"test": _URLs["validation+HotpotQA"]}
),
MRQAConfig(
name="triviaqa",
data_url={"validation": _URLs["validation+TriviaQA"],
"train": _URLs["train+TriviaQA"],
"test": _URLs["validation+TriviaQA"]}
),
]
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
# Format is derived from https://github.com/mrqa/MRQA-Shared-Task-2019#mrqa-format
features=datasets.Features(
{
"subset": datasets.Value("string"),
"context": datasets.Value("string"),
"qid": datasets.Value("string"),
"idx": datasets.Value("int32"),
"question": datasets.Value("string"),
"answers": datasets.Sequence(datasets.Value("string")),
"answer": datasets.Value("string"),
}
),
supervised_keys=None,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
data_dir = dl_manager.download_and_extract(self.config.data_url)
return [
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={
"filepaths_dict": data_dir,
"split": "validation",
},
),
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"filepaths_dict": data_dir,
"split": "train",
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"filepaths_dict": data_dir,
"split": "test",
},
),
]
def _generate_examples(self, filepaths_dict, split):
"""Yields examples."""
for source, filepath in filepaths_dict.items():
if source not in split:
continue
with open(filepath, encoding="utf-8") as f:
header = next(f)
subset = json.loads(header)["header"]["dataset"]
idx = 0
for row in f:
paragraph = json.loads(row)
context = clean_context(paragraph["context"])
for qa in paragraph["qas"]:
qid = qa["qid"]
question = qa["question"].strip()
if question[-1] != "?":
question += "?"
answers = [clean_up_spaces(a) for a in qa["answers"]]
final_row = {
"subset": subset,
"context": clean_up_spaces(context),
"qid": qid,
"idx": idx,
"question": clean_up_spaces(question),
"answers": answers,
"answer": answers[0]
}
idx += 1
yield f"{source}_{qid}", final_row
def clean_context(context):
return (
context.replace("[PAR] ", "\n\n")
.replace("[TLE]", "Title:")
.replace("[SEP]", "\nPassage:").strip()
.replace("<Li>", "")
.replace("</Li>", "")
.replace("<OI>", "")
.replace("</OI>", "")
.replace("<Ol>", "")
.replace("</Ol>", "")
.replace("<Dd>", "")
.replace("</Dd>", "")
.replace("<UI>", "")
.replace("</UI>", "")
.replace("<Ul>", "")
.replace("</Ul>", "")
.replace("<P>", "")
.replace("</P>", "")
.replace("[DOC]", "")
).strip()
def clean_up_spaces(s):
out_string = s
return (
out_string.replace(" .", ".")
.replace(" ?", "?")
.replace(" !", "!")
.replace(" ,", ",")
.replace(" ' ", "'")
.replace(" n't", "n't")
.replace(" 'm", "'m")
.replace(" 's", "'s")
.replace(" 've", "'ve")
.replace(" 're", "'re")
.replace("( ", "(")
.replace(" )", ")")
.replace(" %", "%")
.replace("`` ", "\"")
.replace(" ''", "\"")
.replace(" :", ":")
)
if __name__ == '__main__':
from datasets import load_dataset
ssfd_debug = load_dataset("mrqa.py", name="squad")
x = 5
|