|
|
|
|
|
import csv |
|
import os |
|
from functools import partial |
|
from typing import Dict, Iterable |
|
|
|
import datasets |
|
from datasets import DatasetDict, DownloadManager, load_dataset |
|
|
|
|
|
|
|
VERSION = datasets.Version("0.0.1") |
|
|
|
AVAILABLE_DATASETS = { |
|
'paranames': "https://github.com/bltlab/paranames/releases/download/v2021.03.04.1/paranames.tsv.gz" |
|
} |
|
|
|
|
|
class ParanamesDataset(datasets.GeneratorBasedBuilder): |
|
"""ParanamesDataset dataset.""" |
|
|
|
BUILDER_CONFIGS = [ |
|
datasets.BuilderConfig( |
|
name=data_name, version=VERSION, description=f"{data_name} dataset" |
|
) |
|
for data_name in AVAILABLE_DATASETS |
|
] |
|
|
|
|
|
def _info(self) -> datasets.DatasetInfo: |
|
return datasets.DatasetInfo( |
|
description="", |
|
features=datasets.Features( |
|
{ |
|
"wikidata_id": datasets.Value("string"), |
|
"name": datasets.Value("string"), |
|
"name_origin": datasets.Value("string"), |
|
"language": datasets.Value("string"), |
|
"type": datasets.Value("string"), |
|
} |
|
), |
|
supervised_keys=None, |
|
homepage="https://github.com/bltlab/paranames", |
|
citation="", |
|
) |
|
|
|
def _split_generators( |
|
self, dl_manager: DownloadManager |
|
) -> Iterable[datasets.SplitGenerator]: |
|
downloader = partial( |
|
lambda split: dl_manager.download_and_extract( |
|
AVAILABLE_DATASETS[self.config.name] |
|
) |
|
) |
|
filepath = downloader(AVAILABLE_DATASETS[self.config.name]) |
|
|
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
gen_kwargs={ |
|
"filepath": filepath, |
|
}, |
|
), |
|
] |
|
|
|
def _generate_examples(self, filepath: str) -> Iterable[Dict]: |
|
with open(filepath, encoding="utf-8") as f_in: |
|
csv_reader = csv.reader( |
|
f_in, |
|
delimiter="\t", |
|
) |
|
for idx, row in enumerate(csv_reader): |
|
if idx == 0: |
|
continue |
|
yield idx, { |
|
"wikidata_id": row[0], |
|
"name": row[1], |
|
"name_origin": row[2], |
|
"language": row[3], |
|
"type": row[4], |
|
} |