germandpr / germandpr.py
julianrisch's picture
Update germandpr.py
0113ca5
# coding=utf-8
# Lint as: python3
"""GermanDPR: A German-Language Dataset for Training Dense Passage Retrievers."""
import json
import datasets
logger = datasets.logging.get_logger(__name__)
_CITATION = """
@misc{möller2021germanquad,
title={GermanQuAD and GermanDPR: Improving Non-English Question Answering and Passage Retrieval},
author={Timo Möller and Julian Risch and Malte Pietsch},
year={2021},
eprint={2104.12741},
archivePrefix={arXiv},
primaryClass={cs.CL}
}
"""
_DESCRIPTION = """
We take GermanQuAD as a starting point and add hard negatives from a dump of the full German Wikipedia following the approach of the DPR authors (Karpukhin et al., 2020). The format of the dataset also resembles the one of DPR. GermanDPR comprises 9275 question/answer pairs in the training set and 1025 pairs in the test set. For each pair, there are one positive context and three hard negative contexts.
"""
_URL = "https://germanquad.s3.amazonaws.com/GermanDPR.zip"
class GermanDPRConfig(datasets.BuilderConfig):
"""BuilderConfig for GermanDPR."""
def __init__(self, **kwargs):
"""BuilderConfig for GermanDPR.
Args:
**kwargs: keyword arguments forwarded to super.
"""
super(GermanDPRConfig, self).__init__(**kwargs)
class GermanDPR(datasets.GeneratorBasedBuilder):
"""GermanDPR: A German-Language Dataset for Training Dense Passage Retrievers."""
BUILDER_CONFIGS = [
GermanDPRConfig(
name="plain_text",
version=datasets.Version("1.0.0", ""),
description="Plain text",
),
]
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"question": datasets.Value("string"),
"answers": datasets.features.Sequence(datasets.Value("string")),
"positive_ctxs": datasets.features.Sequence(
{
"title": datasets.Value("string"),
"text": datasets.Value("string"),
"passage_id": datasets.Value("string"),
}
),
"negative_ctxs": datasets.features.Sequence(
{
"title": datasets.Value("string"),
"text": datasets.Value("string"),
"passage_id": datasets.Value("string"),
}
),
"hard_negative_ctxs": datasets.features.Sequence(
{
"title": datasets.Value("string"),
"text": datasets.Value("string"),
"passage_id": datasets.Value("string"),
}
),
}
),
# No default supervised_keys (as we have to pass both question
# and context as input).
supervised_keys=None,
homepage="https://deepset.ai/germanquad",
citation=_CITATION,
)
def _split_generators(self, dl_manager):
dl_dir = dl_manager.download_and_extract(_URL)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
# These kwargs will be passed to _generate_examples
gen_kwargs={"filepath": dl_dir+"/GermanDPR/GermanDPR_train.json"},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
# These kwargs will be passed to _generate_examples
gen_kwargs={"filepath": dl_dir+"/GermanDPR/GermanDPR_test.json"},
),
]
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:
germandpr = json.load(f)
for id, qa in enumerate(germandpr):
yield id, qa