mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
fixed numbering, fixed file() typo, take rules file as an argument in plural5.py
This commit is contained in:
+4
-4
@@ -14,14 +14,14 @@ def build_match_and_apply_functions(pattern, search, replace):
|
||||
return re.sub(search, replace, word)
|
||||
return [matches_rule, apply_rule]
|
||||
|
||||
def rules():
|
||||
with open('plural5-rules.txt') as pattern_file:
|
||||
def rules(rules_filename):
|
||||
with open(rules_filename) 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):
|
||||
for matches_rule, apply_rule in rules():
|
||||
def plural(noun, rules_filename='plural5-rules.txt'):
|
||||
for matches_rule, apply_rule in rules(rules_filename):
|
||||
if matches_rule(noun):
|
||||
return apply_rule(noun)
|
||||
raise ValueError('no matching rule for {0}'.format(noun))
|
||||
|
||||
Reference in New Issue
Block a user