将TextGrid标签读入Praat中的Strings对象中



我一直在想如何从窗口中打开但未作为原始文本文件保存到硬盘的文本网格中读取字符串。我的目标是操作字符串,然后保存它们。

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

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插件中的Index specified labels...命令感兴趣,我也编写了该命令。(我知道:我很擅长命名事物)。

该方法与此类似,但它不使用Strings对象,而是将所有标签以及点的时间戳或间隔的开始和结束转储到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

最新更新