DicoTiar commited on
Commit
9b5b745
1 Parent(s): 7581327

loading script default

Browse files
Files changed (1) hide show
  1. story.py +131 -0
story.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import csv
2
+ import os
3
+
4
+ import datasets
5
+
6
+ _CITATION = """\
7
+ @Dataset{wisdomify:storyteller,
8
+ title = {Korean proverb definitions and examples},
9
+ author={Jongyoon Kim, Yubin Kim, Yongtaek Im
10
+ },
11
+ year={2021}
12
+ }
13
+ """
14
+
15
+ _DESCRIPTION = """\
16
+ This dataset is designed to provide forward and reverse dictionary of Korean proverbs.
17
+ """
18
+
19
+ # TODO: Add a link to an official homepage for the dataset here
20
+ _HOMEPAGE = ""
21
+
22
+ # TODO: Add the licence for the dataset here if you can find it
23
+ _LICENSE = ""
24
+
25
+ # TODO: Add link to the official dataset URLs here
26
+ # If it is dropbox link, you must set 1 for query parameter "dl".
27
+ _URLs = {
28
+ 'definition': "https://www.dropbox.com/s/4uh564afaimtob3/definition.zip?dl=1",
29
+ 'example': "https://www.dropbox.com/s/adlt9n6x5gjs0a6/example.zip?dl=1",
30
+ }
31
+
32
+
33
+ class Story(datasets.GeneratorBasedBuilder):
34
+ # version must be "x.y.z' form
35
+ VERSION = datasets.Version("0.0.0")
36
+
37
+ BUILDER_CONFIGS = [
38
+ datasets.BuilderConfig(name="definition", version=VERSION, description="definition"),
39
+ datasets.BuilderConfig(name="example", version=VERSION, description="example"),
40
+ ]
41
+
42
+ # This config is applied when user load dataset without "name".
43
+ DEFAULT_CONFIG_NAME = "definition"
44
+
45
+ def _info(self):
46
+ # This method specifies the datasets.DatasetInfo object which contains information
47
+ # and typings for the dataset
48
+
49
+ if self.config.name == "definition":
50
+ # These are the features of your dataset like images, labels ...
51
+ features = datasets.Features(
52
+ {
53
+ "wisdom": datasets.Value("string"),
54
+ "def": datasets.Value("string"),
55
+
56
+ }
57
+ )
58
+ elif self.config.name == "example":
59
+ features = datasets.Features(
60
+ {
61
+ "wisdom": datasets.Value("string"),
62
+ "eg": datasets.Value("string"),
63
+ }
64
+ )
65
+ else:
66
+ raise NotImplementedError(f"Wrong name: {self.config.name}")
67
+
68
+ return datasets.DatasetInfo(
69
+ description=_DESCRIPTION,
70
+ features=features,
71
+ supervised_keys=None,
72
+ homepage=_HOMEPAGE,
73
+ license=_LICENSE,
74
+ citation=_CITATION,
75
+ )
76
+
77
+ def _split_generators(self, dl_manager):
78
+ """Returns SplitGenerators."""
79
+ # This method is used when user loads dataset.
80
+ # dl_manager can be used to download and extract the dataset
81
+ # and also can set split depending onf the configuration
82
+
83
+ # Downloading data with _URLs
84
+ downloaded_files = dl_manager.download_and_extract(_URLs[self.config.name])
85
+
86
+ dtp = 'def' if self.config.name == "definition" else 'eg'
87
+
88
+ train_path = os.path.join(downloaded_files, f'train_wisdom2{dtp}.tsv')
89
+ val_path = os.path.join(downloaded_files, f'val_wisdom2{dtp}.tsv')
90
+ test_path = os.path.join(downloaded_files, f'test_wisdom2{dtp}.tsv')
91
+
92
+ return [
93
+ # These gen_kwargs will be passed to _generate_examples
94
+ datasets.SplitGenerator(
95
+ name=datasets.Split.TRAIN,
96
+ gen_kwargs={"filepath": train_path, "split": "train"},
97
+ ),
98
+ datasets.SplitGenerator(
99
+ name=datasets.Split.VALIDATION,
100
+ gen_kwargs={"filepath": val_path, "split": "validation"},
101
+ ),
102
+ datasets.SplitGenerator(
103
+ name=datasets.Split.TEST,
104
+ gen_kwargs={"filepath": test_path, "split": "test"},
105
+ ),
106
+ ]
107
+
108
+ def _generate_examples(self, filepath, split):
109
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
110
+ """ Yields examples as (key, example) tuples. """
111
+ # This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
112
+ # The `key` is here for legacy reason (tfds) and is not important in itself.
113
+
114
+ with open(filepath, encoding="utf-8") as f:
115
+ tsv_reader = csv.reader(f, delimiter="\t")
116
+ for id_, row in enumerate(tsv_reader):
117
+ if id_ == 0:
118
+ continue # first row shows column info
119
+
120
+ if self.config.name == "definition":
121
+ yield id_, {
122
+ "wisdom": row[0],
123
+ "def": row[1],
124
+ }
125
+ elif self.config.name == "example":
126
+ yield id_, {
127
+ "wisdom": row[0],
128
+ "eg": row[1],
129
+ }
130
+ else:
131
+ raise NotImplementedError(f"Wrong name: {self.config.name}")