Skip to contents

If successive intervals have the same label, they are merged together.

Usage

merge_duplicate_intervals

Format

A Praat script

textgrid_in

path of the textgrid file to read in

target_tier

tier to update

textgrid_out

path of the textgrid file to create

Example usage

library(tjm.praat)

# This example textgrid has back-to-back sounding/silence labels
tg <- system.file(
  "demo-textgrids/merge-duplicate-intervals.TextGrid",
  package = "tjm.praat"
)

tg_data <- readtextgrid::read_textgrid(tg)
tg_data[tg_data$tier_num == 3, c("tier_name", "xmin", "xmax", "text")]
#> # A tibble: 9 x 4
#>   tier_name  xmin  xmax text    
#>   <chr>     <dbl> <dbl> <chr>   
#> 1 silence    0     0.25 silent  
#> 2 silence    0.25  0.49 sounding
#> 3 silence    0.49  0.52 sounding
#> 4 silence    0.52  0.55 silent  
#> 5 silence    0.55  0.59 sounding
#> 6 silence    0.59  1.04 sounding
#> 7 silence    1.04  1.16 sounding
#> 8 silence    1.16  1.39 silent  
#> 9 silence    1.39  1.41 silent

# Set path
tg_out <- tempfile("test", fileext = ".TextGrid")

f_merge_duplicate_intervals <- wrap_praat_script(
  script_code_to_run = merge_duplicate_intervals,
  returning = "last-argument"
)

tg_data2 <- f_merge_duplicate_intervals(tg, "silence", tg_out) |>
  readtextgrid::read_textgrid()

tg_data2[tg_data2$tier_num == 3, c("tier_name", "xmin", "xmax", "text")]
#> # A tibble: 5 x 4
#>   tier_name  xmin  xmax text    
#>   <chr>     <dbl> <dbl> <chr>   
#> 1 silence    0     0.25 silent  
#> 2 silence    0.25  0.52 sounding
#> 3 silence    0.52  0.55 silent  
#> 4 silence    0.55  1.16 sounding
#> 5 silence    1.16  1.41 silent

Praat source code

print(f_merge_duplicate_intervals, condense = FALSE)
function (textgrid_in = NULL, target_tier = "silence", textgrid_out = NULL)
# <wrapped_praat_script>
# returning: "last-argument"
form Merge duplicated interval labels
  sentence Textgrid_in
  sentence Target_tier silence
  sentence Textgrid_out
endform
Read from file: textgrid_in$
@findNumberForTier: target_tier$
@labelMerger: "initialize", findNumberForTier.result
while labelMerger.has_next
	@labelMerger: "merge-or-step", findNumberForTier.result
endwhile
Save as text file: textgrid_out$
# Object for merging duplicated textgrid interval labels
procedure labelMerger: .method$, .tier_number
  if .method$ = "initialize"
      .current_position = 1
  endif
  if .method$ = "merge-or-step"
    .can_merge = .current_label$ == .next_label$
    if .can_merge == 1
      Remove right boundary: .tier_number, .current_position
      Set interval text: .tier_number, .current_position, .current_label$
    else
      .current_position = .current_position + 1
    endif
  endif
  if .method$ = "has_next"
    # .has_next is updated whenever procedure is invoked
  endif
  .intervals = Get number of intervals: .tier_number
  .current_label$ = Get label of interval: .tier_number, .current_position
  if .current_position < .intervals
    .has_next = 1
    .next_label$ = Get label of interval: .tier_number, .current_position + 1
  else
    .has_next = 0
  endif
endproc
# Find the number of the (last) tier with a given name
procedure findNumberForTier: .target_tier$
  .tiers = Get number of tiers
  .result = 0
  for .tier_i to .tiers
    .tier_i_name$ = Get tier name: .tier_i
      if .tier_i_name$ == .target_tier$
        .result = .tier_i
      endif
  endfor
endproc