|
import os |
|
import datasets |
|
|
|
from sklearn.model_selection import train_test_split |
|
from textgrid import textgrid |
|
import soundfile as sf |
|
import re |
|
import json |
|
|
|
def cleanup_string(line): |
|
|
|
words_to_remove = ['(ppo)','(ppc)', '(ppb)', '(ppl)', '<s/>','<c/>','<q/>', '<fil/>', '<sta/>', '<nps/>', '<spk/>', '<non/>', '<unk>', '<s>', '<z>', '<nen>'] |
|
|
|
formatted_line = re.sub(r'\s+', ' ', line).strip().lower() |
|
|
|
|
|
for word in words_to_remove: |
|
if re.search(word,formatted_line): |
|
|
|
formatted_line = formatted_line.replace(word,'') |
|
formatted_line = re.sub(r'\s+', ' ', formatted_line).strip().lower() |
|
|
|
|
|
|
|
|
|
if re.search('\[(.*?)\]', formatted_line): |
|
formatted_line = re.sub('\[(.*?)\]', r'\1', formatted_line).strip() |
|
|
|
|
|
|
|
|
|
if re.search('\((.*?)\)', formatted_line): |
|
formatted_line = re.sub('\((.*?)\)', r'\1', formatted_line).strip() |
|
|
|
|
|
|
|
|
|
if re.search('\'(.*?)\'', formatted_line): |
|
formatted_line = re.sub('\'(.*?)\'', r'\1', formatted_line).strip() |
|
|
|
|
|
|
|
punctuation = '''!–;"\,./?@#$%^&*~''' |
|
punctuation_list = str.maketrans("","",punctuation) |
|
formatted_line = re.sub(r'-', ' ', formatted_line) |
|
formatted_line = re.sub(r'_', ' ', formatted_line) |
|
formatted_line = formatted_line.translate(punctuation_list) |
|
formatted_line = re.sub(r'\s+', ' ', formatted_line).strip().lower() |
|
|
|
|
|
return formatted_line |
|
|
|
|
|
|
|
_DESCRIPTION = """\ |
|
The National Speech Corpus (NSC) is the first large-scale Singapore English corpus |
|
spearheaded by the Info-communications and Media Development Authority (IMDA) of Singapore. |
|
""" |
|
|
|
_CITATION = """\ |
|
""" |
|
_CHANNEL_CONFIGS = sorted([ |
|
"Audio Same CloseMic", "Audio Separate IVR", "Audio Separate StandingMic" |
|
]) |
|
|
|
_HOMEPAGE = "https://www.imda.gov.sg/how-we-can-help/national-speech-corpus" |
|
|
|
_LICENSE = "" |
|
|
|
_PATH_TO_DATA = './IMDA - National Speech Corpus/PART3' |
|
|
|
|
|
INTERVAL_MAX_LENGTH = 25 |
|
|
|
class Minds14Config(datasets.BuilderConfig): |
|
"""BuilderConfig for xtreme-s""" |
|
|
|
def __init__( |
|
self, channel, description, homepage, path_to_data |
|
): |
|
super(Minds14Config, self).__init__( |
|
name=channel, |
|
version=datasets.Version("1.0.0", ""), |
|
description=self.description, |
|
) |
|
self.channel = channel |
|
self.description = description |
|
self.homepage = homepage |
|
self.path_to_data = path_to_data |
|
|
|
|
|
def _build_config(channel): |
|
return Minds14Config( |
|
channel=channel, |
|
description=_DESCRIPTION, |
|
homepage=_HOMEPAGE, |
|
path_to_data=_PATH_TO_DATA, |
|
) |
|
|
|
|
|
class NewDataset(datasets.GeneratorBasedBuilder): |
|
"""TODO: Short description of my dataset.""" |
|
|
|
VERSION = datasets.Version("1.1.0") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BUILDER_CONFIGS = [] |
|
for channel in _CHANNEL_CONFIGS + ["all"]: |
|
BUILDER_CONFIGS.append(_build_config(channel)) |
|
|
|
|
|
DEFAULT_CONFIG_NAME = "all" |
|
|
|
def _info(self): |
|
|
|
task_templates = None |
|
features = datasets.Features( |
|
{ |
|
"audio": datasets.features.Audio(sampling_rate=16000), |
|
"transcript": datasets.Value("string"), |
|
"mic": datasets.Value("string"), |
|
"audio_name": datasets.Value("string"), |
|
"interval": datasets.Value("string") |
|
} |
|
) |
|
|
|
return datasets.DatasetInfo( |
|
|
|
description=_DESCRIPTION, |
|
|
|
features=features, |
|
|
|
|
|
supervised_keys=("audio", "transcript"), |
|
|
|
homepage=_HOMEPAGE, |
|
|
|
license=_LICENSE, |
|
|
|
citation=_CITATION, |
|
task_templates=task_templates, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
|
|
|
|
mics = ( |
|
_CHANNEL_CONFIGS |
|
if self.config.channel == "all" |
|
else [self.config.channel] |
|
) |
|
|
|
with (os.path.join(self.config.path_to_data, "directory_list.json"), "r") as f: |
|
directory_dict = json.load(f) |
|
|
|
train_audio_list = [] |
|
test_audio_list = [] |
|
for mic in mics: |
|
audio_list = [] |
|
if mic == "Audio Same CloseMic": |
|
audio_list = [x for x in directory_dict[mic] if (x[-5] == 1) ] |
|
train, test = train_test_split(audio_list, test_size=0.3, random_state=42, shuffle=True) |
|
for path in train: |
|
train_audio_list.append(os.path.join(self.config.path_to_data, mic, path)) |
|
s = list(path) |
|
s[-5] = "2" |
|
train_audio_list.append(os.path.join(self.config.path_to_data, mic, "".join(s))) |
|
for path in test: |
|
test_audio_list.append(os.path.join(self.config.path_to_data, mic, path)) |
|
s = list(path) |
|
s[-5] = "2" |
|
test_audio_list.append(os.path.join(self.config.path_to_data, mic, "".join(s))) |
|
elif mic == "Audio Separate IVR": |
|
audio_list = [x.split("\\")[0] for x in directory_dict[mic]] |
|
train, test = train_test_split(audio_list, test_size=0.3, random_state=42, shuffle=True) |
|
for folder in train: |
|
audios = [os.path.join(self.config.path_to_data, mic, x) for x in directory_dict[mic] if (x.split("\\")[0]==folder)] |
|
train_audio_list.extend(audios) |
|
for folder in test: |
|
audios = [os.path.join(self.config.path_to_data, mic, x) for x in directory_dict[mic] if (x.split("\\")[0]==folder)] |
|
test_audio_list.extend(audios) |
|
elif mic == "Audio Separate StandingMic": |
|
audio_list = [x[:14] for x in directory_dict[mic]] |
|
audio_list = list(set(audio_list)) |
|
train, test = train_test_split(audio_list, test_size=0.3, random_state=42, shuffle=True) |
|
for folder in train: |
|
audios = [os.path.join(self.config.path_to_data, mic, x) for x in directory_dict[mic] if (x[:14]==folder)] |
|
train_audio_list.extend(audios) |
|
for folder in test: |
|
audios = [os.path.join(self.config.path_to_data, mic, x) for x in directory_dict[mic] if (x[:14]==folder)] |
|
test_audio_list.extend(audios) |
|
|
|
print(f"train_audio_list: { train_audio_list}") |
|
print(f"test_audio_list: { test_audio_list}") |
|
|
|
|
|
|
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
gen_kwargs={ |
|
|
|
"audio_list": train_audio_list, |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
gen_kwargs={ |
|
|
|
"audio_list": test_audio_list, |
|
}, |
|
), |
|
] |
|
|
|
|
|
def _generate_examples( |
|
self, |
|
audio_list, |
|
): |
|
id_ = 0 |
|
for audio_path in audio_list: |
|
file = os.path.split(audio_path)[-1] |
|
folder = os.path.split(os.path.split(audio_path)[0])[-1] |
|
|
|
|
|
if folder.split("_")[0] == "conf": |
|
|
|
script_path = os.path.join(self.config.path_to_data, "Scripts Separate", folder+"_"+file[:-4]+".TextGrid") |
|
elif folder.split()[1] == "Same": |
|
|
|
script_path = os.path.join(self.config.path_to_data, "Scripts Same", file[:-4]+".TextGrid") |
|
elif folder.split()[1] == "Separate": |
|
|
|
script_path = os.path.join(self.config.path_to_data, "Scripts Separate", file[:-4]+".TextGrid") |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
tg = textgrid.TextGrid.fromFile(script_path) |
|
except: |
|
print(f"error reading textgrid file") |
|
continue |
|
|
|
|
|
|
|
if os.path.exists(audio_path): |
|
|
|
data, sr = sf.read(audio_path) |
|
result = {} |
|
i = 0 |
|
intervalLength = 0 |
|
intervalStart = 0 |
|
transcript_list = [] |
|
filepath = os.path.join(self.config.path_to_data, 'tmp_clip.wav') |
|
while i < (len(tg[0])-1): |
|
transcript = cleanup_string(tg[0][i].mark) |
|
if intervalLength == 0 and len(transcript) == 0: |
|
intervalStart = tg[0][i].maxTime |
|
i+=1 |
|
continue |
|
intervalLength += tg[0][i].maxTime-tg[0][i].minTime |
|
if intervalLength > INTERVAL_MAX_LENGTH: |
|
print(f"INTERVAL LONGER THAN {intervalLength}") |
|
result["transcript"] = transcript |
|
result["interval"] = "start:"+str(tg[0][i].minTime)+", end:"+str(tg[0][i].maxTime) |
|
result["audio"] = {"path": audio_path, "bytes": data[int(tg[0][i].minTime*sr):int(tg[0][i].maxTime*sr)], "sampling_rate":sr} |
|
yield id_, result |
|
id_+= 1 |
|
intervalLength = 0 |
|
else: |
|
if (intervalLength + tg[0][i+1].maxTime-tg[0][i+1].minTime) < INTERVAL_MAX_LENGTH: |
|
if len(transcript) != 0: |
|
transcript_list.append(transcript) |
|
i+=1 |
|
continue |
|
if len(transcript) == 0: |
|
spliced_audio = data[int(intervalStart*sr):int(tg[0][i].minTime*sr)] |
|
else: |
|
transcript_list.append(transcript) |
|
spliced_audio = data[int(intervalStart*sr):int(tg[0][i].maxTime*sr)] |
|
sf.write(filepath, spliced_audio, sr) |
|
result["interval"] = "start:"+str(intervalStart)+", end:"+str(tg[0][i].maxTime) |
|
result["audio"] = {"path": filepath, "bytes": spliced_audio, "sampling_rate":sr} |
|
result["transcript"] = ' '.join(transcript_list) |
|
yield id_, result |
|
id_+= 1 |
|
intervalLength=0 |
|
intervalStart=tg[0][i].maxTime |
|
transcript_list = [] |
|
i+=1 |