License Python Software Foundation License (Python 2.x)
Lines 28
Keywords
convert (3) pig latin (2)
Included in this Library
Permissions
Viewable by Everyone
Editable by All Siafoo Users
Hide
Need a quick chart or graph for your blog? Try our reStructured Text renderer. Join Siafoo Now or Learn More

Convert Text to *Pig Latin* Atom Feed 1

In Brief Converts words to pig-latin! Also there is some cool regex magic.
# 's
 1"""Convert text to Pig Latin
2
3This program is part of "Dive Into Python", a free Python book for
4experienced programmers. Visit http://diveintopython.org/ for the
5latest version.
6"""
7
8__author__ = "Mark Pilgrim (mark@diveintopython.org)"
9__version__ = "$Revision: 1.2 $"
10__date__ = "$Date: 2004/05/05 21:57:19 $"
11__copyright__ = "Copyright (c) 2002 Mark Pilgrim"
12__license__ = "Python"
13
14import re
15
16def _wordToPigLatin(match):
17 word = match.group()
18 consonants = match.group(1)
19 restOfWord = match.group(2)
20 # put consonants after rest of word, and add "ay"
21 result = "%s%say" % (restOfWord, consonants)
22 # if word was all uppercase, make result uppercase
23 if word == word.upper():
24 result = result.upper()
25 # if word was capitalized, make result capitalized
26 elif word == word.capitalize():
27 result = result.capitalize()
28 return result
29
30def pigLatin(source):
31 pattern = re.compile(r'\b([bcdfghjklmnpqrstvwxyz]*)(\w+)\b', re.IGNORECASE)
32 return pattern.sub(_wordToPigLatin, source)
33

Converts words to pig-latin! Also there is some cool regex magic.