|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""\ |
|
Script for loading air traffic control (ATC) speech datasets for automatic speech recognition (ASR). |
|
This script has been designed for ATC datasets that are in Kaldi format |
|
|
|
Required files: text, wav.scp and segments files |
|
|
|
- Databases |
|
- Training: |
|
- ATCOSIM, LDC-ATCC or, UWB-ATCC corpora. |
|
- Testing: |
|
- ATCO2-test-set-1h or 4h, LDC-ATCC or, UWB-ATCC corpora. |
|
""" |
|
|
|
import os |
|
import re |
|
|
|
import datasets |
|
import numpy as np |
|
import soundfile as sf |
|
from datasets.tasks import AutomaticSpeechRecognition |
|
|
|
_CITATION = """\ |
|
@article{zuluaga2022atco2, |
|
title={ATCO2 corpus: A Large-Scale Dataset for Research on Automatic Speech Recognition and Natural Language Understanding of Air Traffic Control Communications}, |
|
author={Zuluaga-Gomez, Juan and Vesel{\'y}, Karel and Sz{\"o}ke, Igor and Motlicek, Petr and others}, |
|
journal={arXiv preprint arXiv:2211.04054}, |
|
year={2022} |
|
} |
|
@article{zuluaga2022does, |
|
title={How Does Pre-trained Wav2Vec 2.0 Perform on Domain Shifted ASR? An Extensive Benchmark on Air Traffic Control Communications}, |
|
author={Zuluaga-Gomez, Juan and Prasad, Amrutha and Nigmatulina, Iuliia and others}, |
|
journal={2022 IEEE Spoken Language Technology Workshop (SLT), Doha, Qatar}, |
|
year={2022} |
|
} |
|
@article{zuluagabertraffic, |
|
title={BERTraffic: BERT-based Joint Speaker Role and Speaker Change Detection for Air Traffic Control Communications (submitted to @ SLT-2022)}, |
|
author={Zuluaga-Gomez, Juan and Sarfjoo, Seyyed Saeed and Prasad, Amrutha and others}, |
|
journal={2022 IEEE Spoken Language Technology Workshop (SLT), Doha, Qatar}, |
|
year={2022} |
|
} |
|
""" |
|
|
|
_DESCRIPTION = """\ |
|
ATC speech DATASET. This DataLoader works with data in Kaldi format. |
|
- We use the following files: text, segments and wav.scp |
|
- text --> utt_id transcript |
|
- segments --> utt_id recording_id t_begin t_end |
|
- wav.scp --> recording_id /path/to/wav/ |
|
The default dataset is from ATCO2 project, a 1-hour sample: https://www.replaywell.com/atco2/download/ATCO2-ASRdataset-v1_beta.tgz |
|
""" |
|
|
|
_DATA_URL = "http://catalog.elra.info/en-us/repository/browse/ELRA-S0484/" |
|
|
|
_HOMEPAGE = "https://github.com/idiap/w2v2-air-traffic" |
|
|
|
logger = datasets.logging.get_logger(__name__) |
|
|
|
|
|
_SAMPLING_RATE = int(16000) |
|
|
|
|
|
class ATCDataASRConfig(datasets.BuilderConfig): |
|
"""BuilderConfig for air traffic control datasets.""" |
|
|
|
def __init__(self, **kwargs): |
|
""" |
|
Args: |
|
data_dir: `string`, the path to the folder containing the files required to read: json or wav.scp |
|
**kwargs: keyword arguments forwarded to super. |
|
""" |
|
super(ATCDataASRConfig, self).__init__(**kwargs) |
|
|
|
|
|
class ATCDataASR(datasets.GeneratorBasedBuilder): |
|
|
|
DEFAULT_WRITER_BATCH_SIZE = 256 |
|
DEFAULT_CONFIG_NAME = "all" |
|
BUILDER_CONFIGS = [ |
|
|
|
ATCDataASRConfig(name="train", description="ATC train dataset."), |
|
ATCDataASRConfig(name="dev", description="ATC dev dataset."), |
|
ATCDataASRConfig(name="test", description="ATC test dataset."), |
|
|
|
ATCDataASRConfig(name="unsupervised", description="ATC unsupervised dataset."), |
|
] |
|
|
|
|
|
def _info(self): |
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=datasets.Features( |
|
{ |
|
"id": datasets.Value("string"), |
|
"file": datasets.Value("string"), |
|
"audio": datasets.features.Audio(sampling_rate=_SAMPLING_RATE), |
|
"text": datasets.Value("string"), |
|
"segment_start_time": datasets.Value("float"), |
|
"segment_end_time": datasets.Value("float"), |
|
"duration": datasets.Value("float"), |
|
} |
|
), |
|
supervised_keys=("audio", "text"), |
|
homepage=_HOMEPAGE, |
|
citation=_CITATION, |
|
task_templates=[ |
|
AutomaticSpeechRecognition( |
|
audio_column="audio", transcription_column="text" |
|
) |
|
], |
|
) |
|
|
|
def _split_generators(self, dlmanager): |
|
"""Returns SplitGenerators.""" |
|
|
|
split = self.config.name |
|
|
|
|
|
if "unsupervised" in split: |
|
split_name = datasets.Split.TEST |
|
elif "test" in split or "dev" in split or "dummy" in split: |
|
split_name = datasets.Split.TEST |
|
|
|
else: |
|
split_name = datasets.Split.TRAIN |
|
|
|
|
|
filepath = self.config.data_dir |
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=split_name, |
|
|
|
gen_kwargs={ |
|
"filepath": filepath, |
|
"split": split, |
|
}, |
|
) |
|
] |
|
|
|
def _generate_examples(self, filepath, split): |
|
"""You need to pass a path with the kaldi data, the folder should have |
|
audio: wav.scp, |
|
transcripts: text, |
|
timing information: segments |
|
""" |
|
|
|
logger.info("Generating examples located in: %s", filepath) |
|
|
|
text_file = os.path.join(filepath, "text") |
|
wavscp = os.path.join(filepath, "wav.scp") |
|
segments = os.path.join(filepath, "segments") |
|
|
|
id_ = "" |
|
text_dict, wav_dict = {}, {} |
|
segments_dict, utt2wav_id = {}, {} |
|
|
|
line = 0 |
|
|
|
with open(text_file) as text_f: |
|
for line in text_f: |
|
if len(line.split(" ")) > 1: |
|
id_, transcript = line.split(" ", maxsplit=1) |
|
transcript = _remove_special_characters(transcript) |
|
if len(transcript.split(" ")) == 0: |
|
continue |
|
if len(transcript) < 2: |
|
continue |
|
text_dict[id_] = transcript |
|
else: |
|
|
|
if not "test_unsup" in self.config.name: |
|
continue |
|
id_ = line.rstrip().split(" ")[0] |
|
text_dict[id_] = "" |
|
|
|
|
|
with open(wavscp) as text_f: |
|
for line in text_f: |
|
if line: |
|
if len(line.split()) < 2: |
|
continue |
|
id_, wavpath = line.split(" ", maxsplit=1) |
|
|
|
wavpath = [ |
|
x |
|
for x in wavpath.split(" ") |
|
if ".wav" in x or ".WAV" in x or ".flac" in x or ".sph" in x |
|
][0].rstrip() |
|
|
|
|
|
segment, sampling_rate = sf.read(wavpath, dtype=np.int16) |
|
wav_dict[id_] = [wavpath.rstrip(), segment, sampling_rate] |
|
|
|
|
|
with open(segments) as text_f: |
|
for line in text_f: |
|
if line: |
|
if len(line.split()) < 4: |
|
continue |
|
id_, wavid_, start, end = line.rstrip().split(" ") |
|
segments_dict[id_] = start.rstrip(), end.rstrip() |
|
utt2wav_id[id_] = wavid_ |
|
|
|
for rec_id, text in text_dict.items(): |
|
if rec_id in utt2wav_id and rec_id in segments_dict: |
|
|
|
|
|
wavpath, segment, sampling_rate = wav_dict[utt2wav_id[rec_id]] |
|
|
|
seg_start, seg_end = segments_dict[rec_id] |
|
seg_start, seg_end = float(seg_start), float(seg_end) |
|
duration = round((seg_end - seg_start), 3) |
|
|
|
|
|
samples = _extract_audio_segment( |
|
segment, sampling_rate, float(seg_start), float(seg_end) |
|
) |
|
|
|
|
|
example = { |
|
"audio": { |
|
"path": wavpath, |
|
"array": samples, |
|
"sampling_rate": sampling_rate, |
|
}, |
|
"id": rec_id, |
|
"file": wavpath, |
|
"text": text, |
|
"segment_start_time": format(float(seg_start), ".3f"), |
|
"segment_end_time": format(float(seg_end), ".3f"), |
|
"duration": format(float(duration), ".3f"), |
|
} |
|
|
|
yield rec_id, example |
|
|
|
|
|
def _remove_special_characters(text): |
|
"""Function to remove some special chars/symbols from the given transcript""" |
|
|
|
text = text.split(" ") |
|
|
|
text = " ".join( |
|
[ |
|
x |
|
for x in text |
|
if "[" not in x and "]" not in x and "<" not in x and ">" not in x |
|
] |
|
) |
|
|
|
|
|
chars_to_ignore_regex2 = '[\{\[\]\<\>\/\,\?\.\!\u00AC\;\:"\\%\\\]|[0-9]' |
|
|
|
text = re.sub(chars_to_ignore_regex2, "", text).lower() |
|
sentence = text.replace("\u2013", "-") |
|
sentence = sentence.replace("\u2014", "-") |
|
sentence = sentence.replace("\u2018", "'") |
|
sentence = sentence.replace("\u201C", "") |
|
sentence = sentence.replace("\u201D", "") |
|
sentence = sentence.replace("ñ", "n") |
|
sentence = sentence.replace(" - ", " ") |
|
sentence = sentence.replace("-", "") |
|
sentence = sentence.replace("'", " ") |
|
|
|
return sentence.lower().rstrip() |
|
|
|
|
|
def _extract_audio_segment(segment, sampling_rate, start_sec, end_sec): |
|
"""Extracts segment of audio samples (as an ndarray) from the given segment.""" |
|
|
|
start_sample = int(start_sec * sampling_rate) |
|
end_sample = min(int(end_sec * sampling_rate), segment.shape[0]) |
|
samples = segment[start_sample:end_sample] |
|
return samples |
|
|