Skip to content

Instantly share code, notes, and snippets.

@contee213
Created September 5, 2014 08:46
Show Gist options
  • Save contee213/17c34bd0ddb5fa34966f to your computer and use it in GitHub Desktop.
Save contee213/17c34bd0ddb5fa34966f to your computer and use it in GitHub Desktop.
dive to python - functions
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