top of page

47. Convert Text to Pig-Latin

Description

Translates a string of text or a text variable into Pig-Latin.

Code

// Set up the loop
SET Temp-n TO LENGTH( TextVar )
SET LoopLimit-n[ Temp-n ] TO 1

// Start the variables at ""
SET Temp-b TO TRUE
SET PigSuffix-t TO ""
SET Temp-t TO ""

""
REPEAT Loop
// Get the next letter in sequence
SET Temp-t TO MID( "«TextVar:like this»", COUNTER, 1 )

// Skip everything but letters and spaces
IF "abcdefghijklmnopqrstuvwxyz " CONTAINS "«Temp-t:like this»"

// Are we looking at the first of the word?
IF Temp-b
// See if the letter is a consonant or space
IF Temp-t != "a"
AND Temp-t != "e"
AND Temp-t != "i"
AND Temp-t != "o"
AND Temp-t != "u"
AND Temp-t != "y"
AND Temp-t != " "
SET PigSuffix-t TO PigSuffix-t + Temp-t
// A "u" after a "q" should stay with the "q"
ELSE IF Temp-t = "u"
AND PigSuffix-t = "q"
SET PigSuffix-t TO PigSuffix-t + Temp-t
// A space: must be an abreviation
ELSE IF Temp-t = " "
RESULT + PigSuffix-t + "ay "
SET PigSuffix-t TO ""
SET Temp-b TO TRUE
// We've hit a vowel. The word begins.
ELSE
SET Temp-b TO FALSE
RESULT + Temp-t
END IF

// Is this the end of the word?
ELSE IF Temp-t = " "
RESULT + PigSuffix-t + "ay "
SET PigSuffix-t TO ""
SET Temp-b TO TRUE
ELSE IF COUNTER = LENGTH( TextVar )
RESULT + Temp-t + PigSuffix-t + "ay"
SET PigSuffix-t TO ""
SET Temp-b TO TRUE
// Just a letter in the word
ELSE
RESULT + Temp-t
END IF

END IF
END REPEAT

// Reset the temporary variables
SET LoopLimit-n[ Temp-n ] TO UNANSWERED
SET Temp-n TO UNANSWERED
SET Temp-t TO UNANSWERED
SET Temp-b TO UNANSWERED
SET PigSuffix-t TO UNANSWERED

Explanation

Q: Why?

A: Why not? (slow afternoon)

This computation takes a text variable and returns its equivalent in Pig-Latin. Really. The model template has a version which is set up and ready to go without modification.

Limitations: Any character that is not a letter or a space is skipped. Also, the computation does not differentiate between the consonantal and vowel values of "y." In other words, "y" is always a vowel. For example, "hybrid" will correctly translate to "ybridhay," but "yes" incorrectly translates to "yesay" instead of "esyay." But hey, if you have a mission-critical template that absolutely needs this to be right, take an hour and work it out.

The Nitty-Gritty: The computation requires a repeated dialog called Loop. This dialog has a single temporary number variable in it called LoopLimit-n. The mechanics of the Loop are described in Computation 15: Loops Via REPEAT. The computation also requires a few temporary variables -- a number, a text, and a true/false variable named Temp-n, Temp-t, and Temp-b respectively, and an additional text variable called PigSuffix-t. All of these temporary variables should have their Advanced options set to "Ask only in dialog," "Don't warn if unanswered," and "Don't save in answer file."

The computation uses the repeated dialog Loop to cycle through each letter of the text variable TextVar. Each pass through the loop analyzes the next letter in the string. The true/false variable Temp-b tells the computation whether it is examining the first part of a word. If so, the computation stores all consonants up to the first vowel in the variable PigSuffix-t. Once the first vowel is encountered, the computation uses the remaining letters in the word to form the Pig-Latin word. When either a space or the end of the string is encountered, the computation appends the contents of PigSuffix-t and adds an "ay."

Any character that is not a letter or a space is skipped.

bottom of page