FrontPage

Counter: 4757, today: 3, yesterday: 1

Praat Script Tips

Emulation of Perl's $`, $&, and $'

string$ = "abcdefg"
regex$ = "b"

prematch$ = ""
match$ = ""
postmatch$ = ""
pos = index_regex(string$,regex$)
if pos <> 0
    prematch$ = left$(string$,pos-1)
    match$ = right$(string$,length(string$)-pos+1)
    postmatch$ = replace_regex$(match$,regex$,"",1)
    match$ = match$ - postmatch$
endif

clearinfo
printline 'prematch$''tab$''match$''tab$''postmatch$'
a	b	cdefg
$string = "abcdefg";
$regex = "b";

$string =~ m/$regex/;
print "$`\t$&\t$'\n";

Emulation of Perl's split function

Modified May 20, 2011. It still has a bug, however*1.

string$ = "hoge/piyo/foo/bar"
delimiter$ = "/"

if index_regex("",delimiter$) = 1
    tmp$ = replace_regex$(string$,delimiter$,"",0)
    lastSub = length(tmp$)-1
    for i from 0 to lastSub
        arr$[i] = mid$(tmp$,i+1,1)
    endfor
else
    lastSub = 0
    arr$[lastSub] = string$
    repeat
        lastValue$ = arr$[lastSub]
        delimiterPos = index_regex(lastValue$,delimiter$)
        if delimiterPos <> 0
            arr$[lastSub] = left$(lastValue$,delimiterPos-1)
            lastSub = lastSub+1
            arr$[lastSub] = replace_regex$(right$(lastValue$,length(lastValue$)-delimiterPos+1),delimiter$,"",1)
        endif
    until delimiterPos = 0
endif

clearinfo
for i from 0 to lastSub
    hop$ = arr$[i]
    printline 'i''tab$''hop$'
endfor
0	hoge
1	piyo
2	foo
3	bar
$string = "hoge/piyo/foo/bar";
$delimiter = "/";

@arr = split(/$delimiter/,$string);

for ( $i = 0 ; $i <= $#arr ; $i++ ) {
    print "$i\t$arr[$i]\n";
}

Check your Praat Version

requiredVersion = 5209
# commas omitted (the above means "version 5.2.09")

if praatVersion < requiredVersion
    exit Your Praat (Version 'praatVersion') is too old. Use Version 'requiredVersion' or newer.
endif

Get the path separator for your platform

input$ = "c:/foo/bar/hoge.TextGrid"
output$ = "c:/foo/bar/result"
#input$ = "c:\foo\bar\hoge.TextGrid"
#output$ = "c:\foo\bar\result"

if windows = 1
    pathSep$ = "\"
    notPathSep$ = "/"
elsif macintosh + unix >= 1
    pathSep$ = "/"
    notPathSep$ = "\"
else
   exit Sorry. Unknown platform.
endif
input$ = replace$(input$,notPathSep$,pathSep$,0)
output$ = replace$(output$,notPathSep$,pathSep$,0)

clearinfo
printline 'input$'
printline 'output$'
c:\foo\bar\hoge.TextGrid
c:\foo\bar\result

Get the path and filename from a string

dir$ = "c:/hoge/piyo/foo.TextGrid"
#dir$ = "c:\hoge\piyo\foo.TextGrid"

if windows + macintosh + unix = 0
   exit Sorry. Unknown platform.
endif

path$ = left$(dir$,rindex_regex(dir$,"[/\\]"))
filename$ = right$(dir$,length(dir$)-rindex_regex(dir$,"[/\\]"))
## if you have the path separator (pathSep$) already, you can write:
#path$ = left$(dir$,rindex(dir$,pathSep$))
#filename$ = right$(dir$,length(dir$)-rindex(dir$,pathSep$))

clearinfo
printline 'path$'
printline 'filename$'
c:/hoge/piyo/
foo.TextGrid

Upper to Lower Case

str$ = "ABCDEFG"

newstr$ = replace_regex$(str$,"[A-Z]","\L&",0)

echo 'newstr$'
abcdefg

Get the tiernumbers from tiernames

file$ = "N:\sample\sample.TextGrid"

Read from file... 'file$'

segTierNum = 0
wordTierNum = 0
n = Get number of tiers
for i from 1 to n
    currentTier$ = Get tier name... i
    if currentTier$ = "Seg"
        segTierNum = i
    elsif currentTier$ = "Word"
        wordTierNum = i
    endif
endfor
if segTierNum = 0
    exit Seg tier not found in 'file$'
elsif wordTierNum = 0
    exit Word tier not found in 'file$'
endif

clearinfo
printline Seg tier:  'segTierNum'
printline Word tier: 'wordTierNum'

Recover the Object List to the previous state

# Before trying this script, make several objects 
# on the Object List and select some of them.

selectedObjectNum = numberOfSelected()
for i from 1 to selectedObjectNum
    selectedID[i] = selected(i)
endfor

select all
pause All objects are selected. Press "Continue" to recover the previous state.

if selectedObjectNum <> 0 
    select selectedID[1]
    for i from 2 to selectedObjectNum
        plus selectedID[i]
    endfor
endif

*1 About the bug in the emulation of split. When you specify a regular expression which can match the null string "", say "/*", as the delimiter, the greedy property does not work, and the result contains substrings which also match the delimiter (e.g., "/"). Apparently, the command in line 5 (replace_regex$) does not work as you expect. (Check the value of replace_regex$("foo/bar","/*","",0) on your Praat. Check also: perl -e '{$str="foo/bar";$str=~s|/*||g;print "$str\n";}' )

Front page   New List of pages Search Recent changes   Help   RSS of recent changes