Zanurkuj w Pythonie/Źródła/plural/stage2/plural2.py

Z Wikibooks, biblioteki wolnych podręczników.

"""Pluralize English nouns (stage 2)
 
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 plural2.py noun
nouns
"""
 
__author__ = "Mark Pilgrim (mark@diveintopython.org)"
__version__ = "$Revision: 1.4 $"
__date__ = "$Date: 2004/03/18 18:55:45 $"
__copyright__ = "Copyright (c) 2004 Mark Pilgrim"
__license__ = "Python"
 
import re
 
def match_sxz(noun):
    return re.search('[sxz]$', noun)
 
def apply_sxz(noun):
    return re.sub('$', 'es', noun)
 
def match_h(noun):
    return re.search('[^aeioudgkprt]h$', noun)
 
def apply_h(noun):
    return re.sub('$', 'es', noun)
 
def match_y(noun):
    return re.search('[^aeiou]y$', noun)
 
def apply_y(noun):
    return re.sub('y$', 'ies', noun)
 
def match_default(moun):
    return 1
 
def apply_default(noun):
    return noun + 's'
 
rules = ((match_sxz, apply_sxz),
         (match_h, apply_h),
         (match_y, apply_y),
         (match_default, apply_default)
         )
 
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[1])
    else:
        print __doc__