|
import csv |
|
import json |
|
import os |
|
from typing import Any |
|
|
|
import datasets |
|
from datasets.utils import logging |
|
|
|
|
|
_DESCRIPTION = """\ |
|
HuggingFace wrapper for https://github.com/askplatypus/wikidata-simplequestions dataset |
|
Simplequestions dataset based on Wikidata. |
|
""" |
|
|
|
|
|
_HOMEPAGE = "" |
|
|
|
|
|
_LICENSE = "" |
|
|
|
_LANGS = [ |
|
"ru", |
|
"en", |
|
] |
|
|
|
_URL = "https://raw.githubusercontent.com/askplatypus/wikidata-simplequestions/master/" |
|
_DATA_DIRECTORY = "./simplequestion" |
|
VERSION = datasets.Version("0.0.1") |
|
|
|
|
|
class WikidataSimpleQuestionsConfig(datasets.BuilderConfig): |
|
"""BuilderConfig for WikidataSimpleQuestions.""" |
|
|
|
def __init__(self, **kwargs): |
|
"""BuilderConfig for WikidataSimpleQuestions. |
|
Args: |
|
**kwargs: keyword arguments forwarded to super. |
|
""" |
|
super(WikidataSimpleQuestionsConfig, self).__init__(**kwargs) |
|
|
|
|
|
class WikidataSimpleQuestions(datasets.GeneratorBasedBuilder): |
|
"""HuggingFace wrapper for https://github.com/askplatypus/wikidata-simplequestions dataset""" |
|
|
|
BUILDER_CONFIG_CLASS = WikidataSimpleQuestionsConfig |
|
BUILDER_CONFIGS = [] |
|
BUILDER_CONFIGS += [ |
|
WikidataSimpleQuestionsConfig( |
|
name=f"main_{ln}", |
|
version=VERSION, |
|
description="main version of wikidata simplequestions", |
|
) |
|
for ln in _LANGS |
|
] |
|
BUILDER_CONFIGS += [ |
|
WikidataSimpleQuestionsConfig( |
|
name=f"answerable_{ln}", |
|
version=VERSION, |
|
description="answerable version of wikidata simplequestions", |
|
) |
|
for ln in _LANGS |
|
] |
|
|
|
DEFAULT_CONFIG_NAME = "answerable_en" |
|
|
|
def _info(self): |
|
features = datasets.Features( |
|
{ |
|
"subject": datasets.Value("string"), |
|
"property": datasets.Value("string"), |
|
"object": datasets.Value("string"), |
|
"question": datasets.Value("string"), |
|
} |
|
) |
|
|
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=features, |
|
homepage=_HOMEPAGE, |
|
license=_LICENSE, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
if self.config.name == "default": |
|
version, lang = "main", "en" |
|
else: |
|
version, lang = self.config.name.split("_") |
|
|
|
if version == "main": |
|
version = "" |
|
else: |
|
version = "_" + version |
|
|
|
data_dir = os.path.join(self.base_path, _DATA_DIRECTORY) |
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
gen_kwargs={ |
|
"filepath": os.path.join(data_dir, f"annotated_wd_data_train{version}_{lang}.txt"), |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.VALIDATION, |
|
gen_kwargs={ |
|
"filepath": os.path.join(data_dir, f"annotated_wd_data_valid{version}_{lang}.txt"), |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
gen_kwargs={ |
|
"filepath": os.path.join(data_dir, f"annotated_wd_data_test{version}_{lang}.txt"), |
|
}, |
|
), |
|
] |
|
|
|
def _generate_examples(self, filepath): |
|
with open(filepath, encoding="utf-8") as f: |
|
for key, row in enumerate(f): |
|
data = row.split("\t") |
|
yield ( |
|
key, |
|
{ |
|
"subject": data[0], |
|
"property": data[1], |
|
"object": data[2], |
|
"question": data[3], |
|
}, |
|
) |
|
|