parlament_parla_v3 / parlament_parla_v3_asr_a.py
ssolito's picture
Upload parlament_parla_v3_asr_a.py
c519bed verified
from collections import defaultdict
import os
import json
import csv
import datasets
_NAME="parlament_parla_v3_asr_a"
_VERSION="1.0.0"
_DESCRIPTION = """
This is the third version of the ParlamentParla speech corpus for Catalan: a collection of speech recordings with transcriptions intended for Automatic Speech Recognition (ASR) applications.
"""
_CITATION = """
@misc{bscib32024,
title={ParlamentParla v3 - Speech Corpus of Catalan Parliamentary Sessions},
author={Baybars, Kulebi},
publisher={Barcelona Supercomputing Center},
year={2024},
url={https://huggingface.co/datasets/projecte-aina/parlament_parla_v3_asr_a},
}
"""
_HOMEPAGE = "https://huggingface.co/datasets/projecte-aina/parlament_parla_v3_asr_a"
_LICENSE = "CC-BY-4.0, See https://creativecommons.org/licenses/by/4.0/deed.es"
_BASE_DATA_DIR = "corpus/"
_METADATA_CLEAN_TRAIN_SHORT = os.path.join(_BASE_DATA_DIR,"files","clean_train_parlament_short.csv")
_METADATA_CLEAN_TEST_SHORT = os.path.join(_BASE_DATA_DIR,"files", "clean_test_parlament_short.csv")
_METADATA_CLEAN_DEV_SHORT = os.path.join(_BASE_DATA_DIR,"files", "clean_dev_parlament_short.csv")
_METADATA_OTHER_TRAIN_SHORT = os.path.join(_BASE_DATA_DIR,"files","other_train_parlament_short.csv")
_METADATA_OTHER_TEST_SHORT = os.path.join(_BASE_DATA_DIR,"files", "other_test_parlament_short.csv")
_METADATA_OTHER_DEV_SHORT = os.path.join(_BASE_DATA_DIR,"files", "other_dev_parlament_short.csv")
_TARS_CLEAN_TRAIN_SHORT = os.path.join(_BASE_DATA_DIR,"files","tars_clean_train_short.paths")
_TARS_CLEAN_TEST_SHORT = os.path.join(_BASE_DATA_DIR,"files", "tars_clean_test_short.paths")
_TARS_CLEAN_DEV_SHORT = os.path.join(_BASE_DATA_DIR,"files", "tars_clean_dev_short.paths")
_TARS_OTHER_TRAIN_SHORT = os.path.join(_BASE_DATA_DIR,"files","tars_other_train_short.paths")
_TARS_OTHER_TEST_SHORT = os.path.join(_BASE_DATA_DIR,"files", "tars_other_test_short.paths")
_TARS_OTHER_DEV_SHORT = os.path.join(_BASE_DATA_DIR,"files", "tars_other_dev_short.paths")
_METADATA_CLEAN_TRAIN_LONG = os.path.join(_BASE_DATA_DIR,"files","clean_train_parlament_long.csv")
_METADATA_CLEAN_TEST_LONG = os.path.join(_BASE_DATA_DIR,"files", "clean_test_parlament_long.csv")
_METADATA_CLEAN_DEV_LONG = os.path.join(_BASE_DATA_DIR,"files", "clean_dev_parlament_long.csv")
_METADATA_OTHER_TRAIN_LONG = os.path.join(_BASE_DATA_DIR,"files","other_train_parlament_long.csv")
_METADATA_OTHER_TEST_LONG = os.path.join(_BASE_DATA_DIR,"files", "other_test_parlament_long.csv")
_METADATA_OTHER_DEV_LONG = os.path.join(_BASE_DATA_DIR,"files", "other_dev_parlament_long.csv")
_TARS_CLEAN_TRAIN_LONG = os.path.join(_BASE_DATA_DIR,"files","tars_clean_train_long.paths")
_TARS_CLEAN_TEST_LONG = os.path.join(_BASE_DATA_DIR,"files", "tars_clean_test_long.paths")
_TARS_CLEAN_DEV_LONG = os.path.join(_BASE_DATA_DIR,"files", "tars_clean_dev_long.paths")
_TARS_OTHER_TRAIN_LONG = os.path.join(_BASE_DATA_DIR,"files","tars_other_train_long.paths")
_TARS_OTHER_TEST_LONG = os.path.join(_BASE_DATA_DIR,"files", "tars_other_test_long.paths")
_TARS_OTHER_DEV_LONG = os.path.join(_BASE_DATA_DIR,"files", "tars_other_dev_long.paths")
class ParlamentASRConfig(datasets.BuilderConfig):
"""BuilderConfig for Parlament ASR"""
def __init__(self, name, **kwargs):
name=_NAME
super().__init__(name=name, **kwargs)
class ParlamentASR(datasets.GeneratorBasedBuilder):
"""Parlament ASR"""
VERSION = datasets.Version(_VERSION)
BUILDER_CONFIGS = [
ParlamentASRConfig(
name=_NAME,
version=datasets.Version(_VERSION),
)
]
def _info(self):
features = datasets.Features(
{
"identifier": datasets.Value("string"),
"audio": datasets.Audio(sampling_rate=16000),
"segment_path": datasets.Value("string"),
"text": datasets.Value("string"),
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
metadata_clean_train_short=dl_manager.download_and_extract(_METADATA_CLEAN_TRAIN_SHORT)
metadata_clean_test_short=dl_manager.download_and_extract(_METADATA_CLEAN_TEST_SHORT)
metadata_clean_dev_short=dl_manager.download_and_extract(_METADATA_CLEAN_DEV_SHORT)
metadata_other_train_short=dl_manager.download_and_extract(_METADATA_OTHER_TRAIN_SHORT)
metadata_other_test_short=dl_manager.download_and_extract(_METADATA_OTHER_TEST_SHORT)
metadata_other_dev_short=dl_manager.download_and_extract(_METADATA_OTHER_DEV_SHORT)
tars_clean_train_short=dl_manager.download_and_extract(_TARS_CLEAN_TRAIN_SHORT)
tars_clean_test_short=dl_manager.download_and_extract(_TARS_CLEAN_TEST_SHORT)
tars_clean_dev_short=dl_manager.download_and_extract(_TARS_CLEAN_DEV_SHORT)
tars_other_train_short=dl_manager.download_and_extract(_TARS_OTHER_TRAIN_SHORT)
tars_other_test_short=dl_manager.download_and_extract(_TARS_OTHER_TEST_SHORT)
tars_other_dev_short=dl_manager.download_and_extract(_TARS_OTHER_DEV_SHORT)
metadata_clean_train_long=dl_manager.download_and_extract(_METADATA_CLEAN_TRAIN_LONG)
metadata_clean_test_long=dl_manager.download_and_extract(_METADATA_CLEAN_TEST_LONG)
metadata_clean_dev_long=dl_manager.download_and_extract(_METADATA_CLEAN_DEV_LONG)
metadata_other_train_long=dl_manager.download_and_extract(_METADATA_OTHER_TRAIN_LONG)
metadata_other_test_long=dl_manager.download_and_extract(_METADATA_OTHER_TEST_LONG)
metadata_other_dev_long=dl_manager.download_and_extract(_METADATA_OTHER_DEV_LONG)
tars_clean_train_long=dl_manager.download_and_extract(_TARS_CLEAN_TRAIN_LONG)
tars_clean_test_long=dl_manager.download_and_extract(_TARS_CLEAN_TEST_LONG)
tars_clean_dev_long=dl_manager.download_and_extract(_TARS_CLEAN_DEV_LONG)
tars_other_train_long=dl_manager.download_and_extract(_TARS_OTHER_TRAIN_LONG)
tars_other_test_long=dl_manager.download_and_extract(_TARS_OTHER_TEST_LONG)
tars_other_dev_long=dl_manager.download_and_extract(_TARS_OTHER_DEV_LONG)
hash_tar_files=defaultdict(dict)
with open(tars_clean_train_short,'r') as f:
hash_tar_files['clean_train_short']=[path.replace('\n','') for path in f]
with open(tars_clean_test_short,'r') as f:
hash_tar_files['clean_test_short']=[path.replace('\n','') for path in f]
with open(tars_clean_dev_short,'r') as f:
hash_tar_files['clean_dev_short']=[path.replace('\n','') for path in f]
with open(tars_other_train_short,'r') as f:
hash_tar_files['other_train_short']=[path.replace('\n','') for path in f]
with open(tars_other_test_short,'r') as f:
hash_tar_files['other_test_short']=[path.replace('\n','') for path in f]
with open(tars_other_dev_short,'r') as f:
hash_tar_files['other_dev_short']=[path.replace('\n','') for path in f]
with open(tars_clean_train_long,'r') as f:
hash_tar_files['clean_train_long']=[path.replace('\n','') for path in f]
with open(tars_clean_test_long,'r') as f:
hash_tar_files['clean_test_long']=[path.replace('\n','') for path in f]
with open(tars_clean_dev_long,'r') as f:
hash_tar_files['clean_dev_long']=[path.replace('\n','') for path in f]
with open(tars_other_train_long,'r') as f:
hash_tar_files['other_train_long']=[path.replace('\n','') for path in f]
with open(tars_other_test_long,'r') as f:
hash_tar_files['other_test_long']=[path.replace('\n','') for path in f]
with open(tars_other_dev_long,'r') as f:
hash_tar_files['other_dev_long']=[path.replace('\n','') for path in f]
hash_meta_paths={"clean_train_short":metadata_clean_train_short,
"clean_test_short":metadata_clean_test_short,
"clean_dev_short":metadata_clean_dev_short,
"other_train_short":metadata_other_train_short,
"other_test_short":metadata_other_test_short,
"other_dev_short":metadata_other_dev_short,
"clean_train_long":metadata_clean_train_long,
"clean_test_long":metadata_clean_test_long,
"clean_dev_long":metadata_clean_dev_long,
"other_train_long":metadata_other_train_long,
"other_test_long":metadata_other_test_long,
"other_dev_long":metadata_other_dev_long}
audio_paths = dl_manager.download(hash_tar_files)
splits=["clean_train_short","clean_test_short","clean_dev_short","other_train_short","other_test_short","other_dev_short","clean_train_long","clean_test_long","clean_dev_long","other_train_long","other_test_long","other_dev_long"]
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="clean_train_short",
gen_kwargs={
"audio_archives":[dl_manager.iter_archive(archive) for archive in audio_paths["clean_train_short"]],
"local_extracted_archives_paths": local_extracted_audio_paths["clean_train_short"],
"metadata_paths": hash_meta_paths["clean_train_short"],
}
),
datasets.SplitGenerator(
name="clean_test_short",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["clean_test_short"]],
"local_extracted_archives_paths": local_extracted_audio_paths["clean_test_short"],
"metadata_paths": hash_meta_paths["clean_test_short"],
}
),
datasets.SplitGenerator(
name="clean_dev_short",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["clean_dev_short"]],
"local_extracted_archives_paths": local_extracted_audio_paths["clean_dev_short"],
"metadata_paths": hash_meta_paths["clean_dev_short"],
}
),
datasets.SplitGenerator(
name="other_train_short",
gen_kwargs={
"audio_archives":[dl_manager.iter_archive(archive) for archive in audio_paths["other_train_short"]],
"local_extracted_archives_paths": local_extracted_audio_paths["other_train_short"],
"metadata_paths": hash_meta_paths["other_train_short"],
}
),
datasets.SplitGenerator(
name="other_test_short",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["other_test_short"]],
"local_extracted_archives_paths": local_extracted_audio_paths["other_test_short"],
"metadata_paths": hash_meta_paths["other_test_short"],
}
),
datasets.SplitGenerator(
name="other_dev_short",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["other_dev_short"]],
"local_extracted_archives_paths": local_extracted_audio_paths["other_dev_short"],
"metadata_paths": hash_meta_paths["other_dev_short"],
}
),
datasets.SplitGenerator(
name="clean_train_long",
gen_kwargs={
"audio_archives":[dl_manager.iter_archive(archive) for archive in audio_paths["clean_train_long"]],
"local_extracted_archives_paths": local_extracted_audio_paths["clean_train_long"],
"metadata_paths": hash_meta_paths["clean_train_long"],
}
),
datasets.SplitGenerator(
name="clean_test_long",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["clean_test_long"]],
"local_extracted_archives_paths": local_extracted_audio_paths["clean_test_long"],
"metadata_paths": hash_meta_paths["clean_test_long"],
}
),
datasets.SplitGenerator(
name="clean_dev_long",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["clean_dev_long"]],
"local_extracted_archives_paths": local_extracted_audio_paths["clean_dev_long"],
"metadata_paths": hash_meta_paths["clean_dev_long"],
}
),
datasets.SplitGenerator(
name="other_train_long",
gen_kwargs={
"audio_archives":[dl_manager.iter_archive(archive) for archive in audio_paths["other_train_long"]],
"local_extracted_archives_paths": local_extracted_audio_paths["other_train_long"],
"metadata_paths": hash_meta_paths["other_train_long"],
}
),
datasets.SplitGenerator(
name="other_test_long",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["other_test_long"]],
"local_extracted_archives_paths": local_extracted_audio_paths["other_test_long"],
"metadata_paths": hash_meta_paths["other_test_long"],
}
),
datasets.SplitGenerator(
name="other_dev_long",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["other_dev_long"]],
"local_extracted_archives_paths": local_extracted_audio_paths["other_dev_long"],
"metadata_paths": hash_meta_paths["other_dev_long"],
}
),
]
def _generate_examples(self, audio_archives, local_extracted_archives_paths, metadata_paths):
features = ["segment_path","text"]
with open(metadata_paths) as f:
metadata = {x["identifier"]: x for x in csv.DictReader(f, delimiter=",")}
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]
path = os.path.join(local_extracted_archive_path, audio_filename) if local_extracted_archive_path else audio_filename
yield audio_id, {
"identifier": audio_id,
**{feature: metadata[audio_id][feature] for feature in features},
"audio": {"path": path, "bytes": audio_file.read()},
}