|
|
|
|
|
|
|
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.""" |
|
|
|
|
|
|
|
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"): |
|
|
|
fpath = wav_path.with_suffix(".words") |
|
wordlist = self._extract_word_info(fpath) |
|
word_seqs = self._split_words(wordlist, fpath) |
|
|
|
|
|
|
|
|
|
|
|
transcripts = [" ".join([w["label"] for w in wseq]) for wseq in word_seqs] |
|
|
|
|
|
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) |
|
|
|
|
|
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:]: |
|
|
|
if fpath.name == "s1901b.words" and start > 568.739: |
|
break |
|
line = line.rstrip("\n") |
|
if not line: |
|
continue |
|
fields = line.split("; ") |
|
subfields = fields[0].split() |
|
label = subfields[2] |
|
stop = float(subfields[0]) |
|
if label[0] in ['<', '{']: |
|
|
|
wordlist.append({ |
|
"start": start, |
|
"stop": stop, |
|
"label": label, |
|
}) |
|
else: |
|
|
|
if len(fields) < 4: |
|
logging.warning(f"Line \"{line}\" missing fields in file {fpath}") |
|
else: |
|
narrow_trn = fields[2] |
|
|
|
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 ['<', '{']: |
|
|
|
regexp = "|".join([f"<{st}" for st in cls.SPLIT_TAGS]) |
|
match = re.match(regexp, w["label"]) |
|
if match and match.start() == 0 and 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']}") |
|
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': |
|
|
|
continue |
|
|
|
|
|
line = line.split(";")[0].strip() |
|
fields = line.split() |
|
stop = float(fields[0]) |
|
if len(fields) < 3: |
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _split_phones(cls, word_seqs, phonelist, fpath): |
|
phone_seqs = [] |
|
i_pl = 0 |
|
for wl in word_seqs: |
|
segment = [] |
|
|
|
while phonelist[i_pl]["stop"] <= wl[0]["start"]: |
|
i_pl += 1 |
|
|
|
|
|
if (phonelist[i_pl]["label"] and |
|
phonelist[i_pl]["start"] < wl[0]["start"] and |
|
not wl[0]["narrow_transcription"].startswith(phonelist[i_pl]["label"])): |
|
|
|
i_pl += 1 |
|
|
|
while (i_pl < len(phonelist) and |
|
phonelist[i_pl]["stop"] < wl[-1]["stop"]): |
|
segment.append(phonelist[i_pl]) |
|
i_pl += 1 |
|
|
|
|
|
if (phonelist[i_pl]["label"] and |
|
phonelist[i_pl]["start"] < wl[-1]["stop"] and |
|
wl[-1]["narrow_transcription"].endswith(phonelist[i_pl]["label"])): |
|
|
|
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): |
|
|
|
if phone["label"] is None: |
|
return False |
|
|
|
|
|
if phone["start"] >= word["start"] and phone["stop"] <= word["stop"]: |
|
return True |
|
|
|
|
|
if phone["start"] < word["start"]: |
|
|
|
if (included(phone, word) and |
|
word["narrow_transcription"].startswith(phone["label"])): |
|
return True |
|
|
|
return (included(phone, word) and |
|
word["narrow_transcription"].endswith(phone["label"])) |
|
|
|
|
|
def included(phone, word, threshold=0.02): |
|
|
|
|
|
return (phone["start"] >= word["start"] - threshold and |
|
phone["start"] < word["stop"] and |
|
phone["stop"] > word["start"] and |
|
phone["stop"] <= word["stop"] + threshold) |
|
|