File size: 14,811 Bytes
0e7578f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88dc214
0e7578f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# coding=utf-8

"""TIMIT automatic speech recognition and speaker identification dataset."""


import os
import textwrap
import datasets
import itertools
import typing as tp
from pathlib import Path


SAMPLE_RATE = 16_000

_ZIP_FILENAME = 'timit.tar.gz'

_CITATION = """\
@inproceedings{
  title={TIMIT Acoustic-Phonetic Continuous Speech Corpus},
  author={Garofolo, John S., et al},
  ldc_catalog_no={LDC93S1},
  DOI={https://doi.org/10.35111/17gk-bn40},
  journal={Linguistic Data Consortium, Philadelphia},
  year={1983}
}
"""

_DESCRIPTION = """\
The TIMIT corpus of reading speech has been developed to provide speech data for acoustic-phonetic research studies
and for the evaluation of automatic speech recognition systems.
TIMIT contains high quality recordings of 630 individuals/speakers with 8 different American English dialects,
with each individual reading upto 10 phonetically rich sentences.
More info on TIMIT dataset can be understood from the "README" which can be found here:
https://catalog.ldc.upenn.edu/docs/LDC93S1/readme.txt
"""

_HOMEPAGE = "https://catalog.ldc.upenn.edu/LDC93S1"
_SPEAKERS = ['FADG0', 'FAEM0', 'FAJW0', 'FAKS0', 'FALK0', 'FALR0', 'FAPB0', 'FASW0', 'FAWF0', 'FBAS0', 'FBCG1', 'FBCH0', 'FBJL0', 'FBLV0', 'FBMH0', 'FBMJ0', 'FCAG0', 'FCAJ0', 'FCAL1', 'FCAU0', 'FCDR1', 'FCEG0', 'FCFT0', 'FCJF0', 'FCJS0', 'FCKE0', 'FCLT0', 'FCMG0', 'FCMH0', 'FCMH1', 'FCMM0', 'FCMR0', 'FCRH0', 'FCRZ0', 'FCYL0', 'FDAC1', 'FDAS1', 'FDAW0', 'FDFB0', 'FDHC0', 'FDJH0', 'FDKN0', 'FDML0', 'FDMS0', 'FDMY0', 'FDNC0', 'FDRD1', 'FDRW0', 'FDTD0', 'FDXW0', 'FEAC0', 'FEAR0', 'FECD0', 'FEDW0', 'FEEH0', 'FELC0', 'FEME0', 'FETB0', 'FEXM0', 'FGCS0', 'FGDP0', 'FGJD0', 'FGMB0', 'FGMD0', 'FGRW0', 'FGWR0', 'FHES0', 'FHEW0', 'FHLM0', 'FHXS0', 'FISB0', 'FJAS0', 'FJCS0', 'FJDM2', 'FJEM0', 'FJEN0', 'FJHK0', 'FJKL0', 'FJLG0', 'FJLM0', 'FJLR0', 'FJMG0', 'FJRB0', 'FJRE0', 'FJRP1', 'FJSA0', 'FJSJ0', 'FJSK0', 'FJSP0', 'FJWB0', 'FJWB1', 'FJXM0', 'FJXP0', 'FKAA0', 'FKDE0', 'FKDW0', 'FKFB0', 'FKKH0', 'FKLC0', 'FKLC1', 'FKLH0', 'FKMS0', 'FKSR0', 'FLAC0', 'FLAG0', 'FLAS0', 'FLBW0', 'FLEH0', 'FLET0', 'FLHD0', 'FLJA0', 'FLJD0', 'FLJG0', 'FLKD0', 'FLKM0', 'FLMA0', 'FLMC0', 'FLMK0', 'FLNH0', 'FLOD0', 'FLTM0', 'FMAF0', 'FMAH0', 'FMAH1', 'FMBG0', 'FMCM0', 'FMEM0', 'FMGD0', 'FMJB0', 'FMJF0', 'FMJU0', 'FMKC0', 'FMKF0', 'FMLD0', 'FMMH0', 'FMML0', 'FMPG0', 'FNKL0', 'FNLP0', 'FNMR0', 'FNTB0', 'FPAB1', 'FPAC0', 'FPAD0', 'FPAF0', 'FPAS0', 'FPAZ0', 'FPJF0', 'FPKT0', 'FPLS0', 'FPMY0', 'FRAM1', 'FREH0', 'FREW0', 'FRJB0', 'FRLL0', 'FRNG0', 'FSAG0', 'FSAH0', 'FSAK0', 'FSBK0', 'FSCN0', 'FSDC0', 'FSDJ0', 'FSEM0', 'FSGF0', 'FSJG0', 'FSJK1', 'FSJS0', 'FSJW0', 'FSKC0', 'FSKL0', 'FSKP0', 'FSLB1', 'FSLS0', 'FSMA0', 'FSMM0', 'FSMS1', 'FSPM0', 'FSRH0', 'FSSB0', 'FSXA0', 'FTAJ0', 'FTBR0', 'FTBW0', 'FTLG0', 'FTLH0', 'FTMG0', 'FUTB0', 'FVFB0', 'FVKB0', 'FVMH0', 'MABC0', 'MABW0', 'MADC0', 'MADD0', 'MAEB0', 'MAEO0', 'MAFM0', 'MAHH0', 'MAJC0', 'MAJP0', 'MAKB0', 'MAKR0', 'MAPV0', 'MARC0', 'MARW0', 'MBAR0', 'MBBR0', 'MBCG0', 'MBDG0', 'MBEF0', 'MBGT0', 'MBJK0', 'MBJV0', 'MBMA0', 'MBMA1', 'MBML0', 'MBNS0', 'MBOM0', 'MBPM0', 'MBSB0', 'MBTH0', 'MBWM0', 'MBWP0', 'MCAE0', 'MCAL0', 'MCCS0', 'MCDC0', 'MCDD0', 'MCDR0', 'MCEF0', 'MCEM0', 'MCEW0', 'MCHH0', 'MCHL0', 'MCLK0', 'MCLM0', 'MCMB0', 'MCMJ0', 'MCPM0', 'MCRC0', 'MCRE0', 'MCSH0', 'MCSS0', 'MCTH0', 'MCTM0', 'MCTT0', 'MCTW0', 'MCXM0', 'MDAB0', 'MDAC0', 'MDAC2', 'MDAS0', 'MDAW1', 'MDBB0', 'MDBB1', 'MDBP0', 'MDCD0', 'MDCM0', 'MDDC0', 'MDED0', 'MDEF0', 'MDEM0', 'MDHL0', 'MDHS0', 'MDJM0', 'MDKS0', 'MDLB0', 'MDLC0', 'MDLC1', 'MDLC2', 'MDLD0', 'MDLF0', 'MDLH0', 'MDLM0', 'MDLR0', 'MDLR1', 'MDLS0', 'MDMA0', 'MDMT0', 'MDNS0', 'MDPB0', 'MDPK0', 'MDPS0', 'MDRB0', 'MDRD0', 'MDRM0', 'MDSC0', 'MDSJ0', 'MDSS0', 'MDSS1', 'MDTB0', 'MDVC0', 'MDWA0', 'MDWD0', 'MDWH0', 'MDWK0', 'MDWM0', 'MEAL0', 'MEDR0', 'MEFG0', 'MEGJ0', 'MEJL0', 'MEJS0', 'MERS0', 'MESD0', 'MESG0', 'MESJ0', 'MEWM0', 'MFER0', 'MFGK0', 'MFMC0', 'MFRM0', 'MFWK0', 'MFXS0', 'MFXV0', 'MGAF0', 'MGAG0', 'MGAK0', 'MGAR0', 'MGAW0', 'MGES0', 'MGJC0', 'MGJF0', 'MGLB0', 'MGMM0', 'MGRL0', 'MGRP0', 'MGRT0', 'MGSH0', 'MGSL0', 'MGWT0', 'MGXP0', 'MHBS0', 'MHIT0', 'MHJB0', 'MHMG0', 'MHMR0', 'MHPG0', 'MHRM0', 'MHXL0', 'MILB0', 'MJAC0', 'MJAE0', 'MJAI0', 'MJAR0', 'MJBG0', 'MJBR0', 'MJDA0', 'MJDC0', 'MJDE0', 'MJDG0', 'MJDH0', 'MJDM0', 'MJDM1', 'MJEB0', 'MJEB1', 'MJEE0', 'MJES0', 'MJFC0', 'MJFH0', 'MJFR0', 'MJHI0', 'MJJB0', 'MJJG0', 'MJJJ0', 'MJJM0', 'MJKR0', 'MJLB0', 'MJLG1', 'MJLN0', 'MJLS0', 'MJMA0', 'MJMD0', 'MJMM0', 'MJMP0', 'MJPG0', 'MJPM0', 'MJPM1', 'MJRA0', 'MJRF0', 'MJRG0', 'MJRH0', 'MJRH1', 'MJRK0', 'MJRP0', 'MJSR0', 'MJSW0', 'MJTC0', 'MJTH0', 'MJVW0', 'MJWG0', 'MJWS0', 'MJWT0', 'MJXA0', 'MJXL0', 'MKAG0', 'MKAH0', 'MKAJ0', 'MKAM0', 'MKCH0', 'MKCL0', 'MKDB0', 'MKDD0', 'MKDR0', 'MKDT0', 'MKES0', 'MKJL0', 'MKJO0', 'MKLN0', 'MKLR0', 'MKLS0', 'MKLS1', 'MKLT0', 'MKLW0', 'MKRG0', 'MKXL0', 'MLBC0', 'MLEL0', 'MLIH0', 'MLJB0', 'MLJC0', 'MLJH0', 'MLLL0', 'MLNS0', 'MLNT0', 'MLSH0', 'MMAA0', 'MMAB0', 'MMAB1', 'MMAG0', 'MMAM0', 'MMAR0', 'MMBS0', 'MMCC0', 'MMDB0', 'MMDB1', 'MMDG0', 'MMDH0', 'MMDM0', 'MMDM1', 'MMDM2', 'MMDS0', 'MMEA0', 'MMEB0', 'MMGC0', 'MMGG0', 'MMGK0', 'MMJB1', 'MMJR0', 'MMLM0', 'MMPM0', 'MMRP0', 'MMSM0', 'MMVP0', 'MMWB0', 'MMWH0', 'MMWS0', 'MMWS1', 'MMXS0', 'MNET0', 'MNJM0', 'MNLS0', 'MNTW0', 'MPAB0', 'MPAM0', 'MPAM1', 'MPAR0', 'MPCS0', 'MPDF0', 'MPEB0', 'MPFU0', 'MPGH0', 'MPGL0', 'MPGR0', 'MPGR1', 'MPLB0', 'MPMB0', 'MPPC0', 'MPRB0', 'MPRD0', 'MPRK0', 'MPRT0', 'MPSW0', 'MPWM0', 'MRAB0', 'MRAB1', 'MRAI0', 'MRAM0', 'MRAV0', 'MRBC0', 'MRCG0', 'MRCS0', 'MRCW0', 'MRCZ0', 'MRDD0', 'MRDM0', 'MRDS0', 'MREB0', 'MREE0', 'MREH1', 'MREM0', 'MRES0', 'MREW1', 'MRFK0', 'MRFL0', 'MRGG0', 'MRGM0', 'MRGS0', 'MRHL0', 'MRJB1', 'MRJH0', 'MRJM0', 'MRJM1', 'MRJM3', 'MRJM4', 'MRJO0', 'MRJR0', 'MRJS0', 'MRJT0', 'MRKM0', 'MRKO0', 'MRLD0', 'MRLJ0', 'MRLJ1', 'MRLK0', 'MRLR0', 'MRMB0', 'MRMG0', 'MRMH0', 'MRML0', 'MRMS0', 'MRMS1', 'MROA0', 'MRPC0', 'MRPC1', 'MRPP0', 'MRRE0', 'MRRK0', 'MRSO0', 'MRSP0', 'MRTC0', 'MRTJ0', 'MRTK0', 'MRVG0', 'MRWA0', 'MRWS0', 'MRWS1', 'MRXB0', 'MSAH1', 'MSAS0', 'MSAT0', 'MSAT1', 'MSDB0', 'MSDH0', 'MSDS0', 'MSEM1', 'MSES0', 'MSFH0', 'MSFH1', 'MSFV0', 'MSJK0', 'MSJS1', 'MSLB0', 'MSMC0', 'MSMR0', 'MSMS0', 'MSRG0', 'MSRR0', 'MSTF0', 'MSTK0', 'MSVS0', 'MTAA0', 'MTAB0', 'MTAS0', 'MTAS1', 'MTAT0', 'MTAT1', 'MTBC0', 'MTCS0', 'MTDB0', 'MTDP0', 'MTDT0', 'MTEB0', 'MTER0', 'MTHC0', 'MTJG0', 'MTJM0', 'MTJS0', 'MTJU0', 'MTKD0', 'MTKP0', 'MTLB0', 'MTLC0', 'MTLS0', 'MTML0', 'MTMN0', 'MTMR0', 'MTMT0', 'MTPF0', 'MTPG0', 'MTPP0', 'MTPR0', 'MTQC0', 'MTRC0', 'MTRR0', 'MTRT0', 'MTWH0', 'MTWH1', 'MTXS0', 'MVJH0', 'MVLO0', 'MVRW0', 'MWAC0', 'MWAD0', 'MWAR0', 'MWBT0', 'MWCH0', 'MWDK0', 'MWEM0', 'MWEW0', 'MWGR0', 'MWJG0', 'MWRE0', 'MWRP0', 'MWSB0', 'MWSH0', 'MWVW0', 'MZMB0']


