############################################################ # diphone-locator-for-TextGrids.praat ( Written by Kyuchul Yoon kyoon@ling.osu.edu # This script reads legitimate existing diphones (1975 diphones) one by one # and for each diphone, 1) gives a list of all the textgrid filenames containing it # and 2) marks it in the first textgrid and saves the textgrid file in a different # directory. # After the execution, the new folder will contain those textgrids that contain legitimate # existing diphones only. ############################################################ form Specify files and folders word diphoneFile 02.all-diphones-in-recordings.one-column.unix-sort-uniq word inFolder 07.lab.TextGrid.after.LTS.scheme.diphone.textgrid word inFileExt_(with_dot) .TextGrid.prosodic.diphone natural diphoneTier 5 natural tierToCreate 6 word tierName marked.diphones word outFolder_(with_diphones_marked) ..\10.lab.TextGrid.PRAAT.EMU.diphone-labeling word logFile_(list_of_TextGrids) list.of.diphones.and.their.TextGrids.txt word userInfo WHICH.DIPHONE. endform # Create a new directory that will hold textgrids with diphones marked system mkdir 'outFolder$' ################## # READ DIPHONE FILE ################## Read Strings from raw text file... 'diphoneFile$' Rename... diphoneFile numDiphones = Get number of strings pause 'numDiphones' identified. Continue? ################## # FOR EACH DIPHONE ################## for iDiphone from 257 to numDiphones # Give user information about which diphone is being processed at what time fileappend 'userinfo$' Started at'tab$' system time /T >> 'userinfo$' system date /T >> 'userinfo$' fileappend 'userInfo$' 'newline$'diphone 'iDiphone''newline$' select Strings diphoneFile diphoneToProcess$ = Get string... iDiphone # Here, you won't have to look for the new directory if iDiphone = 1 # Check the inFolder to see if the current diphone can be found there # Create a list of TextGrid files to be looked for legitimate existing diphones Create Strings as file list... oldFileList 'inFolder$'\*'inFileExt$' Sort Sort numOldFiles = Get number of strings # pause 'numOldFiles' TextGrids identified. Continue? iOldFile = 1 oldFileFlag = 1 # Repeat doing the following block of codes until the last file while (oldFileFlag = 1 and iOldFile <> (numOldFiles+1)) select Strings oldFileList oldFileName$ = Get string... iOldFile Read from file... 'inFolder$'\'oldFileName$' Rename... oldFile numDiphoneIntervalsOld = Get number of intervals... diphoneTier kInterval = 2 oldIntervalFlag = 1 intervalTextOld$ = Get label of interval... diphoneTier kInterval while (oldIntervalFlag = 1 and kInterval <> numDiphoneIntervalsOld) if diphoneToProcess$ = intervalTextOld$ # Do the modification Insert interval tier... tierToCreate 'tierName$' # Get the starting and end time of the current interval startTime = Get starting point... diphoneTier kInterval endTime = Get end point... diphoneTier kInterval # Insert boundaries at startTime and endTime Insert boundary... tierToCreate startTime Insert boundary... tierToCreate endTime # Get interval number for current interval by using startTime oldIntervalNum = Get interval at time... tierToCreate startTime # Set the interval text at the designated interval Set interval text... tierToCreate oldIntervalNum 'diphoneToProcess$' # Save the textgrid at the new directory Write to text file... 'outFolder$'\'oldFileName$' # Delete the file in inFolder filedelete 'inFolder$'\'oldFileName$' # Skip the rest of the intervals and old files by # flagging the intervals and files oldIntervalFlag = 2 oldFileFlag = 2 endif kInterval = kInterval + 1 select TextGrid oldFile intervalTextOld$ = Get label of interval... diphoneTier kInterval endwhile # Remove the textgrid select TextGrid oldFile Remove # # User info wrt/ the number of diphones identified # fileappend 'userInfo$' diphone 'iDiphone''tab$'iOldFile 'iOldFile''newline$' iOldFile = iOldFile + 1 endwhile # Remove the file list select Strings oldFileList Remove ################################################# # LOOK IN NEW DIRECTORY FIRST AND THEN OLD DIRECTORY # Otherwise, you first need to look at files in the new directory, followed # by those in the old directory ################################################# else # Flag for switching from new to old directory. # Change this into 2 when current diphone is found in the new folder switchFlag = 1 # Check the new folder to see if the current diphone can be found there # Read the list of textgrids Create Strings as file list... newFileList 'outFolder$'\*'inFileExt$' Sort Sort numNewFiles = Get number of strings # pause 'numNewFiles' TextGrids identified. Continue? # Set the index and flag iNewFile = 1 newFileFlag = 1 # Repeat the following block of code as long as the flag is not changed # and unless you reach the last interval while (newFileFlag = 1 and iNewFile <> (numNewFiles+1)) # Read textgrids one by one select Strings newFileList newFileName$ = Get string... iNewFile Read from file... 'outFolder$'\'newFileName$' Rename... newFile # # Give user information on which textgrid file's being processed # fileappend 'userInfo$' 'newFileName$''newline$' # Get the number of diphone intervals for current file numDiphoneIntervalsNew = Get number of intervals... diphoneTier # Start from the second interval (1st one has just ) iInterval = 2 # Set the flag to one. Change it when you find the diphone newIntervalFlag = 1 intervalTextNew$ = Get label of interval... diphoneTier iInterval # Repeat the following block of code as long as the flag is not changed # and unless you reach the last interval while (newIntervalFlag = 1 and iInterval <> numDiphoneIntervalsNew) # If the current interval text corresponds to the diphone if diphoneToProcess$ = intervalTextNew$ # Do the modification, except for creating the new tier of marked diphones # Get the starting and end time of the current interval startTime = Get starting point... diphoneTier iInterval endTime = Get end point... diphoneTier iInterval # Before inserting boundaries, check if boundaries exist already # Also, set a flag so that either one end of the boundary or both boundaries can be inserted alreadyFlag = 1 # Get the number of intervals for the marked.diphones tier nIntervals = Get number of intervals... tierToCreate # For each interval of the marked.diphones tier, check the starting time of each interval # for y to nIntervals indexInterval = 1 while (alreadyFlag = 1 and indexInterval <> (nIntervals+1)) # Get the starting and end time of the current interval startT = Get starting point... tierToCreate indexInterval nextT = Get end point... tierToCreate indexInterval # If the startT and nextT is the same as the two boundaries around a diphone, # then the diphone is the intervening one in between two adjacent diphones # so skip the boundary inserting block and go to the interval text setting stage if (startT = startTime and nextT = endTime) # Do nothing, meaning skip this if-clause. Just change the flag alreadyFlag = 2 # If the diphone to be inserted to marked.diphones tier follows an already # existing diphone, then just add a boundary at endTime and change the flag elsif startT = startTime alreadyFlag = 2 Insert boundary... tierToCreate endTime # If the diphone to be inserted to marked.diphones tier precedes an already # existing diphone, then just add a boundary at startTime and change the flag elsif startT = endTime alreadyFlag = 2 Insert boundary... tierToCreate startTime endif # Increase the index of the interval by one indexInterval = indexInterval + 1 endwhile # If the alreadyFlag is 1, then the boundaries do not overlap at all. So insert both boundaries # Otherwise, if it's 2, then the starting/ending boundary is already inserted, so skip this part # and set the interval text if alreadyFlag = 1 # Insert boundaries at startTime and endTime Insert boundary... tierToCreate startTime Insert boundary... tierToCreate endTime endif # Get interval number for current interval by using startTime newIntervalNum = Get interval at time... tierToCreate startTime # Set the interval text at the designated interval Set interval text... tierToCreate newIntervalNum 'diphoneToProcess$' # Save the textgrid at the new directory, deleting the old file filedelete 'outFolder$'\'newFileName$' Write to text file... 'outFolder$'\'newFileName$' # Skip the rest of the intervals and old files by # flagging the intervals and files newIntervalFlag = 2 newFileFlag = 2 # Flag the switchFlag so that you can skip the next big section for the older folder switchFlag = 2 endif # Increase the counter of intervals by one iInterval = iInterval + 1 # And get the next interval text select TextGrid newFile intervalTextNew$ = Get label of interval... diphoneTier iInterval endwhile # Remove the textgrid select TextGrid newFile Remove # # User info wrt/ the number of diphones identified # fileappend 'userInfo$' diphone 'iDiphone''tab$'iOldFile 'iOldFile''tab$'iNewFile 'iNewFile''newline$' iNewFile = iNewFile + 1 endwhile # Remove the file list select Strings newFileList Remove # If there's no matching diphone in the new directory, go back to the old one # and do the same as above. That is, check the inFolder to see if the current # diphone can be found there. # Create a list of TextGrid files to be looked for legitimate existing diphones Create Strings as file list... oldFileList2 'inFolder$'\*'inFileExt$' Sort Sort numOldFiles2 = Get number of strings # pause 'numOldFiles2' TextGrids identified. Continue? iOldFile2 = 1 oldFileFlag2 = 1 ################################################################# # NEW DIRECTORY FAILED YOU. SO NOW LOOK IN THE OLD FOLDER # Repeat doing the following block of codes until the last file # If the switchFlag is still 1, that means the current diphone was not processed in the new folder # So you need to go to the old folder ################################################################# while (switchFlag = 1 and oldFileFlag2 = 1 and iOldFile2 <> (numOldFiles2+1)) # Read textgrids one by one select Strings oldFileList2 oldFileName2$ = Get string... iOldFile2 Read from file... 'inFolder$'\'oldFileName2$' Rename... oldFile2 # Get the number of diphone intervals for current file numDiphoneIntervalsOld2 = Get number of intervals... diphoneTier # Start from the second interval (1st one has just ) kInterval2 = 2 # Set the flag to one. Change it when you find the diphone oldIntervalFlag2 = 1 intervalTextOld2$ = Get label of interval... diphoneTier kInterval2 # Repeat the following block of code as long as the flag is not changed # and unless you reach the last interval while (oldIntervalFlag2 = 1 and kInterval2 <> numDiphoneIntervalsOld2) # If the current interval text corresponds to the diphone if diphoneToProcess$ = intervalTextOld2$ # Then, do the modification by first creating the marked.diphones tier Insert interval tier... tierToCreate 'tierName$' # Get the starting and end time of the current interval startTime = Get starting point... diphoneTier kInterval2 endTime = Get end point... diphoneTier kInterval2 # Since all the textgrids in the old folder don't have any marked diphones # Insert boundaries at startTime and endTime Insert boundary... tierToCreate startTime Insert boundary... tierToCreate endTime # Get interval number for current interval by using startTime # This is necessary to set the interval text of the newly added diphone interval oldIntervalNum2 = Get interval at time... tierToCreate startTime # Set the interval text at the designated interval Set interval text... tierToCreate oldIntervalNum2 'diphoneToProcess$' # Save the textgrid at the new directory Write to text file... 'outFolder$'\'oldFileName2$' # Delete the old file in inFolder$ because the textgrid has been written to new folder filedelete 'inFolder$'\'oldFileName2$' # Skip the rest of the intervals and old files by # flagging the intervals and files oldIntervalFlag2 = 2 oldFileFlag2 = 2 endif # Increase the counter by one kInterval2 = kInterval2 + 1 # And get the next interval text select TextGrid oldFile2 intervalTextOld2$ = Get label of interval... diphoneTier kInterval2 endwhile # Remove the textgrid select TextGrid oldFile2 Remove # # User info wrt/ the number of diphones identified # fileappend 'userInfo$' diphone 'iDiphone''tab$'iOldFile 'iOldFile''tab$'iNewFile 'iNewFile''newline$' iOldFile2 = iOldFile2 + 1 endwhile # Remove the file list since you're done with all the textgrids in the old folder select Strings oldFileList2 Remove endif # pause Move onto the next diphone? endfor # Give user info. on when the script is ended fileappend 'userinfo$' Ended at'tab$' system time /T >> 'userinfo$' system date /T >> 'userinfo$' select Strings diphoneFile Remove ########### END OF SCRIPT ##############