Skip to content

Commit

Permalink
Handle spaces or tabs as line-prefixes in .TextGrid files
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickreidy committed Jul 11, 2018
1 parent f910766 commit 3112881
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 15 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Expand Up @@ -8,6 +8,9 @@
encoding of file is guessed using `readr::guess_encoding`; however, this
can be overridden by identifying the encoding explicitly (e.g.,
`encoding = "UTF-8"` or `encoding "UTF-16BE"`).
* Some versions of Praat write TextGrid files with spaces; other versions write
with tabs. Previous versions of `textgRid` assumed spaces, but now both
formats can be handled.


# v1.0.1.9001
Expand Down
6 changes: 3 additions & 3 deletions R/IntervalTier-utilities.R
@@ -1,20 +1,20 @@

# Extract the start times of the intervals within a block of @praatText.
.IntervalStartTimes <- function(praatText, pattern = '^ {12}xmin') {
.IntervalStartTimes <- function(praatText, pattern = '^( {12}|\t{3})xmin') {
.start_times <- .TierTimes(praatText, pattern)
return(.start_times)
}


# Extract the end times of the intervals within a block of @praatText.
.IntervalEndTimes <- function(praatText, pattern = '^ {12}xmax') {
.IntervalEndTimes <- function(praatText, pattern = '^( {12}|\t{3})xmax') {
.end_times <- .TierTimes(praatText, pattern)
return(.end_times)
}


# Extract the text-mark values (interval labels) from a block of @praatText.
.IntervalLabels <- function(praatText, pattern = '^ {12}(text|mark)') {
.IntervalLabels <- function(praatText, pattern = '^( {12}|\t{3})(text|mark)') {
.labels <- .TierLabels(praatText, pattern)
return(.labels)
}
Expand Down
4 changes: 2 additions & 2 deletions R/PointTier-utilities.R
@@ -1,13 +1,13 @@

# Extract the times of the points within a block of @praatText.
.PointTimes <- function(praatText, pattern = '^ {12}(time|number)') {
.PointTimes <- function(praatText, pattern = '^( {12}|\t{3})(time|number)') {
.times <- .TierTimes(praatText, pattern)
return(.times)
}


# Extract the labels of the points within a block of @praatText.
.PointLabels <- function(praatText, pattern = '^ {12}(text|mark)') {
.PointLabels <- function(praatText, pattern = '^( {12}|\t{3})(text|mark)') {
.labels <- .TierLabels(praatText, pattern)
return(.labels)
}
Expand Down
2 changes: 1 addition & 1 deletion R/TextGrid-constructor.R
Expand Up @@ -49,7 +49,7 @@ setMethod(
.textgrid <- textGrid
}
new(Class = 'TextGrid',
.PraatText2TierObjects(.textgrid),
.PraatText2TierObjects(.textgrid, pattern = "^( {4}|\t)item"),
startTime = .TextGridTime(.textgrid, pattern = '^xmin'),
endTime = .TextGridTime(.textgrid, pattern = '^xmax')
)
Expand Down
4 changes: 2 additions & 2 deletions R/TextGrid-utilities.R
Expand Up @@ -24,7 +24,7 @@ NULL

# Partition a block of @praatText into a list of sub-blocks, each sub-block
# corresponding to the lines that define a tier within @praatText.
.SplitPraatTextIntoTiers <- function(praatText, pattern = '^ {4}item') {
.SplitPraatTextIntoTiers <- function(praatText, pattern = '^( {4}|\t)item') {
.tier_blocks <- Map(
`[`,
rep(list(praatText), .CountTiers(praatText, pattern)),
Expand All @@ -49,7 +49,7 @@ NULL


# The real engine for creating TextGrid objects.
.PraatText2TierObjects <- function(praatText, pattern = '^ {4}item') {
.PraatText2TierObjects <- function(praatText, pattern = '^( {4}|\t)item') {
.tier_text <- .SplitPraatTextIntoTiers(praatText, pattern)
.tier_classes <- .TierClass(praatText)
.tier_objects <- Map(.TierText2TierObject, .tier_text, .tier_classes)
Expand Down
14 changes: 7 additions & 7 deletions R/Tier-utilities.R
Expand Up @@ -8,7 +8,7 @@


# Count the number of tiers that occur within a block of @praatText.
.CountTiers <- function(praatText, pattern = '^ {4}item') {
.CountTiers <- function(praatText, pattern = '^( {4}|\t)item') {
.count <- length(.TierStartLine(praatText, pattern))
return(.count)
}
Expand All @@ -22,7 +22,7 @@


# Extract the classes of the tiers that occur within a block of @praatText.
.TierClass <- function(praatText, pattern = '^ {8}class') {
.TierClass <- function(praatText, pattern = '^( {8}|\t{2})class') {
.tier_classes <- .Extract(
.Extract(.PraatLines(praatText, pattern),
pattern = '".*"'),
Expand All @@ -34,7 +34,7 @@

# Find the line numbers that mark the ends of tiers within a block
# of @praatText.
.TierEndLine <- function(praatText, pattern = '^ {4}item') {
.TierEndLine <- function(praatText, pattern = '^( {4}|\t)item') {
.end_lines <- `[`(
c(.TierStartLine(praatText, pattern) - 1, length(praatText)),
2:(.CountTiers(praatText, pattern) + 1)
Expand All @@ -45,7 +45,7 @@

# Generate a list of index-vectors. Each index-vector denotes the lines
# within a block of @praatText that are spanned by a tier.
.TierIndices <- function(praatText, pattern = '^ {4}item') {
.TierIndices <- function(praatText, pattern = '^( {4}|\t)item') {
.tier_indices <- Map(
`:`,
.TierStartLine(praatText, pattern),
Expand All @@ -56,7 +56,7 @@


# Extract the names of the tiers that occur within a block of @praatText.
.TierName <- function(praatText, pattern = '^ {8}name') {
.TierName <- function(praatText, pattern = '^( {8}|\t{2})name') {
.tier_names <- .Extract(
.Extract(.PraatLines(praatText, pattern),
pattern = '".*"'),
Expand All @@ -67,7 +67,7 @@


# Extract the numbers of the tiers that occur within a block of @praatText.
.TierNumber <- function(praatText, pattern = '^ {4}item') {
.TierNumber <- function(praatText, pattern = '^( {4}|\t)item') {
.tier_numbers <- .Extract(
.Extract(.PraatLines(praatText, pattern),
pattern = '\\[.*\\]'),
Expand All @@ -80,7 +80,7 @@

# Find the line numbers that mark the beginnings of tiers within
# a block of @praatText.
.TierStartLine <- function(praatText, pattern = '^ {4}item') {
.TierStartLine <- function(praatText, pattern = '^( {4}|\t)item') {
.start_lines <- grep(pattern = pattern, x = praatText)
return(.start_lines)
}
Expand Down

0 comments on commit 3112881

Please sign in to comment.