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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import zipfile
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from typing import Dict, List, Tuple
|
| 5 |
+
|
| 6 |
+
try:
|
| 7 |
+
import audiosegment
|
| 8 |
+
except:
|
| 9 |
+
print("Please install audiosegment to use the `national_speech_corpus_sg_imda` dataloader.")
|
| 10 |
+
import datasets
|
| 11 |
+
import pandas as pd
|
| 12 |
+
|
| 13 |
+
try:
|
| 14 |
+
import textgrid
|
| 15 |
+
except:
|
| 16 |
+
print("Please install textgrid to use the `national_speech_corpus_sg_imda` dataloader.")
|
| 17 |
+
|
| 18 |
+
from seacrowd.utils import schemas
|
| 19 |
+
from seacrowd.utils.configs import SEACrowdConfig
|
| 20 |
+
from seacrowd.utils.constants import Licenses, Tasks
|
| 21 |
+
|
| 22 |
+
_CITATION = """\
|
| 23 |
+
@inproceedings{koh19_interspeech,
|
| 24 |
+
author={Jia Xin Koh and Aqilah Mislan and Kevin Khoo and Brian Ang and Wilson Ang and Charmaine Ng and Ying-Ying Tan},
|
| 25 |
+
title={{Building the Singapore English National Speech Corpus}},
|
| 26 |
+
year=2019,
|
| 27 |
+
booktitle={Proc. Interspeech 2019},
|
| 28 |
+
pages={321--325},
|
| 29 |
+
doi={10.21437/Interspeech.2019-1525},
|
| 30 |
+
issn={2308-457X}
|
| 31 |
+
}
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
_DATASETNAME = "national_speech_corpus_sg_imda"
|
| 35 |
+
|
| 36 |
+
_DESCRIPTION = """\
|
| 37 |
+
The National Speech Corpus (NSC) is the first large-scale Singapore English corpus spearheaded by the Info-communications and Media Development Authority (IMDA) of Singapore.
|
| 38 |
+
It aims to become an important source of open speech data for automatic speech recognition (ASR) research and speech-related applications.
|
| 39 |
+
The NSC improves speech engines’ accuracy of recognition and transcription for locally accented English.
|
| 40 |
+
The NSC is also able to contribute to speech synthesis technology where an AI voice can be produced that is more familiar to Singaporeans, with local terms pronounced more accurately.
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
_HOMEPAGE = "https://www.imda.gov.sg/how-we-can-help/national-speech-corpus"
|
| 44 |
+
|
| 45 |
+
_LANGUAGES = ["eng"] # We follow ISO639-3 language code (https://iso639-3.sil.org/code_tables/639/data)
|
| 46 |
+
|
| 47 |
+
_LICENSE = f"{Licenses.OTHERS.value} | Singapore Open Data Licence V1.0"
|
| 48 |
+
|
| 49 |
+
_LOCAL = True
|
| 50 |
+
|
| 51 |
+
_URLS = {}
|
| 52 |
+
|
| 53 |
+
# paths of all file locations, presented in a list to support different operating systems
|
| 54 |
+
_PATHS = {
|
| 55 |
+
"read_balanced": {
|
| 56 |
+
"metadata": ["PART1", "DOC", "Speaker Information (Part 1).XLSX"],
|
| 57 |
+
"data": {
|
| 58 |
+
"standing_mic": {
|
| 59 |
+
"audio": ["PART1", "DATA", "CHANNEL0", "WAVE"],
|
| 60 |
+
"text": ["PART1", "DATA", "CHANNEL0", "SCRIPT"],
|
| 61 |
+
},
|
| 62 |
+
"boundary_mic": {
|
| 63 |
+
"audio": ["PART1", "DATA", "CHANNEL1", "WAVE"],
|
| 64 |
+
"text": ["PART1", "DATA", "CHANNEL1", "SCRIPT"],
|
| 65 |
+
},
|
| 66 |
+
"phone": {
|
| 67 |
+
"audio": ["PART1", "DATA", "CHANNEL2", "WAVE"],
|
| 68 |
+
"text": ["PART1", "DATA", "CHANNEL2", "SCRIPT"],
|
| 69 |
+
},
|
| 70 |
+
},
|
| 71 |
+
},
|
| 72 |
+
"read_pertinent": {
|
| 73 |
+
"metadata": ["PART2", "DOC", "Speaker Information (Part 2).XLSX"],
|
| 74 |
+
"data": {
|
| 75 |
+
"standing_mic": {
|
| 76 |
+
"audio": ["PART2", "DATA", "CHANNEL0", "WAVE"],
|
| 77 |
+
"text": ["PART2", "DATA", "CHANNEL0", "SCRIPT"],
|
| 78 |
+
},
|
| 79 |
+
"boundary_mic": {
|
| 80 |
+
"audio": ["PART2", "DATA", "CHANNEL1", "WAVE"],
|
| 81 |
+
"text": ["PART2", "DATA", "CHANNEL1", "SCRIPT"],
|
| 82 |
+
},
|
| 83 |
+
"phone": {
|
| 84 |
+
"audio": ["PART2", "DATA", "CHANNEL2", "WAVE"],
|
| 85 |
+
"text": ["PART2", "DATA", "CHANNEL2", "SCRIPT"],
|
| 86 |
+
},
|
| 87 |
+
},
|
| 88 |
+
},
|
| 89 |
+
"conversational_f2f": {
|
| 90 |
+
"metadata": ["PART3", "Documents", "Speakers (Part 3).XLSX"],
|
| 91 |
+
"data": {
|
| 92 |
+
"close_mic": {
|
| 93 |
+
"audio": ["PART3", "Audio Same CloseMic"],
|
| 94 |
+
"text": ["PART3", "Scripts Same"],
|
| 95 |
+
},
|
| 96 |
+
"boundary_mic": {
|
| 97 |
+
"audio": ["PART3", "Audio Same BoundaryMic"],
|
| 98 |
+
"text": ["PART3", "Scripts Same"],
|
| 99 |
+
},
|
| 100 |
+
},
|
| 101 |
+
},
|
| 102 |
+
"conversational_telephone": {
|
| 103 |
+
"metadata": ["PART3", "Documents", "Speakers (Part 3).XLSX"],
|
| 104 |
+
"data": {
|
| 105 |
+
"ivr": {
|
| 106 |
+
"audio": ["PART3", "Audio Separate IVR"],
|
| 107 |
+
"text": ["PART3", "Scripts Separate"],
|
| 108 |
+
},
|
| 109 |
+
"standing_mic": {
|
| 110 |
+
"audio": ["PART3", "Audio Separate StandingMic"],
|
| 111 |
+
"text": ["PART3", "Scripts Separate"],
|
| 112 |
+
},
|
| 113 |
+
},
|
| 114 |
+
},
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
_SUPPORTED_TASKS = [Tasks.SPEECH_RECOGNITION]
|
| 118 |
+
|
| 119 |
+
_SOURCE_VERSION = "2.0.8" # should be 2.08 but HuggingFace does not allow
|
| 120 |
+
|
| 121 |
+
_SEACROWD_VERSION = "2024.06.20"
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
class NationalSpeechCorpusSgIMDADataset(datasets.GeneratorBasedBuilder):
|
| 125 |
+
"""The National Speech Corpus (NSC), the first large-scale Singapore English corpus spearheaded by the Info-communications and Media Development Authority (IMDA) of Singapore."""
|
| 126 |
+
|
| 127 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
| 128 |
+
SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
|
| 129 |
+
|
| 130 |
+
BUILDER_CONFIGS = [
|
| 131 |
+
SEACrowdConfig(
|
| 132 |
+
name="national_speech_corpus_sg_imda_source",
|
| 133 |
+
version=SOURCE_VERSION,
|
| 134 |
+
description="national_speech_corpus_sg_imda source schema",
|
| 135 |
+
schema="source",
|
| 136 |
+
subset_id="national_speech_corpus_sg_imda",
|
| 137 |
+
),
|
| 138 |
+
SEACrowdConfig(
|
| 139 |
+
name="national_speech_corpus_sg_imda_seacrowd_sptext",
|
| 140 |
+
version=SEACROWD_VERSION,
|
| 141 |
+
description="national_speech_corpus_sg_imda SEACrowd schema",
|
| 142 |
+
schema="seacrowd_sptext",
|
| 143 |
+
subset_id="national_speech_corpus_sg_imda",
|
| 144 |
+
),
|
| 145 |
+
]
|
| 146 |
+
|
| 147 |
+
DEFAULT_CONFIG_NAME = "national_speech_corpus_sg_imda_source"
|
| 148 |
+
|
| 149 |
+
def _info(self) -> datasets.DatasetInfo:
|
| 150 |
+
if self.config.schema == "source":
|
| 151 |
+
features = datasets.Features(
|
| 152 |
+
{
|
| 153 |
+
"id": datasets.Value("string"),
|
| 154 |
+
"speaker_id": datasets.Value("string"),
|
| 155 |
+
"path": datasets.Value("string"),
|
| 156 |
+
"audio": datasets.Audio(sampling_rate=16_000),
|
| 157 |
+
"text": datasets.Value("string"),
|
| 158 |
+
}
|
| 159 |
+
)
|
| 160 |
+
elif self.config.schema == "seacrowd_sptext":
|
| 161 |
+
features = schemas.speech_text_features
|
| 162 |
+
|
| 163 |
+
return datasets.DatasetInfo(
|
| 164 |
+
description=_DESCRIPTION,
|
| 165 |
+
features=features,
|
| 166 |
+
homepage=_HOMEPAGE,
|
| 167 |
+
license=_LICENSE,
|
| 168 |
+
citation=_CITATION,
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
| 172 |
+
"""Returns SplitGenerators."""
|
| 173 |
+
if self.config.data_dir is None:
|
| 174 |
+
raise ValueError(f"This is a local dataset. Please download the data from {_HOMEPAGE} and pass the path as the data_dir kwarg to load_dataset.")
|
| 175 |
+
else:
|
| 176 |
+
data_dir = self.config.data_dir
|
| 177 |
+
|
| 178 |
+
splits = []
|
| 179 |
+
for split_name, data in _PATHS.items():
|
| 180 |
+
metadata_path = os.path.join(*data["metadata"])
|
| 181 |
+
for mic_type, data_path in data["data"].items():
|
| 182 |
+
splits.append(
|
| 183 |
+
datasets.SplitGenerator(
|
| 184 |
+
name=f"{split_name}_{mic_type}",
|
| 185 |
+
gen_kwargs={"filepath": data_dir, "audio_dir": os.path.join(*data_path["audio"]), "text_dir": os.path.join(*data_path["text"]), "split": split_name, "mic_type": mic_type, "metadata_path": metadata_path},
|
| 186 |
+
)
|
| 187 |
+
)
|
| 188 |
+
return splits
|
| 189 |
+
|
| 190 |
+
def _read_part1_part2_text(self, text_path):
|
| 191 |
+
text_data = {}
|
| 192 |
+
with open(text_path, encoding="utf-8-sig") as f:
|
| 193 |
+
for line in f:
|
| 194 |
+
comp = line.split("\t")
|
| 195 |
+
text_id = comp[0].strip()
|
| 196 |
+
if len(text_id) > 1:
|
| 197 |
+
text_data[text_id] = comp[1].strip()
|
| 198 |
+
return text_data
|
| 199 |
+
|
| 200 |
+
def _generate_examples(self, filepath: Path, audio_dir: Path, text_dir: Path, split: str, mic_type: str, metadata_path: str, tmp_cache="~/.cache/nsc") -> Tuple[int, Dict]:
|
| 201 |
+
"""Yields examples as (key, example) tuples."""
|
| 202 |
+
|
| 203 |
+
# get speaker info from Excel file
|
| 204 |
+
all_speaker_info = {}
|
| 205 |
+
if split.startswith("read"):
|
| 206 |
+
excel_file = pd.read_excel(open(os.path.join(filepath, metadata_path), "rb"), sheet_name="Speakers", dtype="object")
|
| 207 |
+
column_name = "SCD/PART" + "1" if split == "read_balanced" else 2
|
| 208 |
+
for idx, row in excel_file.iterrows():
|
| 209 |
+
all_speaker_info[str(row[column_name])] = {"gender": row["SEX"]}
|
| 210 |
+
elif split == "conversational_f2f":
|
| 211 |
+
excel_file = pd.read_excel(open(os.path.join(filepath, metadata_path), "rb"), sheet_name="Same Room", dtype="object")
|
| 212 |
+
for idx, row in excel_file.iterrows():
|
| 213 |
+
all_speaker_info[row["SCD"]] = {"age": row["AGE"], "gender": row["SEX"]}
|
| 214 |
+
elif split == "conversational_telephone":
|
| 215 |
+
excel_file = pd.read_excel(open(os.path.join(filepath, metadata_path), "rb"), sheet_name="Separate Room", dtype="object")
|
| 216 |
+
for idx, row in excel_file.iterrows():
|
| 217 |
+
all_speaker_info[f"{row['Conference ID']}_{row['Speaker ID']}"] = {"age": row["Age"], "gender": row["Gender"]}
|
| 218 |
+
|
| 219 |
+
for rel_text_path in os.listdir(os.path.join(filepath, text_dir)):
|
| 220 |
+
text_path = os.path.join(filepath, text_dir, rel_text_path)
|
| 221 |
+
text_name, _ext = os.path.splitext(rel_text_path)
|
| 222 |
+
if not os.path.isfile(text_path):
|
| 223 |
+
continue
|
| 224 |
+
if split.startswith("conversational"):
|
| 225 |
+
if split == "conversational_telephone":
|
| 226 |
+
# conf_{confid}_{confid}_{speaker_id} -> {confid}_{speaker_id}
|
| 227 |
+
speaker_id = text_name.split("_", 2)[-1]
|
| 228 |
+
pass
|
| 229 |
+
else:
|
| 230 |
+
speaker_id = text_name
|
| 231 |
+
speaker_info = all_speaker_info.get(speaker_id, {})
|
| 232 |
+
speaker_info["id"] = speaker_id
|
| 233 |
+
|
| 234 |
+
if mic_type in ["close_mic", "standing_mic"]:
|
| 235 |
+
audio_filename = text_name
|
| 236 |
+
audio_filepath = audio_filename + ".wav"
|
| 237 |
+
audio_path = os.path.join(audio_dir, audio_filepath)
|
| 238 |
+
elif mic_type == "boundary_mic":
|
| 239 |
+
audio_filename = text_name.split("-")[0]
|
| 240 |
+
audio_filepath = audio_filename + ".wav"
|
| 241 |
+
audio_path = os.path.join(audio_dir, audio_filepath)
|
| 242 |
+
elif mic_type == "ivr":
|
| 243 |
+
audio_subdir, audio_filename = text_name.rsplit("_", 1)
|
| 244 |
+
audio_filepath = audio_filename + ".wav"
|
| 245 |
+
audio_path = os.path.join(audio_dir, audio_subdir, audio_filepath)
|
| 246 |
+
|
| 247 |
+
audio_file = audiosegment.from_file(os.path.join(filepath, audio_path)).resample(sample_rate_Hz=16000)
|
| 248 |
+
text_spans = textgrid.TextGrid.fromFile(text_path)
|
| 249 |
+
audio_name, _ = os.path.splitext(os.path.basename(audio_dir))
|
| 250 |
+
for text_span in text_spans[0]:
|
| 251 |
+
start, end, text = text_span.minTime, text_span.maxTime, text_span.mark
|
| 252 |
+
key = f"{audio_name}_{start}_{end}"
|
| 253 |
+
|
| 254 |
+
start_sec, end_sec = int(start * 1000), int(end * 1000)
|
| 255 |
+
segment = audio_file[start_sec:end_sec]
|
| 256 |
+
export_dir = os.path.join(tmp_cache, "segmented", audio_name)
|
| 257 |
+
os.makedirs(export_dir, exist_ok=True)
|
| 258 |
+
segement_filename = os.path.join(export_dir, f"{audio_filename}-{round(start, 0)}-{round(end, 0)}.wav")
|
| 259 |
+
segment.export(segement_filename, format="wav")
|
| 260 |
+
|
| 261 |
+
example = {
|
| 262 |
+
"id": key,
|
| 263 |
+
"speaker_id": speaker_info["id"],
|
| 264 |
+
"path": segement_filename,
|
| 265 |
+
"audio": segement_filename,
|
| 266 |
+
"text": text,
|
| 267 |
+
}
|
| 268 |
+
if self.config.schema == "seacrowd_sptext":
|
| 269 |
+
example["metadata"] = {
|
| 270 |
+
"speaker_gender": speaker_info.get("gender", None), # not all speaker details are available
|
| 271 |
+
"speaker_age": None, # speaker age only available in age groups, but SEACrowd schema requires int64
|
| 272 |
+
}
|
| 273 |
+
yield key, example
|
| 274 |
+
|
| 275 |
+
else:
|
| 276 |
+
text_data = self._read_part1_part2_text(text_path)
|
| 277 |
+
audio_name, session = text_name[1:-1], text_name[-1]
|
| 278 |
+
speaker_id = audio_name
|
| 279 |
+
speaker_info = all_speaker_info.get(speaker_id, {})
|
| 280 |
+
speaker_info["id"] = speaker_id
|
| 281 |
+
for text_id, text in text_data.items():
|
| 282 |
+
with zipfile.ZipFile(os.path.join(filepath, audio_dir, f"SPEAKER{audio_name}.zip")) as zip_file:
|
| 283 |
+
zip_path = os.path.join(f"SPEAKER{audio_name}", f"SESSION{session}", f"{text_id}.WAV")
|
| 284 |
+
extract_path = os.path.join(tmp_cache, audio_dir)
|
| 285 |
+
os.makedirs(extract_path, exist_ok=True)
|
| 286 |
+
audio_path = zip_file.extract(zip_path, path=extract_path)
|
| 287 |
+
|
| 288 |
+
key = text_id
|
| 289 |
+
example = {
|
| 290 |
+
"id": key,
|
| 291 |
+
"speaker_id": speaker_info["id"],
|
| 292 |
+
"path": audio_path,
|
| 293 |
+
"audio": audio_path,
|
| 294 |
+
"text": text,
|
| 295 |
+
}
|
| 296 |
+
if self.config.schema == "seacrowd_sptext":
|
| 297 |
+
example["metadata"] = {
|
| 298 |
+
"speaker_gender": speaker_info.get("gender", None),
|
| 299 |
+
"speaker_age": None, # speaker age only available in age groups, but SEACrowd schema requires int64
|
| 300 |
+
}
|
| 301 |
+
yield key, example
|