舒畅 发表于 2017-5-5 08:22:36

python 之 求英语单词复数的 一种实现方式

#!/usr/bin/evn python
import re;
def GetMatchAndApplyFuncs(strPattern, strSearch, strReplace):
def MatchRule(strWord):
return re.search(strPattern, strWord);
def ApplyRule(strWord):
return re.sub(strSearch, strReplace, strWord);
return (MatchRule, ApplyRule);
g_tlPattern = (
('$',         '$','es'),
('[^aeioudgkprt]h$', '$','es'),
('(qu|[^aeiou])y$','y$', 'ies'),
('$',                '$','s')
);
g_lsRules = ;
def GetPlural(strWord):
if not strWord:
return "";
for fnMatch, fnApply in g_lsRules:
if fnMatch(strWord):
return fnApply(strWord);
if "__main__" == __name__:
strVal = "exhs";
strPlural = GetPlural(strVal);
print("%s plural ==> %s" %(strVal, strPlural));

 
页: [1]
查看完整版本: python 之 求英语单词复数的 一种实现方式