pig_latin.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import re

PIG_LATIN_SUFFIX = 'ay'

# Prefixes for which suffix will simply be appended
RE_SIMPLE_APPEND = re.compile(r'(a|e|i|o|u|xr|yt)')

# Prefixes which will be moved to back before appending suffix
# This includes any single characters left after the simple append
# match - since all vowels were matched back there, this will
# pick up the consonants.
RE_MOVE_TO_BACK = re.compile(r'(thr|sch|squ|th|ch|qu|\w)')


def translate_word(word):
    if RE_SIMPLE_APPEND.match(word):
        return word + PIG_LATIN_SUFFIX
    else:
        m = RE_MOVE_TO_BACK.match(word)
        if m:
            g = m.group()
            return word[len(g):] + g + PIG_LATIN_SUFFIX


def translate(text):
    return ' '.join(map(translate_word, text.split()))

Comments


You're not logged in right now. Please login via GitHub to comment