API Reference

Functions

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

Create and return an empty TextGrid.

By using this function, you will create an empty TextGrid. In order to make this instance useful, add tiers and text content.

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

  • xmin (float, default 0) – The starting time of the TextGrid.

  • xmax (float, 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 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(name='', xmin=0, xmax=1)

A class representation for a TextGrid.

describe()

Display compactly the attributes and structure of TextGrid.

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

Examples

First, read or create a TextGrid().

>>> # Creating TextGrid
>>> tg = mytextgrid.create_textgrid('perro', 0, 1)
>>> # Inserting tiers
>>> tg.insert_point_tier("tone")
>>> tg.insert_interval_tier("segment")
>>> tg.insert_point('tone', 0.66, "H")
>>> # Insert content into the created tiers
>>> tg.insert_point('tone', 0.9, "L")
>>> tg.insert_boundaries('segment', 0.23, 0.30, 0.42, 0.62, 0.70, 0.82, 0.98)
>>> tg.set_interval_text('segment', 1, 'e', 'l', 'p', 'e', 'rr', 'o')

Once this is done, use describe().

>>> # Describing 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 time duration 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(tier)

Search into the TextGrid for the specified tier and return it.

By using this method, you will get access to a tier stored within the TextGrid by its position or name. In the case where two o more tiers have the same name, this methods will return only the first occurrence.

Parameters

tier (int or str) – The position or name of a tier stored in the TextGrid.

Returns

A tier stored in the TextGrid.

Return type

IntervalTier or PointTier

insert_boundaries(tier, *times)

Search for an interval tier and insert one or more boundaries at the specified times.

Parameters
  • tier (int or str) – A position or name of a tier stored in TextGrid.

  • *times (iterable) – The time at which a boundary will be inserted in the selected tier.

See also

mytextgrid.TextGrid.set_interval_text

Set the text for one or more of intervals

Examples

>>> tg = mytextgrid.create_textgrid('banana', 0, 1)
>>> tg.insert_interval_tier('segment')

With the tier parameter, we specify the tier in which boundaries will be inserted. Then, we provide the time boundaries in the *times place.

>>> tg.insert_boundaries('segment', 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.9)

We can also pass an iterable object in the *times parameter.

>>> times = [0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.9]
>>> tg.insert_boundaries('segment', *times)
insert_interval_tier(name, position=None)

Insert an interval tier into TextGrid at specified position.

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

  • position (int, default None, meaning the last position.) – The position of the inserted tier. Must verify 0 <= position <= len(TextGrid).

Returns

An empty tier.

Return type

IntervalTier

See also

mytextgrid.TextGrid.insert_boundaries

Insert one or more boundaries.

mytextgrid.TextGrid.set_interval_text

Set the text for one or more of intervals.

insert_point(tier, time, text='')

Search for a point tier and insert a point at the specified time.

Parameters
  • tier (int or str) – A position or name of a tier stored in the TextGrid.

  • time (float or decimal.Decimal) – The time at which a Point item will be inserted in the tier.

  • text (str, default '') – The text content that will be store in the Point

insert_point_tier(name, position=None)

Insert a point tier into TextGrid at specified position.

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

  • position (int, default None, meaning the last position.) – The position of the inserted tier. Must verify 0 <= position <= len(TextGrid).

Returns

An empty tier.

Return type

PointTier

insert_tier(name, is_interval=True, position=None)

Insert an interval tier into TextGrid at specified position.

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

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

  • position (int, default None, meaning the last position.) – The position of the inserted tier. Must verify 0 <= position <= len(TextGrid).

Returns

An empty tier.

Return type

IntervalTier

remove_tier(tier)

Search for the specified tier and remove it from TextGrid.

Parameters

tier (int or str) – The position or name of a tier stored in the TextGrid.

set_interval_text(tier, interval_position, *text_items)

Search for an interval tier and set the text of one or more of its intervals.

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

Parameters
  • tier (int or str) – A position or name of a tier stored in the TextGrid.

  • interval_position (int) – The start position where text items will be inserted in the tier.

  • *text_items – The text items that will be inserted.

to_csv(path, encoding='utf-8')

Convert TextGrid to a csv file.

Parameters
  • path (str) – The path where the delimited text file will be created.

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

to_json(path, encoding='utf-8')

Write TextGrid to a json file.

Parameters
  • path (str) – The path where the delimited text file will be created.

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

to_textgrid(path, encoding='utf-8')

Write TextGrid to a text file.

Parameters
  • path (str) – The path where the TextGrid file will be created.

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

IntervalTier

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

A class representation for a interval tier.

get_interval_at_time(time)

Search the IntervalTier for an interval position at the specified time.

This method matches the interval where the specified time is greater than or equal to its left boundary or less than its right boundary.

Parameters

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

Returns

Return the interval position of the specified time.

Return type

int

insert_boundaries(*times)

Insert one or more time boundaries into IntervalTier.

Parameters

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

insert_boundary(time)

Insert a time boundary into IntervalTier.

Parameters

time (float or 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)

move_boundary(src_time, dst_time)

Move the selected boundary to another time.

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.

remove_boundary(time)

Remove a time boundary from IntervalTier.

Parameters

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

set_text(position, *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 position.

Parameters
  • position (int) – The start position where text items will be inserted in the position.

  • *text_items – The text items that will be inserted.