-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert_label.py
157 lines (134 loc) · 5.19 KB
/
convert_label.py
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
import re
import os
import sys
class ExtentionException(Exception):
pass
class EmptyLabelException(Exception):
pass
class Segment:
"""
a unit of speech (i.e. phoneme, mora)
"""
def __init__(self, tStart, tEnd, label):
self.tStart = tStart
self.tEnd = tEnd
self.label = label
def __add__(self, other):
return Segment(self.tStart, other.tEnd, self.label + other.label)
def can_follow(self, other):
"""
return True if Segment self can follow Segment other in one mora,
otherwise return False
example: (other, self)
True: ('s', 'a'), ('sh', 'i'), ('ky', 'o:'), ('t', 's')
False: ('a', 'q'), ('a', 's'), ('u', 'e'), ('s', 'ha')
"""
vowels = ['a', 'i', 'u', 'e', 'o', 'a:', 'i:', 'u:', 'e:', 'o:']
consonants = ['w', 'r', 't', 'y', 'p', 's', 'd', 'f', 'g', 'h', 'j',
'k', 'z', 'c', 'b', 'n', 'm']
only_consonants = lambda x: all([c in consonants for c in x])
if only_consonants(other.label) and self.label in vowels:
return True
if only_consonants(other.label) and only_consonants(self.label):
return True
return False
def to_textgrid_lines(self, segmentIndex):
label = '' if self.label in ['silB', 'silE'] else self.label
return [f' intervals [{segmentIndex}]:',
f' xmin = {self.tStart} ',
f' xmax = {self.tEnd} ',
f' text = "{label}" ']
def read_lab(filename):
"""
read label file (.lab) generated by Julius segmentation kit and
return SegmentationLabel object
"""
try:
if not re.search(r'\.lab$', filename):
raise ExtentionException("read_lab supports only .lab")
except ExtentionException as e:
print(e)
return None
with open(filename, 'r') as f:
labeldata = [line.split() for line in f if line != '']
segments = [Segment(tStart=float(line[0]), tEnd=float(line[1]),
label=line[2])
for line in labeldata]
return SegmentationLabel(segments)
class SegmentationLabel:
"""
list of segments
"""
def __init__(self, segments, separatedByMora=False):
self.segments = segments
self.separatedByMora = separatedByMora
def by_moras(self):
"""
return new SegmentationLabel object whose segment are moras
"""
if self.separatedByMora == True:
return self
moraSegments = []
curMoraSegment = None
for segment in self.segments:
if curMoraSegment is None:
curMoraSegment = segment
elif segment.can_follow(curMoraSegment):
curMoraSegment += segment
else:
moraSegments.append(curMoraSegment)
curMoraSegment = segment
if curMoraSegment:
moraSegments.append(curMoraSegment)
return SegmentationLabel(moraSegments, separatedByMora=True)
def _textgrid_headers(self):
segmentKind = 'mora' if self.separatedByMora else 'phoneme'
return ['File type = "ooTextFile"',
'Object class = "TextGrid"',
' ',
'xmin = 0 ',
f'xmax = {self.segments[-1].tEnd} ',
'tiers? <exists> ',
'size = 1 ',
'item []: ',
' item [1]: ',
' class = "IntervalTier" ',
f' name = "{segmentKind}" ',
' xmin = 0 ',
f' xmax = {self.segments[-1].tEnd} ',
f' intervals: size = {len(self.segments)} ']
def to_textgrid(self, textgridFileName):
"""
save to .TextGrid file, which is available for Praat
"""
try:
if not self.segments:
raise EmptyLabelException(f'warning: no label data found in '
f'{textgridFileName}')
except EmptyLabelException as e:
print(e)
return
textgridLines = self._textgrid_headers()
for i, segment in enumerate(self.segments):
textgridLines.extend(segment.to_textgrid_lines(i + 1))
with open(textgridFileName, 'w') as f:
f.write('\n'.join(textgridLines))
if __name__ == '__main__':
args = sys.argv
if len(args) >= 2:
mainDirectory = args[1]
else:
mainDirectory = os.curdir
answer = None
while not answer in ['y', 'Y', 'n', 'N']:
answer = input('change segmentation unit to mora?'\
' (default:phoneme) y/n:')
choosesMora = answer in ['y', 'Y']
for dirPath, dirNames, fileNames in os.walk(mainDirectory):
labFileNames = [n for n in fileNames if re.search(r'\.lab$', n)]
for labFileName in labFileNames:
label = read_lab(os.path.join(dirPath, labFileName))
if choosesMora:
label = label.by_moras()
textgridFileName = re.sub(r"\.lab$", ".TextGrid", labFileName)
label.to_textgrid(os.path.join(dirPath, textgridFileName))