PaddlePaddle/dureader_robust
DureaderRobust is a chinese reading comprehension dataset, designed to evaluate the MRC models from three aspects: over-sensitivity, over-stability and generalization.
6118
1# coding=utf-82# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.3# Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.4#5# Licensed under the Apache License, Version 2.0 (the "License");6# you may not use this file except in compliance with the License.7# You may obtain a copy of the License at8#9# http://www.apache.org/licenses/LICENSE-2.010#11# Unless required by applicable law or agreed to in writing, software12# distributed under the License is distributed on an "AS IS" BASIS,13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14# See the License for the specific language governing permissions and15# limitations under the License.16 17# Lint as: python318 19 20import json21import os22 23import datasets24 25 26logger = datasets.logging.get_logger(__name__)27 28_DESCRIPTION = """\29DureaderRobust is a chinese reading comprehension \30dataset, designed to evaluate the MRC models from \31three aspects: over-sensitivity, over-stability \32and generalization.33"""34 35_URL = "https://bj.bcebos.com/paddlenlp/datasets/dureader_robust-data.tar.gz"36 37 38class DureaderRobustConfig(datasets.BuilderConfig):39 """BuilderConfig for DureaderRobust."""40 41 def __init__(self, **kwargs):42 """BuilderConfig for DureaderRobust.43 44 Args:45 **kwargs: keyword arguments forwarded to super.46 """47 super(DureaderRobustConfig, self).__init__(**kwargs)48 49 50class DureaderRobust(datasets.GeneratorBasedBuilder):51 BUILDER_CONFIGS = [52 DureaderRobustConfig(53 name="plain_text",54 version=datasets.Version("1.0.0", ""),55 description="Plain text",56 ),57 ]58 59 def _info(self):60 return datasets.DatasetInfo(61 description=_DESCRIPTION,62 features=datasets.Features(63 {64 "id": datasets.Value("string"),65 "title": datasets.Value("string"),66 "context": datasets.Value("string"),67 "question": datasets.Value("string"),68 "answers": datasets.features.Sequence(69 {70 "text": datasets.Value("string"),71 "answer_start": datasets.Value("int32"),72 }73 ),74 }75 ),76 # No default supervised_keys (as we have to pass both question77 # and context as input).78 supervised_keys=None,79 homepage="https://arxiv.org/abs/2004.11142",80 )81 82 def _split_generators(self, dl_manager):83 dl_dir = dl_manager.download_and_extract(_URL)84 85 return [86 datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": os.path.join(dl_dir,'dureader_robust-data', 'train.json')}),87 datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": os.path.join(dl_dir,'dureader_robust-data', 'dev.json')}),88 datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": os.path.join(dl_dir,'dureader_robust-data', 'test.json')}),89 ]90 91 def _generate_examples(self, filepath):92 """This function returns the examples in the raw (text) form."""93 logger.info("generating examples from = %s", filepath)94 key = 095 with open(filepath, encoding="utf-8") as f:96 durobust = json.load(f)97 for article in durobust["data"]:98 title = article.get("title", "")99 for paragraph in article["paragraphs"]:100 context = paragraph["context"] # do not strip leading blank spaces GH-2585101 for qa in paragraph["qas"]:102 answer_starts = [answer["answer_start"] for answer in qa.get("answers",'')]103 answers = [answer["text"] for answer in qa.get("answers",'')]104 # Features currently used are "context", "question", and "answers".105 # Others are extracted here for the ease of future expansions.106 yield key, {107 "title": title,108 "context": context,109 "question": qa["question"],110 "id": qa["id"],111 "answers": {112 "answer_start": answer_starts,113 "text": answers,114 },115 }116 key += 1117 