jimregan commited on
Commit
a175fea
·
1 Parent(s): b533f6d
Files changed (1) hide show
  1. nst.py +0 -228
nst.py DELETED
@@ -1,228 +0,0 @@
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
- # Lint as: python3
16
-
17
- from pathlib import Path
18
- import json
19
- import os
20
-
21
- import datasets
22
-
23
-
24
- _DESCRIPTION = """\
25
- This database was created by Nordic Language Technology for the development of automatic speech recognition and dictation in Swedish. In this updated version, the organization of the data have been altered to improve the usefulness of the database.
26
-
27
- In the original version of the material, the files were organized in a specific folder structure where the folder names were meaningful. However, the file names were not meaningful, and there were also cases of files with identical names in different folders. This proved to be impractical, since users had to keep the original folder structure in order to use the data. The files have been renamed, such that the file names are unique and meaningful regardless of the folder structure. The original metadata files were in spl format. These have been converted to JSON format. The converted metadata files are also anonymized and the text encoding has been converted from ANSI to UTF-8.
28
- """
29
-
30
- _URL = "https://www.nb.no/sprakbanken/en/resource-catalogue/oai-nb-no-sbr-56/"
31
-
32
-
33
- _JSON_URL = "https://www.nb.no/sbfil/talegjenkjenning/16kHz_2020/se_2020/ADB_SWE_0467.tar.gz"
34
-
35
-
36
- _AUDIO_URLS = [
37
- "https://www.nb.no/sbfil/talegjenkjenning/16kHz_2020/se_2020/lydfiler_16_1.tar.gz",
38
- "https://www.nb.no/sbfil/talegjenkjenning/16kHz_2020/se_2020/lydfiler_16_2.tar.gz"
39
- ]
40
-
41
-
42
- _REGIONS = [
43
- "Dalarna med omnejd",
44
- "Göteborg med omnejd",
45
- "Mellansverige",
46
- "Norrland",
47
- "Östergötland",
48
- "Östra sydsverige",
49
- "Stockholm med omnejd",
50
- "Västergötland",
51
- "Västra sydsverige",
52
- "Västsverige",
53
- "Unspecified"
54
- ]
55
-
56
- _SEX = [
57
- "Male",
58
- "Female",
59
- "Unspecified"
60
- ]
61
-
62
- class NSTDataset(datasets.GeneratorBasedBuilder):
63
- """NST Dataset for ASR"""
64
-
65
- VERSION = datasets.Version("1.1.0")
66
-
67
- BUILDER_CONFIGS = [
68
- datasets.BuilderConfig(name="speech", version=VERSION, description="Data for speech recognition"),
69
- datasets.BuilderConfig(name="speech_no_norm", version=VERSION, description="Data with original text (no normalisation)"),
70
- # datasets.BuilderConfig(name="dialects", version=VERSION, description="Data for dialect classification"),
71
- ]
72
-
73
- def _info(self):
74
- features = datasets.Features(
75
- {
76
- "speaker_id": datasets.Value("string"),
77
- "age": datasets.Value("string"),
78
- "gender": datasets.ClassLabel(names=_SEX),
79
- "region_of_birth": datasets.ClassLabel(names=_REGIONS),
80
- "region_of_youth": datasets.ClassLabel(names=_REGIONS),
81
- "text": datasets.Value("string"),
82
- "path": datasets.Value("string"),
83
- "audio": datasets.Audio(sampling_rate=16_000)
84
- }
85
- )
86
-
87
- return datasets.DatasetInfo(
88
- description=_DESCRIPTION,
89
- features=features,
90
- supervised_keys=None,
91
- homepage=_URL,
92
- )
93
-
94
- # split is hardcoded to 'train' for now; there is a test set, but
95
- # it has not been modernised
96
- def _split_generators(self, dl_manager):
97
- if hasattr(dl_manager, 'manual_dir') and dl_manager.manual_dir is not None:
98
- data_dir = os.path.abspath(os.path.expanduser(dl_manager.manual_dir))
99
- JSON_FILE = _JSON_URL.split("/")[-1]
100
- AUDIO_FILES = [
101
- os.path.join(data_dir, a.split("/")[-1]) for a in _AUDIO_URLS
102
- ]
103
- json_dir = dl_manager.extract(os.path.join(data_dir, JSON_FILE))
104
- audio_dirs = dl_manager.extract(AUDIO_FILES)
105
- else:
106
- json_dir = dl_manager.download_and_extract(_JSON_URL)
107
- audio_dirs = dl_manager.download_and_extract(_AUDIO_URLS)
108
- return [
109
- datasets.SplitGenerator(
110
- name=datasets.Split.TRAIN,
111
- gen_kwargs={
112
- "split": "train",
113
- "json_dir": json_dir,
114
- "audio_dirs": audio_dirs,
115
- },
116
- ),
117
- ]
118
-
119
- def _generate_examples(
120
- self, split, json_dir, audio_dirs
121
- ):
122
- """Yields examples as (key, example) tuples. """
123
- json_path = Path(json_dir)
124
- for json_filename in json_path.glob("*.json"):
125
- with open(json_filename) as json_file:
126
- data = json.load(json_file)
127
- speaker_data = _get_speaker_data(data["info"])
128
- pid = data["pid"]
129
- if "val_recordings" not in data:
130
- continue
131
- for recording in data["val_recordings"]:
132
- bare_path = recording['file'].replace(".wav", "")
133
- text = recording["text"]
134
- if self.config.name != "speech_no_norm":
135
- text = normalise(text)
136
- if text is None or text == "":
137
- continue
138
- lang_part = pid[0:2]
139
- for num in ["1", "2"]:
140
- tar_path = f"{lang_part}/{pid}/{pid}_{bare_path}-{num}.wav"
141
- for adir in audio_dirs:
142
- fpath = Path(adir) / tar_path
143
- if fpath.exists():
144
- with open(fpath, "rb") as audiofile:
145
- yield str(fpath), {
146
- "speaker_id": speaker_data["speaker_id"],
147
- "age": speaker_data["age"],
148
- "gender": speaker_data["gender"],
149
- "region_of_birth": speaker_data["region_of_birth"],
150
- "region_of_youth": speaker_data["region_of_youth"],
151
- "text": text,
152
- "path": str(fpath),
153
- "audio": {
154
- "path": str(fpath),
155
- "bytes": audiofile.read()
156
- }
157
- }
158
-
159
-
160
- def _get_speaker_data(data):
161
- out = {}
162
- if "Age" in data:
163
- if data["Age"] == "":
164
- out["age"] = "Unspecified"
165
- else:
166
- out["age"] = data["Age"]
167
- else:
168
- out["age"] = "Unspecified"
169
-
170
- if "Region_of_Birth" in data:
171
- if data["Region_of_Birth"] == "":
172
- out["region_of_birth"] = "Unspecified"
173
- elif data["Region_of_Birth"] not in _REGIONS:
174
- print("Unknown option for Region_of_Birth: " + data["Region_of_Birth"])
175
- out["region_of_birth"] = "Unspecified"
176
- else:
177
- out["region_of_birth"] = data["Region_of_Birth"]
178
- else:
179
- out["region_of_birth"] = "Unspecified"
180
-
181
- if "Region_of_Youth" in data:
182
- if data["Region_of_Youth"] == "":
183
- out["region_of_youth"] = "Unspecified"
184
- elif data["Region_of_Youth"] not in _REGIONS:
185
- print("Unknown option for Region_of_Youth: " + data["Region_of_Youth"])
186
- out["region_of_youth"] = "Unspecified"
187
- else:
188
- out["region_of_youth"] = data["Region_of_Youth"]
189
- else:
190
- out["region_of_youth"] = "Unspecified"
191
-
192
- if "Speaker_ID" in data:
193
- if data["Speaker_ID"] == "":
194
- out["speaker_id"] = "Unspecified"
195
- else:
196
- out["speaker_id"] = data["Speaker_ID"]
197
- else:
198
- out["speaker_id"] = "Unspecified"
199
-
200
- if 'Sex' in data:
201
- if data["Sex"] == "":
202
- out["gender"] = "Unspecified"
203
- elif data["Sex"] not in _SEX:
204
- print("Unknown option for Sex: " + data["Sex"])
205
- out["gender"] = "Unspecified"
206
- else:
207
- out["gender"] = data["Sex"]
208
- else:
209
- out["gender"] = "Unspecified"
210
-
211
- return out
212
-
213
-
214
- def normalise(text: str) -> str:
215
- MARKERS = ["[fil]", "[int]", "[spk]", "[sta]"]
216
- text = text.lower()
217
- for mark in MARKERS:
218
- text = text.replace(mark, "")
219
- outtext = ""
220
- last_char = ""
221
- for char in text:
222
- if char in "abcdefghijklmnopqrstuvwxyzåäö: ":
223
- if char == " " and last_char == " ":
224
- continue
225
- else:
226
- outtext = outtext + char
227
- last_char = char
228
- return outtext