class TimitConfig(datasets.BuilderConfig):
    """BuilderConfig for TIMIT."""
    
    def __init__(self, features, **kwargs):
        super(TimitConfig, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs)
        self.features = features


class TIMIT(datasets.GeneratorBasedBuilder):

    BUILDER_CONFIGS = [
        TimitConfig(
            features=datasets.Features(
                {
                    "file": datasets.Value("string"),
                    "audio": datasets.Audio(sampling_rate=SAMPLE_RATE),
                    "speaker_id": datasets.Value("string"),
                    "label": datasets.ClassLabel(names=_SPEAKERS),
                }
            ),
            name="si", 
            description=textwrap.dedent(
                """\
                Speaker Identification (SI) classifies each utterance for its speaker identity as a multi-class
                classification, where speakers are in the same pre-defined set for both training and testing. 
                The evaluation metric is accuracy (ACC).
                """
            ),
        ), 
        TimitConfig(
            features=datasets.Features(
                {
                    "file": datasets.Value("string"),
                    "audio": datasets.Audio(sampling_rate=SAMPLE_RATE), 
                    "phonetic_detail": datasets.Sequence(
                        {
                            "start": datasets.Value("int64"),
                            "stop": datasets.Value("int64"),
                            "utterance": datasets.Value("string"),
                        }
                    ),
                    "word_detail": datasets.Sequence(
                        {
                            "start": datasets.Value("int64"),
                            "stop": datasets.Value("int64"),
                            "utterance": datasets.Value("string"),
                        }
                    ),
                    "text": datasets.Value("string"),
                }
            ),
            name="asr", 
            description=textwrap.dedent(
                """\
                ASR transcribes utterances into words. While PR analyses the
                improvement in modeling phonetics, ASR reflects the significance of
                the improvement in a real-world scenario. 
                The evaluation metric is word error rate (WER).
                """
            ),
        )
    ]

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=self.config.features,
            supervised_keys=None,
            homepage=_HOMEPAGE,
            citation=_CITATION,
            task_templates=None,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        archive_path = dl_manager.extract(_ZIP_FILENAME)

        if self.config.name == "si":
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN, gen_kwargs={"archive_path": archive_path, "split": "train"}
                ), 
                datasets.SplitGenerator(
                    name=datasets.Split.VALIDATION, gen_kwargs={"archive_path": archive_path, "split": "validation"}
                ), 
                datasets.SplitGenerator(
                    name=datasets.Split.TEST, gen_kwargs={"archive_path": archive_path, "split": "test"}
                ), 
            ]
        elif self.config.name == 'asr':
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN, gen_kwargs={"archive_path": archive_path, "split": "train"}
                ), 
                datasets.SplitGenerator(
                    name=datasets.Split.TEST, gen_kwargs={"archive_path": archive_path, "split": "test"}
                ), 
            ]

    def _generate_examples(self, archive_path, split=None):
        if self.config.name == 'si':
            extensions = ['.wav']
            _, _walker = fast_scandir(archive_path, extensions, recursive=True)
            _train_walker, _val_walker, _test_walker = [], [], []
            _pair_list = [(Path(fileid).parent.stem, fileid) for fileid in _walker]
            for key, group in itertools.groupby(_pair_list, lambda x: x[0]):
                _files_per_speaker = [item[1] for item in group]
                _train_walker.extend(_files_per_speaker[:6])
                _val_walker.extend(_files_per_speaker[6:8])
                _test_walker.extend(_files_per_speaker[8:])
            if split == 'train':
                files = _train_walker
            elif split == 'validation':
                files = _val_walker
            elif split == 'test':
                files = _test_walker
            for guid, audio_path in enumerate(files):
                yield guid, {
                    "id": str(guid),
                    "file": audio_path, 
                    "audio": audio_path, 
                    "speaker_id": Path(audio_path).parent.stem, 
                    "label": Path(audio_path).parent.stem, 
                }
        elif self.config.name == 'asr':
            wav_paths = sorted(Path(archive_path).glob(f"**/{split}/**/*.wav"))
            wav_paths = wav_paths if wav_paths else sorted(Path(archive_path).glob(f"**/{split.upper()}/**/*.wav"))
            for guid, wav_path in enumerate(wav_paths):
                # Extract phonemes
                phn_path = with_case_insensitive_suffix(Path(str(wav_path).replace('.wav', '')), ".phn")
                with phn_path.open(encoding="utf-8") as op:
                    phonetic_detail = [
                        {
                            "start": i.split(" ")[0],
                            "stop": i.split(" ")[1],
                            "utterance": " ".join(i.split(" ")[2:]).strip(),
                        }
                        for i in op.readlines()
                    ]
    
                # Extract words
                txt_path = with_case_insensitive_suffix(Path(str(wav_path).replace('.wav', '')), ".wrd")
                with txt_path.open(encoding="utf-8") as op:
                    word_detail = [
                        {
                            "start": i.split(" ")[0],
                            "stop": i.split(" ")[1],
                            "utterance": " ".join(i.split(" ")[2:]).strip(),
                        }
                        for i in op.readlines()
                    ]
                    text = ' '.join([w['utterance'] for w in word_detail])
                yield guid, {
                    "id": str(guid),
                    "file": str(wav_path), 
                    "audio": str(wav_path), 
                    "phonetic_detail": phonetic_detail, 
                    "word_detail": word_detail, 
                    "text": text, 
                }


def fast_scandir(path: str, exts: tp.List[str], recursive: bool = False):
    # Scan files recursively faster than glob
    # From github.com/drscotthawley/aeiou/blob/main/aeiou/core.py
    subfolders, files = [], []

    try:  # hope to avoid 'permission denied' by this try
        for f in os.scandir(path):
            try:  # 'hope to avoid too many levels of symbolic links' error
                if f.is_dir():
                    subfolders.append(f.path)
                elif f.is_file():
                    if os.path.splitext(f.name)[1].lower() in exts:
                        files.append(f.path)
            except Exception:
                pass
    except Exception:
        pass

    if recursive:
        for path in list(subfolders):
            sf, f = fast_scandir(path, exts, recursive=recursive)
            subfolders.extend(sf)
            files.extend(f)  # type: ignore

    return subfolders, files


def with_case_insensitive_suffix(path: Path, suffix: str):
    path = path.with_suffix(suffix.lower())
    path = path if path.exists() else path.with_suffix(suffix.upper())
    return path