File size: 2,727 Bytes
91738eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import csv
import os
from functools import partial
from typing import Dict, Iterable

import datasets
from datasets import DatasetDict, DownloadManager, load_dataset

from data.data_loader_base import DataLoaderBase

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, DataLoaderBase):
    """ParanamesDataset dataset."""

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name=data_name, version=VERSION, description=f"{data_name} dataset"
        )
        for data_name in AVAILABLE_DATASETS
    ]

    @staticmethod
    def load(data_name_config: str = "paranames") -> DatasetDict:
        ds = load_dataset(__file__, data_name_config)
        return ds

    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])

        # There is no predefined train/val/test split for this dataset.
        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],
                }