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|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2021 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
|
| 3 |
+
# Copyright 2021 Jim O'Regan
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 14 |
+
# See the License for the specific language governing permissions and
|
| 15 |
+
# limitations under the License.
|
| 16 |
+
|
| 17 |
+
# Lint as: python3
|
| 18 |
+
"""ClarinPL Studio automatic speech recognition dataset."""
|
| 19 |
+
|
| 20 |
+
import os
|
| 21 |
+
import glob
|
| 22 |
+
|
| 23 |
+
import datasets
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
_CITATION = """\
|
| 27 |
+
@article{korvzinek2017polish,
|
| 28 |
+
title={Polish read speech corpus for speech tools and services},
|
| 29 |
+
author={Kor{\v{z}}inek, Danijel and Marasek, Krzysztof and Brocki, {\L}ukasz and Wo{\l}k, Krzysztof},
|
| 30 |
+
journal={arXiv preprint arXiv:1706.00245},
|
| 31 |
+
year={2017}
|
| 32 |
+
}
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
_DESCRIPTION = """\
|
| 36 |
+
The corpus consists of 317 speakers recorded in 554
|
| 37 |
+
sessions, where each session consists of 20 read sentences and 10 phonetically rich words. The size of
|
| 38 |
+
the audio portion of the corpus amounts to around 56 hours, with transcriptions containing 356674 words
|
| 39 |
+
from a vocabulary of size 46361.
|
| 40 |
+
|
| 41 |
+
Note that in order to limit the required storage for preparing this dataset, the audio
|
| 42 |
+
is stored in the .wav format and is not converted to a float32 array. To convert the audio
|
| 43 |
+
file to a float32 array, please make use of the `.map()` function as follows:
|
| 44 |
+
|
| 45 |
+
```python
|
| 46 |
+
import soundfile as sf
|
| 47 |
+
|
| 48 |
+
def map_to_array(batch):
|
| 49 |
+
speech_array, _ = sf.read(batch["file"])
|
| 50 |
+
batch["speech"] = speech_array
|
| 51 |
+
return batch
|
| 52 |
+
|
| 53 |
+
dataset = dataset.map(map_to_array, remove_columns=["file"])
|
| 54 |
+
```
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
_URL = "https://mowa.clarin-pl.eu/"
|
| 58 |
+
_DS_URL = "http://mowa.clarin-pl.eu/korpusy/audio.tar.gz"
|
| 59 |
+
_TRAIN_URL = "https://raw.githubusercontent.com/danijel3/ClarinStudioKaldi/master/local_clarin/train.sessions"
|
| 60 |
+
_TEST_URL = "https://raw.githubusercontent.com/danijel3/ClarinStudioKaldi/master/local_clarin/test.sessions"
|
| 61 |
+
_VALID_URL = "https://raw.githubusercontent.com/danijel3/ClarinStudioKaldi/master/local_clarin/dev.sessions"
|
| 62 |
+
|
| 63 |
+
class ClarinPLStudioASRConfig(datasets.BuilderConfig):
|
| 64 |
+
"""BuilderConfig for ClarinPLStudioASR."""
|
| 65 |
+
|
| 66 |
+
def __init__(self, **kwargs):
|
| 67 |
+
"""
|
| 68 |
+
Args:
|
| 69 |
+
data_dir: `string`, the path to the folder containing the files in the
|
| 70 |
+
downloaded .tar
|
| 71 |
+
citation: `string`, citation for the data set
|
| 72 |
+
url: `string`, url for information about the data set
|
| 73 |
+
**kwargs: keyword arguments forwarded to super.
|
| 74 |
+
"""
|
| 75 |
+
super(ClarinPLStudioASRConfig, self).__init__(version=datasets.Version("2.1.0", ""), **kwargs)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
class ClarinPLStudio(datasets.GeneratorBasedBuilder):
|
| 79 |
+
"""ClarinPL Studio dataset."""
|
| 80 |
+
|
| 81 |
+
BUILDER_CONFIGS = [
|
| 82 |
+
ClarinPLStudioASRConfig(name="clean", description="'Clean' speech."),
|
| 83 |
+
]
|
| 84 |
+
|
| 85 |
+
def _info(self):
|
| 86 |
+
return datasets.DatasetInfo(
|
| 87 |
+
description=_DESCRIPTION,
|
| 88 |
+
features=datasets.Features(
|
| 89 |
+
{
|
| 90 |
+
"file": datasets.Value("string"),
|
| 91 |
+
"text": datasets.Value("string"),
|
| 92 |
+
"speaker_id": datasets.Value("string"),
|
| 93 |
+
"id": datasets.Value("string"),
|
| 94 |
+
}
|
| 95 |
+
),
|
| 96 |
+
supervised_keys=("file", "text"),
|
| 97 |
+
homepage=_URL,
|
| 98 |
+
citation=_CITATION,
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
def _split_generators(self, dl_manager):
|
| 102 |
+
def get_sessions(path):
|
| 103 |
+
sessions = []
|
| 104 |
+
with open(path, 'r') as f:
|
| 105 |
+
for line in f:
|
| 106 |
+
sessions.append(line.strip())
|
| 107 |
+
return sessions
|
| 108 |
+
archive_path = dl_manager.download_and_extract(_DS_URL)
|
| 109 |
+
train_sessions_path = dl_manager.download(_TRAIN_URL)
|
| 110 |
+
test_sessions_path = dl_manager.download(_TEST_URL)
|
| 111 |
+
valid_sessions_path = dl_manager.download(_VALID_URL)
|
| 112 |
+
|
| 113 |
+
train_sessions = get_sessions(train_sessions_path)
|
| 114 |
+
test_sessions = get_sessions(test_sessions_path)
|
| 115 |
+
valid_sessions = get_sessions(valid_sessions_path)
|
| 116 |
+
|
| 117 |
+
archive_path = os.path.join(archive_path, "audio")
|
| 118 |
+
return [
|
| 119 |
+
datasets.SplitGenerator(name="train", gen_kwargs={
|
| 120 |
+
"archive_path": archive_path,
|
| 121 |
+
"sessions": train_sessions
|
| 122 |
+
}),
|
| 123 |
+
datasets.SplitGenerator(name="test", gen_kwargs={
|
| 124 |
+
"archive_path": archive_path,
|
| 125 |
+
"sessions": test_sessions
|
| 126 |
+
}),
|
| 127 |
+
datasets.SplitGenerator(name="valid", gen_kwargs={
|
| 128 |
+
"archive_path": archive_path,
|
| 129 |
+
"sessions": valid_sessions
|
| 130 |
+
}),
|
| 131 |
+
]
|
| 132 |
+
|
| 133 |
+
def _generate_examples(self, archive_path, sessions):
|
| 134 |
+
"""Generate examples from a ClarinPL Studio archive_path."""
|
| 135 |
+
def get_single_line(path):
|
| 136 |
+
lines = []
|
| 137 |
+
with open(path, 'r', encoding="utf-8") as f:
|
| 138 |
+
for line in f:
|
| 139 |
+
line = line.strip()
|
| 140 |
+
lines.append(line)
|
| 141 |
+
assert(len(lines) == 1)
|
| 142 |
+
return lines[0]
|
| 143 |
+
for session in sessions:
|
| 144 |
+
session_path = os.path.join(archive_path, session)
|
| 145 |
+
speaker = get_single_line(os.path.join(session_path, "spk.txt"))
|
| 146 |
+
text_glob = os.path.join(session_path, "*.txt")
|
| 147 |
+
for text_file in sorted(glob.glob(text_glob)):
|
| 148 |
+
if text_file.endswith("spk.txt"):
|
| 149 |
+
continue
|
| 150 |
+
basename = os.path.basename(text_file)
|
| 151 |
+
basename = basename.replace('.txt', '')
|
| 152 |
+
key = f'{session}_{basename}'
|
| 153 |
+
text = get_single_line(text_file)
|
| 154 |
+
audio = text_file.replace('.txt', '.wav')
|
| 155 |
+
example = {
|
| 156 |
+
"id": key,
|
| 157 |
+
"speaker_id": speaker,
|
| 158 |
+
"file": audio,
|
| 159 |
+
"text": text,
|
| 160 |
+
}
|
| 161 |
+
yield key, example
|