【Python学习日记】函数列表 动态创建函数 文件读取 迭代器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | import re def match_sxz(noun): return re.search( '[sxz]$' ,noun) def apply_sxz(noun): return re.sub( '$' , 'es' ,noun) def match_h(noun): return re.search( '[^aeioudgkprt]h' ,noun) def apply_h(noun): return re.sub( '$' , 'es' ,noun) def match_y(noun): return re.search( '[^aeiou]y$' ,noun) def apply_y(noun): return re.sub( 'y$' , 'ies' ,noun) def match_default(noun): return True def apply_default(noun): return re.sub( '$' , 's' ,noun) rules = ((match_sxz, apply_sxz), (match_h, apply_h), (match_y, apply_y), (match_default, apply_default) ) def plural(noun): for match_rule, apply_rule in rules: if (match_rule(noun)): return apply_rule(noun) print (plural( 'body' )); |
改成动态创建函数的方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import re def build_match_apply_function(pattern, search, replace): def match_rule(word): return re.search(pattern, word) def apply_rule(word): return re.sub(search, replace, word) return (match_rule, apply_rule) patterns = \ ( ( '[sxz]$' , '$' , 'es' ), ( '[^aeioudgkprt]h$' , '$' , 'es' ), ( '(qu|[^aeiou]y$)' , 'y$' , 'ies' ), ( '$' , '$' , 's' ), ) rules = [build_match_apply_function(pattern, search, replace) for (pattern, search, replace) in patterns] def plural(noun): for match_rule, apply_rule in rules: if (match_rule(noun)): return apply_rule(noun) print (plural( 'body' )); |
下面把规则存放到一个文件中,通过读取文件内容的方式,来完成
把规则写到plural.txt中
1 2 3 4 | [sxz]$ $ es [^aeioudgkprt]h$ $ es [^aeiou]y$ y$ ies $ $ s |
程序代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import re def build_match_apply_function(pattern, search, replace): def match_rule(word): return re.search(pattern, word) def apply_rule(word): return re.sub(search, replace, word) return (match_rule, apply_rule) rules = [] with open ( 'plural.txt' ,encoding = 'gb2312' ) as pattern_file: for line in pattern_file: pattern, search, replace = line.split( None , 3 ) rules.append(build_match_apply_function( pattern, search, replace)) def plural(noun): for match_rule, apply_rule in rules: if (match_rule(noun)): return apply_rule(noun) print (plural( 'body' )); |
继续改进,用生成器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import re def build_match_apply_function(pattern, search, replace): def match_rule(word): return re.search(pattern, word) def apply_rule(word): return re.sub(search, replace, word) return (match_rule, apply_rule) def rules(rule_filename): with open (rule_filename,encoding = 'gb2312' ) as pattern_file: for line in pattern_file: pattern, search, replace = line.split( None , 3 ) yield build_match_apply_function(pattern, search, replace) def plural(noun, rule_filename = 'plural.txt' ): for match_rule, apply_rule in rules(rule_filename): if (match_rule(noun)): return apply_rule(noun) raise ValueError( 'no matching rule for {0}' . format (noun)) print (plural( 'body' )); |
改用迭代器读取文件,并缓存
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | 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] class LazyRules: rules_filename = 'plural.txt' def __init__( self ): self .pattern_file = open ( self .rules_filename, encoding = 'gb2312' ) 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 plural(noun): for matches_rule, apply_rule in rules: if matches_rule(noun): return apply_rule(noun) print (plural( 'bony' )); |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· C# 13 中的新增功能实操
· Ollama本地部署大模型总结
· 2025成都.NET开发者Connect圆满结束
· langchain0.3教程:从0到1打造一个智能聊天机器人
· 用一种新的分类方法梳理设计模式的脉络