import json import datasets from datasets.tasks import QuestionAnsweringExtractive _CITATION = """""" _DESCRIPTION = """\ Manually generated dataset for policies qa """ _URLS = { "train": "./data/train.json", "test": "./data/test.json" } class PoliciesQAConfig(datasets.BuilderConfig): """BuilderConfig for Ineract Policies.""" def __init__(self, **kwargs): """BuilderConfig for Ineract Policies. Args: **kwargs: keyword arguments forwarded to super. """ super(PoliciesQAConfig, self).__init__(**kwargs) class PoliciesQA(datasets.GeneratorBasedBuilder): """Ineract Policies: The Policy Question Answering Dataset. Version 0.1""" BUILDER_CONFIGS = [ PoliciesQAConfig( name="plain_text", version=datasets.Version("1.0.0", ""), description="Plain text", ), ] DEFAULT_CONFIG_NAME = "plain_text" def _info(self): return datasets.DatasetInfo( description=_DESCRIPTION, features=datasets.Features( { "id": datasets.Value("string"), "context": datasets.Value("string"), "question": datasets.Value("string"), "answers": datasets.features.Sequence( { "text": datasets.Value("string"), "answer_start": datasets.Value("int32"), } ), } ), # No default supervised_keys (as we have to pass both question # and context as input). supervised_keys=None, homepage="ineract.com", task_templates=[ QuestionAnsweringExtractive( question_column="question", context_column="context", answers_column="answers" ) ], citation=_CITATION, ) def _split_generators(self, dl_manager): downloaded_files = dl_manager.download_and_extract(_URLS) return [ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={ "filepath": downloaded_files["train"], "split": "train"}), datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={ "filepath": downloaded_files["test"], "split": "test"}) ] def _generate_examples(self, filepath, split): """This function returns the examples in the raw (text) form.""" key = 0 with open(filepath, encoding="utf-8") as f: policies = json.load(f) for policy in policies["data"]: id = policy["id"] context = policy["context"] question = policy["question"] answer_starts = [answer_start for answer_start in policy["answers"]["answer_start"]] answers = [ answer_text for answer_text in policy["answers"]["text"]] yield key, { "id": id, "context": context, "question": question, "answers": { "answer_start": answer_starts, "text": answers, }, } key += 1