# labels2textGrid.praat # Convert text file(s) with phonetic alignement to TextGrid file(s). # The input text file contains 3 columns: label, start_time, end_time. # Columns may be separated by any of the separator characters given # in 'seps$', typically a blank, a tab or a comma. # Time intervals in the inputfile need not be contiguous, but can be. # However, time intervals should not overlap. # Times may be specified in the time unit selected in the script form. # Input files can be specified using wildcard, to convert multiple # files in one step. # Output files are written to the directory of the input files. # Modifications by Piet Mertens # 2004-10-10 PietM, rewritten # 2005-05-19 PietM, added comments form labels_to_textgrid comment Files to convert (wildcard allowed) : word Input_directory c:\corpus\ comment File(s) text Input_files *.txt optionmenu Units_(s): 4 option 0.001 option 0.01 option 0.1 option 1 option 0.0000625 endform # separators: blank, comma or tab seps$ = " ," + tab$ factor = 'units$' clearinfo filespec$ = input_directory$ + input_files$ Create Strings as file list... filelist 'filespec$' nrofFiles = Get number of strings for iFile to nrofFiles select Strings filelist fname$ = Get string... iFile filespec$ = input_directory$ + fname$ printline Opening label file ('iFile') : 'filespec$' # Read the entire file into a strings object Read Strings from raw text file... 'filespec$' file$ = selected$("Strings") nlabels = Get number of strings # printline Number of lines 'nlabels' # Create destination textgrid # Obtain starttime from first line call getdatafield 1 2 starttime = extractNumber(field$,"") * factor # Obtain endtime from last line call getdatafield nlabels 3 endtime = extractNumber(field$,"") * factor Create TextGrid... 'starttime' 'endtime' 'file$' prev = starttime for i from 1 to nlabels call getdatafield i 1 label$ = field$ call getdatafield i 2 timeL = extractNumber(field$,"") * factor call getdatafield i 3 timeR = extractNumber(field$,"") * factor midtime = timeL + (timeR-timeL)/2 #printline i 'i' label 'label$' : 'timeL' .. 'timeR' select TextGrid 'file$' if (timeL <> prev) Insert boundary... 1 'timeL' prev = timeL endif if (timeR <> prev and timeR <> endtime) Insert boundary... 1 'timeR' prev = timeR endif intervalnr = Get interval at time... 1 'midtime' Set interval text... 1 intervalnr 'label$' endfor select TextGrid 'file$' printline Writing TextGrid 'input_directory$''file$'.TextGrid Write to text file... 'input_directory$''file$'.TextGrid select Strings 'file$' Remove endfor select TextGrid 'file$' Remove select Strings filelist Remove exit # Extract field from line of strings object. # The line contains 3 fields: label, starttime, endtime. procedure getdatafield linenr fieldnr select Strings 'file$' line$ = Get string... 'linenr' len = length (line$) j = 1 # Read next field while (fieldnr >= 1) j1 = j ; j1 = startpos of field ch$ = mid$ (line$,j,1) while (index(seps$,ch$) = 0 and j <= len) j2 = j ; j2 = endpos of field j += 1 if (j < len) ch$ = mid$ (line$,j,1) endif endwhile field$ = mid$ (line$,j1,j2-j1+1) # Skip separator after field while (j < len and index(seps$,ch$) > 0) j += 1 ch$ = mid$ (line$,j,1) endwhile fieldnr -= 1 endwhile endproc exit