File size: 2,607 Bytes
02f9232 5c03281 02f9232 ec49feb 02f9232 d0ece79 5c03281 02f9232 d0ece79 48949cc 9e6a0f5 d0ece79 48949cc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import datasets
import os
import json
_DESCRIPTION = """\
Contains Kanji images with corresponding radicals ids from WaniKani or https://api.robanohashi.org/docs/index.html
"""
_METADATA_URL = "https://huggingface.co/datasets/martingrzzler/kanjis2radicals/raw/main/kanji_metadata.jsonl"
_IMAGES_URL = "https://huggingface.co/datasets/martingrzzler/kanjis2radicals/resolve/main/kanji.tar.gz"
class Kanji2Radicals(datasets.GeneratorBasedBuilder):
"""Kanji to radicals dataset."""
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"kanji_image": datasets.Image(),
"radicals": {
"id": datasets.Value("int32"),
"characters": datasets.Value("string"),
"meanings": datasets.Value("string"),
"radicals": datasets.Sequence(
{
"characters": datasets.Value("string"),
"id": datasets.Value("int32"),
"slug": datasets.Value("string"),
}
),
},
}
),
supervised_keys=None,
homepage="https://robanohashi.org/",
)
def _split_generators(self, dl_manager):
metadata_path = dl_manager.download_and_extract(_METADATA_URL)
images_path = dl_manager.download_and_extract(_IMAGES_URL)
images_iter = dl_manager.iter_archive(images_path)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"metadata_path": metadata_path,
"images_iter": images_iter,
},
),
]
def _generate_examples(self, metadata_path, images_iter):
radicals = []
with open(metadata_path, encoding="utf-8") as f:
for line in f:
metadata = json.loads(line)
radicals.append(metadata)
for idx, (image_path, image) in enumerate(images_iter):
print(f"Processing image {idx}...")
print(image_path)
if not os.path.isdir(image_path): # Add this check
print("Skipping")
print(image_path)
continue
yield image_path, {
"radicals": radicals[idx],
"kanji_image": image.read(),
}
|