-
Notifications
You must be signed in to change notification settings - Fork 24
/
add_syllable_and_word_tiers.praat
474 lines (418 loc) · 13.1 KB
/
add_syllable_and_word_tiers.praat
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# This script adds syllable and word tier units for utterance intervals
# in a TextGrid object.
# The script only works for the Finnish language.
# Utterance transcriptions must be quasi-orthographic.
#
# This script is distributed under the GNU General Public License.
# Copyright 31.5.2004 Mietta Lennes
form Add syllables and words
sentence Utterance_tier_name utterance
sentence Written_sentence_tier_name_(optional) "written sentence"
sentence Word_tier_name word
sentence Syllable_tier_name syllable
sentence Phone_tier_name phone
comment A TextGrid plus a LongSound must be selected!
endform
echo Adding syllables and words to TextGrid...
sound$ = selected$ ("LongSound", 1)
grid$ = selected$ ("TextGrid", 1)
select TextGrid 'grid$'
total_duration = Get duration
numberOfHandledUtterances = 0
# Make a temporary copy of the TextGrid for safe editing:
Copy... temp
select TextGrid temp
# If necessary, add tiers for phones, syllables,
call GetTier 'phone_tier_name$' phone_tier
newtier = phone_tier + 1
call GetTier 'syllable_tier_name$' syllable_tier
if syllable_tier = 0
Insert interval tier... newtier 'syllable_tier_name$'
newtier = newtier + 1
endif
call GetTier 'word_tier_name$' word_tier
if word_tier = 0
Insert interval tier... newtier 'word_tier_name$'
endif
# Update all tier numbers:
call GetTier 'phone_tier_name$' phone_tier
call GetTier 'syllable_tier_name$' syllable_tier
call GetTier 'word_tier_name$' word_tier
call GetTier 'utterance_tier_name$' utterance_tier
printline Written sentence tier = 'written_sentence_tier'
select LongSound 'sound$'
plus TextGrid temp
Edit
editor TextGrid temp
for seltier from 2 to utterance_tier
Select next tier
endfor
if utterance_tier > 0
endeditor
select TextGrid temp
numberOfUtterances = Get number of intervals... utterance_tier
for utterance to numberOfUtterances
select TextGrid temp
numberOfUtterances = Get number of intervals... utterance_tier
utterance_text$ = Get label of interval... utterance_tier utterance
call IsLegalLabel 'utterance_text$'
if legal = 1
numberOfHandledUtterances = numberOfHandledUtterances + 1
uttstart = Get starting point... utterance_tier utterance
uttend = Get end point... utterance_tier utterance
# Check if syllables or words exist for this utterance:
endcheck = uttend - 0.01
firstsyll = Get interval at time... syllable_tier uttstart
lastsyll = Get interval at time... syllable_tier endcheck
sylltext$ = Get label of interval... syllable_tier firstsyll
if firstsyll = lastsyll and sylltext$ = ""
# Move utterance boundaries a little to match phone boundaries:
if phone_tier > 0
call CheckUtteranceBoundaries utterance_tier utterance
endif
uttdur = uttend - uttstart
uttchar = length (utterance_text$)
call GetWords 'utterance_text$'
uttnetchar = uttchar - (words - 1)
uttunit = uttdur / uttnetchar
# Add word and syllable boundaries and labels
thisword = Get interval at time... word_tier uttstart
wstart = Get starting point... word_tier thisword
if wstart < uttstart
Insert boundary... word_tier uttstart
endif
thisword = Get interval at time... word_tier uttstart
wend = uttstart
thissyll = Get interval at time... syllable_tier uttstart
sstart = Get starting point... syllable_tier thissyll
if sstart < uttstart
Insert boundary... syllable_tier uttstart
endif
thissyll = Get interval at time... syllable_tier uttstart
for w to words
w$ = word'w'$
wlength = length (w$)
wstart = wend
wend = wend + (wlength * uttunit)
if w = words
wend = uttend
endif
thisword = Get interval at time... word_tier wend
Set interval text... word_tier thisword 'w$'
call CheckIfBoundaryExists word_tier wend
if boundary_exists = 0
Insert boundary... word_tier wend
endif
call GetSyllables 'w$'
send = wstart
for s to syllables
s$ = syllable's'$
slength = length (s$)
sstart = send
send = send + (slength * uttunit)
if s = syllables
send = wend
if w = words
send = uttend
endif
endif
thissyll = Get interval at time... syllable_tier send
Set interval text... syllable_tier thissyll 's$'
call CheckIfBoundaryExists syllable_tier send
if boundary_exists = 0
Insert boundary... syllable_tier send
endif
endfor
endfor
# Go to the editor window and select a region around the utterance:
selstart = uttstart - 0.3
if selstart < 0
selstart = 0
endif
selend = uttend + 0.3
if selend > total_duration
selend = total_duration
endif
editor TextGrid temp
Select... selstart selend
Zoom to selection
Select... uttstart uttend
# Done. Now return to original selections.
endeditor
select LongSound 'sound$'
plus TextGrid 'grid$'
editor TextGrid temp
# Here we copy the edited temporary TextGrid to the
# original TextGrid.
endeditor
select TextGrid 'grid$'
Remove
select TextGrid temp
Copy... 'grid$'
select LongSound 'sound$'
plus TextGrid 'grid$'
endif
endif
endfor
else
# This script cannot proceed without an utterance tier.
exit Utterance tier "'utterance_tier_name$'" does not exist!
Remove
select LongSound 'sound$'
plus TextGrid 'grid$'
endif
printline 'numberOfHandledUtterances' non-empty utterance intervals were checked.
printline Please remember to save the TextGrid!
#-------------
procedure GetTier name$ variable$
numberOfTiers = Get number of tiers
itier = 1
repeat
tier$ = Get tier name... itier
itier = itier + 1
until tier$ = name$ or itier > numberOfTiers
if tier$ <> name$
'variable$' = 0
else
'variable$' = itier - 1
endif
endproc
#-----------------
procedure IsLegalLabel string$
# This will exclude syllable labels starting with a dot (.), and empty labels.
if string$ <> "" and
... string$ <> "xxx" and
... left$ (string$, 1) <> "."
legal = 1
else
legal = 0
endif
endproc
#-----------------
procedure GetWords string$
words = 0
while index (string$, " ")
words = words + 1
word'words'$ = left$ (string$, (index (string$, " ") - 1))
string$ = right$ (string$, (length (string$) - index (string$, " ")))
endwhile
words = words + 1
word'words'$ = string$
if word'words'$ = ""
words = words - 1
endif
endproc
#-----------------
procedure GetSyllables wstring$
# The input must be a legal word string.
numberOfChars = length (wstring$)
syllables = 0
while wstring$ <> ""
syllables = syllables + 1
syllable'syllables'$ = ""
vowel = 0
char$ = left$ (wstring$, 1)
call IsVowel 'char$'
# onset of syllable, until we meet a vowel character:
while vowel = 0 and wstring$ <> ""
syllable'syllables'$ = syllable'syllables'$ + left$ (wstring$, 1)
wstring$ = right$ (wstring$, (length (wstring$) - 1))
if left$ (wstring$, 1) = "-"
syllable'syllables'$ = syllable'syllables'$ + left$ (wstring$, 1)
wstring$ = right$ (wstring$, (length (wstring$) - 1))
endif
char$ = left$ (wstring$, 1)
call IsVowel 'char$'
endwhile
# nucleus of syllable:
vowels = 0
lastvowel$ = ""
boundary = 0
while vowel = 1 and vowels < 2 and boundary = 0 and wstring$ <> ""
vowels = vowels + 1
vowel$ = left$ (wstring$, 1)
if ((lastvowel$ <> "o" or lastvowel$ <> "a") or vowel$ <> "e")
syllable'syllables'$ = syllable'syllables'$ + left$ (wstring$, 1)
wstring$ = right$ (wstring$, (length (wstring$) - 1))
if left$ (wstring$, 1) = "-"
syllable'syllables'$ = syllable'syllables'$ + left$ (wstring$, 1)
wstring$ = right$ (wstring$, (length (wstring$) - 1))
endif
char$ = left$ (wstring$, 1)
call IsVowel 'char$'
lastvowel$ = vowel$
else
boundary = 1
endif
endwhile
# consonants before next vowel or end of word:
consonants = 0
consonants$ = ""
boundary = 0
while vowel = 0 and wstring$ <> ""
consonants = consonants + 1
consonants$ = consonants$ + left$ (wstring$, 1)
wstring$ = right$ (wstring$, (length (wstring$) - 1))
if left$ (wstring$, 1) = "-"
consonants$ = consonants$ + left$ (wstring$, 1)
wstring$ = right$ (wstring$, (length (wstring$) - 1))
endif
char$ = left$ (wstring$, 1)
call IsVowel 'char$'
endwhile
# if these are word final consonants, add all to this syllable:
if wstring$ = ""
syllable'syllables'$ = syllable'syllables'$ + consonants$
# otherwise, put last consonant to next syllable:
else
if consonants > 1
syllable'syllables'$ = syllable'syllables'$ + left$ (consonants$, length (consonants$) - 1)
wstring$ = right$ (consonants$, 1) + wstring$
else
wstring$ = consonants$ + wstring$
endif
endif
endwhile
endproc
#-----------------
procedure IsVowel string$
# If the leftmost character of the string is a vowel symbol, returns vowel = 1.
if left$ (string$, 1) = "a" or
... left$ (string$, 1) = "A" or
... left$ (string$, 1) = "e" or
... left$ (string$, 1) = "E" or
... left$ (string$, 1) = "i" or
... left$ (string$, 1) = "I" or
... left$ (string$, 1) = "o" or
... left$ (string$, 1) = "O" or
... left$ (string$, 1) = "u" or
... left$ (string$, 1) = "U" or
... left$ (string$, 1) = "y" or
... left$ (string$, 1) = "Y" or
... left$ (string$, 1) = "ä" or
... left$ (string$, 1) = "Ä" or
... left$ (string$, 1) = "@" or
... left$ (string$, 1) = "ö" or
... left$ (string$, 1) = "Ö" or
... left$ (string$, 1) = "7" or
... left$ (string$, 1) = "&" or
... left$ (string$, 1) = ">" or
... left$ (string$, 1) = "2" or
... left$ (string$, 1) = "3" or
... left$ (string$, 1) = "4" or
... left$ (string$, 1) = "5" or
... left$ (string$, 1) = "6" or
... left$ (string$, 1) = "8"
vowel = 1
else
vowel = 0
endif
endproc
#----------------
procedure CheckUtteranceBoundaries utterance_tier utterance
# Given that phone_tier exists:
if phone_tier > 0
uttlabel$ = Get label of interval... utterance_tier utterance
uttstart = Get starting point... utterance_tier utterance
firstphone = Get interval at time... phone_tier uttstart
firstphonestart = Get starting point... phone_tier firstphone
firstphoneend = Get end point... phone_tier firstphone
startdiff = uttstart - firstphonestart
enddiff = firstphoneend - uttstart
prevutterance = utterance - 1
if prevutterance >= 1
prevuttstart = Get starting point... utterance_tier prevutterance
else
prevuttstart = 0
endif
if startdiff < 0.5 or enddiff < 0.5
if startdiff < enddiff
newuttstart = firstphonestart
else
newuttstart = firstphoneend
endif
firstphone = Get interval at time... phone_tier newuttstart
firstphonelabel$ = Get label of interval... phone_tier firstphone
newuttstart = Get starting point... phone_tier firstphone
if firstphonelabel$ = ""
firstphone = Get interval at time... phone_tier newuttstart
firstphonelabel$ = Get label of interval... phone_tier firstphone
newuttstart = Get starting point... phone_tier firstphone
endif
if firstphonelabel$ <> "" and newuttstart > prevuttstart and newuttstart <> uttstart
# Now we can change the starting point of this utterance:
Set interval text... utterance_tier utterance
call CheckIfBoundaryExists utterance_tier newuttstart
if boundary_exists = 0
Insert boundary... utterance_tier newuttstart
endif
editor TextGrid temp
Move cursor to... uttstart
if newuttstart < uttstart
Select previous interval
Select next interval
else
Select next interval
Select previous interval
endif
Remove
Move cursor to... 0
endeditor
utterance = Get interval at time... utterance_tier newuttstart
Set interval text... utterance_tier utterance 'uttlabel$'
uttstart = newuttstart
endif
endif
uttend = Get end point... utterance_tier utterance
lastphone = Get interval at time... phone_tier uttend
lastphonestart = Get starting point... phone_tier lastphone
lastphoneend = Get end point... phone_tier lastphone
startdiff = uttend - lastphonestart
enddiff = lastphoneend - uttend
nextutterance = utterance + 1
if nextutterance >= numberOfUtterances
nextuttend = total_duration
else
nextuttend = Get end point... utterance_tier nextutterance
endif
if startdiff < 0.5 or enddiff < 0.5
if startdiff < enddiff
newuttend = lastphonestart
else
newuttend = lastphoneend
endif
lastphone = Get interval at time... phone_tier newuttend
lastphonelabel$ = Get label of interval... phone_tier lastphone
newuttend = Get end point... phone_tier lastphone
if lastphonelabel$ = ""
lastphone = lastphone - 1
lastphonelabel$ = Get label of interval... phone_tier lastphone
newuttend = Get end point... phone_tier lastphone
endif
if lastphonelabel$ <> "" and newuttend < nextuttend and newuttend <> uttend
# Now we can change the starting point of this utterance:
Insert boundary... utterance_tier newuttend
editor TextGrid temp
Move cursor to... uttend
if newuttend > uttend
Select next interval
else
Select previous interval
Select next interval
endif
Remove
endeditor
uttend = newuttend
endif
endif
endif
endproc
#-------------
procedure CheckIfBoundaryExists temptier temptime
boundary_exists = 0
tempinterval = Get interval at time... temptier temptime
tempstart = Get starting point... temptier tempinterval
if tempstart = temptime
boundary_exists = 1
endif
endproc