我试图弄清楚如何从窗口中打开但未作为原始文本文件保存到硬盘的文本网格中读取字符串。我的目标是操纵字符串并在以后保存它们。
我想做这样的事情,但不能真正理解语法是如何工作的。
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
...
答案 0 :(得分: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$
是预定义变量,这是一个标签页。)
答案 1 :(得分:0)
TextGrid
标签无法直接翻译为Strings
个对象,因为与TextGrid
不同,Strings
个对象没有层。因此,您可以使用代码来获取TextGrid
中特定层的所有标签,并将它们推送到Strings
对象。
Strings
这里的问题是Praat不希望你自己填充Strings
个对象,所以没有Create empty Strings...
。但是,您可以破坏其中一个现有命令来执行此操作:
Create Strings as tokens: ""
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
你可以尝试一下:
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
如果您有兴趣将所有标签放在更易于管理的软件包中,您可能也会对tgutils
plugin中的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