TextGrid
labels are not directly translatable to a Strings
object because, unlike a TextGrid
, Strings
objects do not have tiers. So you could have code that takes all of the labels of a specific tier in a TextGrid
and pushes them into a Strings
object.
0. Creating an empty Strings
The problem here is that Praat does not want you to populate Strings
object yourself, so there is no Create empty Strings...
. However, you can subvert one of the existing commands to do this:
Create Strings as tokens: ""
1. Pushing the labels to a Strings
object
Now that we have an empty Strings
to populate, we can get to work:
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. Profit!
You can try it out:
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. Bonus
If you are interested in getting all the labels in a more manageable package, you might also be interested in the Index specified labels...
command from the tgutils
plugin, which I also wrote. (I know: I'm amazing at naming things).
That one does something similar to this, but instead of using a Strings
object, it dumps all the labels to a Table
, as well as the timestamp of points, or the start and end of intervals. And you can also specify subsets of labels to consider using a literal match or a regular expression.
With it, you can re-write @labelsToStrings
to look like this:
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