Saturday, April 12, 2014

Scripting with im3: Parsing a list...

(scriptStr#"DT NN NN VBZ DT JJ NN")
(splitOver(scriptStr, " ",scriptList))
(nWords#(len(scriptList)))
(c#0)

(::LOOP::)
    (inScr#(toString(scriptList[c])))
    (inTxt#(rndWord(inScr,"","anywhere",0)))
    (find(inWord,inTxt))
    (inPhone#(phoneTag(inWord)))
    (inPhone#(left(inPhone,4)))
    (rndWord(inScr,inPhone,"anywhere",0))
    ((Inc(c,1)))
    (if(c=nWords)) { (goto(::UNLOOP::)) }
    (goto(::LOOP::))
(::UNLOOP::)





Let's take a run through of this im3 script, since it is doing something fairly complicated. First, we define a string which has several part of speech tags in it. We take this string, and using the splitOver routine, separate the words into a list, every time we hit a space. We then use the (len(list)) command to determine how many individual words are in the list. We create a variable called c which will act as a counter.

(scriptStr#"DT NN NN VBZ DT JJ NN")
(splitOver(scriptStr, " ",scriptList))
(nWords#(len(scriptList)))
(c#0)


Then we define a label, (::LOOP::).

(::LOOP::)

We create a string to read in the first element in our list scriptList, and hold it's contents in the variable inScr.

(inScr#(toString(scriptList[c])))

We create another variable called inTxt which holds the contents of a randomly generated word with the first part of speech in the list scriptList. inTxt now holds a random word of the first part of speech read in from scriptStr; in this case DT, a determiner.

(inTxt#(rndWord(inScr,"","anywhere",0)))

We use the find to command to look up inTxt and save the word's record to inWord.

(find(inWord,inTxt))

We create another string to hold the phonetic data.

    (inPhone#(phoneTag(inWord)))
    (inPhone#(left(inPhone,4)))


And we call LEFT to cut 4 characters from the left hand side of the variable inPhone. So left("1234567890", 2) would = "12".

Then we generate another random word with the new phonetic data:

    (rndWord(inScr,inPhone,"anywhere",0))

And we increment our counter:

    ((Inc(c,1)))

Note the double brackets. This prevents SMUP from returning data to our output window. It Eats data that is double brackets. Try running this code without the double brackets!

    (if(c=nWords)) { (goto(::UNLOOP::)) }
    (goto(::LOOP::))
(::UNLOOP::)


Here we have our exit condition. If our counter is equal to the number of words in our list, then we go to (::UNLOOP::) or else we just go back to (::LOOP::).

And there you have it.

Try copying this into SMUP and giving it a run.


No comments:

Post a Comment