Python中的复数迭代器
这是代码,工作正常,只有以s,x,z结尾的单词没有被拾取Python中的复数迭代器,python,python-3.x,sublimetext2,Python,Python 3.x,Sublimetext2,这是代码,工作正常,只有以s,x,z结尾的单词没有被拾取 import re def plural(noun): for matches_rule, apply_rule in rules: if matches_rule(noun): return apply_rule(noun) def build_match_and_apply_functions(pattern, search, replace): def matches_rul
import re
def plural(noun):
for matches_rule, apply_rule in rules:
if matches_rule(noun):
return apply_rule(noun)
def build_match_and_apply_functions(pattern, search, replace):
def matches_rule(word):
return re.search(pattern,word)
def apply_rule(word):
return re.sub(search, replace, word)
return (matches_rule, apply_rule)
def rules(rules_filename):
with open(rules_filename, encoding='utf-8') as pattern_file:
for line in pattern_file:
pattern, search, replace = line.split(None, 3)
yield build_match_and_apply_functions(pattern, search, replace)
def plural(noun, rules_filename='plurals.txt'):
for matches_rules, apply_rule in rules(rules_filename):
if matches_rules(noun):
return apply_rule(noun)
raise ValueError('no matching rule for {0}'.format(noun))
r3 = plural('tax')
print(r3)
这是规则文件
[sxz]$ $ es
[^aeioudgkprt]h$ $ es
[^aeiou]y$ y$ ies
$ $ s
我正在崇高文本2上执行此代码。有人能找出错误吗?我刚刚运行了您的代码,它似乎工作正常。“税收”->“税收”,“牧场”->“牧场”,“鸭子”->“鸭子”,“天空”->“天空”您是否也在使用升华文本2进行测试?不。因此,如果我们排除了python代码本身的问题,并假设这是一个专门针对升华文本的问题,那么您可能应该用它来标记它。