Sequence Navigation

Let’s say we began with this textgrid and read it in with AlignedTextGrid().

the dog
from aligned_textgrid import AlignedTextGrid
from aligned_textgrid import Word, Phone

the_dog = AlignedTextGrid(
    textgrid_path="../resources/the_dog.TextGrid", 
    entry_classes=[Word, Phone]
    )
print(the_dog)
AlignedTextGrid with 1 groups, each with [2] tiers. [['Word', 'Phone']]

Let’s grab the objects representing the schwa in “the” and the the entire word “dog”.

AH0 = the_dog.tier_groups[0].tier_list[1].sequence_list[1]
dog = the_dog.tier_groups[0].tier_list[0].sequence_list[1]

Both of these objects have got basic interval information available as attributes, specifically .start, .end and .label.

[AH0.start, AH0.end, AH0.label]
[0.1827542202196579, 0.308291607646728, 'AH0']
[dog.start, dog.end, dog.label]
[0.308291607646728, 0.9665869095874072, 'dog']

From within these sequence intervals, we can also access information from the precedence and hierarchical relationships.

The Precedence and Hierarchy structure

Here is a schematic illustration of how each Sequence Interval is related to the others.

Sequence Structure

Working with precedence

For our AH0 object, we can access its preceding phone with .prev

AH0.prev
Class Phone, label: DH, .superset_class: Word, .super_instance: the, .subset_class: Bottom_wp

Importantly, this is the actual preceding Sequence Interval object, so we an access all of it’s interval information like we did for AH0 itself.

[AH0.prev.start, AH0.prev.end, AH0.prev.label]
[0.0, 0.1827542202196579, 'DH']

Because AH0 is the final Sequence Interval within this word, its .fol attribute is a reference to a dummy boundary interval.

[AH0.fol.start, AH0.fol.end, AH0.fol.label]
[None, None, '#']

The dog object from the word tier has the same .prev and .fol methods available.

[dog.prev.start, dog.prev.end, dog.prev.label]
[0.0, 0.308291607646728, 'the']
[dog.fol.start, dog.fol.end, dog.fol.label]
[None, None, '#']

Using hierarchy

Moving upwards

You can also navigate upwards and downwards with these objects. The most general attribute to get the Sequence Interval above is .super_instance

AH0.super_instance
Class Word, label: the, .superset_class: Top_wp, .super_instance, None, .subset_class: Phone, .subset_list: ['DH', 'AH0']

However, the Phone class from aligned_textgrid.sequences.word_and_phone also has an .inword attribute for convenience.

AH0.inword
Class Word, label: the, .superset_class: Top_wp, .super_instance, None, .subset_class: Phone, .subset_list: ['DH', 'AH0']
[AH0.inword.start, AH0.inword.end, AH0.inword.label]
[0.0, 0.308291607646728, 'the']

Moving Downwards

From the dog object, we can navigate downwards with the .subset_list attribute, which returns a list of each phone Sequence Interval.

dog.subset_list
[Class Phone, label: D, .superset_class: Word, .super_instance: dog, .subset_class: Bottom_wp,
 Class Phone, label: AO1, .superset_class: Word, .super_instance: dog, .subset_class: Bottom_wp,
 Class Phone, label: G, .superset_class: Word, .super_instance: dog, .subset_class: Bottom_wp]

We can also directly index into the subset list with dog[]

Sequence indexing
dog[2]
Class Phone, label: G, .superset_class: Word, .super_instance: dog, .subset_class: Bottom_wp
[dog[2].start, dog[2].end, dog[2].label]
[0.8356850885224085, 0.9665869095874072, 'G']

There are also special attributes .first and .last to access the first and last sequences within the subset list.

[dog.first.label, dog.last.label]
['D', 'G']

Moving all around

Since each of these attributes (.prev, .fol, .inword and .subset_list) returns the actual Sequence Interval objects, we can chain them together to create a path through the hierarchy. For example, we can see that AH0 is the final phone in its word.

AH0.fol.label
'#'

To get the label of the first phone of the following word, we can move up to .inword, over one with .fol and then index down into the subset list with .first.

hierarchy path

There is an easier way to do this with tier references. This example is just to illustrate how a path can be constructed

AH0.inword.fol.first
Class Phone, label: D, .superset_class: Word, .super_instance: dog, .subset_class: Bottom_wp

Reuse

GPLv3