File size: 2,788 Bytes
21a91b1 |
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 |
from datasets import DatasetBuilder, SplitGenerator, Split, Features, Value, Sequence, BuilderConfig, GeneratorBasedBuilder
import datasets
from datasets.utils.download_manager import DownloadManager
from typing import List, Any, Tuple
import json
import os
# Mapping for song_type and group_type
song_type_mapping = {
1: "presentación",
2: "pasodoble/tango",
3: "cuplé",
4: "estribillo",
5: "popurrí",
6: "cuarteta",
}
group_type_mapping = {
1: "coro",
2: "comparsa",
3: "chirigota",
4: "cuarteto",
}
class CadizCarnivalConfig(BuilderConfig):
def __init__(self, **kwargs):
super().__init__(version=datasets.Version("1.0.2"), **kwargs)
class CadizCarnivalDataset(GeneratorBasedBuilder):
VERSION = "1.0.0"
BUILDER_CONFIGS = [
CadizCarnivalConfig(name="accurate", description="This part of my dataset covers accurate data"),
CadizCarnivalConfig(name="midaccurate", description="This part of my dataset covers midaccurate data"),
]
def _info(self):
return datasets.DatasetInfo(
description="_DESCRIPTION",
features=datasets.Features({
"id": Value("string"),
"authors": Sequence(Value("string")),
"song_type": Value("string"),
"year": Value("string"),
"group": Value("string"),
"group_type": Value("string"),
"lyrics": Sequence(Value("string")),
}),
supervised_keys=None,
homepage="https://letrascarnavalcadiz.com/",
citation="_CITATION",
)
def _split_generators(self, dl_manager: DownloadManager) -> List[SplitGenerator]:
urls_to_download = {
"accurate": "https://huggingface.co/datasets/IES-Rafael-Alberti/letras-carnaval-cadiz/raw/main/data/accurate-00000-of-00001.json",
"midaccurate": "https://huggingface.co/datasets/IES-Rafael-Alberti/letras-carnaval-cadiz/raw/main/data/midaccurate-00000-of-00001.json"
}
downloaded_files = dl_manager.download_and_extract(urls_to_download)
if self.config.name == "accurate":
return [SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["accurate"]})]
elif self.config.name == "midaccurate":
return [SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["midaccurate"]})]
def _generate_examples(self, filepath: str):
with open(filepath, encoding="utf-8") as f:
data = json.load(f)
for item in data:
item["song_type"] = song_type_mapping.get(item["song_type"], "indefinido")
item["group_type"] = group_type_mapping.get(item["group_type"], "indefinido")
yield item["id"], item
|