masri_test / masri_test.py
carlosdanielhernandezmena's picture
Fixing a windows compatibility issue
d56766c
raw
history blame
5.21 kB
from collections import defaultdict
import os
import json
import csv
import datasets
_NAME="masri_test"
_VERSION="1.0.0"
_AUDIO_EXTENSIONS=".flac"
_DESCRIPTION = """
The MASRI-TEST CORPUS was created out of YouTube videos belonging to the channel of the University of Malta. It has a length of 1 hour and it is gender balanced, as it has the same number of male and female speakers.
"""
_CITATION = """
@misc{carlosmenamasritest2020,
title={MASRI-TEST CORPUS: Audio and Transcriptions in Maltese extracted from the YouTube channel of the University of Malta.},
author={Hernandez Mena, Carlos Daniel and Brincat, Ayrton Didier and Gatt, Albert and DeMarco, Andrea and Borg, Claudia and van der Plas, Lonneke and Meza Ruiz, Iván Vladimir},
journal={MASRI Project, Malta},
year={2020},
url={https://www.um.edu.mt/projects/masri/},
}
"""
_HOMEPAGE = "https://www.um.edu.mt/projects/masri/"
_LICENSE = "CC-BY-4.0. The copyright remains with the original owners of the video. See https://creativecommons.org/licenses/by/4.0/"
_BASE_DATA_DIR = "corpus/"
_METADATA_TEST = os.path.join(_BASE_DATA_DIR,"files", "metadata_test.tsv")
_TARS_TEST = os.path.join(_BASE_DATA_DIR,"files", "tars_test.paths")
class MasriTestConfig(datasets.BuilderConfig):
"""BuilderConfig for MASRI-TEST Corpus"""
def __init__(self, name, **kwargs):
name=_NAME
super().__init__(name=name, **kwargs)
class MasriTest(datasets.GeneratorBasedBuilder):
"""MASRI-TEST Corpus"""
VERSION = datasets.Version(_VERSION)
BUILDER_CONFIGS = [
MasriTestConfig(
name=_NAME,
version=datasets.Version(_VERSION),
)
]
def _info(self):
features = datasets.Features(
{
"audio_id": datasets.Value("string"),
"audio": datasets.Audio(sampling_rate=16000),
"speaker_id": datasets.Value("string"),
"gender": datasets.Value("string"),
"duration": datasets.Value("float32"),
"normalized_text": datasets.Value("string"),
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
metadata_test=dl_manager.download_and_extract(_METADATA_TEST)
tars_test=dl_manager.download_and_extract(_TARS_TEST)
hash_tar_files=defaultdict(dict)
with open(tars_test,'r') as f:
hash_tar_files['test']=[path.replace('\n','') for path in f]
hash_meta_paths={"test":metadata_test}
audio_paths = dl_manager.download(hash_tar_files)
splits=["test"]
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=datasets.Split.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"],
}
),
]
def _generate_examples(self, audio_archives, local_extracted_archives_paths, metadata_paths):
features = ["speaker_id","gender","duration","normalized_text"]
with open(metadata_paths) as f:
metadata = {x["audio_id"]: 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 = audio_filename.split(os.sep)[-1].split(_AUDIO_EXTENSIONS)[0]
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, {
"audio_id": audio_id,
**{feature: metadata[audio_id][feature] for feature in features},
"audio": {"path": path, "bytes": audio_file.read()},
}