imvladikon commited on
Commit
91738eb
·
1 Parent(s): 334ca2d

Create new file

Browse files
Files changed (1) hide show
  1. paranames.py +87 -0
paranames.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ import csv
4
+ import os
5
+ from functools import partial
6
+ from typing import Dict, Iterable
7
+
8
+ import datasets
9
+ from datasets import DatasetDict, DownloadManager, load_dataset
10
+
11
+ from data.data_loader_base import DataLoaderBase
12
+
13
+ VERSION = datasets.Version("0.0.1")
14
+
15
+ AVAILABLE_DATASETS = {
16
+ 'paranames': "https://github.com/bltlab/paranames/releases/download/v2021.03.04.1/paranames.tsv.gz"
17
+ }
18
+
19
+
20
+ class ParanamesDataset(datasets.GeneratorBasedBuilder, DataLoaderBase):
21
+ """ParanamesDataset dataset."""
22
+
23
+ BUILDER_CONFIGS = [
24
+ datasets.BuilderConfig(
25
+ name=data_name, version=VERSION, description=f"{data_name} dataset"
26
+ )
27
+ for data_name in AVAILABLE_DATASETS
28
+ ]
29
+
30
+ @staticmethod
31
+ def load(data_name_config: str = "paranames") -> DatasetDict:
32
+ ds = load_dataset(__file__, data_name_config)
33
+ return ds
34
+
35
+ def _info(self) -> datasets.DatasetInfo:
36
+ return datasets.DatasetInfo(
37
+ description="",
38
+ features=datasets.Features(
39
+ {
40
+ "wikidata_id": datasets.Value("string"),
41
+ "name": datasets.Value("string"),
42
+ "name_origin": datasets.Value("string"),
43
+ "language": datasets.Value("string"),
44
+ "type": datasets.Value("string"),
45
+ }
46
+ ),
47
+ supervised_keys=None,
48
+ homepage="https://github.com/bltlab/paranames",
49
+ citation="",
50
+ )
51
+
52
+ def _split_generators(
53
+ self, dl_manager: DownloadManager
54
+ ) -> Iterable[datasets.SplitGenerator]:
55
+ downloader = partial(
56
+ lambda split: dl_manager.download_and_extract(
57
+ AVAILABLE_DATASETS[self.config.name]
58
+ )
59
+ )
60
+ filepath = downloader(AVAILABLE_DATASETS[self.config.name])
61
+
62
+ # There is no predefined train/val/test split for this dataset.
63
+ return [
64
+ datasets.SplitGenerator(
65
+ name=datasets.Split.TRAIN,
66
+ gen_kwargs={
67
+ "filepath": filepath,
68
+ },
69
+ ),
70
+ ]
71
+
72
+ def _generate_examples(self, filepath: str) -> Iterable[Dict]:
73
+ with open(filepath, encoding="utf-8") as f_in:
74
+ csv_reader = csv.reader(
75
+ f_in,
76
+ delimiter="\t",
77
+ )
78
+ for idx, row in enumerate(csv_reader):
79
+ if idx == 0:
80
+ continue
81
+ yield idx, {
82
+ "wikidata_id": row[0],
83
+ "name": row[1],
84
+ "name_origin": row[2],
85
+ "language": row[3],
86
+ "type": row[4],
87
+ }