Loading ...
Sorry, an error occurred while loading the content.
 

Praat script for looping through a text grid

Expand Messages
  • lisa.lipani
    Hello all, I am trying to write a praat script that reads in a number of files, and then uses a procedure to look for words in each sound file and text grid
    Message 1 of 3 , Oct 24, 2017

      Hello all,

       

      I am trying to write a praat script that reads in a number of files, and then uses a procedure to look for words in each sound file and text grid combination. The script finds the first word in the procedure (strikes), but then I get the error message "Interval number too large". I've looked at this other thread in this users group, but I can't figure out my problem. I'm relatively new to praat scripting, sorry. Here's what I have so far:


      directory$ = "directory"

      listfile$ = "test.txt"


      Read Strings from raw text file... 'directory$'/'listfile$'

      last = Get number of strings


      # loop through each file

      for a from 1 to last

      listfile2$ = listfile$ - ".txt"

      select Strings 'listfile2$'

      textgrid$ = Get string... 'a'

      Read from file... 'directory$'/'textgrid$'

      object_name$ = selected$("TextGrid")


      Read from file... 'directory$'/'object_name$'.wav


      # rearrange tiers 

      select TextGrid 'object_name$'

      Duplicate tier: 3, 1, "MAU"

      Remove tier: 4

      Insert interval tier: 1, "subphone"


      # find target word

      n = Get number of intervals... 3

      for i to n


      @instance: "strikes"

      @instance: "raindrops"

      @instance: "and"

      @instance: "rainbow"

      @instance: "into"

      @instance: "round"

      @instance: "its"

      @instance: "its"

      @instance: "ends"

      @instance: "legend"

      @instance: "end"

      @instance: "finds"

      @instance: "looks"

      @instance: "and"

      @instance: "friends"

      @instance: "rainbow"


      procedure instance: .target_word$


      select TextGrid 'object_name$'


      label$ = Get label of interval: 3, i

      if label$ == .target_word$

      index = i

      i += n


      # get the start and end point of the word

      startpoint = Get starting point... 3 index

      endpoint = Get end point... 3 index



      plus Sound 'object_name$'

      View & Edit

      editor TextGrid 'object_name$'



      # annotation

      Select... startpoint endpoint

      Zoom to selection

      pause Annotate stops then continue

      Close

      endeditor



      endif # if the label = target word

      endfor # for number of intervals


      endproc


      #select TextGrid 'object_name$'

      #Write to text file: directory$ + "/" + object_name$ + "_editedtext.TextGrid"


      endfor # for each of the files


      clearinfo

      print That's it!


    • José Joaquín Atria
      I just wrote a detailed explanation of what is going on in this script over on StackOverflow [1]. Some of these behaviours (as seen on Praat 6.0.30) should
      Message 2 of 3 , Oct 25, 2017
        I just wrote a detailed explanation of what is going on in this script
        over on StackOverflow [1].

        Some of these behaviours (as seen on Praat 6.0.30) should probably be
        revisited to avoid this type of confusion.

        [1]: https://stackoverflow.com/a/46933067/807650
        José Joaquín Atria

        www.pinguinorodriguez.cl


        On Tue, Oct 24, 2017 at 4:48 PM, lisa.lipani@... [praat-users]
        <praat-users-noreply@...> wrote:
        >
        >
        > Hello all,
        >
        >
        >
        > I am trying to write a praat script that reads in a number of files, and
        > then uses a procedure to look for words in each sound file and text grid
        > combination. The script finds the first word in the procedure (strikes), but
        > then I get the error message "Interval number too large". I've looked at
        > this other thread in this users group, but I can't figure out my problem.
        > I'm relatively new to praat scripting, sorry. Here's what I have so far:
        >
        >
        > directory$ = "directory"
        >
        > listfile$ = "test.txt"
        >
        >
        > Read Strings from raw text file... 'directory$'/'listfile$'
        >
        > last = Get number of strings
        >
        >
        > # loop through each file
        >
        > for a from 1 to last
        >
        > listfile2$ = listfile$ - ".txt"
        >
        > select Strings 'listfile2$'
        >
        > textgrid$ = Get string... 'a'
        >
        > Read from file... 'directory$'/'textgrid$'
        >
        > object_name$ = selected$("TextGrid")
        >
        >
        > Read from file... 'directory$'/'object_name$'.wav
        >
        >
        > # rearrange tiers
        >
        > select TextGrid 'object_name$'
        >
        > Duplicate tier: 3, 1, "MAU"
        >
        > Remove tier: 4
        >
        > Insert interval tier: 1, "subphone"
        >
        >
        > # find target word
        >
        > n = Get number of intervals... 3
        >
        > for i to n
        >
        >
        > @instance: "strikes"
        >
        > @instance: "raindrops"
        >
        > @instance: "and"
        >
        > @instance: "rainbow"
        >
        > @instance: "into"
        >
        > @instance: "round"
        >
        > @instance: "its"
        >
        > @instance: "its"
        >
        > @instance: "ends"
        >
        > @instance: "legend"
        >
        > @instance: "end"
        >
        > @instance: "finds"
        >
        > @instance: "looks"
        >
        > @instance: "and"
        >
        > @instance: "friends"
        >
        > @instance: "rainbow"
        >
        >
        > procedure instance: .target_word$
        >
        >
        > select TextGrid 'object_name$'
        >
        >
        > label$ = Get label of interval: 3, i
        >
        > if label$ == .target_word$
        >
        > index = i
        >
        > i += n
        >
        >
        > # get the start and end point of the word
        >
        > startpoint = Get starting point... 3 index
        >
        > endpoint = Get end point... 3 index
        >
        >
        >
        > plus Sound 'object_name$'
        >
        > View & Edit
        >
        > editor TextGrid 'object_name$'
        >
        >
        >
        > # annotation
        >
        > Select... startpoint endpoint
        >
        > Zoom to selection
        >
        > pause Annotate stops then continue
        >
        > Close
        >
        > endeditor
        >
        >
        >
        > endif # if the label = target word
        >
        > endfor # for number of intervals
        >
        >
        > endproc
        >
        >
        > #select TextGrid 'object_name$'
        >
        > #Write to text file: directory$ + "/" + object_name$ +
        > "_editedtext.TextGrid"
        >
        >
        > endfor # for each of the files
        >
        >
        > clearinfo
        >
        > print That's it!
        >
        >
        >
      • Paul Boersma
        ... yes, these are several syntax errors in the script, and ideally Praat should be made capable of detecting all of them. Some constructs that would count as
        Message 3 of 3 , Oct 25, 2017
          On 25 Oct 2017, at 14:57, José Joaquín Atria jjatria@... [praat-users] <praat-users-noreply@...> wrote:

          Some of these behaviours (as seen on Praat 6.0.30) should probably be
          revisited to avoid this type of confusion.

          yes, these are several syntax errors in the script, and ideally Praat should be made capable of detecting all of them.

          Some constructs that would count as syntax errors in Praat scripts:

          - when there is a "for" statement, the corresponding "endfor" has to be on the same level, e.g. if a procedure contains an "endfor" then the corresponding "for" should be in the same procedure (and precede the "endfor"), and if an "if/endif" block contains an "endfor" then the corresponding "for" should be in the same "if/endif" block (and precede the "endfor")
          - when there is a "if" statement, the corresponding "endif" has to be on the same level, e.g. if a procedure contains an "endif" then the corresponding "if" should be in the same procedure (and precede the "endif"), and if a "for/endfor" loop contains an "endif" then the corresponding "if" should be in the same "for/endfor" loop (and precede the "endif")
          - you cannot use "goto" to jump into a procedure from outside
          - you cannot use "goto" to jump out of a procedure (but you *can* use "goto" to jump out of an "if/endif" block or out of a "for/endfor" loop)

          These restrictions are the same in all procedural programming languages. Praat does not check all of these restrictions, which partly has to do with still having to support variable interpolation with single quotes. Our lack of checking has the advantage that you can have a valid script with an "exit" followed by any kind of nonsense, but it has the disadvantage that Praat cannot perform all syntax checks.

          In Lisa's script, the syntax errors are real programming errors. The eight calls to "@instance" provide eight "endfors", whereas there are only two "fors" to close, so even the interpretation of "endfor" that José considers (closing the last "for") would make the script crash at the third "@instance" (at the latest). The next version of Praat will point out that the first "endfor" that it sees is not matched by any "for", so it will stop inside the first "@instance".

          The script can be repaired by moving "endproc" just below the "endif" (other mistakes, though, are the use of "select all" and the raising of i, as José noted).

          You would easily have spotted the syntax error if you had followed the indenting guidelines from the manual, instead of starting each line with a random number of tabs.
          _____

          Paul Boersma
          Professor of Phonetic Sciences
          University of Amsterdam
          Spuistraat 134, room 632
          1012VB Amsterdam, The Netherlands
          http://www.fon.hum.uva.nl/paul/

        Your message has been successfully submitted and will be delivered to recipients shortly.