Package 'readtextgrid'

Title: Read in a 'Praat' 'TextGrid' File
Description: 'Praat' <https://www.fon.hum.uva.nl/praat/> is a widely used tool for manipulating, annotating and analyzing speech and acoustic data. It stores annotation data in a format called a 'TextGrid'. This package provides a way to read these files into R.
Authors: Tristan Mahr [aut, cre]
Maintainer: Tristan Mahr <[email protected]>
License: GPL-3
Version: 0.1.2.9000
Built: 2024-11-22 04:06:30 UTC
Source: https://github.com/tjmahr/readtextgrid

Help Index


Locate the path of an example textgrid file

Description

Locate the path of an example textgrid file

Usage

example_textgrid(which = 1)

Arguments

which

index of the textgrid to load

Details

This function is a wrapper over system.file() to locate the paths to bundled textgrids. These files are used to test or demonstrate functionality of the package.

Two files are included:

  1. "Mary_John_bell.TextGrid" - the default TextGrid created by Praat's Create TextGrid command. This file is saved as UTF-8 encoding.

  2. "utf_16_be.TextGrid" - a TextGrid with some IPA characters entered using Praat's IPA character selector. This file is saved with UTF-16 encoding.

  3. "nested-intervals.TextGrid" - A textgrid containing an "utterance" tier, a "words" tier, and a "phones" tier. This file is typical of forced alignment textgrids where utterances contain words which contain speech segments. In this case, alignment was made by hand so that word and phone boundaries do not correspond exactly.

Value

Path of "Mary_John_bell.TextGrid" bundled with the readtextgrid package.


Pivot a textgrid into wide format, respecting nested tiers

Description

Pivot a textgrid into wide format, respecting nested tiers

Usage

pivot_textgrid_tiers(data, tiers, join_cols = "file")

Arguments

data

a textgrid dataframe created with read_textgrid()

tiers

character vector of tiers to pivot into wide format. When tiers has more than 1 element, the tiers are treated as nested. For example, if tiers is c("utterance", "word", "phone"), where "utterance" intervals contain "word" intervals which in turn contain "phone" intervals, the output will have one row per "phone" interval and include ⁠utterance_*⁠ and ⁠word_*⁠ columns for the utterance and word intervals that contain each phone interval. tiers should be ordered from broadest to narrowest (e.g, "word" preceding "phone").

join_cols

character vector of the columns that will uniquely identify a textgrid file. Defaults to "file" because these columns have identical values for tiers read from the same textgrid file.

Details

For the joining nested intervals, two intervals a and b are combined into the same row if they match on the values in the join_cols columns and if the a$xmin <= b$xmid and b$xmid <= a$xmax. That is, if the midpoint of b is contained inside the interval a.

Value

a dataframe with just the intervals from tiers named in tiers converted into a wide format. Columns are renamed so that the text column is pivot into columns named after the tier names. For example, the text column in a words tier is renamed to words. The xmax, xmin, annotation_num, tier_num, tier_type are also prefixed with the tier name. For example, the xmax column in a words tier is renamed to words_xmax. An additional helper column xmid is added and prefixed appropriately. See examples below.

Examples

data <- example_textgrid(3) |>
  read_textgrid()
data

# With a single tier, we get just that tier with the columns prefixed with
# the tier_name
pivot_textgrid_tiers(data, "utterance")
pivot_textgrid_tiers(data, "words")

# With multiple tiers, intervals in one tier that contain intervals in
# another tier are combined into the same row.
a <- pivot_textgrid_tiers(data, c("utterance", "words"))
cols <- c(
  "utterance", "utterance_xmin", "utterance_xmax",
  "words", "words_xmin", "words_xmax"
)
a[cols]

a <- pivot_textgrid_tiers(data, c("utterance", "words", "phones"))
cols <- c(cols, "phones", "phones_xmin", "phones_xmax")
a[cols]

Read a textgrid file into a tibble

Description

Read a textgrid file into a tibble

Usage

read_textgrid(path, file = NULL, encoding = NULL)

read_textgrid_lines(lines, file = NULL)

Arguments

path

a path to a textgrid

file

an optional value to use for the file column. For read_textgrid(), the default is the base filename of the input file. For read_textgrid_lines(), the default is NA.

encoding

the encoding of the textgrid. The default value NULL uses readr::guess_encoding() to guess the encoding of the textgrid. If an encoding is provided, it is forwarded to ⁠[readr::locale()]⁠ and ⁠[readr::read_lines()]⁠.

lines

alternatively, the lines of a textgrid file

Value

a tibble with one row per textgrid annotation

Examples

tg <- system.file("Mary_John_bell.TextGrid", package = "readtextgrid")
read_textgrid(tg)