File size: 13,406 Bytes
822dd80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6950bd
 
822dd80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55bda3f
822dd80
 
 
 
 
 
 
 
 
 
 
a6950bd
822dd80
 
 
a6950bd
822dd80
a6950bd
 
 
 
 
 
 
 
822dd80
 
 
 
 
a6950bd
822dd80
 
a6950bd
 
822dd80
 
 
 
 
 
 
 
 
55bda3f
 
 
822dd80
55bda3f
822dd80
55bda3f
822dd80
55bda3f
 
a6950bd
55bda3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
822dd80
 
55bda3f
822dd80
 
 
 
 
 
 
 
55bda3f
 
a6950bd
 
55bda3f
 
 
822dd80
 
 
a6950bd
 
 
 
 
 
 
822dd80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6950bd
 
 
822dd80
 
 
 
 
a6950bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
822dd80
 
 
 
a6950bd
 
 
 
 
822dd80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55bda3f
 
822dd80
55bda3f
 
822dd80
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# coding=utf-8
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
import logging
from pathlib import Path
import re

import datasets


_CITATION = """\
@misc{pitt2007Buckeye,
title = {Buckeye {Corpus} of {Conversational} {Speech} (2nd release).},
url = {www.buckeyecorpus.osu.edu},
publisher = {Columbus, OH: Department of Psychology, Ohio State University (Distributor)},
author = {Pitt, M.A. and Dilley, L. and Johnson, K. and Kiesling, S. and Raymond, W. and Hume, E. and Fosler-Lussier, E.},
year = {2007},
}
"""

_DESCRIPTION = """\
The Buckeye Corpus of conversational speech contains high-quality recordings
from 40 speakers in Columbus OH conversing freely with an interviewer. The
speech has been orthographically transcribed and phonetically labeled.
"""
_HOMEPAGE = "https://buckeyecorpus.osu.edu"

_LICENSE = "FREE for noncommercial uses"


