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.io.textgrid.TextGrid

mytextgrid.read_textgrid(filepath, format_='long', encoding=None)

Read a TextGrid file and return a TextGrid object.

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

  • format ({'long'}) – The TextGrid format.

  • 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.io.textgrid.TextGrid

mytextgrid.read_textgrid_from_stream(*args, **kwds)

Read a stream into a TextGrid object.

Parameters:
  • stream (str or io.StringIO) – The content of a full-formatted TextGrid.

  • name (str) – The name of the TextGrid.

  • path (str or pathlib.Path) – The path of the TextGrid.

Returns:

A TextGrid instance.

Return type:

mytextgrid.io.textgrid.TextGrid

Classes

TextGrid

class mytextgrid.io.textgrid.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)
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.

Returns:

An empty tier.

Return type:

IntervalTier or PointTier

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)
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, format_='long', 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, textgrid=None)

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.

Interval

class mytextgrid.core.interval_tier.Interval(xmin, xmax, text='', tier=None)

A class representation for an interval.

duration()

Return the duration of the interval.

Returns:

The duration of the interval.

Return type:

decimal.Decimal

property text

Return _text attribute.

textgrid()

Return the TextGrid parent or None.

tier()

Return the IntervalTier parent or None.

property xmax

Return _xmax attribute.

property xmin

Return _xmin attribute.

PointTier

class mytextgrid.core.point_tier.PointTier(name='', xmin=0, xmax=1, textgrid=None)

Represent a tier that contains Point objects.

get_index_at_time(time)

Get the index of an existing Point.

Parameters:

time (int, float, str or decimal.Decimal) – The position in seconds of the mytextgrid.core.point_tier.Interval.

Returns:

Return the index of the point. If not found, return None.

Return type:

int or None

get_point_at_time(time)

Get the point at the specified time in the PointTier.

Parameters:

time (int, float, str or decimal.Decimal) – The position in seconds of the mytextgrid.core.point_tier.Interval.

Returns:

Return the index of the point. If not found, return None.

Return type:

mytextgrid.core.point_tier.Point or None

insert_point(time, text='')

Insert a Point into PointTier.

Parameters:
  • time (int, float, str or decimal.Decimal) – The time in seconds where a Point object will be inserted.

  • text (str) – The text of the selected Point.

insert_points(*times)

Insert various empty points into PointTier.

Parameters:

*times (float or decimal.Decimal) – One or more time items where Point items will be inserted.

remove_point(index)

Remove a Point.

Parameters:

index (int) – The index of the Point in PointTier. It must be 0 <= index < len(PointTier).

Point

class mytextgrid.core.point_tier.Point(time, text='', tier=None)

Represent a Point object which is the minimal unit of a PointTier object

property text

Return _text attribute.

textgrid()

Return the TextGrid parent or None.

tier()

Return the IntervalTier parent or None.

property time

Return _time attribute.

property xmax

Return _time attribute.

property xmin

Return _time attribute.