annotated_catalan_common_voice_v17 / annotated_catalan_common_voice_v17.py
carlosdanielhernandezmena's picture
Creating the repo structure along with the dataloader
86e9383
raw
history blame
11.1 kB
from collections import defaultdict
import os
import json
import csv
import datasets
_NAME="annotated_catalan_common_voice_v17"
_VERSION="1.0.0"
_AUDIO_EXTENSIONS=".mp3"
_DESCRIPTION = """
This version of the Catalan sentences of the Common Voice corpus v17
includes metadata (gender and accent) for 263 speakers annotated by a team of experts.
"""
_CITATION = """
@misc{armentanoannotated2024,
title={Annotated Catalan Common Voice v17},
author={Armentano, Carme},
publisher={Barcelona Supercomputing Center}
year={2024},
url={https://huggingface.co/datasets/projecte-aina/annotated_catalan_common_voice_v17},
}
"""
_HOMEPAGE = "https://huggingface.co/datasets/projecte-aina/annotated_catalan_common_voice_v17"
_LICENSE = "CC-BY-4.0, See https://creativecommons.org/licenses/by/4.0/"
_BASE_DATA_DIR = "corpus/"
_METADATA_DEV = os.path.join(_BASE_DATA_DIR,"files","annotated_dev.tsv")
_METADATA_INVALIDATED = os.path.join(_BASE_DATA_DIR,"files","annotated_invalidated.tsv")
_METADATA_OTHER = os.path.join(_BASE_DATA_DIR,"files","annotated_other.tsv")
_METADATA_TEST = os.path.join(_BASE_DATA_DIR,"files","annotated_test.tsv")
_METADATA_TRAIN = os.path.join(_BASE_DATA_DIR,"files","annotated_train.tsv")
_METADATA_VALIDATED = os.path.join(_BASE_DATA_DIR,"files","annotated_validated.tsv")
_TARS_DEV = os.path.join(_BASE_DATA_DIR,"files","annotated_dev.paths")
_TARS_INVALIDATED = os.path.join(_BASE_DATA_DIR,"files","annotated_invalidated.paths")
_TARS_OTHER = os.path.join(_BASE_DATA_DIR,"files","annotated_other.paths")
_TARS_TEST = os.path.join(_BASE_DATA_DIR,"files","annotated_test.paths")
_TARS_TRAIN = os.path.join(_BASE_DATA_DIR,"files","annotated_train.paths")
_TARS_VALIDATED = os.path.join(_BASE_DATA_DIR,"files","annotated_validated.paths")
class AnnotatedCatalanCommonVoicev17Config(datasets.BuilderConfig):
"""BuilderConfig for The Annotated Catalan Common Voice v17"""
def __init__(self, name, **kwargs):
name=_NAME
super().__init__(name=name, **kwargs)
class AnnotatedCatalanCommonVoicev17(datasets.GeneratorBasedBuilder):
"""Annotated Catalan Common Voice v17"""
VERSION = datasets.Version(_VERSION)
BUILDER_CONFIGS = [
AnnotatedCatalanCommonVoicev17Config(
name=_NAME,
version=datasets.Version(_VERSION),
)
]
def _info(self):
features = datasets.Features(
{
"audio": datasets.Audio(sampling_rate=16000),
"client_id": datasets.Value("string"),
"path": datasets.Value("string"),
"sentence_id": datasets.Value("string"),
"sentence": datasets.Value("string"),
"sentence_domain": datasets.Value("string"),
"up_votes": datasets.Value("int32"),
"down_votes": datasets.Value("int32"),
"age": datasets.Value("string"),
"gender": datasets.Value("string"),
"accents": datasets.Value("string"),
"variant": datasets.Value("string"),
"locale": datasets.Value("string"),
"segment": datasets.Value("string"),
"mean quality": datasets.Value("string"),
"stdev quality": datasets.Value("string"),
"annotated_accent": datasets.Value("string"),
"annotated_accent_agreement": datasets.Value("string"),
"annotated_gender": datasets.Value("string"),
"annotated_gender_agreement": datasets.Value("string"),
"propagated_gender": datasets.Value("string"),
"propagated_accents": datasets.Value("string"),
"propagated_accents_norm": datasets.Value("string"),
"variant_norm": datasets.Value("string"),
"assigned_accent": datasets.Value("string"),
"assigned_gender": datasets.Value("string"),
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
metadata_dev=dl_manager.download_and_extract(_METADATA_DEV)
metadata_invalidated=dl_manager.download_and_extract(_METADATA_INVALIDATED)
metadata_other=dl_manager.download_and_extract(_METADATA_OTHER)
metadata_test=dl_manager.download_and_extract(_METADATA_TEST)
metadata_train=dl_manager.download_and_extract(_METADATA_TRAIN)
metadata_validated=dl_manager.download_and_extract(_METADATA_VALIDATED)
tars_dev=dl_manager.download_and_extract(_TARS_DEV)
tars_invalidated=dl_manager.download_and_extract(_TARS_INVALIDATED)
tars_other=dl_manager.download_and_extract(_TARS_OTHER)
tars_test=dl_manager.download_and_extract(_TARS_TEST)
tars_train=dl_manager.download_and_extract(_TARS_TRAIN)
tars_validated=dl_manager.download_and_extract(_TARS_VALIDATED)
hash_tar_files=defaultdict(dict)
with open(tars_dev,'r') as f:
hash_tar_files['validation']=[path.replace('\n','') for path in f]
with open(tars_invalidated,'r') as f:
hash_tar_files['invalidated']=[path.replace('\n','') for path in f]
with open(tars_other,'r') as f:
hash_tar_files['other']=[path.replace('\n','') for path in f]
with open(tars_test,'r') as f:
hash_tar_files['test']=[path.replace('\n','') for path in f]
with open(tars_train,'r') as f:
hash_tar_files['train']=[path.replace('\n','') for path in f]
with open(tars_validated,'r') as f:
hash_tar_files['validated']=[path.replace('\n','') for path in f]
hash_meta_paths={"validation":metadata_dev,
"invalidated":metadata_invalidated,
"other":metadata_other,
"test":metadata_test,
"train":metadata_train,
"validated":metadata_validated}
audio_paths = dl_manager.download(hash_tar_files)
splits=["validation","invalidated","other","test","train","validated"]
local_extracted_audio_paths = (
dl_manager.extract(audio_paths) if not dl_manager.is_streaming else
{
split:[None] * len(audio_paths[split]) for split in splits
}
)
return [
datasets.SplitGenerator(
name="validation",
gen_kwargs={
"audio_archives":[dl_manager.iter_archive(archive) for archive in audio_paths["validation"]],
"local_extracted_archives_paths": local_extracted_audio_paths["validation"],
"metadata_paths": hash_meta_paths["validation"],
}
),
datasets.SplitGenerator(
name="invalidated",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["invalidated"]],
"local_extracted_archives_paths": local_extracted_audio_paths["invalidated"],
"metadata_paths": hash_meta_paths["invalidated"],
}
),
datasets.SplitGenerator(
name="other",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["other"]],
"local_extracted_archives_paths": local_extracted_audio_paths["other"],
"metadata_paths": hash_meta_paths["other"],
}
),
datasets.SplitGenerator(
name="test",
gen_kwargs={
"audio_archives":[dl_manager.iter_archive(archive) for archive in audio_paths["test"]],
"local_extracted_archives_paths": local_extracted_audio_paths["test"],
"metadata_paths": hash_meta_paths["test"],
}
),
datasets.SplitGenerator(
name="train",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["train"]],
"local_extracted_archives_paths": local_extracted_audio_paths["train"],
"metadata_paths": hash_meta_paths["train"],
}
),
datasets.SplitGenerator(
name="validated",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["validated"]],
"local_extracted_archives_paths": local_extracted_audio_paths["validated"],
"metadata_paths": hash_meta_paths["validated"],
}
),
]
def _generate_examples(self, audio_archives, local_extracted_archives_paths, metadata_paths):
features = ["client_id","path","sentence_id","sentence","sentence_domain","up_votes",
"down_votes","age","gender","accents","variant","locale","segment",
"mean quality","stdev quality","annotated_accent","annotated_accent_agreement",
"annotated_gender","annotated_gender_agreement","propagated_gender",
"propagated_accents","propagated_accents_norm","variant_norm","assigned_accent",
"assigned_gender"]
with open(metadata_paths) as f:
metadata = {x["path"]: x for x in csv.DictReader(f, delimiter="\t")}
for audio_archive, local_extracted_archive_path in zip(audio_archives, local_extracted_archives_paths):
for audio_filename, audio_file in audio_archive:
audio_id =os.path.splitext(os.path.basename(audio_filename))[0]
audio_id=audio_id+_AUDIO_EXTENSIONS
path = os.path.join(local_extracted_archive_path, audio_filename) if local_extracted_archive_path else audio_filename
try:
yield audio_id, {
"path": audio_id,
**{feature: metadata[audio_id][feature] for feature in features},
"audio": {"path": path, "bytes": audio_file.read()},
}
except:
continue