Datasets:
Create modern_haiku.py
Browse files- modern_haiku.py +120 -0
modern_haiku.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import datasets as ds
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
_SUBSET_NAMES = ["all", "spring", "summer", "autumn", "winter", "none", "kigo"]
|
| 6 |
+
|
| 7 |
+
_COMMON_FEATURES = {
|
| 8 |
+
"id": ds.Value("int64"),
|
| 9 |
+
"haiku": ds.Value("string"),
|
| 10 |
+
"author": ds.Value("string"),
|
| 11 |
+
"foreword": ds.Value("string"),
|
| 12 |
+
"source": ds.Value("string"),
|
| 13 |
+
"comment": ds.Value("string"),
|
| 14 |
+
"reviewer": ds.Value("string"),
|
| 15 |
+
"note": ds.Value("string"),
|
| 16 |
+
}
|
| 17 |
+
_KIGO_FEATURES = {
|
| 18 |
+
"id": ds.Value("int64"),
|
| 19 |
+
"word": ds.Value("string"),
|
| 20 |
+
"kana": ds.Value("string"),
|
| 21 |
+
"old_kana": ds.Value("string"),
|
| 22 |
+
"season": ds.Value("string"),
|
| 23 |
+
"subtitle": ds.Sequence(ds.Value("string")),
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
# TODO: Add description of the dataset here
|
| 27 |
+
# You can copy an official description
|
| 28 |
+
_DESCRIPTION = """\
|
| 29 |
+
This new dataset is designed to solve this great NLP task and is crafted with a lot of care.
|
| 30 |
+
"""
|
| 31 |
+
|
| 32 |
+
# TODO: Add a link to an official homepage for the dataset here
|
| 33 |
+
_HOMEPAGE = ""
|
| 34 |
+
|
| 35 |
+
# TODO: Add the licence for the dataset here if you can find it
|
| 36 |
+
_LICENSE = ""
|
| 37 |
+
|
| 38 |
+
_FEATURES = {
|
| 39 |
+
"all": ds.Features(
|
| 40 |
+
{**_COMMON_FEATURES, "season": ds.Value("string"), "kigo": _KIGO_FEATURES}
|
| 41 |
+
),
|
| 42 |
+
"spring": ds.Features({**_COMMON_FEATURES, "kigo": _KIGO_FEATURES}),
|
| 43 |
+
"summer": ds.Features({**_COMMON_FEATURES, "kigo": _KIGO_FEATURES}),
|
| 44 |
+
"autumn": ds.Features({**_COMMON_FEATURES, "kigo": _KIGO_FEATURES}),
|
| 45 |
+
"winter": ds.Features({**_COMMON_FEATURES, "kigo": _KIGO_FEATURES}),
|
| 46 |
+
"none": ds.Features(_COMMON_FEATURES),
|
| 47 |
+
"kigo": ds.Features(_KIGO_FEATURES),
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
_DATA_URL = "https://pub-6dee886ee0a5425c8fb25fe18f3acc73.r2.dev/public/datasets/modern_haiku/data.json"
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
class ModernHaikuDataset(ds.GeneratorBasedBuilder):
|
| 54 |
+
VERSION = ds.Version("0.0.1")
|
| 55 |
+
|
| 56 |
+
BUILDER_CONFIGS = [ds.BuilderConfig(name=subset) for subset in _SUBSET_NAMES]
|
| 57 |
+
DEFAULT_CONFIG_NAME = "all"
|
| 58 |
+
|
| 59 |
+
def _info(self):
|
| 60 |
+
return ds.DatasetInfo(
|
| 61 |
+
description=_DESCRIPTION,
|
| 62 |
+
features=_FEATURES.get(self.config.name),
|
| 63 |
+
homepage=_HOMEPAGE,
|
| 64 |
+
license=_LICENSE,
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
def _split_generators(self, dl_manager):
|
| 68 |
+
data_dir = dl_manager.download(_DATA_URL)
|
| 69 |
+
return [
|
| 70 |
+
ds.SplitGenerator(
|
| 71 |
+
name=ds.Split.TRAIN,
|
| 72 |
+
gen_kwargs={
|
| 73 |
+
"filepath": data_dir,
|
| 74 |
+
},
|
| 75 |
+
),
|
| 76 |
+
]
|
| 77 |
+
|
| 78 |
+
def _generate_examples(self, filepath):
|
| 79 |
+
with open(filepath, "r", encoding="utf-8") as f:
|
| 80 |
+
data: list[dict] = json.load(f)
|
| 81 |
+
|
| 82 |
+
key = 0
|
| 83 |
+
|
| 84 |
+
if self.config.name == "all":
|
| 85 |
+
for row in data:
|
| 86 |
+
yield key, row
|
| 87 |
+
key += 1
|
| 88 |
+
|
| 89 |
+
elif self.config.name == "none":
|
| 90 |
+
data = [row for row in data if row["season"] == self.config.name]
|
| 91 |
+
for row in data:
|
| 92 |
+
row.pop("season")
|
| 93 |
+
row.pop("kigo")
|
| 94 |
+
|
| 95 |
+
yield key, row
|
| 96 |
+
key += 1
|
| 97 |
+
|
| 98 |
+
elif self.config.name != "kigo":
|
| 99 |
+
data = [row for row in data if row["season"] == self.config.name]
|
| 100 |
+
for row in data:
|
| 101 |
+
row.pop("season")
|
| 102 |
+
|
| 103 |
+
yield key, row
|
| 104 |
+
key += 1
|
| 105 |
+
|
| 106 |
+
else:
|
| 107 |
+
all_kigo = {}
|
| 108 |
+
for row in data:
|
| 109 |
+
if row["kigo"] is None:
|
| 110 |
+
continue
|
| 111 |
+
kigo = row["kigo"]
|
| 112 |
+
id = kigo["id"]
|
| 113 |
+
if all_kigo.get(id) is None:
|
| 114 |
+
all_kigo[id] = kigo
|
| 115 |
+
else:
|
| 116 |
+
continue
|
| 117 |
+
|
| 118 |
+
for id, kigo in all_kigo.items():
|
| 119 |
+
yield key, kigo
|
| 120 |
+
key += 1
|