Skip to main content

Create, read, modify and query TextGrid files.

Project description

Python package: mytextgrid

This is a python package to work with Praat annotation files. You can create, read, write and query TextGrids.

The following tutorial will walk you through the basics. For more information, visit the documentation website

Getting started

1. Installation

You can get the lastest release of this package using the pip installer:

pip install -U mytextgrid

After that, you can import the package as in the following line.

import mytextgrid

2. The basics

Reading a TextGrid from a file

To read an existing TextGrid file use the read_from_file() function. TextGrid files come in three formats: long, short and binary. At this moment, only the long format is supported.

import mytextgrid

# Read TextGrid
>>> path = r'C:\Users\rolan\Documents\projects\Mary_John_bell.TextGrid'
>>> tg = mytextgrid.read_from_file(path)

Describe a TextGrid

>>> tg.describe()

TextGrid:
    Startig time (sec):    0
    Ending time (sec):     1
    Number of tiers:       3
Tiers summary:
    0   IntervalTier    Mary    (size = 1)
    1   IntervalTier    John    (size = 1)
    2   PointTier       bell    (size = 0)

Manipulating a TextGrid

# Insert tier
>>> tone_tier = tg.insert_tier("tone", False)
>>> segment_tier = tg.insert_tier("segment")
>>> word_tier = tg.insert_tier("word")
>>> phrase_tier = tg.insert_tier("phrase")

# Point tier: Inserting points
>>> tone_tier.insert_point(0.66, "H")
>>> tone_tier.insert_point(0.9, "L")

# Interval tier: Inserting boundaries
>>> segment_tier.insert_boundaries(0.23, 0.30, 0.42, 0.62, 0.70, 0.82, 0.98)
>>> word_tier.insert_boundaries(0.23, 0.42, 0.98)
>>> phrase_tier.insert_boundaries(0.23, 0.98)

# Interval tier: Populate intervals with text
>>> segment_tier.set_text_at_index(1, 'e', 'l', 'p', 'e', 'rr', 'o')
>>> word_tier.set_text_at_index(1, 'el')
>>> word_tier.set_text_at_index(2, 'perro')
>>> phrase_tier.set_text_at_index(1, 'el perro')

# Remove a tier
>>> tg.remove_tier(0)
>>> tg.describe()

traversing through a TextGrid

A TextGrid object is a container that stores one or more Tier objects. Each tier, at the same time, is a container itself and stores two types of objects: Intervals or Points. Depending on that, a tier can be a IntervalTier or PointTier. To iterate through these containers use the for loop as in the following example.

# Iterate through a TextGrid
for tier in tg:
    print(tier.name)
   # Iterate through tiers
    if tier.is_interval():
        for interval in tier:
            # For interval tiers
            # Print Interval attributes
            print(interval.xmin)
            print(interval.xmax)
            print(interval.text)
    else:
        # For point tiers
        for point in tier:
            # Print Point attributes
            print(point.time)
            print(point.text)

Writing TextGrid to a file

You can write a TextGrid to different types of files.

tg.write('example1-long.TextGrid')
tg.write('example1-short.TextGrid', True) # Write the TextGrid a short format TextGrid

# Write to a JSON file
tg.write_as_json('example1.json')

Creating a TextGrid from scratch

Creating a TextGrid from the scratch is easy, just take a look to the following lines of code.

# Create an empty TextGrid
>>> new_tg = mytextgrid.create_textgrid(xmin = 0, xmax = 1)

Becareful, the resulting object does not contain any tier.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mytextgrid-0.9.0.tar.gz (26.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mytextgrid-0.9.0-py3-none-any.whl (29.9 kB view details)

Uploaded Python 3

File details

Details for the file mytextgrid-0.9.0.tar.gz.

File metadata

  • Download URL: mytextgrid-0.9.0.tar.gz
  • Upload date:
  • Size: 26.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for mytextgrid-0.9.0.tar.gz
Algorithm Hash digest
SHA256 de9232eceeb81a1eca6ccd11d3a337f821d032cb32b87d9eb478c9c44c20b2f6
MD5 0e592d8a45f56f8cbeb5f52a69815fc2
BLAKE2b-256 3844872ad44faa195cda48383a00b4095a6dd62a751b52ab61adc606d71f4c74

See more details on using hashes here.

File details

Details for the file mytextgrid-0.9.0-py3-none-any.whl.

File metadata

  • Download URL: mytextgrid-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 29.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for mytextgrid-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bd2889c35200cc583406e4edbc9aca0bed43cd078c513af2eee44b75681e5be1
MD5 6054f4101db834a4b06622e6525461df
BLAKE2b-256 65e1e71f06b7be576f90d5ad33031ca7090659973d1b4572de70c0852c925e6d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page