list of functions not work as intended in python 3 -
this dive python: has 2 parts: 1st part: file called plural-rules.txt:
'[sxz]$' '$' 'es' '[^aeioudgkprt]h$' '$' 'es' '[^aeiou]y$' 'y$' 'ies' '$' '$' 's'
second a.py:
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) rules=[] open('plural-rules.txt') pattern_file: line in pattern_file: pattern,search, replace=line.split(none, 3) #print(pattern, search, replace) #print(line,end='') rules.append(build_match_and_apply_functions(pattern, search, replace)) def plural(word): matches, plurals in rules: if matches(word): return plurals(word) print(rules[2][1]('shobby')) ---------------------1 print("shobby's plural is", plural('shobby'))----------------------2
1 print out: shobby 2 print out: shobby's plural none
you need remove '
s surround tokens. this:
rules.append(build_match_and_apply_functions(pattern.strip("'"), search.strip("'"), replace.strip("'")))
or build input file this:
[sxz]$ $ es [^aeioudgkprt]h$ $ es [^aeiou]y$ y$ ies $ $ s
Comments
Post a Comment