优化程序结构,增加程序的可读性
#coding:utf-8
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('[^adioudgkprt]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(noun):
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 matchRule,applyRule in rules:
if matchRule(noun):
return applyRule(n)
if __name__ == '__main__':
plural(noun)