class BuckeyeASRDataset(datasets.GeneratorBasedBuilder):
    """Buckeye ASR dataset."""

    VERSION = datasets.Version("1.0.0")
    PHONE_EXCLUSION_LIST = ["{B_TRANS}", "<EXCLUDE-name>", "IVER", "NOISE", "SIL", "VOCNOISE"]
    SPLIT_TAGS = ['SIL', 'IVER']

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "file": datasets.Value("string"),
                    "audio": datasets.Value("string"),
                    "text": datasets.Value("string"),
                    "phonetic_detail": datasets.Sequence(
                        {
                            "start": datasets.Value("float"),
                            "stop": datasets.Value("float"),
                            "label": datasets.Value("string"),
                        }
                    ),
                    "word_detail": datasets.Sequence(
                        {
                            "start": datasets.Value("float"),
                            "stop": datasets.Value("float"),
                            "label": datasets.Value("string"),
                            "broad_transcription": datasets.Value("string"),
                            "narrow_transcription": datasets.Value("string"),
                            "syntactic_class": datasets.Value("string"),
                        }
                    ),
                    "start": datasets.Value("float"),
                    "stop": datasets.Value("float"),
                    "speaker_id": datasets.Value("string"),
                    "id": datasets.Value("string"),
                }
            ),
            supervised_keys=("file", "text"),
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        # We use 10% of the data for validation and test sets, with no speaker
        # overlap between the sets. We also balance for speaker's gender and
        # age.
        spkr_ids_dev = [18, 34, 36, 37]
        spkr_ids_test = [20, 38, 39, 40]

        def is_training(i):
            return i not in spkr_ids_dev and i not in spkr_ids_test
        spkr_ids_train = filter(is_training, range(1, 41))

        paths = {
            "train": [f"{dl_manager.manual_dir}/s{i:02n}" for i in spkr_ids_train],
            "dev": [f"{dl_manager.manual_dir}/s{i:02n}" for i in spkr_ids_dev],
            "test": [f"{dl_manager.manual_dir}/s{i:02n}" for i in spkr_ids_test],
        }
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"paths": paths["train"]},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={"paths": paths["test"]},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={"paths": paths["dev"]},
            ),
        ]

    def _generate_examples(self, paths):
        """ Yields examples as (key, example) tuples. """
        for p in paths:
            for wav_path in Path(p).glob("*.wav"):
                # Extract words
                fpath = wav_path.with_suffix(".words")
                wordlist = self._extract_word_info(fpath)
                word_seqs = self._split_words(wordlist, fpath)

                # Extract transcript
                # To avoid conflict between the transcripts (`.txt` files) and
                # the word alignment files (`.words` files), we compute the
                # transcripts from the latter.
                transcripts = [" ".join([w["label"] for w in wseq]) for wseq in word_seqs]

                # Extract phones
                fpath = wav_path.with_suffix(".phones")
                phonelist = self._extract_phone_info(fpath)
                phone_seqs = self._split_phones(word_seqs, phonelist, fpath)
                assert len(phone_seqs) == len(word_seqs)

                # id_ must be a unique key
                for idx in range(len(transcripts)):
                    ws = word_seqs[idx]
                    id_ = f"{wav_path.stem}_{idx}"
                    if phone_seqs[idx]:
                        start = min(ws[0]["start"], phone_seqs[idx][0]["start"])
                        stop = max(ws[-1]["stop"], phone_seqs[idx][-1]["stop"])
                    else:
                        start = ws[0]["start"]
                        stop = ws[-1]["stop"]
                    shift_timestamps(ws, -start)
                    shift_timestamps(phone_seqs[idx], -start)
                    example = {
                        "file": wav_path,
                        "audio": wav_path,
                        "text": transcripts[idx],
                        "phonetic_detail": phone_seqs[idx],
                        "word_detail": ws,
                        "speaker_id": id_[1:3],
                        "id": id_,
                        "start": start,
                        "stop": stop,
                    }
                    yield id_, example

    def _extract_word_info(cls, fpath):
        with open(fpath) as f:
            lines = f.readlines()
        start = 0
        wordlist = []
        for line in lines[9:]:
            # File s1901b.words goes beyond the end of the audio
            if fpath.name == "s1901b.words" and start > 568.739:
                break
            line = line.rstrip("\n")
            if not line:  # Skipping empty lines
                continue
            fields = line.split("; ")
            subfields = fields[0].split()
            label = subfields[2]
            stop = float(subfields[0])
            if label[0] in ['<', '{']:
                # Handling tags (tags sometime miss transcriptions)
                wordlist.append({
                    "start": start,
                    "stop": stop,
                    "label": label,
                })
            else:
                # Handling words
                if len(fields) < 4:
                    logging.warning(f"Line \"{line}\" missing fields in file {fpath}")
                else:
                    narrow_trn = fields[2]
                    # Warning if the narrow_transcription is empty
                    if not narrow_trn:
                        logging.warning(f"Narrow transcription is empty in {fpath}")
                    wordlist.append({
                        "start": start,
                        "stop": stop,
                        "label": label,
                        "broad_transcription": fields[1],
                        "narrow_transcription": narrow_trn,
                        "syntactic_class": fields[3],
                    })
            start = stop
        return wordlist

    def _split_words(cls, wordlist, fpath):
        word_seqs = []
        segment = []
        for w in wordlist:
            if w["label"][0] in ['<', '{']:
                # Segmenting if the label starts with one of the SPLIT_TAGS
                regexp = "|".join([f"<{st}" for st in cls.SPLIT_TAGS])
                match = re.match(regexp, w["label"])
                if match and match.start() == 0 and segment:
                    # The model can't handle segments shorter than 0.25 ms
                    if segment[-1]["stop"] - segment[0]["start"] >= 0.025:
                        word_seqs.append(segment)
                    else:
                        logging.warning(
                            f"Sequence shorter than 25 ms in {fpath} starting "
                            f"at {segment[0]['start']}")
                    segment = []
            else:
                segment.append(w)
        if segment:
            if segment[-1]["stop"] - segment[0]["start"] >= 0.025:
                word_seqs.append(segment)
            else:
                logging.warning(
                    f"Sequence shorter than 25 ms in {fpath} starting "
                    f"at {segment[0]['start']}")
        return word_seqs

    def _extract_phone_info(cls, fpath):
        with open(fpath) as f:
            lines = f.readlines()
        start = 0
        phonelist = []
        for i, line in enumerate(lines[9:]):
            if line == '\n':
                # empty line -> skip
                continue
            # some lines contain "; *" after the last field
            # we should get rid of that
            line = line.split(";")[0].strip()
            fields = line.split()
            stop = float(fields[0])
            if len(fields) < 3:
                # phone without label
                logging.warning(
                    f"Phone missing label in file {fpath} at time {stop}.")
                phone = None
            else:
                phone = fields[2]
            phonelist.append({
                "start": start,
                "stop": stop,
                "label": phone,
            })
            start = stop
        return phonelist

    # Matching phone and word alignments based on exact timestamps doesn't
    # work as the timestamps don't always match.
    # Also, the narrow transcription can conflict with the phone alignments
    # in many ways, including:
    #  * wrong phone label in one of the two (e.g. `tq` instead of `t`).
    #  * phone absent from the narrow transcription can appear in the
    #    alignments with no duration.
    #  * if two consecutive phones are the same in the narrow transcription,
    #    they can be merged in the alignements.
    # We are conservative here and keep every phone that is completely included
    # in the utterance + overlapping starting/ending phones if they match the
    # first/last phone of the narrow transcription.
    def _split_phones(cls, word_seqs, phonelist, fpath):
        phone_seqs = []
        i_pl = 0
        for wl in word_seqs:
            segment = []
            # skip phones that precede the first word's start
            while phonelist[i_pl]["stop"] <= wl[0]["start"]:
                i_pl += 1
            # a phone which overlaps with the start of the first word might
            # not belong with the utterance
            if (phonelist[i_pl]["label"] and
                    phonelist[i_pl]["start"] < wl[0]["start"] and
                    not wl[0]["narrow_transcription"].startswith(phonelist[i_pl]["label"])):
                # skip the phone
                i_pl += 1
            # take all phones fully included in the utterance
            while (i_pl < len(phonelist) and
                   phonelist[i_pl]["stop"] < wl[-1]["stop"]):
                segment.append(phonelist[i_pl])
                i_pl += 1
            # a phone which overlaps with the end of the last word might
            # belong with the utterance
            if (phonelist[i_pl]["label"] and
                    phonelist[i_pl]["start"] < wl[-1]["stop"] and
                    wl[-1]["narrow_transcription"].endswith(phonelist[i_pl]["label"])):
                # add the phone
                segment.append(phonelist[i_pl])
                i_pl += 1
            phone_seqs.append(segment)
        return phone_seqs


def shift_timestamps(detail, shift):
    for item in detail:
        item["start"] += shift
        item["stop"] += shift

def matching(phone, word):
    # Reject phones without label
    if phone["label"] is None:
        return False
    # Phones that are totally encompassed by the word timewise are considered
    # part of the word
    if phone["start"] >= word["start"] and phone["stop"] <= word["stop"]:
        return True
    # In case of overlap with the start or the end, we further request a time
    # difference <= 20 ms and check the label
    if phone["start"] < word["start"]:
        # Possible overlap with the start
        if (included(phone, word) and
                word["narrow_transcription"].startswith(phone["label"])):
            return True
    # Last option is an overlap with the end of the word
    return (included(phone, word) and
            word["narrow_transcription"].endswith(phone["label"]))


def included(phone, word, threshold=0.02):
    # We accept an overlap with time difference up to the threshold at the
    # start or the end
    return (phone["start"] >= word["start"] - threshold and
            phone["start"] < word["stop"] and
            phone["stop"] > word["start"] and
            phone["stop"] <= word["stop"] + threshold)