story / story.py
DicoTiar
loading script default
9b5b745
import csv
import os
import datasets
_CITATION = """\
@Dataset{wisdomify:storyteller,
title = {Korean proverb definitions and examples},
author={Jongyoon Kim, Yubin Kim, Yongtaek Im
},
year={2021}
}
"""
_DESCRIPTION = """\
This dataset is designed to provide forward and reverse dictionary of Korean proverbs.
"""
# TODO: Add a link to an official homepage for the dataset here
_HOMEPAGE = ""
# TODO: Add the licence for the dataset here if you can find it
_LICENSE = ""
# TODO: Add link to the official dataset URLs here
# If it is dropbox link, you must set 1 for query parameter "dl".
_URLs = {
'definition': "https://www.dropbox.com/s/4uh564afaimtob3/definition.zip?dl=1",
'example': "https://www.dropbox.com/s/adlt9n6x5gjs0a6/example.zip?dl=1",
}
class Story(datasets.GeneratorBasedBuilder):
# version must be "x.y.z' form
VERSION = datasets.Version("0.0.0")
BUILDER_CONFIGS = [
datasets.BuilderConfig(name="definition", version=VERSION, description="definition"),
datasets.BuilderConfig(name="example", version=VERSION, description="example"),
]
# This config is applied when user load dataset without "name".
DEFAULT_CONFIG_NAME = "definition"
def _info(self):
# This method specifies the datasets.DatasetInfo object which contains information
# and typings for the dataset
if self.config.name == "definition":
# These are the features of your dataset like images, labels ...
features = datasets.Features(
{
"wisdom": datasets.Value("string"),
"def": datasets.Value("string"),
}
)
elif self.config.name == "example":
features = datasets.Features(
{
"wisdom": datasets.Value("string"),
"eg": datasets.Value("string"),
}
)
else:
raise NotImplementedError(f"Wrong name: {self.config.name}")
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
supervised_keys=None,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
# This method is used when user loads dataset.
# dl_manager can be used to download and extract the dataset
# and also can set split depending onf the configuration
# Downloading data with _URLs
downloaded_files = dl_manager.download_and_extract(_URLs[self.config.name])
dtp = 'def' if self.config.name == "definition" else 'eg'
train_path = os.path.join(downloaded_files, f'train_wisdom2{dtp}.tsv')
val_path = os.path.join(downloaded_files, f'val_wisdom2{dtp}.tsv')
test_path = os.path.join(downloaded_files, f'test_wisdom2{dtp}.tsv')
return [
# These gen_kwargs will be passed to _generate_examples
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={"filepath": train_path, "split": "train"},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={"filepath": val_path, "split": "validation"},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={"filepath": test_path, "split": "test"},
),
]
def _generate_examples(self, filepath, split):
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
""" Yields examples as (key, example) tuples. """
# This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
# The `key` is here for legacy reason (tfds) and is not important in itself.
with open(filepath, encoding="utf-8") as f:
tsv_reader = csv.reader(f, delimiter="\t")
for id_, row in enumerate(tsv_reader):
if id_ == 0:
continue # first row shows column info
if self.config.name == "definition":
yield id_, {
"wisdom": row[0],
"def": row[1],
}
elif self.config.name == "example":
yield id_, {
"wisdom": row[0],
"eg": row[1],
}
else:
raise NotImplementedError(f"Wrong name: {self.config.name}")