Search code examples
praat

Praat scripting: extract duration of interval on a different tier


I have two interval tiers in my selected TextGrid, one with utterances and one with disfluency features marked. I've been able to script a way of extracting the information so that I have the feature in the first column, then its start time, end time, duration, and the corresponding utterance on the other tier. I would like to add a column that tells me the duration of the corresponding utterance, but as I've tried to script it I can only get it to repeat the duration of the feature.

Here's my existing code:

tierWords = 2
tierdis = 3
resultfile$ = "/Users/alice/Desktop/results.tsv"
writeFile: resultfile$
titleline$ = "Dis   Start   End Duration    Utterance   'newline$'"
fileappend "'resultfile$'" 'titleline$'
#select your TextGrid
tg = selected("TextGrid")
nint_tierdis = Get number of intervals: tierdis
for int_tierdis to nint_tierdis
    selectObject: tg
    dis$ = Get label of interval: tierdis, int_tierdis
    if dis$ <> ""
        appendFile: resultfile$, dis$
        start = Get starting point: tierdis, int_tierdis
        end = Get end point: tierdis, int_tierdis
        duration = end-start
        appendFile: resultfile$, tab$, start, tab$, end, tab$, duration
        extractedTg = Extract part: start, end, "yes"
        nint_tierWords = Get number of intervals: tierWords
        for int_tierWords to nint_tierWords
            word$ = Get label of interval: tierWords, int_tierWords
            appendFile: resultfile$, tab$, word$, newline$
        endfor
removeObject: extractedTg
endif
endfor

I hope this makes sense, TIA!


Solution

  • I'm assuming that the number of intervals on the two tiers are not aligned, so simply switching the tier you are querying with the same interval number in the loop will not work. I'm going to make the assumption that the interval you actually want to measure the duration of is always aligned with the midpoint of the intervals you are looping throuh on the tierdis.

    In that case, you can add something like this, where I get the midpoint of the current interval on tierdis, and then use that timestamp to get the # of the corresponding interval on tierWords. From there, I use that interval number to get the start, end, and calculate the duration.

      midpoint = (start+end)/2
      int_other = Get interval at time: tierWords, midpoint
      start_other = Get starting point: tierWords, int_other
      end_other = Get end point: tierWords, int_other
      duration_other = end_other-start_other
    

    Therefore the full script would look like this:

    tierWords = 2
    tierdis = 3
    resultfile$ = "/Users/alice/Desktop/results.tsv"
    writeFile: resultfile$
    titleline$ = "Dis   Start   End Duration    Utterance   'newline$'"
    fileappend "'resultfile$'" 'titleline$'
    #select your TextGrid
    tg = selected("TextGrid")
    nint_tierdis = Get number of intervals: tierdis
    for int_tierdis to nint_tierdis
        selectObject: tg
        dis$ = Get label of interval: tierdis, int_tierdis
        if dis$ <> ""
            appendFile: resultfile$, dis$
            start = Get starting point: tierdis, int_tierdis
            end = Get end point: tierdis, int_tierdis
            midpoint = (start+end)/2
            int_other = Get interval at time: tierWords, midpoint
            start_other = Get starting point: tierWords, int_other
            end_other = Get end point: tierWords, int_other
            duration_other = end_other-start_other
            duration = end-start
            appendFile: resultfile$, tab$, start, tab$, end, tab$, duration
            extractedTg = Extract part: start, end, "yes"
            nint_tierWords = Get number of intervals: tierWords
            for int_tierWords to nint_tierWords
                word$ = Get label of interval: tierWords, int_tierWords
                appendFile: resultfile$, tab$, word$, newline$
            endfor
    removeObject: extractedTg
    endif
    endfor
    
    

    Let me know if this works, or if you have any questions!