TextGrid to lab
Not this simple; also need to merge segments
from praatio import textgrid
from pathlib import Path
def tg_to_lab(filename, target="phones"):
tg = textgrid.openTextgrid(filename, False)
if not tg.tierNameList or target not in tg.tierNameList:
return []
phones = tg.tierDict[target]
out = []
def it_to_dict(it):
ret = {}
ret['start'] = it.start
ret['end'] = it.end
ret['label'] = it.label
return ret
for phone in phones.entryList:
tmp_phone = it_to_dict(phone)
start = int(tmp_phone['start'] * 10000000)
end = int(tmp_phone['end'] * 10000000)
label = tmp_phone['label']
out.append(f"{start} {end} {label}")
return out
inpath = Path("/PATH/TO/FILES/INPUT")
outpath = Path("/PATH/TO/FILES/OUTPUT")
for filename in inpath.glob("*.TextGrid"):
out = outpath / f"{filename.stem}.lab"
lab = tg_to_lab(filename)
with open(out, "w") as outf:
for line in lab:
outf.write(line + "\n")