common-sense-mrc / common-sense-mrc.py
jinmang2's picture
publish version 0.0.0
cea7647
import os
import pandas as pd
import datasets
_DATA_URLS = {
"train": "./ko_wiki_v1_squad.json",
}
_VERSION = "0.0.0"
_LICENSE = ""
_CITATION = ""
_HOMEPAGE = ""
_DESCRIPTION = ""
class CommonSenseMRCConfig(datasets.BuilderConfig):
def __init__(self, data_url, features, **kwargs):
super().__init__(version=datasets.Version(_VERSION, ""), **kwargs)
self.features = features
self.data_url = data_url
class CommonSenseMRC(datasets.GeneratorBasedBuilder):
DEFAULT_CONFIG_NAME = "common_sense"
BUILDER_CONFIGS = [
CommonSenseMRCConfig(
name="common_sense",
data_url=_DATA_URLS,
features=datasets.Features(
{
"answers": datasets.Sequence(
feature={
"text": datasets.Value(dtype="string"),
"answer_start": datasets.Value(dtype="int32"),
},
),
"context": datasets.Value(dtype="string"),
"guid": datasets.Value(dtype="string"),
"question": datasets.Value(dtype="string"),
"title": datasets.Value(dtype="string"),
}
)
),
]
def _info(self):
return datasets.DatasetInfo(
features=self.config.features,
description=_DESCRIPTION,
homepage=_HOMEPAGE,
citation=_CITATION,
license=_LICENSE,
)
def _split_generators(self, dl_manager):
data_path = dl_manager.download_and_extract(self.config.data_url)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
# These kwargs will be passed to _generate_examples
gen_kwargs={"data_file": data_path["train"]}
),
]
def _generate_examples(self, data_file: str):
idx = 0
common_sense_mrc = pd.read_json(data_file)
for example in common_sense_mrc["data"].tolist():
paragraphs = example["paragraphs"]
title = example["title"]
for paragraph in paragraphs:
qas = paragraph["qas"]
context = paragraph["context"]
for qa in qas:
text = [answers["text"] for answers in qa["answers"]]
answer_start = [answers["answer_start"] for answers in qa["answers"]]
features = {
"guid": str(qa["id"]),
"question": qa["question"],
"answers": {"text": text, "answer_start": answer_start},
"context": context,
"title": title,
}
yield idx, features
idx += 1