|
import json |
|
from typing import List |
|
|
|
import datasets |
|
|
|
LANGUAGES = ["ar", "de", "es", "fr", "hi", "it", "ja", "pt"] |
|
DATA_PATH = "mintaka_test.json" |
|
|
|
|
|
class MintakaQAConfig(datasets.BuilderConfig): |
|
def __init__(self, language, **kwargs): |
|
super().__init__(**kwargs) |
|
self.language = language |
|
|
|
|
|
class MintakaQA(datasets.GeneratorBasedBuilder): |
|
BUILDER_CONFIG_CLASS = MintakaQAConfig |
|
|
|
BUILDER_CONFIGS = [ |
|
MintakaQAConfig(name=language, language=language) for language in LANGUAGES |
|
] |
|
|
|
def _info(self): |
|
return datasets.DatasetInfo( |
|
description="Mintaka: A Complex, Natural, and Multilingual Dataset for End-to-End Question Answering.", |
|
features=datasets.Features( |
|
{ |
|
"id": datasets.Value("string"), |
|
"question": datasets.Value("string"), |
|
"answer": datasets.Value("string"), |
|
} |
|
), |
|
homepage="https://github.com/amazon-science/mintaka", |
|
citation="https://aclanthology.org/2022.coling-1.138", |
|
) |
|
|
|
def _split_generators( |
|
self, dl_manager: datasets.DownloadManager |
|
) -> List[datasets.SplitGenerator]: |
|
downloaded_file = dl_manager.download_and_extract(DATA_PATH) |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
gen_kwargs={"filepath": downloaded_file} |
|
), |
|
] |
|
|
|
def _generate_examples(self, filepath): |
|
with open(filepath) as f: |
|
data = json.load(f) |
|
id_ = 0 |
|
for example in data: |
|
if example["answer"]["answerType"] == "entity": |
|
question = example["translations"][self.config.language] |
|
answer = ( |
|
example["answer"]["answer"][0]["label"][self.config.language] |
|
if example["answer"]["answer"] |
|
else example["answer"]["mention"] |
|
) |
|
if not answer or not question: |
|
continue |
|
|
|
yield id_, {"id": id_, "question": question, "answer": answer} |
|
|
|
id_ += 1 |
|
|