Over the last few weeks I’ve been working on a Perl English to Pig Latin script. For testing I used the text of The Time Machine by: H.G. Wells from Project Guttenberg. You can get the full code, and the text I used for testing at my GitHub Repo. The idea of this program, is that it can take an English Language text file, and change it into Pig Latin.
The first paragraph of The Time Machine follows:
The Time Traveller (for so it will be convenient to speak of him) was expounding a recondite matter to us. His grey eyes shone and twinkled, and his usually pale face was flushed and animated. The fire burned brightly, and the soft radiance of the incandescent lights in the lilies of silver caught the bubbles that flashed and passed in our glasses. Our chairs, being his patents, embraced and caressed us rather than submitted to be sat upon, and there was that luxurious after-dinner atmosphere when thought roams gracefully free of the trammels of precision. And he put it to us in this way–marking the points with a lean forefinger–as we sat and lazily admired his earnestness over this new paradox (as we thought it) and his fecundity.
Attach stripped consonants to the end of the word.
Add ay to the end of the word.
If the word starts with a vowel then add way to the end of it.
The results of my program work very well with English text, but mangle non-English text, like URLs.
The Code
openFile takes in a filename from the commandline, and attempts to open it.
saveFile takes in a filename, checks to see if the file exists, exits if the file exists, or writes the converted text to the file.
alterVowelStarts adds “way” to the end of words which begin with a,e,i,o, or u.
findFirstVowel finds the location in a word of the first vowel.
alterConsStarts alters words which begin with a consonent, by moving the starting consonants to the end of the word and adding “ay”.
removeFirstPunct strips any leading punctuation, and returns a reference to an array containing the remaining word, and the punctuation.
removeLastPunct removes any punctuation at the end of the word.
lcCaps makes all words lowercase, and returns a reference to an array containing a flag indicating if the word was completely capitalized, had first letter capitalization, or was lowercase.
alterWord runs the process of converting each word. It calls many of the subroutines.
splitLine splits each line of the text file into words, converts them, by calling alterWord, and returns a string containing the altered line.
main runs the whole process. It calls directly, openFile,