在第7章中,作者创建了一个迭代器,用于构建将单数英语名词转换为复数的规则:
class LazyRules:
rules_filename = 'plural5-rules.txt'
def __init__(self):
self.pattern_file = open(self.rules_filename, encoding='utf-8')
self.cache = []
def __iter__(self):
self.cache_index = 0
return self
def __next__(self):
self.cache_index += 1
if len(self.cache) >= self.cache_index:
return self.cache[self.cache_index - 1]
if self.pattern_file.closed:
raise StopIteration
line = self.pattern_file.readline()
if not line:
self.pattern_file.close()
raise StopIteration
pattern, search, replace = line.split(None, 3)
funcs = build_match_and_apply_functions(
pattern, search, replace)
self.cache.append(funcs)
return funcs
rules = LazyRules()
虽然本书对此代码的每一部分提供了非常明确和详尽的解释,但它并没有解释如何使用它? 如何使用这些规则更改名词?
我尝试将这一位添加到课程中:
def build_match_and_apply_functions(self, 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 plural(self, noun):
for matches_rule, apply_rule in self.__next__():
if matches_rule(noun):
return apply_rule(noun)
raise ValueError('no matching rule for {0}'.format(noun))
更新:我将代码更改为 janos 中的评论。 所以它现在看起来像这样:
class LazyRules:
rules_filename = 'plural5-rules.txt'
def __init__(self):
self.pattern_file = open(self.rules_filename, encoding='utf-8')
self.cache = []
def __iter__(self):
self.cache_index = 0
return self
def __next__(self):
self.cache_index += 1
if len(self.cache) >= self.cache_index:
return self.cache[self.cache_index - 1]
if self.pattern_file.closed:
raise StopIteration
line = self.pattern_file.readline()
if not line:
self.pattern_file.close()
raise StopIteration
pattern, search, replace = line.split(None, 3)
funcs = build_match_and_apply_functions(
pattern, search, replace)
self.cache.append(funcs)
return funcs
rules = LazyRules()
import re
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 plural(noun):
for matches_rule, apply_rule in rules:
if matches_rule(noun):
return apply_rule(noun)
raise ValueError('no matching rule for {0}'.format(noun))
它现在有效!!!!谢谢 janos !
但是,我还有另一个问题:为什么在build_match_and_apply_functions()
函数matches_rule(word)
中有一个变量' word' ,而在the plural()
函数中matches_rule(noun)
有一个变量'名词' ,不应该将变量命名为相同?
答案 0 :(得分:0)
plural
不应该是LazyRules
的方法,而应该是一个独立的函数。它应该发布rules
(这是自定义迭代器类LazyRules
的一个实例):
def plural(self, noun):
for matches_rule, apply_rule in rules:
if matches_rule(noun):
return apply_rule(noun)
raise ValueError('no matching rule for {0}'.format(noun))