智能建站源码,wordpress 新增页面,仙霞新村街道网站建设,html静态网站模板python esmre库实现word查找
前言#xff1a; 在文本中匹配特定的字符串#xff0c;一般可以用普通的字符串匹配算法#xff0c;KMP算法#xff1b; python中提供了一个库#xff0c;esmre, 通过预先将字符串存到esm对象中#xff0c;利用这些字符串从候选的字符串中进行…python esmre库实现word查找
前言 在文本中匹配特定的字符串一般可以用普通的字符串匹配算法KMP算法 python中提供了一个库esmre, 通过预先将字符串存到esm对象中利用这些字符串从候选的字符串中进行匹配返回匹配位置支持同一个词语的多次匹配。效率比正则表达式快。
import esm
import reindex esm.Index()
index.enter(he)
index.enter(she)
index.enter(his)
index.enter(hers)
index.fix()
query1 this here is history
query2 Those are his sheep!# 使用esmre
out1 index.query(query1)
out2 index.query(query2)
print(out1, out1, \nstr1, query1[out1[0][0][0]:out1[0][0][1]])
print(out2, out2, \nstr2, query1[out2[0][0][0]:out2[0][0][1]])# 使用正则表达式
out3 re.search(rhe|she|his|hers, query1)
print(out3,out3)out4 re.search(rxxx|yyy, query2)
print(out4,out4)
out1 [((1, 4), his), ((5, 7), he), ((13, 16), his)]
str1 his
out2 [((10, 13), his), ((14, 17), she), ((15, 17), he)]
str2 is
out3 re.Match object; span(1, 4), matchhis
out4 None参考 1.敏感词匹配——python使用esmre实现ac自动机[多模匹配] 2,.esmre 1.0.1 3.python ac模块_python使用esmre代替ahocorasick实现ac自动机[多模匹配]