๐Ÿ“˜ Pig Latin using Perl 6

Convert the given text to Pig Latin.

Pig Latin is a pseudo-language, each word of which is derived from the corresponding English word, following a couple of simple rules:

  1. If the word starts with consonant letters (including consonant sounds represented by letter combinations such as qu), move them all to the end of the word.
  2. Append the ayending.

Here is a program that implements this algorithm.

my $text = prompt('English > ');

$text ~~ s:i:g/ << ([qu]? <-[aeiou]>+) (\w*) >> /$1$0/;
$text ~~ s:i:g/ << (\w+) >> /$0ay/;

say $text; # you are welcome โ†’ouyay areay elcomeway

For simplicity, both steps are done via their own regex replacements. The first one finds the words that starts with eitherย quor with a character that is not a vowel (in other words, which is neitherย a,e,ย i,ย o, orย u). The two captured parts of the word are switched in the replacement part:ย $1$0.

The second substitution instruction finds all the words (at this point, the words that had initially started with consonants are already modified and the words, starting with vowels, stay original) and appends theย ay ending to it.ย 

Theย << andย >> anchors bind the regexes to word borders.

As an exercise, modify the program to take care of capital letters in the original sentence so thatย You becomesย Ouyay and notย ouYay.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s