holylovenia
commited on
Commit
•
ed939dc
1
Parent(s):
1552280
Upload ucla_phonetic.py with huggingface_hub
Browse files- ucla_phonetic.py +158 -0
ucla_phonetic.py
ADDED
@@ -0,0 +1,158 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor.
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
|
16 |
+
"""
|
17 |
+
This dataset contains audio recordings and phonetic transcriptions of word utterances for various low-resource SEA languages.
|
18 |
+
Each language has a directory of text and audio files, with the latter forming one data subset.
|
19 |
+
The dataset is prepared from the online UCLA phonetic dataset, which contains 7000 utterances across 100 low-resource languages, phonetically aligned using various automatic approaches, and manually fixed for misalignments.
|
20 |
+
"""
|
21 |
+
import os
|
22 |
+
from pathlib import Path
|
23 |
+
from typing import Dict, List, Tuple
|
24 |
+
|
25 |
+
import datasets
|
26 |
+
|
27 |
+
from seacrowd.utils import schemas
|
28 |
+
from seacrowd.utils.configs import SEACrowdConfig
|
29 |
+
from seacrowd.utils.constants import Licenses, Tasks
|
30 |
+
|
31 |
+
_CITATION = """\
|
32 |
+
@inproceedings{li2021multilingual,
|
33 |
+
title={Multilingual phonetic dataset for low resource speech recognition},
|
34 |
+
author={Li, Xinjian and Mortensen, David R and Metze, Florian and Black, Alan W},
|
35 |
+
booktitle={ICASSP 2021-2021 IEEE International Conference on Acoustics, Speech and Signal Processing (ICASSP)},
|
36 |
+
pages={6958--6962},
|
37 |
+
year={2021},
|
38 |
+
organization={IEEE}
|
39 |
+
}
|
40 |
+
"""
|
41 |
+
|
42 |
+
_DATASETNAME = "ucla_phonetic"
|
43 |
+
|
44 |
+
_DESCRIPTION = """\
|
45 |
+
This dataset contains audio recordings and phonetic transcriptions of word utterances for various low-resource SEA languages.
|
46 |
+
Each language has a directory of text and audio files, with the latter forming one data subset.
|
47 |
+
The dataset is prepared from the online UCLA phonetic dataset, which contains 7000 utterances across 100 low-resource languages, phonetically aligned using various automatic approaches, and manually fixed for misalignments.
|
48 |
+
"""
|
49 |
+
|
50 |
+
_HOMEPAGE = "https://github.com/xinjli/ucla-phonetic-corpus"
|
51 |
+
|
52 |
+
_LANGUAGES = ["ace", "brv", "hil", "hni", "ilo", "khm", "mak", "mya", "pam"]
|
53 |
+
|
54 |
+
_LICENSE = Licenses.CC_BY_NC_SA_4_0.value
|
55 |
+
|
56 |
+
_LOCAL = False
|
57 |
+
|
58 |
+
_DATA_URL = "https://github.com/xinjli/ucla-phonetic-corpus/releases/download/v1.0/data.tar.gz"
|
59 |
+
|
60 |
+
_SUPPORTED_TASKS = [Tasks.SPEECH_RECOGNITION]
|
61 |
+
|
62 |
+
_SOURCE_VERSION = "1.0.0"
|
63 |
+
_SEACROWD_VERSION = "2024.06.20"
|
64 |
+
|
65 |
+
|
66 |
+
def seacrowd_config_constructor(lang, schema, version):
|
67 |
+
if lang not in _LANGUAGES:
|
68 |
+
raise ValueError(f"Invalid lang {lang}")
|
69 |
+
|
70 |
+
if schema not in ["source", "seacrowd_sptext"]:
|
71 |
+
raise ValueError(f"Invalid schema: {schema}")
|
72 |
+
|
73 |
+
return SEACrowdConfig(
|
74 |
+
name=f"ucla_phonetic_{lang}_{schema}",
|
75 |
+
version=datasets.Version(version),
|
76 |
+
description=f"UCLA Phonetic {schema} for {lang}",
|
77 |
+
schema=schema,
|
78 |
+
subset_id=f"{lang}_{schema}",
|
79 |
+
)
|
80 |
+
|
81 |
+
|
82 |
+
class UCLAPhoneticDataset(datasets.GeneratorBasedBuilder):
|
83 |
+
"""This dataset contains audio recordings and phonetic transcriptions of word utterances for various low-resource SEA languages."""
|
84 |
+
|
85 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
86 |
+
SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
|
87 |
+
|
88 |
+
BUILDER_CONFIGS = (
|
89 |
+
[
|
90 |
+
SEACrowdConfig(
|
91 |
+
name="ucla_phonetic_source",
|
92 |
+
version=datasets.Version(_SOURCE_VERSION),
|
93 |
+
description="UCLA Phonetic source for ace",
|
94 |
+
schema="source",
|
95 |
+
subset_id="ace_source",
|
96 |
+
),
|
97 |
+
SEACrowdConfig(
|
98 |
+
name="ucla_phonetic_seacrowd_sptext",
|
99 |
+
version=datasets.Version(_SOURCE_VERSION),
|
100 |
+
description="UCLA Phonetic seacrowd+sptext for ace",
|
101 |
+
schema="seacrowd_sptext",
|
102 |
+
subset_id="ace_seacrowd_sptext",
|
103 |
+
),
|
104 |
+
]
|
105 |
+
+ [seacrowd_config_constructor(lang, "source", _SOURCE_VERSION) for lang in _LANGUAGES]
|
106 |
+
+ [seacrowd_config_constructor(lang, "seacrowd_sptext", _SEACROWD_VERSION) for lang in _LANGUAGES]
|
107 |
+
)
|
108 |
+
|
109 |
+
DEFAULT_CONFIG_NAME = "ucla_phonetic_source"
|
110 |
+
|
111 |
+
def _info(self) -> datasets.DatasetInfo:
|
112 |
+
|
113 |
+
if self.config.schema == "source":
|
114 |
+
features = datasets.Features({"id": datasets.Value("string"), "text": datasets.Value("string"), "audio": datasets.Audio(sampling_rate=16_000)})
|
115 |
+
elif self.config.schema == "seacrowd_sptext":
|
116 |
+
features = schemas.speech_text_features
|
117 |
+
|
118 |
+
return datasets.DatasetInfo(
|
119 |
+
description=_DESCRIPTION,
|
120 |
+
features=features,
|
121 |
+
homepage=_HOMEPAGE,
|
122 |
+
license=_LICENSE,
|
123 |
+
citation=_CITATION,
|
124 |
+
)
|
125 |
+
|
126 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
127 |
+
"""Returns SplitGenerators."""
|
128 |
+
|
129 |
+
lang, schema = self.config.subset_id.split("_", maxsplit=1)
|
130 |
+
data_dir = dl_manager.download_and_extract(_DATA_URL)
|
131 |
+
|
132 |
+
return [
|
133 |
+
datasets.SplitGenerator(
|
134 |
+
name=datasets.Split.TRAIN,
|
135 |
+
# Whatever you put in gen_kwargs will be passed to _generate_examples
|
136 |
+
gen_kwargs={
|
137 |
+
"filepath": os.path.join(data_dir, "data", lang, "text.txt"),
|
138 |
+
"audiopath": Path(os.path.join(data_dir, "data", lang, "audio")),
|
139 |
+
},
|
140 |
+
)
|
141 |
+
]
|
142 |
+
|
143 |
+
def _generate_examples(self, filepath: Path, audiopath: Path) -> Tuple[int, Dict]:
|
144 |
+
|
145 |
+
audiofiles = {}
|
146 |
+
for audiofile in audiopath.iterdir():
|
147 |
+
audio_idx = os.path.basename(audiofile).split(".")[0]
|
148 |
+
audiofiles[audio_idx] = audiofile
|
149 |
+
|
150 |
+
if self.config.schema == "source":
|
151 |
+
for line_idx, line in enumerate(open(filepath)):
|
152 |
+
audio_idx, text = line.strip().split(maxsplit=1)
|
153 |
+
yield line_idx, {"id": line_idx, "text": text, "audio": str(audiofiles[audio_idx])}
|
154 |
+
|
155 |
+
elif self.config.schema == "seacrowd_sptext":
|
156 |
+
for line_idx, line in enumerate(open(filepath)):
|
157 |
+
audio_idx, text = line.strip().split(maxsplit=1)
|
158 |
+
yield line_idx, {"id": line_idx, "path": str(audiofiles[audio_idx]), "audio": str(audiofiles[audio_idx]), "text": text, "speaker_id": None, "metadata": {"speaker_age": None, "speaker_gender": None}}
|