1

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

我想做这样的事情,但并不真正理解语法是如何工作的。

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
...
4

2 回答 2

1

您需要遍历 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$是一个预定义的变量,它是一个选项卡)。

于 2017-01-26T09:26:54.023 回答
0

TextGrid标签不能直接转换为Strings对象,因为与 a 不同TextGridStrings对象没有层。因此,您可以编写将 a 中特定层的所有标签的代码,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.奖金

如果您有兴趣在一个更易于管理的包中获取所有标签,您可能还对插件Index specified labels...中的命令感兴趣,我也写了该命令。(我知道:我很擅长命名)。tgutils

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

有了它,你可以重新写成@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
于 2017-01-27T00:10:06.623 回答