HomeAppliancesDataset / HomeAppliancesDataset.py
TunahanGokcimen's picture
Create HomeAppliancesDataset.py
51883bf
"""QnAData Question Answering Dataset"""
import json
import datasets
from datasets.tasks import QuestionAnsweringExtractive
logger = datasets.logging.get_logger(__name__)
_CITATION = """\
a
"""
_DESCRIPTION = """\
a
"""
_URL = "https://raw.githubusercontent.com/Gokcimen/Home_Appliance_Dataset/master/"
_URLS = {
"train": _URL + "train.json",
"test": _URL + "test.json",
"dev": _URL + "dev.json",
}
class QnADataConfig(datasets.BuilderConfig):
"""BuilderConfig for QnAData."""
def __init__(self, **kwargs):
"""BuilderConfig for QnAData.
Args:
**kwargs: keyword arguments forwarded to super.
"""
super(QnADataConfig, self).__init__(**kwargs)
class QnAData(datasets.GeneratorBasedBuilder):
"""The QnAData Question Answering Dataset. Version 1.0."""
BUILDER_CONFIGS = [
QnADataConfig(
name="plain_text",
version=datasets.Version("1.0.0"),
description="Plain text",
),
]
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"id": datasets.Value("string"),
"title": datasets.Value("string"),
"context": datasets.Value("string"),
"question": datasets.Value("string"),
"answers": datasets.features.Sequence(
{
"text": datasets.Value("string"),
"answer_start": datasets.Value("int32"),
"answer_end": datasets.Value("int32"),
}
),
}
),
# No default supervised_keys (as we have to pass both question
# and context as input).
supervised_keys=None,
homepage="https://raw.githubusercontent.com/Gokcimen/Home_Appliance_Dataset/master/train.json",
citation=_CITATION,
task_templates=[
QuestionAnsweringExtractive(
question_column="question", context_column="context", answers_column="answers"
)
],
)
def _split_generators(self, dl_manager):
urls_to_download = _URLS
downloaded_files = dl_manager.download_and_extract(urls_to_download)
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloaded_files["test"]}),
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["dev"]}),
]
def _generate_examples(self, filepath):
"""This function returns the examples in the raw (text) form."""
logger.info("generating examples from = %s", filepath)
with open(filepath, encoding="utf-8") as f:
dataset = json.load(f)
for article in dataset["data"]:
title = article.get("title", "").strip()
for paragraph in article["paragraphs"]:
context = paragraph["context"].strip()
for qa in paragraph["qas"]:
question = qa["question"].strip()
id_ = qa["id"]
answer_starts = [answer["answer_start"] for answer in qa["answers"]]
answer_end = [answer["answer_end"] for answer in qa["answers"]]
answers = [answer["text"].strip() for answer in qa["answers"]]
# Features currently used are "context", "question", and "answers".
# Others are extracted here for the ease of future expansions.
yield id_, {
"title": title,
"context": context,
"question": question,
"id": id_,
"answers": {
"answer_start": answer_starts,
"answer_end": answer_end,
"text": answers,
},
}