Praat

class pympi.Praat.TextGrid(file_path=None, xmin=0, xmax=None, codec='utf-8')

Read write and edit Praat’s TextGrid files.

Note

All times are in seconds and can have decimals

Variables:
  • xmin (float) – Minimum x value.
  • xmax (float) – Maximum x value.
  • tier_num (int) – Number of tiers.
  • tiers (list) – Internal (unsorted) list of tiers.
  • codec (str) – Codec of the input file.
__init__(file_path=None, xmin=0, xmax=None, codec='utf-8')

Construct either a new TextGrid object or read one from a file/stream. When you create an empty TextGrid you must at least specify the xmax. When you want to load a TextGrid from file you need to specify at least the file_path and optionally the codec. Binary, short and normal TextGrids are supported.

Parameters:
  • file_path (str) – Path to read from, - for stdin. If None an empty TextGrid will be created.
  • xmin (int) – Xmin value, only needed when not loading from file.
  • xmax (int) – Xmax value, needed when not loading from file.
  • codec (str) – Text encoding for the input. Note that this will be ignored for binary TextGrids.
Raises Exception:
 

If filepath is not specified but no xmax

add_tier(name, tier_type='IntervalTier', number=None)

Add an IntervalTier or a TextTier on the specified location.

Parameters:
  • name (str) – Name of the tier, duplicate names is allowed.
  • tier_type (str) – Type of the tier.
  • number (int) – Place to insert the tier, when None the number is generated and the tier will be placed on the bottom.
Returns:

The created tier.

Raises ValueError:
 

If the number is out of bounds.

change_tier_name(name_num, name2)

Changes the name of the tier, when multiple tiers exist with that name only the first is renamed.

Parameters:
  • name_num (int or str) – Name or number of the tier to rename.
  • name2 (str) – New name of the tier.
Raises IndexError:
 

If the tier doesn’t exist.

from_file(ifile, codec='ascii')

Read textgrid from stream.

Parameters:
  • ifile (file) – Stream to read from.
  • codec (str) – Text encoding for the input. Note that this will be ignored for binary TextGrids.
get_tier(name_num)

Gives a tier, when multiple tiers exist with that name only the first is returned.

Parameters:name_num (int or str) – Name or number of the tier to return.
Returns:The tier.
Raises IndexError:
 If the tier doesn’t exist.
get_tier_name_num()

Give all tiers with their numbers.

Yield:Enumerate of the form [(num1, tier1),  ... (numn, tiern)]
get_tiers()

Give all tiers.

Yields:All tiers
remove_tier(name_num)

Remove a tier, when multiple tiers exist with that name only the first is removed.

Parameters:name_num (int or str) – Name or number of the tier to remove.
Raises IndexError:
 If there is no tier with that number.
sort_tiers(key=<function <lambda> at 0x19d3668>)

Sort the tiers given the key. Example key functions:

Sort according to the tiername in a list:

lambda x: ['name1', 'name2' ... 'namen'].index(x.name).

Sort according to the number of annotations:

lambda x: len(list(x.get_intervals()))

Parameters:key (func) – A key function. Default sorts alphabetically.
to_eaf(skipempty=True, pointlength=0.1)

Convert the object to an pympi.Elan.Eaf object

Parameters:
  • pointlength (int) – Length of respective interval from points in seconds
  • skipempty (bool) – Skip the empty annotations
Returns:

pympi.Elan.Eaf object

Raises:
  • ImportError – If the Eaf module can’t be loaded.
  • ValueError – If the pointlength is not strictly positive.
to_file(filepath, codec='utf-8', mode='normal')

Write the object to a file.

Parameters:
  • filepath (str) – Path of the fil.
  • codec (str) – Text encoding.
  • mode (string) – Flag to for write mode, possible modes: ‘n’/’normal’, ‘s’/’short’ and ‘b’/’binary’
class pympi.Praat.Tier(xmin, xmax, name=None, tier_type=None)

Class representing a TextGrid tier, either an Interval or TextTier

Variables:
  • name (str) – Name of the tier.
  • intervals (list) – List of intervals where each interval is (start, [end,] value).
  • tier_type (str) – Type of the tier(‘IntervalTier’ or ‘TextTier’).
  • xmin (int) – Minimum x value.
  • xmax (int) – Maximum x value.
__init__(xmin, xmax, name=None, tier_type=None)

Creates a tier, if lines is None a new tier is created.

Parameters:
  • name (str) – Name of the tier.
  • tier_type (str) – Type of the tier(‘IntervalTier’ or ‘TextTier’).
Raises TierTypeException:
 

If the tier type is unknown.

add_interval(begin, end, value, check=True)

Add an interval to the IntervalTier.

Parameters:
  • begin (float) – Start time of the interval.
  • end (float) – End time of the interval.
  • value (str) – Text of the interval.
  • check (bool) – Flag to check for overlap.
Raises Exception:
 

If overlap, begin > end or wrong tiertype.

add_point(point, value, check=True)

Add a point to the TextTier

Parameters:
  • point (int) – Time of the point.
  • value (str) – Text of the point.
  • check (bool) – Flag to check for overlap.
Raises Exception:
 

If overlap or wrong tiertype.

clear_intervals()

Removes all the intervals in the tier

get_all_intervals()

Returns the true list of intervals including the empty intervals.

get_intervals(sort=False)

Give all the intervals or points.

Parameters:sort (bool) – Flag for yielding the intervals or points sorted.
Yields:All the intervals
remove_interval(time)

Remove an interval, if no interval is found nothing happens.

Parameters:time (int) – Time of the interval.
Raises TierTypeException:
 If the tier is not a IntervalTier.
remove_point(time)

Remove a point, if no point is found nothing happens.

Parameters:time (int) – Time of the point.
Raises TierTypeException:
 If the tier is not a TextTier.