-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsyllable.py
340 lines (269 loc) · 9.74 KB
/
syllable.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
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
330
331
332
333
334
335
336
337
338
339
340
import re, copy, sys, random
from cmuparser import CMUtranscribe
from syllable_types import Cluster, Consonant, Vowel, Empty, Rime, Syllable
from phoneme_types import *
phoneme_classify = re.compile('''
((?P<Vowel>AO|UW|EH|AH|AA|IY|IH|UH|AE|AW|AY|ER|EY|OW|OY)
|(?P<Consonant>CH|DH|HH|JH|NG|SH|TH|ZH|Z|S|P|R|K|L|M|N|F|G|D|B|T|V|W|Y\d*)
)
((?P<Stress>0|1|2)
)?
''',re.VERBOSE)
def factory(phoneme):
# argument is a string of phonemes e.g.'B IH0 K AH0 Z'
phoneme_list = phoneme.split()
def phoneme_fact(phoneme):
# match against regular expression
phoneme_feature = re.match(phoneme_classify,phoneme).groupdict()
#input is phoneme feature dictionary
if phoneme_feature['Consonant']:
# return consonant object
return Consonant(**phoneme_feature)
elif phoneme_feature['Vowel']:
# return vowel object
return Vowel(**phoneme_feature)
else:
# unknown phoneme class
raise Exception('unkown Phoneme Class: cannot create appropriate Phoneme object')
def cluster_fact(cluster_list, phenome):
current_cluster = cluster_list.pop()
'''Consonants must be grouped together into clusters '''
if current_cluster.type() == Consonant and type(phenome) == Consonant or current_cluster.type() == None:
# Adjacent phenomes of type consonant belong to the same cluster, if the
# current cluster.last_phenome == None that means it's empty
# update current cluster
current_cluster.add_phenome(phenome)
# append to cluster list
cluster_list.append(current_cluster)
# return cluster list
return cluster_list
else:
# create new cluster add phenome to it and return
cluster_list.append(current_cluster)
cluster_list.append(Cluster(phenome))
return cluster_list
def syllable_fact(syllable_list, cluster):
syllable = syllable_list.pop()
push = syllable_list.append
if syllable.onset_is_empty() and syllable.nucleus_is_empty() and cluster.type() == Consonant:
# cluster is assigned to the onset of the current syllable
syllable.set_onset(cluster)
push(syllable)
return syllable_list
if cluster.type() == Vowel:
if syllable.has_nucleus():
# this cluster becomes the nucleus of a new syllable
# push current syllable back on the syllable stack
push(syllable)
# create new syllable
new_syllable = Syllable(nucleus = cluster)
# push new_syllable onto the stack
push(new_syllable)
return syllable_list
else:
# syllable does not have nucleus so this cluster becomes the
# nucleus on the current syllable
syllable.set_nucleus(cluster)
push(syllable)
return syllable_list
if syllable.has_nucleus() and cluster.type() == Consonant:
if syllable.has_coda():
# this cluster is the onset of the next syllable
new_syllable = Syllable(onset = cluster)
# push syllable onto stack
push(new_syllable)
return syllable_list
elif syllable.coda_is_empty():
# Onset Maximalism dictates we push consonant clusters to
# the onset of the next syllable, unless the nuclear cluster is LIGHT and
# has primary stress
maximal_coda, maximal_onset = onset_rules(cluster)
if syllable.is_short() and syllable.get_stress() == '1' and not maximal_coda:
# The syllable is LIGHT and the consonat cluster is therefore ambisyllabic
# it belongs to both the current syllable and the next
# coda is empty
light_coda = coda_rules(maximal_onset)
syllable.set_coda(light_coda)
push(syllable)
new_syllable = Syllable(onset = maximal_onset)
push(new_syllable)
return syllable_list
else:
# add cluster only to the next syllable
if maximal_coda:
syllable.set_coda(maximal_coda)
push(syllable)
else:
push(syllable)
if maximal_onset:
new_syllable = Syllable(onset = maximal_onset)
else:
new_syllable = Syllable()
push(new_syllable)
return syllable_list
def check_last_syllable(syllable_list):
# the syllable algorithm may assign a consanant cluster to a Syllable that does not have
# a nucleus, this is not allowed in the English language.
# check the last syllable
syllable = syllable_list.pop()
push = syllable_list.append
if syllable.nucleus_is_empty():
if syllable.has_onset():
# pop the previous syllable
prev_syllable = syllable_list.pop()
onset = syllable.get_onset()
# set the coda of the previous syllable to the value of theorphan onset
if prev_syllable.has_coda():
#add phoneme
coda_cluster = prev_syllable.get_coda()
if coda_cluster != onset:
for phoneme in onset.phoneme_list: coda_cluster.add_phoneme(phoneme)
# for phoneme in phonemes: coda_cluster.add_phoneme(phoneme)
else:
prev_syllable.set_coda(onset)
push(prev_syllable)
else:
# There is no violation, push syllable back on the stack
push(syllable)
return syllable_list
# Create a list of phoneme clusters from phoneme list
cluster_list = reduce(cluster_fact, map(phoneme_fact, phoneme_list),[Cluster()])
# Apply syllable creation rules to list of phenome clusters
syllable_list = reduce(syllable_fact, cluster_list, [Syllable()])
# Validate last syllable, and return completed syllable list
return check_last_syllable(syllable_list)
def coda_rules(cluster):
''' checks if the cluster is a valid onset or whther it needs to be split'''
coda_cluster = copy.deepcopy(cluster)
phonemes = map(str, coda_cluster.get_phoneme())
def _split_and_update(phoneme, phonemes = phonemes, coda_cluster = coda_cluster):
index = phonemes.index(phoneme)
# split on phoneme and discar the rest
head = coda_cluster.phoneme_list[:index-1]
# update cluster
coda_cluster.phoneme_list = head
# update string list
phonemes = phonemes[:index-1]
return (phonemes, coda_cluster)
#rule 1 - no /HH/ is coda
if HH in phonemes:
phonemes, coda_cluster = _split_and_update(HH)
#rule 2 - no glides in coda
if L in phonemes:
phonemes, coda_cluster = _split_and_update(L)
if R in phonemes:
phonemes, coda_cluster = _split_and_update(R)
if W in phonemes:
phonemes, coda_cluster = _split_and_update(W)
if Y in phonemes:
phonemes, coda_cluster = _split_and_update(Y)
#rule 3 - if complex coda second consonant must not be
# /NG/ /ZH/ /DH/
if len(phonemes) > 1 and phonemes[1] in [NG,DH,ZH]:
phoneme = coda_cluster.phoneme_list[1]
# update cluster
coda_cluster.phoneme_list = [phoneme]
#update string list
phonemes = phonemes[0:1]
if coda_cluster.phoneme_list == []:
coda_cluster = None
return coda_cluster
def onset_rules(cluster):
''' checks if the cluster is a valid onset or whther it needs to be split'''
phonemes = map(str, cluster.get_phoneme())
coda_cluster = Cluster()
def _split_and_update(phoneme, phonemes = phonemes, coda_cluster = coda_cluster):
#_get index of phoneme
index = phonemes.index(phoneme)
# split on phoneme and add tail coda cluster
tail = cluster.phoneme_list[:index]
# remaining phonemes
head = cluster.phoneme_list[index+1:]
#extend list
coda_cluster.phoneme_list.extend(tail)
#update cluster
cluster.phoneme_list = head
#update string list
phonemes = phonemes[index+1:]
return (phonemes, coda_cluster)
def _remove_and_update(phonemes = phonemes, coda_cluster = coda_cluster):
head = cluster.phoneme_list[0]
rest = cluster.phoneme_list[1:]
#extend list
coda_cluster.phoneme_list.extend([head])
#update cluster
cluster.phoneme_list = rest
#update string list
phonemes = phonemes[1:]
return (phonemes, coda_cluster)
#rule 1 - /NG/ cannot exist in a valid onset
# does /NG? exist? split on NG add NG to coda
if NG in phonemes:
phonemes, coda_cluster = _split_and_update(NG)
#rule 2a - no affricates in complex onsets
# /CH/ exist? split on affricate
if CH in phonemes:
phonemes, coda_cluster = _split_and_update(CH)
#rule 2b - no affrictes in complex onsets
# /JH/ exist? split on affricate
if JH in phonemes:
phonemes, coda_cluster = _split_and_update(JH)
#rule 3 - first consonant in a complex onset must be obstruent
# if first consonant stop or fricative or nasal
if len(phonemes) > 1 and not phonemes[0] in [B,D,G,K,P,T,DH,F,S,SH,TH,V,ZH,M,N]:
phonemes, coda_cluster = _remove_and_update()
#rule 4 - second consonant in a complex onset must be a voiced obstruent
# if not OBSTRUENT and VOICED? split on second consonant
if len(phonemes) > 1 and not phonemes[1] in [B,M,V,D,N,Z,ZH]:
phonemes, coda_cluster = _remove_and_update()
#rule 5 - if first consonant in a omplex onmset is not /s/
# the second consonant must be liquid or glide /L/ /R/ /W/ /Y/
if len(phonemes) > 1 and not phonemes[0] == S and not phonemes[1] in [L,R,W,Y]:
phonemes, coda_cluster = _remove_and_update()
if coda_cluster.get_phoneme() == []:
coda_cluster = None
if cluster.get_phoneme() == []:
cluster = None
return (coda_cluster, cluster)
''' Public Method '''
def generate(word):
phoneme_list = CMUtranscribe(word)
if phoneme_list:
return map(factory, phoneme_list)
else:
return None
''' Test '''
def get_raw(word):
return CMUtranscribe(word)
def test():
words = open('./CMU_dictionary/american-english')
words = words.readlines()
for i in range(100):
word = random.choice(words)[:-1]
syllable = generate(word)
raw = get_raw(word)
if syllable:
counter = 0
for s in syllable:
counter += 1
word += '\n' + str(counter) + ': ' + str(map(str,s))
print word
for w in raw:
print 'raw: ', w
print '\n'
if __name__ == '__main__':
try:
word = sys.argv[1]
syllable = generate(word)
raw = get_raw(word)
if syllable:
counter = 0
for s in syllable:
counter += 1
word += '\n' + str(counter) + ': ' + str(map(str,s))
print word
for w in raw:
print 'raw: ', w
print '\n'
except:
test()