Modifying a Textgrid

You can modify a textgrid by adding an additional entry class with the .interleave_class() method.

from aligned_textgrid import AlignedTextGrid, custom_classes
two_speaker = AlignedTextGrid(
    textgrid_path = "../resources/KY25A_1.TextGrid",
    entry_classes = custom_classes(["Word", "Phone"])
)

two_speaker
AlignedTextGrid with 2 groups named ['KY25A', 'IVR'] each with [2, 2] tiers. [['Word', 'Phone'], ['Word', 'Phone']]

If we wanted to add a syllable layer in between the Word and Phone tier, we could do so like this.

two_speaker.interleave_class(
    name = "Syllable",
    above = "Phone",
    #below = "Word",
    timing_from = "below",
    copy_labels = True
)
two_speaker
AlignedTextGrid with 2 groups named ['KY25A', 'IVR'] each with [3, 3] tiers. [['Word', 'Syllable', 'Phone'], ['Word', 'Syllable', 'Phone']]

Now, you can begin building syllables on the Syllable tier using fuse methods. Here’s the first step.

import re

for speaker in two_speaker:
    for interval in speaker.Syllable:
        if re.match(r"[AEIOU]", interval.label):
            if not (re.match(r"[AEIOU]", interval.prev.label) or 
                    interval.prev.label == "NG" or
                    interval.prev.label == "#"):
                interval.fuse_leftwards()

two_speaker[0].Word[10].contains
[Class Syllable, label: S, .superset_class: Word, .super_instance: start, .subset_class: Phone, .subset_list: ['S'],
 Class Syllable, label: T AA1, .superset_class: Word, .super_instance: start, .subset_class: Phone, .subset_list: ['T', 'AA1'],
 Class Syllable, label: R, .superset_class: Word, .super_instance: start, .subset_class: Phone, .subset_list: ['R'],
 Class Syllable, label: T, .superset_class: Word, .super_instance: start, .subset_class: Phone, .subset_list: ['T']]

Reuse

GPLv3