jimregan commited on
Commit
b707341
1 Parent(s): 85f9bda

add script (untested)

Browse files
Files changed (1) hide show
  1. nst.py +200 -0
nst.py ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
20
+ import datasets
21
+ from datasets.tasks import AutomaticSpeechRecognition
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="dialects", version=VERSION, description="Data for dialect classification"),
70
+ ]
71
+
72
+ def _info(self):
73
+ features = datasets.Features(
74
+ {
75
+ "speaker_id": datasets.Value("string"),
76
+ "age": datasets.Value("string"),
77
+ "gender": datasets.ClassLabel(names=_SEX),
78
+ "region_of_birth": datasets.ClassLabel(names=_REGIONS),
79
+ "region_of_youth": datasets.ClassLabel(names=_REGIONS),
80
+ "text": datasets.Value("string"),
81
+ "path": datasets.Value("string"),
82
+ "audio": datasets.Audio(sampling_rate=16_000)
83
+ }
84
+ )
85
+
86
+ return datasets.DatasetInfo(
87
+ description=_DESCRIPTION,
88
+ features=features,
89
+ supervised_keys=None,
90
+ homepage=_URL,
91
+ task_templates=[
92
+ AutomaticSpeechRecognition(audio_file_path_column="path", transcription_column="text")
93
+ ],
94
+ )
95
+
96
+ # split is hardcoded to 'train' for now; there is a test set, but
97
+ # it has not been modernised
98
+ def _split_generators(self, dl_manager):
99
+ json_dir = dl_manager.download_and_extract(_JSON_URL)
100
+ audio_dirs = dl_manager.download_and_extract(_AUDIO_URLS)
101
+ return [
102
+ datasets.SplitGenerator(
103
+ name=datasets.Split.TRAIN,
104
+ gen_kwargs={
105
+ "split": "train",
106
+ "json_dir": json_dir,
107
+ "audio_dirs": audio_dirs,
108
+ },
109
+ ),
110
+ ]
111
+
112
+ def _generate_examples(
113
+ self, split, json_dir, audio_dirs
114
+ ):
115
+ """Yields examples as (key, example) tuples. """
116
+ json_path = Path(json_dir)
117
+ for json_filename in json_path.glob("*.json"):
118
+ with open(json_filename) as json_file:
119
+ data = json.load(json_file)
120
+ speaker_data = _get_speaker_data(data["info"])
121
+ pid = data["pid"]
122
+ print(pid)
123
+ for recording in data["val_recordings"]:
124
+ bare_path = recording['file'].replace(".wav", "")
125
+ text = recording["text"]
126
+ lang_part = pid[0:2]
127
+ for num in ["1", "2"]:
128
+ tar_path = f"{lang_part}/{pid}/{pid}_{bare_path}-{num}.wav"
129
+ for adir in audio_dirs:
130
+ fpath = Path(adir) / tar_path
131
+ if fpath.exists():
132
+ with open(fpath, "rb") as audiofile:
133
+ yield str(fpath), {
134
+ "speaker_id": speaker_data["speaker_id"],
135
+ "age": speaker_data["age"],
136
+ "gender": speaker_data["gender"],
137
+ "region_of_birth": speaker_data["region_of_birth"],
138
+ "region_of_youth": speaker_data["region_of_youth"],
139
+ "text": text,
140
+ "path": str(fpath),
141
+ "audio": {
142
+ "path": str(fpath),
143
+ "bytes": audiofile.read()
144
+ }
145
+ }
146
+
147
+
148
+ def _get_speaker_data(data):
149
+ out = {}
150
+ if "Age" in data:
151
+ if data["Age"] == "":
152
+ out["age"] = "Unspecified"
153
+ else:
154
+ out["age"] = data["Age"]
155
+ else:
156
+ out["age"] = "Unspecified"
157
+
158
+ if "Region_of_Birth" in data:
159
+ if data["Region_of_Birth"] == "":
160
+ out["region_of_birth"] = "Unspecified"
161
+ elif data["Region_of_Birth"] not in _REGIONS:
162
+ print("Unknown option for Region_of_Birth: " + data["Region_of_Birth"])
163
+ out["region_of_birth"] = "Unspecified"
164
+ else:
165
+ out["region_of_birth"] = data["Region_of_Birth"]
166
+ else:
167
+ out["region_of_birth"] = "Unspecified"
168
+
169
+ if "Region_of_Youth" in data:
170
+ if data["Region_of_Youth"] == "":
171
+ out["region_of_youth"] = "Unspecified"
172
+ elif data["Region_of_Youth"] not in _REGIONS:
173
+ print("Unknown option for Region_of_Youth: " + data["Region_of_Youth"])
174
+ out["region_of_youth"] = "Unspecified"
175
+ else:
176
+ out["region_of_youth"] = data["Region_of_Youth"]
177
+ else:
178
+ out["region_of_youth"] = "Unspecified"
179
+
180
+ if "Speaker_ID" in data:
181
+ if data["Speaker_ID"] == "":
182
+ out["speaker_id"] = "Unspecified"
183
+ else:
184
+ out["speaker_id"] = data["Speaker_ID"]
185
+ else:
186
+ out["speaker_id"] = "Unspecified"
187
+
188
+ if 'Sex' in data:
189
+ if data["Sex"] == "":
190
+ out["gender"] = "Unspecified"
191
+ elif data["Sex"] not in _SEX:
192
+ print("Unknown option for Sex: " + data["Sex"])
193
+ out["gender"] = "Unspecified"
194
+ else:
195
+ out["gender"] = data["Sex"]
196
+ else:
197
+ out["gender"] = "Unspecified"
198
+
199
+ return out
200
+