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Copyright 2022 The Google and 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 |
+
import os
|
17 |
+
from collections import OrderedDict
|
18 |
+
|
19 |
+
import datasets
|
20 |
+
|
21 |
+
logger = datasets.logging.get_logger(__name__)
|
22 |
+
|
23 |
+
|
24 |
+
""" FLEURS Dataset"""
|
25 |
+
|
26 |
+
_FLEURS_LANG_TO_ID = OrderedDict([("Afrikaans", "af"), ("Amharic", "am"), ("Arabic", "ar"), ("Armenian", "hy"), ("Assamese", "as"), ("Asturian", "ast"), ("Azerbaijani", "az"), ("Belarusian", "be"), ("Bengali", "bn"), ("Bosnian", "bs"), ("Bulgarian", "bg"), ("Burmese", "my"), ("Catalan", "ca"), ("Cebuano", "ceb"), ("Mandarin Chinese", "cmn_hans"), ("Cantonese Chinese", "yue_hant"), ("Croatian", "hr"), ("Czech", "cs"), ("Danish", "da"), ("Dutch", "nl"), ("English", "en"), ("Estonian", "et"), ("Filipino", "fil"), ("Finnish", "fi"), ("French", "fr"), ("Fula", "ff"), ("Galician", "gl"), ("Ganda", "lg"), ("Georgian", "ka"), ("German", "de"), ("Greek", "el"), ("Gujarati", "gu"), ("Hausa", "ha"), ("Hebrew", "he"), ("Hindi", "hi"), ("Hungarian", "hu"), ("Icelandic", "is"), ("Igbo", "ig"), ("Indonesian", "id"), ("Irish", "ga"), ("Italian", "it"), ("Japanese", "ja"), ("Javanese", "jv"), ("Kabuverdianu", "kea"), ("Kamba", "kam"), ("Kannada", "kn"), ("Kazakh", "kk"), ("Khmer", "km"), ("Korean", "ko"), ("Kyrgyz", "ky"), ("Lao", "lo"), ("Latvian", "lv"), ("Lingala", "ln"), ("Lithuanian", "lt"), ("Luo", "luo"), ("Luxembourgish", "lb"), ("Macedonian", "mk"), ("Malay", "ms"), ("Malayalam", "ml"), ("Maltese", "mt"), ("Maori", "mi"), ("Marathi", "mr"), ("Mongolian", "mn"), ("Nepali", "ne"), ("Northern-Sotho", "nso"), ("Norwegian", "nb"), ("Nyanja", "ny"), ("Occitan", "oc"), ("Oriya", "or"), ("Oromo", "om"), ("Pashto", "ps"), ("Persian", "fa"), ("Polish", "pl"), ("Portuguese", "pt"), ("Punjabi", "pa"), ("Romanian", "ro"), ("Russian", "ru"), ("Serbian", "sr"), ("Shona", "sn"), ("Sindhi", "sd"), ("Slovak", "sk"), ("Slovenian", "sl"), ("Somali", "so"), ("Sorani-Kurdish", "ckb"), ("Spanish", "es"), ("Swahili", "sw"), ("Swedish", "sv"), ("Tajik", "tg"), ("Tamil", "ta"), ("Telugu", "te"), ("Thai", "th"), ("Turkish", "tr"), ("Ukrainian", "uk"), ("Umbundu", "umb"), ("Urdu", "ur"), ("Uzbek", "uz"), ("Vietnamese", "vi"), ("Welsh", "cy"), ("Wolof", "wo"), ("Xhosa", "xh"), ("Yoruba", "yo"), ("Zulu", "zu")])
|
27 |
+
_FLEURS_LANG_SHORT_TO_LONG = {v: k for k, v in _FLEURS_LANG_TO_ID.items()}
|
28 |
+
|
29 |
+
|
30 |
+
_FLEURS_LANG = sorted(["af_za", "am_et", "ar_eg", "as_in", "ast_es", "az_az", "be_by", "bn_in", "bs_ba", "ca_es", "ceb_ph", "cmn_hans_cn", "yue_hant_hk", "cs_cz", "cy_gb", "da_dk", "de_de", "el_gr", "en_us", "es_419", "et_ee", "fa_ir", "ff_sn", "fi_fi", "fil_ph", "fr_fr", "ga_ie", "gl_es", "gu_in", "ha_ng", "he_il", "hi_in", "hr_hr", "hu_hu", "hy_am", "id_id", "ig_ng", "is_is", "it_it", "ja_jp", "jv_id", "ka_ge", "kam_ke", "kea_cv", "kk_kz", "km_kh", "kn_in", "ko_kr", "ckb_iq", "ky_kg", "lb_lu", "lg_ug", "ln_cd", "lo_la", "lt_lt", "luo_ke", "lv_lv", "mi_nz", "mk_mk", "ml_in", "mn_mn", "mr_in", "ms_my", "mt_mt", "my_mm", "nb_no", "ne_np", "nl_nl", "nso_za", "ny_mw", "oc_fr", "om_et", "or_in", "pa_in", "pl_pl", "ps_af", "pt_br", "ro_ro", "ru_ru", "bg_bg", "sd_in", "sk_sk", "sl_si", "sn_zw", "so_so", "sr_rs", "sv_se", "sw_ke", "ta_in", "te_in", "tg_tj", "th_th", "tr_tr", "uk_ua", "umb_ao", "ur_pk", "uz_uz", "vi_vn", "wo_sn", "xh_za", "yo_ng", "zu_za"])
|
31 |
+
_FLEURS_LONG_TO_LANG = {_FLEURS_LANG_SHORT_TO_LONG["_".join(k.split("_")[:-1]) or k]: k for k in _FLEURS_LANG}
|
32 |
+
_FLEURS_LANG_TO_LONG = {v: k for k, v in _FLEURS_LONG_TO_LANG.items()}
|
33 |
+
|
34 |
+
_FLEURS_GROUP_TO_LONG = OrderedDict({
|
35 |
+
"western_european_we": ["Asturian", "Bosnian", "Catalan", "Croatian", "Danish", "Dutch", "English", "Finnish", "French", "Galician", "German", "Greek", "Hungarian", "Icelandic", "Irish", "Italian", "Kabuverdianu", "Luxembourgish", "Maltese", "Norwegian", "Occitan", "Portuguese", "Spanish", "Swedish", "Welsh"],
|
36 |
+
"eastern_european_ee": ["Armenian", "Belarusian", "Bulgarian", "Czech", "Estonian", "Georgian", "Latvian", "Lithuanian", "Macedonian", "Polish", "Romanian", "Russian", "Serbian", "Slovak", "Slovenian", "Ukrainian"],
|
37 |
+
"central_asia_middle_north_african_cmn": ["Arabic", "Azerbaijani", "Hebrew", "Kazakh", "Kyrgyz", "Mongolian", "Pashto", "Persian", "Sorani-Kurdish", "Tajik", "Turkish", "Uzbek"],
|
38 |
+
"sub_saharan_african_ssa": ["Afrikaans", "Amharic", "Fula", "Ganda", "Hausa", "Igbo", "Kamba", "Lingala", "Luo", "Northern-Sotho", "Nyanja", "Oromo", "Shona", "Somali", "Swahili", "Umbundu", "Wolof", "Xhosa", "Yoruba", "Zulu"],
|
39 |
+
"south_asian_sa": ["Assamese", "Bengali", "Gujarati", "Hindi", "Kannada", "Malayalam", "Marathi", "Nepali", "Oriya", "Punjabi", "Sindhi", "Tamil", "Telugu", "Urdu"],
|
40 |
+
"south_east_asian_sea": ["Burmese", "Cebuano", "Filipino", "Indonesian", "Javanese", "Khmer", "Lao", "Malay", "Maori", "Thai", "Vietnamese"],
|
41 |
+
"chinese_japanase_korean_cjk": ["Mandarin Chinese", "Cantonese Chinese", "Japanese", "Korean"],
|
42 |
+
})
|
43 |
+
_FLEURS_LONG_TO_GROUP = {a: k for k, v in _FLEURS_GROUP_TO_LONG.items() for a in v}
|
44 |
+
_FLEURS_LANG_TO_GROUP = {_FLEURS_LONG_TO_LANG[k]: v for k, v in _FLEURS_LONG_TO_GROUP.items()}
|
45 |
+
|
46 |
+
_ALL_LANG = _FLEURS_LANG
|
47 |
+
_ALL_CONFIGS = []
|
48 |
+
|
49 |
+
for langs in _FLEURS_LANG:
|
50 |
+
_ALL_CONFIGS.append(langs)
|
51 |
+
|
52 |
+
_ALL_CONFIGS.append("all")
|
53 |
+
|
54 |
+
# TODO(FLEURS)
|
55 |
+
_DESCRIPTION = "FLEURS is the speech version of the FLORES machine translation benchmark, covering 2000 n-way parallel sentences in n=102 languages."
|
56 |
+
_CITATION = ""
|
57 |
+
_HOMEPAGE_URL = ""
|
58 |
+
|
59 |
+
_BASE_PATH = "data/{langs}/"
|
60 |
+
_DATA_URL = _BASE_PATH + "audio/{split}.tar.gz"
|
61 |
+
_META_URL = _BASE_PATH + "{split}.tsv"
|
62 |
+
|
63 |
+
|
64 |
+
class FleursConfig(datasets.BuilderConfig):
|
65 |
+
"""BuilderConfig for xtreme-s"""
|
66 |
+
|
67 |
+
def __init__(
|
68 |
+
self, name, description, citation, homepage
|
69 |
+
):
|
70 |
+
super(FleursConfig, self).__init__(
|
71 |
+
name=self.name,
|
72 |
+
version=datasets.Version("2.0.0", ""),
|
73 |
+
description=self.description,
|
74 |
+
)
|
75 |
+
self.name = name
|
76 |
+
self.description = description
|
77 |
+
self.citation = citation
|
78 |
+
self.homepage = homepage
|
79 |
+
|
80 |
+
|
81 |
+
def _build_config(name):
|
82 |
+
return FleursConfig(
|
83 |
+
name=name,
|
84 |
+
description=_DESCRIPTION,
|
85 |
+
citation=_CITATION,
|
86 |
+
homepage=_HOMEPAGE_URL,
|
87 |
+
)
|
88 |
+
|
89 |
+
|
90 |
+
class Fleurs(datasets.GeneratorBasedBuilder):
|
91 |
+
|
92 |
+
DEFAULT_WRITER_BATCH_SIZE = 1000
|
93 |
+
BUILDER_CONFIGS = [_build_config(name) for name in _ALL_CONFIGS]
|
94 |
+
|
95 |
+
def _info(self):
|
96 |
+
task_templates = None
|
97 |
+
langs = _ALL_CONFIGS
|
98 |
+
features = datasets.Features(
|
99 |
+
{
|
100 |
+
"id": datasets.Value("int32"),
|
101 |
+
"num_samples": datasets.Value("int32"),
|
102 |
+
"path": datasets.Value("string"),
|
103 |
+
"audio": datasets.Audio(sampling_rate=16_000),
|
104 |
+
"transcription": datasets.Value("string"),
|
105 |
+
"raw_transcription": datasets.Value("string"),
|
106 |
+
"gender": datasets.ClassLabel(names=["male", "female", "other"]),
|
107 |
+
"lang_id": datasets.ClassLabel(names=langs),
|
108 |
+
"language": datasets.Value("string"),
|
109 |
+
"lang_group_id": datasets.ClassLabel(
|
110 |
+
names=list(_FLEURS_GROUP_TO_LONG.keys())
|
111 |
+
),
|
112 |
+
}
|
113 |
+
)
|
114 |
+
|
115 |
+
return datasets.DatasetInfo(
|
116 |
+
description=self.config.description + "\n" + _DESCRIPTION,
|
117 |
+
features=features,
|
118 |
+
supervised_keys=("audio", "transcription"),
|
119 |
+
homepage=self.config.homepage,
|
120 |
+
citation=self.config.citation + "\n" + _CITATION,
|
121 |
+
task_templates=task_templates,
|
122 |
+
)
|
123 |
+
|
124 |
+
# Fleurs
|
125 |
+
def _split_generators(self, dl_manager):
|
126 |
+
splits = ["train", "dev", "test"]
|
127 |
+
|
128 |
+
# metadata_path = dl_manager.download_and_extract(_METADATA_URL)
|
129 |
+
|
130 |
+
if self.config.name == "all":
|
131 |
+
data_urls = {split: [_DATA_URL.format(langs=langs,split=split) for langs in _FLEURS_LANG] for split in splits}
|
132 |
+
meta_urls = {split: [_META_URL.format(langs=langs,split=split) for langs in _FLEURS_LANG] for split in splits}
|
133 |
+
else:
|
134 |
+
data_urls = {split: [_DATA_URL.format(langs=self.config.name, split=split)] for split in splits}
|
135 |
+
meta_urls = {split: [_META_URL.format(langs=self.config.name, split=split)] for split in splits}
|
136 |
+
|
137 |
+
archive_paths = dl_manager.download(data_urls)
|
138 |
+
local_extracted_archives = dl_manager.extract(archive_paths) if not dl_manager.is_streaming else {}
|
139 |
+
archive_iters = {split: [dl_manager.iter_archive(path) for path in paths] for split, paths in archive_paths.items()}
|
140 |
+
|
141 |
+
meta_paths = dl_manager.download(meta_urls)
|
142 |
+
|
143 |
+
return [
|
144 |
+
datasets.SplitGenerator(
|
145 |
+
name=datasets.Split.TRAIN,
|
146 |
+
gen_kwargs={
|
147 |
+
"local_extracted_archives": local_extracted_archives.get("train", [None] * len(meta_paths.get("train"))),
|
148 |
+
"archive_iters": archive_iters.get("train"),
|
149 |
+
"text_paths": meta_paths.get("train")
|
150 |
+
},
|
151 |
+
),
|
152 |
+
datasets.SplitGenerator(
|
153 |
+
name=datasets.Split.VALIDATION,
|
154 |
+
gen_kwargs={
|
155 |
+
"local_extracted_archives": local_extracted_archives.get("dev", [None] * len(meta_paths.get("dev"))),
|
156 |
+
"archive_iters": archive_iters.get("dev"),
|
157 |
+
"text_paths": meta_paths.get("dev")
|
158 |
+
},
|
159 |
+
),
|
160 |
+
datasets.SplitGenerator(
|
161 |
+
name=datasets.Split.TEST,
|
162 |
+
gen_kwargs={
|
163 |
+
"local_extracted_archives": local_extracted_archives.get("test", [None] * len(meta_paths.get("test"))),
|
164 |
+
"archive_iters": archive_iters.get("test"),
|
165 |
+
"text_paths": meta_paths.get("test")
|
166 |
+
},
|
167 |
+
),
|
168 |
+
]
|
169 |
+
|
170 |
+
def _get_data(self, lines, lang_id):
|
171 |
+
data = {}
|
172 |
+
gender_to_id = {"MALE": 0, "FEMALE": 1, "OTHER": 2}
|
173 |
+
for line in lines:
|
174 |
+
if isinstance(line, bytes):
|
175 |
+
line = line.decode("utf-8")
|
176 |
+
(
|
177 |
+
_id,
|
178 |
+
file_name,
|
179 |
+
raw_transcription
|
180 |
+
) = line.strip().split("\t")
|
181 |
+
|
182 |
+
lang_group = _FLEURS_LANG_TO_GROUP[lang_id]
|
183 |
+
|
184 |
+
data[file_name] = {
|
185 |
+
"id": int(_id),
|
186 |
+
"raw_transcription": "",
|
187 |
+
"transcription": raw_transcription,
|
188 |
+
"num_samples": int(0),
|
189 |
+
"gender": gender_to_id["OTHER"],
|
190 |
+
"lang_id": _FLEURS_LANG.index(lang_id),
|
191 |
+
"language": _FLEURS_LANG_TO_LONG[lang_id],
|
192 |
+
"lang_group_id": list(_FLEURS_GROUP_TO_LONG.keys()).index(
|
193 |
+
lang_group
|
194 |
+
),
|
195 |
+
}
|
196 |
+
|
197 |
+
return data
|
198 |
+
|
199 |
+
def _generate_examples(self, local_extracted_archives, archive_iters, text_paths):
|
200 |
+
assert len(local_extracted_archives) == len(archive_iters) == len(text_paths)
|
201 |
+
key = 0
|
202 |
+
|
203 |
+
if self.config.name == "all":
|
204 |
+
langs = _FLEURS_LANG
|
205 |
+
else:
|
206 |
+
langs = [self.config.name]
|
207 |
+
|
208 |
+
for archive, text_path, local_extracted_path, lang_id in zip(archive_iters, text_paths, local_extracted_archives, langs):
|
209 |
+
with open(text_path, encoding="utf-8") as f:
|
210 |
+
lines = f.readlines()
|
211 |
+
data = self._get_data(lines, lang_id)
|
212 |
+
|
213 |
+
for audio_path, audio_file in archive:
|
214 |
+
audio_filename = audio_path.split("/")[-1]
|
215 |
+
if audio_filename not in data.keys():
|
216 |
+
continue
|
217 |
+
|
218 |
+
result = data[audio_filename]
|
219 |
+
extracted_audio_path = (
|
220 |
+
os.path.join(local_extracted_path, audio_filename)
|
221 |
+
if local_extracted_path is not None
|
222 |
+
else None
|
223 |
+
)
|
224 |
+
result["path"] = extracted_audio_path
|
225 |
+
result["audio"] = {"path": audio_path, "bytes": audio_file.read()}
|
226 |
+
yield key, result
|
227 |
+
key += 1
|