Created
September 5, 2014 08:46
-
-
Save contee213/17c34bd0ddb5fa34966f to your computer and use it in GitHub Desktop.
dive to python - functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
patterns = ( | |
('[sxz]$', '$', 'es'), | |
('[^aeioudgkprt]h$', '$', 'es'), | |
('[^aeiou]y$', 'y$', 'ies'), | |
('$', '$', 's') | |
) | |
def mls(pattern, remove, replace): | |
def match(word): | |
return re.search(pattern, word) | |
def apply(word): | |
return re.sub(remove, replace, word) | |
return (match, apply) | |
rules = [ mls(pattern, remove, replace) | |
for (pattern, remove, replace) in patterns] | |
def nls(noun): | |
for (match, apply) in rules: | |
if match(noun): | |
return apply(noun) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment