martingrzzler
commited on
Commit
•
02f9232
1
Parent(s):
0b80811
Upload kanjis2radicals.py
Browse files- kanjis2radicals.py +61 -0
kanjis2radicals.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datasets
|
2 |
+
import json
|
3 |
+
|
4 |
+
_DESCRIPTION = """\
|
5 |
+
Contains Kanji images with corresponding radicals ids from WaniKani or https://api.robanohashi.org/docs/index.html
|
6 |
+
"""
|
7 |
+
|
8 |
+
_METADATA_URL = "https://huggingface.co/datasets/martingrzzler/kanjis2radicals/raw/main/kanji_metadata.jsonl"
|
9 |
+
_IMAGES_URL = "https://huggingface.co/datasets/martingrzzler/kanjis2radicals/resolve/main/kanji_images.tar.gz"
|
10 |
+
|
11 |
+
|
12 |
+
class Kanji2Radicals(datasets.GeneratorBasedBuilder):
|
13 |
+
"""Kanji to radicals dataset."""
|
14 |
+
|
15 |
+
def _info(self):
|
16 |
+
return datasets.DatasetInfo(
|
17 |
+
description=_DESCRIPTION,
|
18 |
+
features=datasets.Features(
|
19 |
+
{
|
20 |
+
"kanji_image": datasets.Image(),
|
21 |
+
"radicals": {
|
22 |
+
"id": datasets.Value("int32"),
|
23 |
+
"characters": datasets.Value("string"),
|
24 |
+
"meanings": datasets.Value("string"),
|
25 |
+
"radicals": datasets.Sequence(
|
26 |
+
{
|
27 |
+
"characters": datasets.Value("string"),
|
28 |
+
"id": datasets.Value("int32"),
|
29 |
+
"slug": datasets.Value("string"),
|
30 |
+
}
|
31 |
+
),
|
32 |
+
},
|
33 |
+
}
|
34 |
+
),
|
35 |
+
supervised_keys=None,
|
36 |
+
homepage="https://robanohashi.org/",
|
37 |
+
)
|
38 |
+
|
39 |
+
def _split_generators(self, dl_manager):
|
40 |
+
metadata_path = dl_manager.download_and_extract(_METADATA_URL)
|
41 |
+
images_path = dl_manager.download_and_extract(_IMAGES_URL)
|
42 |
+
images_iter = dl_manager.iter_archive(images_path)
|
43 |
+
|
44 |
+
return [
|
45 |
+
datasets.SplitGenerator(
|
46 |
+
name=datasets.Split.TRAIN,
|
47 |
+
gen_kwargs={
|
48 |
+
"metadata_path": metadata_path,
|
49 |
+
"images_iter": images_iter,
|
50 |
+
},
|
51 |
+
),
|
52 |
+
]
|
53 |
+
|
54 |
+
def _generate_examples(self, metadata_path, images_iter):
|
55 |
+
with open(metadata_path, encoding="utf-8") as f:
|
56 |
+
for line in f:
|
57 |
+
metadata = json.loads(line)
|
58 |
+
yield metadata["id"], {
|
59 |
+
"kanji_image": next(images_iter).read(),
|
60 |
+
"radicals": metadata,
|
61 |
+
}
|