Search code examples
praat

Praat scripting: Extracting syllable tier together with accent tier with extra changes


I want to extract syllables together with the corresponding accents. If a syllable doesn't have an accent, there will be "no" on the accent part.

My coding example looks like this:

writeInfo: ""

selectObject: "TextGrid example"

# syllable tier is 1
# accent tier is 2
number = Get number of intervals: 1
for i from 1 to number
syllable$ = Get label of intervals: 1, i 
# It seems to be not possible to get time of interval
# I want to get the time of the whole interval, like it's done with points
syllable_time = Get time of interval: 1, i
accent = Get point at time: 2, syllable_time
accent$ = Get label of point: 2, accent
    #if no accent$
    #appendInfoLine syllable$, "      ", "no"
    #elif accent$ <> "-" and accent$ <> "%"
    #appendInfoLine syllable$, "      ", accent$
    #endif
endfor

The result should look like this:

"de:6       no
I           no
"Ra:n       H*L
"vIl        no
"an         no
"zaI        no
n@m         no
a:          no
"tOm        H*

Adding

Layers 1 and 2:

enter image description here


Solution

  • You can do that in a few steps. First, add an additional 'no' to layer 2:

    #Select TexGrid
    selectObject: 1
    
    number = Get number of intervals: 1
    
    for i from 1 to number
    time_start = Get start point: 1, i
    #time_end = Get end point: 1, i
    name$ = Get label of interval: 1, i
    point$ = Get label of point: 2, i
    
    Insert point: 2, time_start, "no"
    endfor
    

    Then, extract the information from layer 2 and save it to a file:

    #Select TextGrid
    selectObject: 1
    
    number = Get number of points: 2
    for n from 1 to number
    accent_time = Get time of point: 2, n
    syllable = Get interval at time: 1, accent_time 
    syllable$ = Get label of interval: 1, syllable
    accent$ = Get label of point: 2, n
    
    writeFileLine: "myFile.txt", accent$
    
    endfor
    

    As the last step, you need to remove those additional 'no''s from your outcome. Let's do that in Python (Mention all the pitch shapes you have, so the program knows what lines you want to get rid of):

    fo = open("myFile.txt", "r")
    st = fo.read();
    lis = st.split()
    fo.close()
    
    
    for i, choice in enumerate(lis):
        if choice == 'H*L' and lis[i-1] == 'no':
            lis.pop(i-1)
        elif choice == 'H*' and lis[i-1] == 'no':
            lis.pop(i-1)
        elif choice == 'L*H' and lis[i-1] == 'no':
            lis.pop(i-1)
        elif choice == 'L%' and lis[i-1] == 'no':
            lis.pop(i-1)
        elif choice == 'H%' and lis[i-1] == 'no':
            lis.pop(i-1)
        elif choice == 'L*' and lis[i-1] == 'no':
            lis.pop(i-1)
        elif choice == '!H*L' and lis[i-1] == 'no':
            lis.pop(i-1)
        elif choice == '!H*' and lis[i-1] == 'no':
            lis.pop(i-1)
        elif choice == 'H*L?' and lis[i-1] == 'no':
            lis.pop(i-1)
        elif choice == '..L' and lis[i-1] == 'no':
            lis.pop(i-1)
        elif choice == 'L*HL' and lis[i-1] == 'no':
            lis.pop(i-1)
        elif choice == '*?' and lis[i-1] == 'no':
            lis.pop(i-1)
        elif choice == 'L*H?' and lis[i-1] == 'no':
            lis.pop(i-1)
        elif choice == 'H*?' and lis[i-1] == 'no':
            lis.pop(i-1)
        elif choice == '..H' and lis[i-1] == 'no':
            lis.pop(i-1)
        elif choice == 'L*?' and lis[i-1] == 'no':
            lis.pop(i-1)
        elif choice == '!H' and lis[i-1] == 'no':
            lis.pop(i-1)
        elif choice == 'H!' and lis[i-1] == 'no':
            lis.pop(i-1)
        elif choice == 'HH*L' and lis[i-1] == 'no':
            lis.pop(i-1)
        elif choice == '!H*L?' and lis[i-1] == 'no':
            lis.pop(i-1)
        elif choice == '.L' and lis[i-1] == 'no':
            lis.pop(i-1)
        elif choice == 'L*!H' and lis[i-1] == 'no':
            lis.pop(i-1)
        elif choice == 'L*HL?' and lis[i-1] == 'no':
            lis.pop(i-1)
        elif choice == 'LH*L' and lis[i-1] == 'no':
            lis.pop(i-1)
    
    
    with open("output.txt", "w") as my_file:
        for i in lis:
            my_file.write(i + "\n")