Getting started¶
mytextgrid
is a package written in Python to manipulate TextGrid files. A TextGrid is a type of representation for time-aligned annotations used by Praat, a software for doing phonetics.
With mytextgrid
you can read a TextGrid file or just create it from scratch. Once a TextGrid is loaded in Python, you can modify it or query it. In this tutorial I will walk you through the basics of this package.
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:
1import mytextgrid
Basic operations¶
A TextGrid
is type of object that represent a time-aligned annotation. They have many uses when working with sound files in Praat. Some people use them in phonetic transcriptions, others make notes of different kind; other people use annotations to run scripts over some specific parts of a sound file.
A TextGrid is hierarchical structured. It contains one or more tiers, which at the same time, contain time-aligned marks. These marks can be of Interval
or Point
type. Marks in intervals have a starting and ending time, while points refer to a specific point in time. Tiers that store Interval
objects are IntervalTier
, whereas Point
objects are stored in PointTier
tiers.
Reading a TextGrid file¶
To read a TextGrid file you can use mytextgrid.read_from_file()
function. This returns a mytextgrid.TextGrid
object.
1import mytextgrid
2
3path = r'C:\Users\rolan\Documents\projects\sentence1.TextGrid'
4tg = mytextgrid.read_from_file(path)
Warning
TextGrid files come in three formats: long, short and binary. At this moment, only the long format is supported.
Traversing a TextGrid¶
We can use the for
statement to visit all the tiers within a TextGrid
.
5# tg is a variable that contains a TextGrid object.
6
7# TextGrid info
8print(tg.name)
9print(tg.xmin)
10print(tg.xmax)
11
12# Iterate through a TextGrid
13for tier in tg:
14 print(tier.name)
We can also visit elements within a tier in the same way.
5# Iterate through a TextGrid
6for tier in tg:
7 # Iterate through tiers
8 if tier.is_interval():
9 # For interval tiers
10 for interval in tier:
11 # Print Interval attributes
12 print(interval.xmin)
13 print(interval.xmax)
14 print(interval.text)
15 else:
16 # For point tiers
17 for point in tier:
18 # Print Point attributes
19 print(point.time)
20 print(point.text)
Creating a TextGrid from scratch¶
To create a TextGrid file you can use mytextgrid.create_textgrid()
function. This returns an empty mytextgrid.TextGrid
object.
1import mytextgrid
2
3tg = mytextgrid.create_textgrid(name = 'dog', xmin = 0, xmax = 1)
In the function, we need to specify the name (name
) of the TextGrid, also its starting (xmin
) and ending (xmax
) time. The function returns a empty TextGrid object which is assigned to the variable tg
.
Warning
An empty TextGrid does not contain any tier. In order we can work with this object, we need to insert at least one tier.
Manipulating a TextGrid¶
Now that you have created an empty TextGrid, let’s insert some tiers and add text.
4# First, let's insert three tiers
5tg.insert_point_tier("tone")
6tg.insert_interval_tier("segment")
7tg.insert_interval_tier("word")
8tg.insert_interval_tier("phrase")
9
10# Insert points and intervals
11tg.insert_boundaries('segment', 0.23, 0.30, 0.42, 0.62, 0.70, 0.82, 0.98)
12tg.insert_boundaries('word', 0.23, 0.42, 0.98)
13tg.insert_boundaries('phrase', 0.23, 0.98)
14
15tg.insert_point('tone', 0.66, "H")
16tg.insert_point('tone', 0.9, "L")
17
18# Add text to intervals
19tg.set_interval_text('segment', 1, 'e', 'l', 'p', 'e', 'rr', 'o')
20tg.set_interval_text('word', 1, 'el')
21tg.set_interval_text('word', 2, 'perro')
22tg.set_interval_text('phrase', 1, 'el perro')
Writing TextGrid to a file¶
You can save a TextGrid
object as a text file. For that, you can choose between three types of formats: TextGrid, json or a csv table.
23path = r'C:\Users\user\Documents\sentence1.TextGrid'
24tg.to_textgrid(path)
23csv_path = r'C:\Users\user\Documents\sentence1.csv'
24tg.to_csv(csv_path)
23json_path = r'C:\Users\user\Documents\sentence1.json'
24tg.to_json(csv_path)