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 mytextgrid -U

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.8.0.tar.gz (25.8 kB view details)

Uploaded Source

Built Distribution

mytextgrid-0.8.0-py3-none-any.whl (29.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mytextgrid-0.8.0.tar.gz
  • Upload date:
  • Size: 25.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for mytextgrid-0.8.0.tar.gz
Algorithm Hash digest
SHA256 b349aa1a4910120bf4c4f015475555b37f3da4b21a8934afdbecf8cde80d58f2
MD5 5307d6356c1339ad9520c596546acc1b
BLAKE2b-256 bfae636d047eadca5c75a27955d9e7bd41ebe79e252fdd9987fba386290bab0a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mytextgrid-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 29.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for mytextgrid-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2747e04067d750fd8d354d2a78177712e69c1d683559df7a1241af9e49c10a49
MD5 769ece94be4ed883267ad19ab1ffc97b
BLAKE2b-256 9e5a677b9e778c89ba81e691cf80fde1b3ccd3bf441995ff79fb2785286fb41c

See more details on using hashes here.

Supported by

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