"""Pluralize English nouns (stage 4)

This program is part of "Dive Into Python", a free Python book for experienced programmers. Visit http://diveintopython.org/ for the latest version.

Command line usage: $ python plural4.py noun nouns """

__author__ = "Mark Pilgrim ([email protected])" __version__ = "$Revision: 1.3 $" __date__ = "$Date: 2004/03/18 16:43:37 $" __copyright__ = "Copyright (c) 2004 Mark Pilgrim" __license__ = "Python"

import re

def buildMatchAndApplyFunctions((pattern, search, replace)): matchFunction = lambda word: re.search(pattern, word) applyFunction = lambda word: re.sub(search, replace, word) return (matchFunction, applyFunction)

patterns = \ ( ('[sxz]$', '$', 'es'), ('[^aeioudgkprt]h$', '$', 'es'), ('(qu|[^aeiou])y$', 'y$', 'ies'), ('$', '$', 's') ) rules = map(buildMatchAndApplyFunctions, patterns)

def plural(noun): for matchesRule, applyRule in rules: if matchesRule(noun): return applyRule(noun)

if __name__ == '__main__': import sys if sys.argv[1:]: print plural(sys.argv) else: print __doc__