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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2021 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
|
| 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 |
+
# Lint as: python3
|
| 17 |
+
"""CSJ: Corpus of Spontaneous Japanese for Automatic Speech Recognition."""
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
import os
|
| 21 |
+
import re
|
| 22 |
+
from pathlib import Path
|
| 23 |
+
|
| 24 |
+
import datasets
|
| 25 |
+
import numpy as np
|
| 26 |
+
import librosa
|
| 27 |
+
from datasets.tasks import AutomaticSpeechRecognition
|
| 28 |
+
import soundfile as sf
|
| 29 |
+
|
| 30 |
+
_CITATION = """\
|
| 31 |
+
@article{article,
|
| 32 |
+
author = {Maekawa, Kikuo},
|
| 33 |
+
year = {2003},
|
| 34 |
+
month = {01},
|
| 35 |
+
pages = {},
|
| 36 |
+
title = {Corpus of Spontaneous Japanese: Its design and evaluation},
|
| 37 |
+
journal = {Proceedings of SSPR}
|
| 38 |
+
}
|
| 39 |
+
"""
|
| 40 |
+
|
| 41 |
+
_DESCRIPTION = """\
|
| 42 |
+
Corpus of Spontaneous Japanese, or CSJ, is a large-scale database of spontaneous Japanese. It contains speech signal and transcription of about 7 million words along with various annotations like POS and phonetic labels. After describing its design issues, preliminary evaluation of the CSJ was presented. The results suggest strongly the usefulness of the CSJ as the resource for the study of spontaneous speech.
|
| 43 |
+
"""
|
| 44 |
+
|
| 45 |
+
_HOMEPAGE = "https://clrd.ninjal.ac.jp/csj/en/"
|
| 46 |
+
|
| 47 |
+
_ROOT_DIRNAME = "csj"
|
| 48 |
+
|
| 49 |
+
class CSJConfig(datasets.BuilderConfig):
|
| 50 |
+
"""BuilderConfig for CSJ."""
|
| 51 |
+
|
| 52 |
+
def __init__(self, **kwargs):
|
| 53 |
+
"""
|
| 54 |
+
Args:
|
| 55 |
+
data_dir: `string`, the path to the folder containing the files in the
|
| 56 |
+
downloaded .tar
|
| 57 |
+
citation: `string`, citation for the data set
|
| 58 |
+
url: `string`, url for information about the data set
|
| 59 |
+
**kwargs: keyword arguments forwarded to super.
|
| 60 |
+
"""
|
| 61 |
+
# version history
|
| 62 |
+
# 0.1.0: First release
|
| 63 |
+
super(CSJConfig, self).__init__(version=datasets.Version("0.1.0", ""), **kwargs)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
class CSJ(datasets.GeneratorBasedBuilder):
|
| 68 |
+
"""CSJ dataset."""
|
| 69 |
+
DEFAULT_CONFIG_NAME = "all"
|
| 70 |
+
BUILDER_CONFIGS = [
|
| 71 |
+
CSJConfig(name="core", description="'core' speech."),
|
| 72 |
+
CSJConfig(name="noncore", description="'noncore', more challenging, speech."),
|
| 73 |
+
CSJConfig(name="all", description="Combined clean and other dataset."),
|
| 74 |
+
]
|
| 75 |
+
|
| 76 |
+
@property
|
| 77 |
+
def manual_download_instructions(self):
|
| 78 |
+
return (
|
| 79 |
+
"To use CSJ you have to download it manually. "
|
| 80 |
+
"Please create an account and download the dataset from "
|
| 81 |
+
"https://clrd.ninjal.ac.jp/csj/en/ \n"
|
| 82 |
+
"Then load the dataset with: "
|
| 83 |
+
"`datasets.load_dataset('csj', data_dir='path/to/folder/folder_name')`"
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
def _info(self):
|
| 87 |
+
return datasets.DatasetInfo(
|
| 88 |
+
description=_DESCRIPTION,
|
| 89 |
+
features=datasets.Features(
|
| 90 |
+
{
|
| 91 |
+
"id": datasets.Value("string"),
|
| 92 |
+
"audio": datasets.Audio(sampling_rate=16_000),
|
| 93 |
+
"text": datasets.Value("string"),
|
| 94 |
+
"katakana": datasets.Value("string")
|
| 95 |
+
}
|
| 96 |
+
),
|
| 97 |
+
supervised_keys=("file", "katakana"),
|
| 98 |
+
homepage=_HOMEPAGE,
|
| 99 |
+
citation=_CITATION,
|
| 100 |
+
task_templates=[AutomaticSpeechRecognition(audio_column="audio", transcription_column="katakana")],
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
def _split_generators(self, dl_manager):
|
| 104 |
+
# Step 1. Extract all zip files
|
| 105 |
+
# Step 2. Get scripts
|
| 106 |
+
# Step 3. Generate samples
|
| 107 |
+
data_dir = os.path.abspath(os.path.expanduser(dl_manager.manual_dir))
|
| 108 |
+
data_dir = os.path.join(data_dir, _ROOT_DIRNAME)
|
| 109 |
+
if not os.path.exists(data_dir):
|
| 110 |
+
raise FileNotFoundError(
|
| 111 |
+
f"{data_dir} does not exist. Make sure you insert a manual dir via"
|
| 112 |
+
"`datasets.load_dataset('csj', data_dir=...)`"
|
| 113 |
+
"that includes files. Manual download instructions:"
|
| 114 |
+
f"{self.manual_download_instructions}"
|
| 115 |
+
)
|
| 116 |
+
if self.config.name == 'default':
|
| 117 |
+
self.config.name = 'all'
|
| 118 |
+
|
| 119 |
+
archive_paths = {}
|
| 120 |
+
for fname in os.listdir(data_dir):
|
| 121 |
+
if (fname.startswith(self.config.name) or (self.config.name == 'all')) and fname.endswith('.zip'):
|
| 122 |
+
fname_no_ext = os.path.splitext(fname)[0]
|
| 123 |
+
archive_paths[fname_no_ext] = os.path.join(data_dir, fname)
|
| 124 |
+
local_extracted_archives = dl_manager.extract(archive_paths)
|
| 125 |
+
|
| 126 |
+
split_keys = {
|
| 127 |
+
"train": [],
|
| 128 |
+
"valid": [],
|
| 129 |
+
"test": []
|
| 130 |
+
}
|
| 131 |
+
if self.config.name == 'all':
|
| 132 |
+
split_keys["train"] = ["core.train", "noncore.train"]
|
| 133 |
+
split_keys["valid"] = ["core.valid", "noncore.valid"]
|
| 134 |
+
split_keys["test"] = ["core.test", "noncore.test"]
|
| 135 |
+
else:
|
| 136 |
+
for k in split_keys:
|
| 137 |
+
split_keys[k] = [f"{self.config.name}.{k}"]
|
| 138 |
+
|
| 139 |
+
return [
|
| 140 |
+
datasets.SplitGenerator(
|
| 141 |
+
name=datasets.Split.TRAIN,
|
| 142 |
+
gen_kwargs={
|
| 143 |
+
"target_keys": split_keys["train"],
|
| 144 |
+
"local_extracted_archives": local_extracted_archives
|
| 145 |
+
}
|
| 146 |
+
),
|
| 147 |
+
datasets.SplitGenerator(
|
| 148 |
+
name=datasets.Split.VALIDATION,
|
| 149 |
+
gen_kwargs={
|
| 150 |
+
"target_keys": split_keys["valid"],
|
| 151 |
+
"local_extracted_archives": local_extracted_archives
|
| 152 |
+
}
|
| 153 |
+
),
|
| 154 |
+
datasets.SplitGenerator(
|
| 155 |
+
name=datasets.Split.TEST,
|
| 156 |
+
gen_kwargs={
|
| 157 |
+
"target_keys": split_keys["test"],
|
| 158 |
+
"local_extracted_archives": local_extracted_archives
|
| 159 |
+
}
|
| 160 |
+
)
|
| 161 |
+
]
|
| 162 |
+
|
| 163 |
+
def _generate_examples(self, target_keys, local_extracted_archives):
|
| 164 |
+
"""Generate examples from KsponSpeech archive_path based on the test/train trn information."""
|
| 165 |
+
# Iterating the contents of the data to extract the relevant information
|
| 166 |
+
"""
|
| 167 |
+
audio_data = {
|
| 168 |
+
target_key: {
|
| 169 |
+
file_id: {
|
| 170 |
+
seg_id: String,
|
| 171 |
+
duration: Tuple(Float, Float),
|
| 172 |
+
channel: String
|
| 173 |
+
}
|
| 174 |
+
}
|
| 175 |
+
}
|
| 176 |
+
"""
|
| 177 |
+
|
| 178 |
+
metadata = {}
|
| 179 |
+
for k in target_keys:
|
| 180 |
+
local_extracted_archive = os.path.join(local_extracted_archives[k], k)
|
| 181 |
+
for fname in os.listdir(local_extracted_archive):
|
| 182 |
+
if fname.endswith('.trn'):
|
| 183 |
+
with open(os.path.join(local_extracted_archive, fname), encoding='cp932') as f:
|
| 184 |
+
words = []
|
| 185 |
+
seg_data = {}
|
| 186 |
+
is_stereo = False
|
| 187 |
+
file_id = os.path.splitext(fname)[0]
|
| 188 |
+
sentence = ''
|
| 189 |
+
katakana_sentence = ''
|
| 190 |
+
seg_id = ''
|
| 191 |
+
metadata[file_id] = {
|
| 192 |
+
'path': os.path.join(local_extracted_archive, fname),
|
| 193 |
+
'data': {}
|
| 194 |
+
}
|
| 195 |
+
for line in f:
|
| 196 |
+
if not line.startswith('%'):
|
| 197 |
+
if 'L:' in line or 'R:' in line:
|
| 198 |
+
# audio line
|
| 199 |
+
items = line.split(" ")
|
| 200 |
+
if len(items) == 3:
|
| 201 |
+
if seg_id != '' and sentence != '' and katakana_sentence != '':
|
| 202 |
+
metadata[file_id]['data'][seg_id]['text'] = sentence.strip()
|
| 203 |
+
metadata[file_id]['data'][seg_id]['katakana'] = parse_tag(katakana_sentence).strip()
|
| 204 |
+
sentence = ''
|
| 205 |
+
katakana_sentence = ''
|
| 206 |
+
seg_id, duration, channel_slot = items
|
| 207 |
+
start_sec, end_sec = duration.split("-")
|
| 208 |
+
channel = channel_slot.split(":")[0]
|
| 209 |
+
metadata[file_id]['data'][seg_id] = {
|
| 210 |
+
'duration': (float(start_sec), float(end_sec)),
|
| 211 |
+
'channel': channel
|
| 212 |
+
}
|
| 213 |
+
if channel == 'R':
|
| 214 |
+
is_stereo = True
|
| 215 |
+
else:
|
| 216 |
+
print(f"None audio line contains ':' at {fname}\n->{line}")
|
| 217 |
+
elif '&' in line:
|
| 218 |
+
# text line
|
| 219 |
+
text, katakana = line.split('&')
|
| 220 |
+
text = text.strip()
|
| 221 |
+
katakana = katakana.strip()
|
| 222 |
+
sentence += ' ' + text
|
| 223 |
+
katakana_sentence += ' ' + katakana
|
| 224 |
+
else:
|
| 225 |
+
print(f"Unknown line type. at {fname}\n->{line}")
|
| 226 |
+
elif '<EOT>' in line:
|
| 227 |
+
if seg_id != '' and sentence != '' and katakana_sentence != '':
|
| 228 |
+
metadata[file_id]['data'][seg_id]['text'] = sentence.strip()
|
| 229 |
+
metadata[file_id]['data'][seg_id]['katakana'] = parse_tag(katakana_sentence).strip()
|
| 230 |
+
sentence = ''
|
| 231 |
+
katakana_sentence = ''
|
| 232 |
+
|
| 233 |
+
if is_stereo:
|
| 234 |
+
file_id_left = file_id+'-L'
|
| 235 |
+
file_id_right = file_id+'-R'
|
| 236 |
+
metadata[file_id_left] = {
|
| 237 |
+
'path': metadata[file_id]['path'].replace(file_id, file_id_left),
|
| 238 |
+
'data': {}
|
| 239 |
+
}
|
| 240 |
+
metadata[file_id_right] = {
|
| 241 |
+
'path': metadata[file_id]['path'].replace(file_id, file_id_right),
|
| 242 |
+
'data': {}
|
| 243 |
+
}
|
| 244 |
+
for seg_id in metadata[file_id]['data']:
|
| 245 |
+
if metadata[file_id]['data'][seg_id]['channel'] == 'L':
|
| 246 |
+
metadata[file_id_left]['data'][seg_id] = metadata[file_id]['data'][seg_id]
|
| 247 |
+
elif metadata[file_id]['data'][seg_id]['channel'] == 'R':
|
| 248 |
+
metadata[file_id_right]['data'][seg_id] = metadata[file_id]['data'][seg_id]
|
| 249 |
+
else:
|
| 250 |
+
print(f"Unknwon channel. at {file_id}, {seg_id}, {metadata[file_id]['data'][seg_id]['channel']}")
|
| 251 |
+
del metadata[file_id]
|
| 252 |
+
|
| 253 |
+
key = 0
|
| 254 |
+
for file_id in metadata:
|
| 255 |
+
audio_path = metadata[file_id]['path'].replace('.trn','.wav')
|
| 256 |
+
if os.path.exists(audio_path):
|
| 257 |
+
audio_array, sampling_rate = sf.read(audio_path)
|
| 258 |
+
for seg_id in metadata[file_id]['data']:
|
| 259 |
+
if "katakana" in metadata[file_id]['data'][seg_id] and len(metadata[file_id]['data'][seg_id]['katakana']) > 0:
|
| 260 |
+
start_sec, end_sec = metadata[file_id]['data'][seg_id]["duration"]
|
| 261 |
+
start_idx = int(start_sec * sampling_rate)
|
| 262 |
+
end_idx = int(end_sec * sampling_rate)
|
| 263 |
+
audio_segment = audio_array[start_idx:end_idx]
|
| 264 |
+
audio = {
|
| 265 |
+
"path": f"{audio_path}:{start_sec}-{end_sec}",
|
| 266 |
+
"array": audio_segment,
|
| 267 |
+
"sampling_rate": sampling_rate
|
| 268 |
+
}
|
| 269 |
+
yield key, {
|
| 270 |
+
"id": file_id + '.' + seg_id,
|
| 271 |
+
"audio": audio,
|
| 272 |
+
"text": metadata[file_id]['data'][seg_id]["text"],
|
| 273 |
+
"katakana": metadata[file_id]['data'][seg_id]["katakana"]
|
| 274 |
+
}
|
| 275 |
+
key += 1
|
| 276 |
+
else:
|
| 277 |
+
print(f"Audio doesn't exist: {audio_path}")
|
| 278 |
+
|
| 279 |
+
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
"""
|
| 283 |
+
(F) ํ๋ฌ
|
| 284 |
+
(F text) -> text
|
| 285 |
+
(D) ๋ค์ ๋งํ๊ธฐ
|
| 286 |
+
(D text) -> text
|
| 287 |
+
(D2) ์กฐ์ฌ ๋ฑ์ ๋ค์ ๋งํ๊ธฐ
|
| 288 |
+
(D2 text) -> text
|
| 289 |
+
(?) ์์ ๋ฃ๊ธฐ ์ด๋ ค์์ ์ ์ฌ์ ์์ ์ด ์๋ ๊ฒฝ์ฐ
|
| 290 |
+
(? text) -> text
|
| 291 |
+
(? text1, text2) -> text1
|
| 292 |
+
(?) text -> text
|
| 293 |
+
(M) ์์ด๋ ๋จ์ด์ ์ธ์ฉ
|
| 294 |
+
(M text) -> text
|
| 295 |
+
(R) ๊ฐ์ธ์ ๋ณด
|
| 296 |
+
(R xxx) -> ''
|
| 297 |
+
(X) ๋น๋ฌธ๋ฒ
|
| 298 |
+
(X text) -> text
|
| 299 |
+
(A) ์ํ๋ฒณ ๋๋ ์ซ์, ๊ธฐํธ์ ํ๊ธฐ ; ์์ ๋ฐ์ ๋ค๋ ํ๊ธฐ
|
| 300 |
+
(A text; notation) -> text
|
| 301 |
+
(K) ์ด๋ค ์์ธ์ผ๋ก ํ์ํ๊ธฐ๊ฐ ํ ์ ์์ ๋
|
| 302 |
+
(K ใฒ(F ใใผ) ใ ใ;ๅทฆ) -> ใฒใใผใ ใ
|
| 303 |
+
(W) ์ผ์์ ๋ฐ์ ์ค์
|
| 304 |
+
(W mistake_pronounciation; correct_pronounciation) -> mistake_pronounciation
|
| 305 |
+
(B) ๋ฐฐ๊ฒฝ์ง์ ๋ถ์กฑ์ ๋ฐ๋ฅธ ๋ง ์ค์
|
| 306 |
+
(B mistake_pronounciation; correct_pronounciation) -> mistake_pronounciation
|
| 307 |
+
(็ฌ) ์์ผ๋ฉด์ ๋งํจ
|
| 308 |
+
(็ฌ text) -> text
|
| 309 |
+
(ๆณฃ) ์ธ๋ฉด์ ๋งํจ
|
| 310 |
+
(ๆณฃ text) -> text
|
| 311 |
+
(ๅณ) ๊ธฐ์นจํ๋ฉด์ ๋งํจ
|
| 312 |
+
(ๅณ text) -> text
|
| 313 |
+
(L) ์์ญ์ด๊ฑฐ๋ ์์ ๋ชฉ์๋ฆฌ๋ก ๋งํจ
|
| 314 |
+
(L text) -> text
|
| 315 |
+
|
| 316 |
+
<FV> ๋ณด์ปฌ ํ๋ผ์ด ๋ฑ์ผ๋ก ๋ชจ์์ ์๋ณ ํ ์์๋ ๊ฒฝ์ฐ
|
| 317 |
+
<VN> "์/ํ /ํ" ์๋ฆฌ๋ฅผ ํ์
ํ๊ธฐ ์ด๋ ค์ด ๊ฒฝ์ฐ
|
| 318 |
+
<H> ์ธ๋ฐ์์ด ๋ชจ์์ ๊ธธ๊ฒ ๋ฐ์
|
| 319 |
+
<Q> ์ธ๋ฐ์์ด ์์์ ๊ธธ๊ฒ ๋ฐ์
|
| 320 |
+
<็ฌ> ์์
|
| 321 |
+
<ๆณฃ> ์
|
| 322 |
+
<ๅณ> ๊ธฐ์นจ
|
| 323 |
+
<ๆฏ> ์จ์๋ฆฌ
|
| 324 |
+
<P> 2์ด ์ด์์ ์ ์
|
| 325 |
+
<P:starttime-endtime> -> ''
|
| 326 |
+
"""
|
| 327 |
+
def deal_with_tag(tag, text):
|
| 328 |
+
result = text
|
| 329 |
+
if tag == '?':
|
| 330 |
+
if ',' in text:
|
| 331 |
+
result = text.split(',')[0].strip()
|
| 332 |
+
else:
|
| 333 |
+
result = text.strip()
|
| 334 |
+
elif tag == 'R':
|
| 335 |
+
result = ''
|
| 336 |
+
elif tag == 'A' or tag == 'B' or tag == 'W' or tag == 'K':
|
| 337 |
+
result = text.split(';')[0].strip()
|
| 338 |
+
return result
|
| 339 |
+
|
| 340 |
+
def parse_tag(text):
|
| 341 |
+
tag_stack = []
|
| 342 |
+
content_stack = []
|
| 343 |
+
tag_flag = False
|
| 344 |
+
tag2_flag = False
|
| 345 |
+
content_flag = False
|
| 346 |
+
tag = ''
|
| 347 |
+
content = ''
|
| 348 |
+
result = ''
|
| 349 |
+
for c in text:
|
| 350 |
+
if tag2_flag:
|
| 351 |
+
if c == '>':
|
| 352 |
+
tag2_flag = False
|
| 353 |
+
else:
|
| 354 |
+
if tag_flag:
|
| 355 |
+
if c == ' ' or c == '?':
|
| 356 |
+
tag += c
|
| 357 |
+
tag_stack.append(tag.strip())
|
| 358 |
+
tag = ''
|
| 359 |
+
tag_flag = False
|
| 360 |
+
content_flag = True
|
| 361 |
+
else:
|
| 362 |
+
tag += c
|
| 363 |
+
elif c == '<':
|
| 364 |
+
tag2_flag = True
|
| 365 |
+
elif c == '(':
|
| 366 |
+
if content_flag:
|
| 367 |
+
content_stack.append(content)
|
| 368 |
+
content = ''
|
| 369 |
+
tag_flag = True
|
| 370 |
+
elif c == ')':
|
| 371 |
+
if content_flag:
|
| 372 |
+
processed_content = deal_with_tag(tag_stack.pop(), content)
|
| 373 |
+
if len(content_stack) == 0:
|
| 374 |
+
result += processed_content
|
| 375 |
+
content = ''
|
| 376 |
+
content_flag = False
|
| 377 |
+
else:
|
| 378 |
+
content = content_stack.pop()
|
| 379 |
+
content += processed_content
|
| 380 |
+
else:
|
| 381 |
+
content = ''
|
| 382 |
+
elif content_flag:
|
| 383 |
+
content += c
|
| 384 |
+
else:
|
| 385 |
+
result += c
|
| 386 |
+
assert '(' not in result, text
|
| 387 |
+
assert ')' not in result, text
|
| 388 |
+
assert '<' not in result, text
|
| 389 |
+
assert '>' not in result, text
|
| 390 |
+
return result
|