textgrids
is a module for handling Praat TextGrid files in any format (short text, long text, or binary). The module implements five classes, from largest to smallest:
TextGrid
-- adict
with tier names as keys andTier
s as valuesTier
-- alist
of eitherInterval
orPoint
objectsInterval
-- anobject
representing Praat intervalsPoint
-- anamedtuple
representing Praat pointsTranscript
-- astr
with special methods for transcription handling
All Praat text objects are represented as Transcript
objects.
The module also exports the following variables:
diacritics
-- adict
of all diacritics with their Unicode counterpartsinline_diacritics
-- adict
of inline (symbol-like) diacriticsindex_diacritics
-- adict
of over/understrike diacriticssymbols
-- adict
of special Praat symbols with their Unicode counterpartsversion
-- module version as stringvowels
-- alist
of all vowels in either Praat or Unicode notation
This file documents praat-textgrids
version 1.2.0.
Copyright © 2019 Legisign.org, Tommi Nieminen [email protected]
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.
Besides textgrids.version
, which contains the module version number as string, the module exports the following properties:
symbols
is a dict
that contains all the Praat special notation symbols (as keys) and their Unicode counterparts (as values).
vowels
is a list
of all vowel symbols in either Praat notation (e.g., "\as"
) or in Unicode. It is used by Interval
methods containsvowel()
and startswithvowel()
, so changing it, for example, adding new symbols to it or removing symbols used for other purposes in a specific case, will change how those methods function.
diacritics
is a dict
of all diacritics in Praat notation (as keys) and their Unicode counterparts (as values).
inline_diacritics
and index_diacritics
are subsets of diacritics
. The former are semantically diacritics but appear as inline symbols, the latter are the “true” diacritics (i.e., under- or overstrikes) that need special handling when transcoding.
TextGrid
is a dict
whose keys are tier names (strings) and values are Tier
objects. The constructor takes an optional filename argument for easy loading and parsing textgrid files.
All the properties of dict
s plus:
filename
holds the textgrid filename, if any.read()
andwrite()
methods both set or update it.
All the methods of dict
s plus:
parse()
-- parse stringdata
into a TextGridread()
-- read a TextGrid filename
write()
-- write a TextGrid filename
tier_from_csv()
-- read a textgrid tier from a CSV filetier_to_csv()
-- write a textgrid tier into a CSV file
parse()
takes an obligatory string argument (Praat-format textgrid data) and an optional argument binary=BOOLEAN
. If passed binary data, the argument has to be given.
read()
and write()
each take an obligatory filename argument. read()
can take an optional argument binary=BOOLEAN
. Opening a binary file the argument has to be given.
tier_from_csv()
and tier_to_csv()
both take two obligatory arguments, the tier name and the filename, in that order.
Tier
is a list of either Interval
or Point
objects.
NOTE: Tier
only allows adding Interval
or Point
objects. Adding anything else or mixing Interval
s and Point
s will trigger an exception.
All the properties of list
s plus:
is_point_tier
-- Boolean value:True
for point tier,False
for interval tier.
All the methods of list
s plus:
concat()
-- concatenate intervalsto_csv()
-- convert tier data into a CSV-like list
concat()
returns a TypeError
if used with a point tier. It takes two optional arguments, first=
and last=
, both of which are integer indexes with the usual Python semantics: 0 stands for the first element, -1 for the last element, these being also the defaults.
to_csv()
returns a CSV-like list. It’s mainly intended to be used from the TextGrid
level method tier_to_csv()
but can be called directly if writing to a file is not desired.
Interval
is an object
class.
dur
-- interval duration (float
)mid
-- interval midpoint (float
)text
-- text label (Transcript
)xmax
-- interval end time (float
)xmin
-- interval start time (float
)
containsvowel()
-- Boolean: does the interval contain a vowel?startswithvowel()
-- Boolean: does the interval start with a vowel?timegrid()
-- create a grid of even time slices
containsvowel()
and startswithvowel()
check for possible vowels in both Praat notation and Unicode but can of course make an error if symbols are used in an unexpected way. They don’t take arguments.
NOTE: At the moment there is no endswithvowel()
as might perhaps be expected. This is a result of an early implementation bug and might get corrected in the future.
timegrid()
returns a list of timepoints (in float
) evenly distributed from xmin
to xmax
. It takes an optional integer argument specifying the number of timepoints desired; the default is 3. It raises a ValueError
if the argument is not an integer or is less than 1.
Point
is a namedtuple
with two properties: text
and xpos
.
text
-- text label (Transcript
)xpos
-- temporal position (float
)
Transcript
is a str
-derived class with one special method: transcode()
.
All the properties of str
s.
All the methods of str
s plus:
transcode()
-- convert Praat notation to Unicode or vice versa.
Without arguments, transcode()
assumes its input to be in Praat notation and converts it to Unicode; no check is made as to whether the input really is in Praat notation but nothing should happen if it isn’t. User should take care and handle any exceptions.
Optional to_unicode=False
argument inverts the direction of the transcoding from Unicode to Praat. Again, it is not checked whether input is in Unicode.
With optional retain_diacritics=True
argument the transcoding does not remove over- and understrike diacritics from the result.
import sys
import textgrids
for arg in sys.argv[1:]:
# Try to open the file as textgrid
try:
grid = textgrids.TextGrid(arg)
# Discard and try the next one
except:
continue
# Assume "syllables" is the name of the tier
# containing syllable information
for syll in grid['syllables']:
# Convert Praat to Unicode in the label
label = syll.text.transcode()
# Print label and syllable duration, CSV-like
print('"{}";{}'.format(label, syll.dur))
-
TextGrid.read()
andTextGrid.parse()
should analyze the file or data and automatically select either text or binary handling as needed. (After all, when parsing text files, short or long format is automatically recognized.) -
TextGrid.__str()__
will continue to produce long text format in the future too, butTextGrid.write()
should be able to produce any of the three formats.