将 TextGrid 标签读入 Praat 中的字符串对象

Read TextGrid labels into a Strings object in Praat

我一直在试图弄清楚如何从 window 中打开但未作为原始文本文件保存到硬盘的文本网格中读取字符串。我的目标是操作字符串并在以后保存它们。

我想做这样的事情,但不太明白语法是如何工作的。

tG$ = selectObject: selected$("TextGrid")
stringID = Read Strings from tG

numberOfStrings = Get number of strings
for stringNumber from 0 to numberOfStrings
    selectObject: stringID
    line$ = Get string: stringNumber
...

您需要遍历 TextGrid 中的间隔并使用 appendFileLine 将标签输出到文本文件。例如:

# Your need to select the TextGrid manually, and it has only one tier (tier number 1)

outputFile$ = "~/Desktop/output.txt"

writeFile: outputFile$, ""                        ; start from an empty .txt

numberOfIntervals = Get number of intervals: 1    ; (this is tier 1)

for interval to numberOfIntervals
    label$ = Get label of interval: 1, interval
    if label$ != ""                               ; (we just want non-empty intervals)
        xmin = Get start time of interval: 1, interval
        xmax = Get end time of interval: 1, interval
        appendFileLine: outputFile$, "'label$''tab$''xmin''tab$''xmax'"
    endif
endfor

此脚本将输出一个 .txt 文件,其中包含制表符分隔值:label、xmin、xmax。您可以根据需要更改 appendFileLine 参数(tab$ 是一个预定义变量,它是...一个选项卡)。

TextGrid 标签不能直接转换为 Strings 对象,因为与 TextGrid 不同,Strings 对象没有层次。因此,您可以使用代码获取 TextGrid 中特定层的所有标签,并将它们推送到 Strings 对象中。

0。创建一个空 Strings

这里的问题是 Praat 不希望您自己填充 Strings 对象,所以没有 Create empty Strings...。但是,您可以颠覆现有命令之一来执行此操作:

Create Strings as tokens: ""

1。将标签推送到 Strings 对象

现在我们有一个空的 Strings 可以填充,我们可以开始工作了:

procedure labelsToStrings: .tier
  .textgrid = selected("TextGrid")

  # Make sure this works with interval and point tiers
  .item$ = if do("Is interval tier...", .tier) then
    ... "interval" else "point" fi

  # Make the empty Strings
  .strings = Create Strings as tokens: ""
  Rename: selected$("TextGrid")

  # Fetch each label, and insert it to the Strings object
  selectObject: .textgrid
  .n = do("Get number of " + .item$ + "s...", .tier)
  for .i to .n
    selectObject: .textgrid
    .label$ = do$("Get label of " + .item$ + "...", .tier, .i)

    # I assume you don't care about empty labels?
    if .label$ != ""
      selectObject: .strings
      Insert string: 0, .label$
    endif
  endfor

  # Make sure the new object is selected
  selectObject: .strings
endproc

2。盈利!

你可以试试看:

synth = Create SpeechSynthesizer: "English", "default"
To Sound: "This is some text.", "yes"
sound    = selected("Sound")
textgrid = selected("TextGrid")

selectObject: textgrid
@labelsToStrings: 4

removeObject: sound, synth
View & Edit

3。奖金

如果您有兴趣将所有标签放在一个更易于管理的包中,您可能还对 tgutils plugin 中的 Index specified labels... 命令感兴趣,我也写过。 (我知道:我很擅长命名事物)。

那个人做了类似的事情,但不是使用 Strings 对象,而是将所有标签转储到 Table,以及点的时间戳,或开始和间隔结束。您还可以指定要考虑使用文字匹配或正则表达式的标签子集。

有了它,你可以re-write @labelsToStrings看起来像这样:

procedure labelsToStrings: .tier
  .name$ = selected$("TextGrid")

  runScript: preferencesDirectory$ + "/plugin_tgutils/scripts/" +
    ... "index_specified_labels.praat", .tier, ".+", "yes"
  .table = selected("Table")

  Create Strings as tokens: ""
  Rename: .name$
  for .i to Object_'.table'.nrow
    .label$ = Object_'.table'$[.i, "label"]
    Insert string: 0, .label$
  endfor
  removeObject: .table
endproc