API Reference

Functions

mytextgrid.create_textgrid(xmin=0, xmax=1)

Create and return an empty TextGrid.

An empty TextGrid does not contain any tier.

Parameters
  • xmin (int, float, str or decimal.Decimal, default 0) – The starting time of the TextGrid.

  • xmax (int, float, str or decimal.Decimal, default 1) – The ending time of the TextGrid.

Returns

A TextGrid instance.

Return type

mytextgrid.TextGrid

mytextgrid.read_from_file(path, encoding=None)

Read a TextGrid file with full text format and return a TextGrid object.

Parameters
  • path (str) – The path of the TextGrid file.

  • encoding (str, default None, detect automatically the encoding.) – The name of the encoding used to decode the file. See the codecs module for the list supported encodings.

Returns

A TextGrid instance.

Return type

mytextgrid.TextGrid

Classes

TextGrid

class mytextgrid.TextGrid(xmin=0, xmax=1)

A class representation for a TextGrid.

describe()

Show the attributes and structure of a TextGrid.

Show tier information: index, type, name and size.

Examples

>>> tg = mytextgrid.create_textgrid(0, 1)
>>> tone_tier = tg.insert_tier("tone", False)
>>> tone_tier.insert_tier('tone', 0.66, "H")
>>> tone_tier.insert_tier('tone', 0.9, "L")
>>> segment_tier = tg.insert_tier("segment")
>>> segment_tier.insert_boundaries('segment', 0.23, 0.30, 0.42, 0.62, 0.70, 0.82, 0.98)
>>> segment_tier.set_interval_text('segment', 1, 'e', 'l', 'p', 'e', 'rr', 'o')

Use describe() to describe a TextGrid.

>>> tg.describe()
    TextGrid:
        Name:                  perro
        Startig time (sec):    0
        Ending time (sec):     1
        Number of tiers:       2
    Tiers Summary:
    0   TextTier        tone    (size = 2)
    1   IntervalTier    segment (size = 8)
get_duration()

Return the duration of the TextGrid in seconds.

Returns

Time duration in seconds.

Return type

decimal.Decimal

Examples

>>> tg = mytextgrid.create_textgrid('banana', 0, 1.2)
>>> tg.get_duration()
    1.2
get_tier_by_name(tier_name)

Return a list of tier objects with a specific name.

Parameters

tier (str) – The name of a tier.

Returns

list of class – A list of tiers objects

Return type

mytextgrid.core.tier.Tier

insert_tier(name, interval_tier=True, index=None)

Insert a tier in the TextGrid.

Parameters
  • name (str) – The name of the tier.

  • interval_tier (bool) – If True, insert an IntervalTier. Otherwise, insert a PointTier.

  • index (int, default None, meaning the last index.) – The index of the tier.

Examples

>>> tg = mytextgrid.create_textgrid('banana', 0, 1.2)

Insert an interval tier.

>>> tg.insert_tier('word')

To create a point tier, set interval_tier as False.

>>> tg.insert_tier('tone', interval_tier = False)

With index, you can insert a tier at a specific position.

>>> tg.insert_tier('phrase', index = 0)
Returns

An empty tier.

Return type

IntervalTier or PointTier

remove_tier(index)

Remove a tier from the TextGrid.

Parameters

tier (int) – The tier index.

Returns

The removed tier.

Return type

IntervalTier or PointTier

property tiers

Return the _tiers attribute.

to_dict()

Convert a TextGrid into a dict.

write(path, short_format=False, encoding='utf-8')

Write a TextGrid object as a text file.

Parameters
  • path (str or pathlib.Path) – The path where the TextGrid file will be written.

  • short_format (bool, default False) – True to output TextGrid file in long format. False for short format.

  • encoding (str, default 'utf-8') – The encoding of the file.

write_as_json(*args, **kwds)

Write a TextGrid object as a JSON file.

Parameters
  • path (str or pathlib.Path) – The path where the JSON file will be written.

  • encoding (str, default utf-8) – The encoding of the resulting file.

property xmax

Return the _xmax attribute.

property xmin

Return the _xmin attribute.

IntervalTier

class mytextgrid.core.interval_tier.IntervalTier(name='', xmin=0, xmax=1)

A class representation for an interval tier.

get_index_at_time(time)

Return the index of an interval at the given time.

If time relies on the boundary between two intervals, it returns the index of the right interval.

Parameters

time (int, float, str or decimal.Decimal) – The time in seconds to be evaluated.

Returns

Return the interval index at the specified time.

Return type

int or None

get_index_at_time_boundary(time)

Get the index of an interval at a given boundary.

Only counts the inserted boundaries.

Parameters

time (int, float, str or decimal.Decimal) – The time to match with any boundary.

Returns

Return an integer if the

Return type

int or None

get_interval_at_time(time)

Return the interval at the specified time.

If time relies on the boundary between two intervals, it returns the the right interval.

Parameters

time (int, float, str or decimal.Decimal) – The time in seconds to be evaluated.

Returns

Return the interval index at the specified time.

Return type

mytextgrid.core.interval_tier.Interval or None

insert_boundaries(*times)

Insert one or more time boundaries into a IntervalTier.

Parameters

*time (float or decimal.Decimal) – The times (in seconds) of the new boundaries.

insert_boundary(time)

Insert a time boundary into a IntervalTier.

Parameters

time (int, float, str or :clas::decimal.Decimal) – The time (in seconds) of the new boundary.

Returns

The left and right interval position at the inserted boundary.

Return type

tuple of (int, int)

Raises

ValueError – If the specified time already exists.

move_boundary(src_time, dst_time)

Move a boundary to another location between the time range of the left and right intervals of the source location.

Parameters
  • src_time (float or decimal.Decimal) – The time (in seconds) of an existing boundary.

  • dst_time (float or decimal.Decimal) – The time (in seconds) where the boundary will be moved to.

Raises

ValueError – If the new time location is outside of the time range of the left and right intervals.

remove_boundary(time)

Remove a time boundary from a IntervalTier.

Parameters

time (int, float, str or :clas::decimal.Decimal) – The time (in seconds) of the new boundary.

Raises

ValueError – If there is there is not a boundary at the specified time.

set_text_at_index(index, *text_items)

Set the text of one or more of intervals.

If more than one text item is provided, intervals will be set from left to right counting from the starting index.

Parameters
  • index (int) – The index at which text items will be inserted.

  • text_items (str or iterable of str) – The text items that will be inserted.