Advertisement
treyhunner

pig latin

May 19th, 2016
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. import re
  2.  
  3.  
  4. def translate1(sentence):
  5.     PIG_LATIN_RE = re.compile(
  6.         r'''
  7.        \b
  8.        (
  9.            [xy][^aeiou]\w*
  10.        |
  11.            [^aeiou\s]*
  12.            (?: (?<=q)u )?
  13.        )
  14.        (\w*)
  15.        \b
  16.        ''', re.VERBOSE)
  17.     return PIG_LATIN_RE.sub(r'\2\1ay', sentence)
  18.  
  19.  
  20.  
  21. def substitute_word(match):
  22.     if re.search(r'^[xy][^aeiou]', match.group(1)):
  23.         return match.group(0) + "ay"
  24.     else:
  25.         return "{1}{0}ay".format(*match.groups())
  26.  
  27.  
  28. def translate2(sentence):
  29.     PIG_LATIN_RE = re.compile(
  30.         r'''
  31.        \b
  32.        (
  33.            [^aeiou\s]*
  34.            (?: (?<=q)u )?
  35.        )
  36.        (\w*)
  37.        \b
  38.        ''', re.VERBOSE)
  39.     return PIG_LATIN_RE.sub(substitute_word, sentence)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement