Last active
August 1, 2021 07:41
-
-
Save cstorm125/a24858ffc5e4af4b18a0d5a99c7f5420 to your computer and use it in GitHub Desktop.
Common Voice (th) 7.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
# Copyright 2021 The HuggingFace Datasets Authors and the current dataset script contributor. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
""" Common Voice Dataset""" | |
import os | |
import datasets | |
from datasets.tasks import AutomaticSpeechRecognition | |
_DATA_URL = "https://voice-prod-bundler-ee1969a6ce8178826482b88e843c335139bd3fb4.s3.amazonaws.com/cv-corpus-7.0-2021-07-21/cv-corpus-7.0-2021-07-21-{}.tar.gz" | |
_CITATION = """\ | |
@inproceedings{commonvoice:2020, | |
author = {Ardila, R. and Branson, M. and Davis, K. and Henretty, M. and Kohler, M. and Meyer, J. and Morais, R. and Saunders, L. and Tyers, F. M. and Weber, G.}, | |
title = {Common Voice: A Massively-Multilingual Speech Corpus}, | |
booktitle = {Proceedings of the 12th Conference on Language Resources and Evaluation (LREC 2020)}, | |
pages = {4211--4215}, | |
year = 2020 | |
} | |
""" | |
_DESCRIPTION = """\ | |
Common Voice is Mozilla's initiative to help teach machines how real people speak. | |
The dataset currently consists of 7,335 validated hours of speech in 60 languages, but we’re always adding more voices and languages. | |
""" | |
_HOMEPAGE = "https://commonvoice.mozilla.org/en/datasets" | |
_LICENSE = "https://github.com/common-voice/common-voice/blob/main/LICENSE" | |
_LANGUAGES = { | |
"th": { | |
"Language": "Thai", | |
"Date": "2021-07-21", | |
"Size": "5 GB", | |
"Version": "th_255h_2021-07-21", | |
"Validated_Hr_Total": 133, | |
"Overall_Hr_Total": 255, | |
"Number_Of_Voice": 7212, | |
}, | |
} | |
class CommonVoiceConfig(datasets.BuilderConfig): | |
"""BuilderConfig for CommonVoice.""" | |
def __init__(self, name, sub_version, **kwargs): | |
""" | |
Args: | |
data_dir: `string`, the path to the folder containing the files in the | |
downloaded .tar | |
citation: `string`, citation for the data set | |
url: `string`, url for information about the data set | |
**kwargs: keyword arguments forwarded to super. | |
""" | |
self.sub_version = sub_version | |
self.language = kwargs.pop("language", None) | |
self.date_of_snapshot = kwargs.pop("date", None) | |
self.size = kwargs.pop("size", None) | |
self.validated_hr_total = kwargs.pop("val_hrs", None) | |
self.total_hr_total = kwargs.pop("total_hrs", None) | |
self.num_of_voice = kwargs.pop("num_of_voice", None) | |
description = f"Common Voice speech to text dataset in {self.language} version {self.sub_version} of {self.date_of_snapshot}. The dataset comprises {self.validated_hr_total} of validated transcribed speech data from {self.num_of_voice} speakers. The dataset has a size of {self.size}" | |
super(CommonVoiceConfig, self).__init__( | |
name=name, version=datasets.Version("7.0.0", ""), description=description, **kwargs | |
) | |
class CommonVoice(datasets.GeneratorBasedBuilder): | |
BUILDER_CONFIGS = [ | |
CommonVoiceConfig( | |
name=lang_id, | |
language=_LANGUAGES[lang_id]["Language"], | |
sub_version=_LANGUAGES[lang_id]["Version"], | |
date=_LANGUAGES[lang_id]["Date"], | |
size=_LANGUAGES[lang_id]["Size"], | |
val_hrs=_LANGUAGES[lang_id]["Validated_Hr_Total"], | |
total_hrs=_LANGUAGES[lang_id]["Overall_Hr_Total"], | |
num_of_voice=_LANGUAGES[lang_id]["Number_Of_Voice"], | |
) | |
for lang_id in _LANGUAGES.keys() | |
] | |
def _info(self): | |
features = datasets.Features( | |
{ | |
"client_id": datasets.Value("string"), | |
"path": datasets.Value("string"), | |
"sentence": datasets.Value("string"), | |
"up_votes": datasets.Value("int64"), | |
"down_votes": datasets.Value("int64"), | |
"age": datasets.Value("string"), | |
"gender": datasets.Value("string"), | |
"accent": datasets.Value("string"), | |
"locale": datasets.Value("string"), | |
"segment": datasets.Value("string"), | |
} | |
) | |
return datasets.DatasetInfo( | |
description=_DESCRIPTION, | |
features=features, | |
supervised_keys=None, | |
homepage=_HOMEPAGE, | |
license=_LICENSE, | |
citation=_CITATION, | |
task_templates=[ | |
AutomaticSpeechRecognition(audio_file_path_column="path", transcription_column="sentence") | |
], | |
) | |
def _split_generators(self, dl_manager): | |
"""Returns SplitGenerators.""" | |
dl_path = dl_manager.download_and_extract(_DATA_URL.format(self.config.name)) | |
abs_path_to_data = os.path.join(dl_path, "cv-corpus-7.0-2021-07-21", self.config.name) | |
abs_path_to_clips = os.path.join(abs_path_to_data, "clips") | |
return [ | |
datasets.SplitGenerator( | |
name=datasets.Split.TRAIN, | |
gen_kwargs={ | |
"filepath": os.path.join(abs_path_to_data, "train.tsv"), | |
"path_to_clips": abs_path_to_clips, | |
}, | |
), | |
datasets.SplitGenerator( | |
name=datasets.Split.TEST, | |
gen_kwargs={ | |
"filepath": os.path.join(abs_path_to_data, "test.tsv"), | |
"path_to_clips": abs_path_to_clips, | |
}, | |
), | |
datasets.SplitGenerator( | |
name=datasets.Split.VALIDATION, | |
gen_kwargs={ | |
"filepath": os.path.join(abs_path_to_data, "dev.tsv"), | |
"path_to_clips": abs_path_to_clips, | |
}, | |
), | |
] | |
def _generate_examples(self, filepath, path_to_clips): | |
"""Yields examples.""" | |
data_fields = list(self._info().features.keys()) | |
path_idx = data_fields.index("path") | |
with open(filepath, encoding="utf-8") as f: | |
lines = f.readlines() | |
headline = lines[0] | |
column_names = headline.strip().split("\t") | |
assert ( | |
column_names == data_fields | |
), f"The file should have {data_fields} as column names, but has {column_names}" | |
for id_, line in enumerate(lines[1:]): | |
field_values = line.strip().split("\t") | |
# set absolute path for mp3 audio file | |
field_values[path_idx] = os.path.join(path_to_clips, field_values[path_idx]) | |
# if data is incomplete, fill with empty values | |
if len(field_values) < len(data_fields): | |
field_values += (len(data_fields) - len(field_values)) * ["''"] | |
yield id_, {key: value for key, value in zip(data_fields, field_values)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment