-
Notifications
You must be signed in to change notification settings - Fork 24
/
make_textgrid_from_segment_data.praat
77 lines (67 loc) · 2.68 KB
/
make_textgrid_from_segment_data.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
# This script will read simple text files that contain information about
# the segmentations of the corresponding sound files and import the segment boundaries and labels
# to new TextGrid objects, which will be saved in the same directory.
# The text files should have the format:
# starting point of seg1 - space - segment label - line break.
# The segments must be ordered according to time points.
#
# This script is distributed under the GNU General Public License.
# Copyright 16.3.2002 Mietta Lennes
# A bug was fixed 30.6.2003 (segment labels were going to wrong places)
form Make TextGrids for text and sound files
sentence Directory ../tmp/
sentence Sound_file_extension .wav
sentence Text_file_extension .phn
endform
overwrite = 0
# Check the contents of the user-specified directory and open appropriate files:
Create Strings as file list... list 'directory$'*
numberOfFiles = Get number of strings
for soundfile to numberOfFiles
soundfilename$ = Get string... soundfile
if right$ (soundfilename$, (length (sound_file_extension$))) = sound_file_extension$
# if a sound file was found, check if there is a corresponding text file:
filename$ = left$ (soundfilename$, (length (soundfilename$) - (length (sound_file_extension$))))
for textfile to numberOfFiles
textfilename$ = Get string... textfile
# check if the left part of the filename is identical to left part of sound filename
if left$ (textfilename$, (length (filename$))) = filename$ and (right$ (textfilename$, (length (textfilename$) - length (filename$))) = text_file_extension$)
# open both files if they match
Read Strings from raw text file... 'directory$''textfilename$'
Read from file... 'directory$''soundfilename$'
call BuildTextGrid
select Strings 'filename$'
Remove
select Strings list
endif
endfor
endif
endfor
select Strings list
Remove
#-------------------
procedure BuildTextGrid
To TextGrid... segments
select Strings 'filename$'
numberOfSegments = Get number of strings
interval = 1
for segment from 1 to numberOfSegments
select Strings 'filename$'
string$ = Get string... segment
start$ = left$ (string$, (index (string$, " ") - 1))
start = 'start$'
label$ = right$ (string$, (length (string$) - index (string$, " ")))
select TextGrid 'filename$'
if start > 0
Insert boundary... 1 start
interval = interval + 1
endif
Set interval text... 1 interval 'label$'
endfor
gridfilename$ = "'directory$''filename$'.TextGrid"
if fileReadable (gridfilename$) and overwrite = 0
pause There appear to be TextGrid files for the sound files in 'directory$'. Do you want to overwrite them with new TextGrids?
overwrite = 1
endif
Write to text file... 'gridfilename$'
endproc