commonvoice_benchmark_catalan_accents / commonvoice_benchmark_catalan_accents.py
carlosdanielhernandezmena's picture
Fixing Error: _csv.Error: field larger than field limit (131072)
c024467 verified
from collections import defaultdict
import os
import json
import csv
csv.field_size_limit(100000000)
import datasets
_NAME="commonvoice_benchmark_catalan_accents"
_VERSION="1.0.0"
_AUDIO_EXTENSIONS=".mp3"
_DESCRIPTION = """
A new presentation of the corpus Catalan Common Voice v17.0 - metadata annotated version with the splits redefined to benchmark ASR models with various Catalan accent
"""
_CITATION = """
@misc{armentanoaccents2024,
title={Common Voice Benchmark Catalan Accents},
author={Armentano, Carme},
publisher={Barcelona Supercomputing Center}
year={2024},
url={https://huggingface.co/datasets/projecte-aina/commonvoice_benchmark_catalan_accents},
}
"""
_HOMEPAGE = "https://huggingface.co/datasets/projecte-aina/commonvoice_benchmark_catalan_accents"
_LICENSE = "CC-BY-4.0, See https://creativecommons.org/licenses/by/4.0/"
_BASE_DATA_DIR = "corpus/"
_METADATA_TRAIN = os.path.join(_BASE_DATA_DIR,"files","train.tsv")
_METADATA_BALEARIC_FEM = os.path.join(_BASE_DATA_DIR,"files","balearic_female.tsv")
_METADATA_BALEARIC_MALE = os.path.join(_BASE_DATA_DIR,"files","balearic_male.tsv")
_METADATA_CENTRAL_FEMALE = os.path.join(_BASE_DATA_DIR,"files","central_female.tsv")
_METADATA_CENTRAL_MALE = os.path.join(_BASE_DATA_DIR,"files","central_male.tsv")
_METADATA_NORTHERN_FEMALE = os.path.join(_BASE_DATA_DIR,"files","northern_female.tsv")
_METADATA_NORTHERN_MALE = os.path.join(_BASE_DATA_DIR,"files","northern_male.tsv")
_METADATA_NORTHWESTERN_FEMALE = os.path.join(_BASE_DATA_DIR,"files","northwestern_female.tsv")
_METADATA_NORTHWESTERN_MALE = os.path.join(_BASE_DATA_DIR,"files","northwestern_male.tsv")
_METADATA_VALENCIAN_FEMALE = os.path.join(_BASE_DATA_DIR,"files","valencian_female.tsv")
_METADATA_VALENCIAN_MALE = os.path.join(_BASE_DATA_DIR,"files","valencian_male.tsv")
_TARS_REPO = os.path.join(_BASE_DATA_DIR,"files","tars_repo.paths")
class CommonVoiceBenchmarkCatalanAccentsConfig(datasets.BuilderConfig):
"""BuilderConfig for The Common Voice Benchmark Catalan Accents"""
def __init__(self, name, **kwargs):
name=_NAME
super().__init__(name=name, **kwargs)
class CommonVoiceBenchmarkCatalanAccents(datasets.GeneratorBasedBuilder):
"""Common Voice Benchmark Catalan Accents"""
VERSION = datasets.Version(_VERSION)
BUILDER_CONFIGS = [
CommonVoiceBenchmarkCatalanAccentsConfig(
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": 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"),
"duration": datasets.Value("float32"),
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
metadata_train=dl_manager.download_and_extract(_METADATA_TRAIN)
metadata_balearic_fem=dl_manager.download_and_extract(_METADATA_BALEARIC_FEM)
metadata_balearic_male=dl_manager.download_and_extract(_METADATA_BALEARIC_MALE)
metadata_central_female=dl_manager.download_and_extract(_METADATA_CENTRAL_FEMALE)
metadata_central_male=dl_manager.download_and_extract(_METADATA_CENTRAL_MALE)
metadata_northern_female=dl_manager.download_and_extract(_METADATA_NORTHERN_FEMALE)
metadata_northern_male=dl_manager.download_and_extract(_METADATA_NORTHWESTERN_MALE)
metadata_northwestern_female=dl_manager.download_and_extract(_METADATA_NORTHWESTERN_FEMALE)
metadata_northwestern_male=dl_manager.download_and_extract(_METADATA_NORTHWESTERN_MALE)
metadata_valencian_female=dl_manager.download_and_extract(_METADATA_VALENCIAN_FEMALE)
metadata_valencian_male=dl_manager.download_and_extract(_METADATA_VALENCIAN_MALE)
tars_repo=dl_manager.download_and_extract(_TARS_REPO)
hash_tar_files=defaultdict(dict)
with open(tars_repo,'r') as f:
hash_tar_files['train']=[path.replace('\n','') for path in f]
with open(tars_repo,'r') as f:
hash_tar_files['balearic_fem']=[path.replace('\n','') for path in f]
with open(tars_repo,'r') as f:
hash_tar_files['balearic_male']=[path.replace('\n','') for path in f]
with open(tars_repo,'r') as f:
hash_tar_files['central_female']=[path.replace('\n','') for path in f]
with open(tars_repo,'r') as f:
hash_tar_files['central_male']=[path.replace('\n','') for path in f]
with open(tars_repo,'r') as f:
hash_tar_files['northern_female']=[path.replace('\n','') for path in f]
with open(tars_repo,'r') as f:
hash_tar_files['northern_male']=[path.replace('\n','') for path in f]
with open(tars_repo,'r') as f:
hash_tar_files['northwestern_female']=[path.replace('\n','') for path in f]
with open(tars_repo,'r') as f:
hash_tar_files['northwestern_male']=[path.replace('\n','') for path in f]
with open(tars_repo,'r') as f:
hash_tar_files['valencian_female']=[path.replace('\n','') for path in f]
with open(tars_repo,'r') as f:
hash_tar_files['valencian_male']=[path.replace('\n','') for path in f]
hash_meta_paths={"train":metadata_train,
"balearic_fem":metadata_balearic_fem,
"balearic_male":metadata_balearic_male,
"central_female":metadata_central_female,
"central_male":metadata_central_male,
"northern_female":metadata_northern_female,
"northern_male":metadata_northern_male,
"northwestern_female":metadata_northwestern_female,
"northwestern_male":metadata_northwestern_male,
"valencian_female":metadata_valencian_female,
"valencian_male":metadata_valencian_male}
audio_paths = dl_manager.download(hash_tar_files)
splits=["train","balearic_fem","balearic_male","central_female","central_male","northern_female",
"northern_male","northwestern_female","northwestern_male","valencian_female","valencian_male"]
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="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="balearic_fem",
gen_kwargs={
"audio_archives":[dl_manager.iter_archive(archive) for archive in audio_paths["balearic_fem"]],
"local_extracted_archives_paths": local_extracted_audio_paths["balearic_fem"],
"metadata_paths": hash_meta_paths["balearic_fem"],
}
),
datasets.SplitGenerator(
name="balearic_male",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["balearic_male"]],
"local_extracted_archives_paths": local_extracted_audio_paths["balearic_male"],
"metadata_paths": hash_meta_paths["balearic_male"],
}
),
datasets.SplitGenerator(
name="central_female",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["central_female"]],
"local_extracted_archives_paths": local_extracted_audio_paths["central_female"],
"metadata_paths": hash_meta_paths["central_female"],
}
),
datasets.SplitGenerator(
name="central_male",
gen_kwargs={
"audio_archives":[dl_manager.iter_archive(archive) for archive in audio_paths["central_male"]],
"local_extracted_archives_paths": local_extracted_audio_paths["central_male"],
"metadata_paths": hash_meta_paths["central_male"],
}
),
datasets.SplitGenerator(
name="northern_female",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["northern_female"]],
"local_extracted_archives_paths": local_extracted_audio_paths["northern_female"],
"metadata_paths": hash_meta_paths["northern_female"],
}
),
datasets.SplitGenerator(
name="northern_male",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["northern_male"]],
"local_extracted_archives_paths": local_extracted_audio_paths["northern_male"],
"metadata_paths": hash_meta_paths["northern_male"],
}
),
datasets.SplitGenerator(
name="northwestern_female",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["northwestern_female"]],
"local_extracted_archives_paths": local_extracted_audio_paths["northwestern_female"],
"metadata_paths": hash_meta_paths["northwestern_female"],
}
),
datasets.SplitGenerator(
name="northwestern_male",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["northwestern_male"]],
"local_extracted_archives_paths": local_extracted_audio_paths["northwestern_male"],
"metadata_paths": hash_meta_paths["northwestern_male"],
}
),
datasets.SplitGenerator(
name="valencian_female",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["valencian_female"]],
"local_extracted_archives_paths": local_extracted_audio_paths["valencian_female"],
"metadata_paths": hash_meta_paths["valencian_female"],
}
),
datasets.SplitGenerator(
name="valencian_male",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["valencian_male"]],
"local_extracted_archives_paths": local_extracted_audio_paths["valencian_male"],
"metadata_paths": hash_meta_paths["valencian_male"],
}
),
]
def _generate_examples(self, audio_archives, local_extracted_archives_paths, metadata_paths):
features = ["client_id","sentence","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",
"duration","path"]
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