Jzuluaga commited on
Commit
c770e0d
1 Parent(s): d14c888

updating the repo with the loader script

Browse files
Files changed (1) hide show
  1. atc_data_loader.py +282 -0
atc_data_loader.py ADDED
@@ -0,0 +1,282 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ #
4
+ # SPDX-FileCopyrightText: Copyright © <2022> Idiap Research Institute <[email protected]>
5
+ #
6
+ # SPDX-FileContributor: Juan Zuluaga-Gomez <[email protected]>
7
+ #
8
+ # SPDX-License-Identifier: MIT-License
9
+
10
+ """\
11
+ Script for loading air traffic control (ATC) speech datasets for automatic speech recognition (ASR).
12
+ This script has been designed for ATC datasets that are in Kaldi format
13
+
14
+ Required files: text, wav.scp and segments files
15
+
16
+ - Databases
17
+ - Training:
18
+ - ATCOSIM, LDC-ATCC or, UWB-ATCC corpora.
19
+ - Testing:
20
+ - ATCO2-test-set-1h or 4h, LDC-ATCC or, UWB-ATCC corpora.
21
+ """
22
+
23
+ import os
24
+ import re
25
+
26
+ import datasets
27
+ import numpy as np
28
+ import soundfile as sf
29
+ from datasets.tasks import AutomaticSpeechRecognition
30
+
31
+ _CITATION = """\
32
+ @article{zuluaga2022atco2,
33
+ title={ATCO2 corpus: A Large-Scale Dataset for Research on Automatic Speech Recognition and Natural Language Understanding of Air Traffic Control Communications},
34
+ author={Zuluaga-Gomez, Juan and Vesel{\'y}, Karel and Sz{\"o}ke, Igor and Motlicek, Petr and others},
35
+ journal={arXiv preprint arXiv:2211.04054},
36
+ year={2022}
37
+ }
38
+ @article{zuluaga2022does,
39
+ title={How Does Pre-trained Wav2Vec 2.0 Perform on Domain Shifted ASR? An Extensive Benchmark on Air Traffic Control Communications},
40
+ author={Zuluaga-Gomez, Juan and Prasad, Amrutha and Nigmatulina, Iuliia and others},
41
+ journal={2022 IEEE Spoken Language Technology Workshop (SLT), Doha, Qatar},
42
+ year={2022}
43
+ }
44
+ @article{zuluagabertraffic,
45
+ title={BERTraffic: BERT-based Joint Speaker Role and Speaker Change Detection for Air Traffic Control Communications (submitted to @ SLT-2022)},
46
+ author={Zuluaga-Gomez, Juan and Sarfjoo, Seyyed Saeed and Prasad, Amrutha and others},
47
+ journal={2022 IEEE Spoken Language Technology Workshop (SLT), Doha, Qatar},
48
+ year={2022}
49
+ }
50
+ """
51
+
52
+ _DESCRIPTION = """\
53
+ ATC speech DATASET. This DataLoader works with data in Kaldi format.
54
+ - We use the following files: text, segments and wav.scp
55
+ - text --> utt_id transcript
56
+ - segments --> utt_id recording_id t_begin t_end
57
+ - wav.scp --> recording_id /path/to/wav/
58
+ The default dataset is from ATCO2 project, a 1-hour sample: https://www.replaywell.com/atco2/download/ATCO2-ASRdataset-v1_beta.tgz
59
+ """
60
+
61
+ _DATA_URL = "http://catalog.elra.info/en-us/repository/browse/ELRA-S0484/"
62
+
63
+ _HOMEPAGE = "https://github.com/idiap/w2v2-air-traffic"
64
+
65
+ logger = datasets.logging.get_logger(__name__)
66
+
67
+ # Our models work with audio data at 16kHZ,
68
+ _SAMPLING_RATE = int(16000)
69
+
70
+
71
+ class ATCDataASRConfig(datasets.BuilderConfig):
72
+ """BuilderConfig for air traffic control datasets."""
73
+
74
+ def __init__(self, **kwargs):
75
+ """
76
+ Args:
77
+ data_dir: `string`, the path to the folder containing the files required to read: json or wav.scp
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(ATCDataASRConfig, self).__init__(**kwargs)
81
+
82
+
83
+ class ATCDataASR(datasets.GeneratorBasedBuilder):
84
+
85
+ DEFAULT_WRITER_BATCH_SIZE = 256
86
+ DEFAULT_CONFIG_NAME = "all"
87
+ BUILDER_CONFIGS = [
88
+ # TRAIN, DEV AND TEST DATASETS
89
+ ATCDataASRConfig(name="train", description="ATC train dataset."),
90
+ ATCDataASRConfig(name="dev", description="ATC dev dataset."),
91
+ ATCDataASRConfig(name="test", description="ATC test dataset."),
92
+ # UNSUPERVISED DATASETS
93
+ ATCDataASRConfig(name="unsupervised", description="ATC unsupervised dataset."),
94
+ ]
95
+
96
+ # provide some information about the Dataset we just gathered
97
+ def _info(self):
98
+ return datasets.DatasetInfo(
99
+ description=_DESCRIPTION,
100
+ features=datasets.Features(
101
+ {
102
+ "id": datasets.Value("string"),
103
+ "file": datasets.Value("string"),
104
+ "audio": datasets.features.Audio(sampling_rate=_SAMPLING_RATE),
105
+ "text": datasets.Value("string"),
106
+ "segment_start_time": datasets.Value("float"),
107
+ "segment_end_time": datasets.Value("float"),
108
+ "duration": datasets.Value("float"),
109
+ }
110
+ ),
111
+ supervised_keys=("audio", "text"),
112
+ homepage=_HOMEPAGE,
113
+ citation=_CITATION,
114
+ task_templates=[
115
+ AutomaticSpeechRecognition(
116
+ audio_column="audio", transcription_column="text"
117
+ )
118
+ ],
119
+ )
120
+
121
+ def _split_generators(self, dlmanager):
122
+ """Returns SplitGenerators."""
123
+
124
+ split = self.config.name
125
+
126
+ # UNSUPERVISED set (used only for decoding)
127
+ if "unsupervised" in split:
128
+ split_name = datasets.Split.TEST
129
+ elif "test" in split or "dev" in split or "dummy" in split:
130
+ split_name = datasets.Split.TEST
131
+ # The last option left is: Train set
132
+ else:
133
+ split_name = datasets.Split.TRAIN
134
+
135
+ # you need to pass a data directory where the Kaldi folder is stored
136
+ filepath = self.config.data_dir
137
+
138
+ return [
139
+ datasets.SplitGenerator(
140
+ name=split_name,
141
+ # These kwargs will be passed to _generate_examples
142
+ gen_kwargs={
143
+ "filepath": filepath,
144
+ "split": split,
145
+ },
146
+ )
147
+ ]
148
+
149
+ def _generate_examples(self, filepath, split):
150
+ """You need to pass a path with the kaldi data, the folder should have
151
+ audio: wav.scp,
152
+ transcripts: text,
153
+ timing information: segments
154
+ """
155
+
156
+ logger.info("Generating examples located in: %s", filepath)
157
+
158
+ text_file = os.path.join(filepath, "text")
159
+ wavscp = os.path.join(filepath, "wav.scp")
160
+ segments = os.path.join(filepath, "segments")
161
+
162
+ id_ = ""
163
+ text_dict, wav_dict = {}, {}
164
+ segments_dict, utt2wav_id = {}, {}
165
+
166
+ line = 0
167
+ # get the text file
168
+ with open(text_file) as text_f:
169
+ for line in text_f:
170
+ if len(line.split(" ")) > 1:
171
+ id_, transcript = line.split(" ", maxsplit=1)
172
+ transcript = _remove_special_characters(transcript)
173
+ if len(transcript.split(" ")) == 0:
174
+ continue
175
+ if len(transcript) < 2:
176
+ continue
177
+ text_dict[id_] = transcript
178
+ else: # line is empty
179
+ # if unsupervised set, then it's normal. else, continue
180
+ if not "test_unsup" in self.config.name:
181
+ continue
182
+ id_ = line.rstrip().split(" ")[0]
183
+ text_dict[id_] = ""
184
+
185
+ # get wav.scp and load data into memory
186
+ with open(wavscp) as text_f:
187
+ for line in text_f:
188
+ if line:
189
+ if len(line.split()) < 2:
190
+ continue
191
+ id_, wavpath = line.split(" ", maxsplit=1)
192
+ # only selects the part that ends of wav, flac or sph
193
+ wavpath = [
194
+ x
195
+ for x in wavpath.split(" ")
196
+ if ".wav" in x or ".WAV" in x or ".flac" in x or ".sph" in x
197
+ ][0].rstrip()
198
+
199
+ # make the output
200
+ segment, sampling_rate = sf.read(wavpath, dtype=np.int16)
201
+ wav_dict[id_] = [wavpath.rstrip(), segment, sampling_rate]
202
+
203
+ # get segments dictionary
204
+ with open(segments) as text_f:
205
+ for line in text_f:
206
+ if line:
207
+ if len(line.split()) < 4:
208
+ continue
209
+ id_, wavid_, start, end = line.rstrip().split(" ")
210
+ segments_dict[id_] = start.rstrip(), end.rstrip()
211
+ utt2wav_id[id_] = wavid_
212
+
213
+ for rec_id, text in text_dict.items():
214
+ if rec_id in utt2wav_id and rec_id in segments_dict:
215
+
216
+ # get audio data from memory and the path of the file
217
+ wavpath, segment, sampling_rate = wav_dict[utt2wav_id[rec_id]]
218
+ # get timing information
219
+ seg_start, seg_end = segments_dict[rec_id]
220
+ seg_start, seg_end = float(seg_start), float(seg_end)
221
+ duration = round((seg_end - seg_start), 3)
222
+
223
+ # get the samples, bytes, already cropping by segment,
224
+ samples = _extract_audio_segment(
225
+ segment, sampling_rate, float(seg_start), float(seg_end)
226
+ )
227
+
228
+ # output data for given dataset
229
+ example = {
230
+ "audio": {
231
+ "path": wavpath,
232
+ "array": samples,
233
+ "sampling_rate": sampling_rate,
234
+ },
235
+ "id": rec_id,
236
+ "file": wavpath,
237
+ "text": text,
238
+ "segment_start_time": format(float(seg_start), ".3f"),
239
+ "segment_end_time": format(float(seg_end), ".3f"),
240
+ "duration": format(float(duration), ".3f"),
241
+ }
242
+
243
+ yield rec_id, example
244
+
245
+
246
+ def _remove_special_characters(text):
247
+ """Function to remove some special chars/symbols from the given transcript"""
248
+
249
+ text = text.split(" ")
250
+ # first remove words between [] and <>
251
+ text = " ".join(
252
+ [
253
+ x
254
+ for x in text
255
+ if "[" not in x and "]" not in x and "<" not in x and ">" not in x
256
+ ]
257
+ )
258
+
259
+ # regex with predifined symbols to ignore/remove,
260
+ chars_to_ignore_regex2 = '[\{\[\]\<\>\/\,\?\.\!\u00AC\;\:"\\%\\\]|[0-9]'
261
+
262
+ text = re.sub(chars_to_ignore_regex2, "", text).lower()
263
+ sentence = text.replace("\u2013", "-")
264
+ sentence = sentence.replace("\u2014", "-")
265
+ sentence = sentence.replace("\u2018", "'")
266
+ sentence = sentence.replace("\u201C", "")
267
+ sentence = sentence.replace("\u201D", "")
268
+ sentence = sentence.replace("ñ", "n")
269
+ sentence = sentence.replace(" - ", " ")
270
+ sentence = sentence.replace("-", "")
271
+ sentence = sentence.replace("'", " ")
272
+
273
+ return sentence.lower().rstrip()
274
+
275
+
276
+ def _extract_audio_segment(segment, sampling_rate, start_sec, end_sec):
277
+ """Extracts segment of audio samples (as an ndarray) from the given segment."""
278
+ # The dataset only contains mono audio.
279
+ start_sample = int(start_sec * sampling_rate)
280
+ end_sample = min(int(end_sec * sampling_rate), segment.shape[0])
281
+ samples = segment[start_sample:end_sample]
282
+ return samples