# -*- coding: utf-8 -*-
#正向最大匹配法
text='研究生命的最初起源'#即将被分词的文本
dic=['研究','研究生','生命','命','的','最','初','起源'] #在这个字典进行匹配
ww=7 #每次取七个字符来匹配
MMresult=[]
index=0
text_length=len(text)
while text_length>index:
for size in range(ww+index,index,-1):
print(size,':',ww+index,index,-1)#输出遍历索引的过程
piece=text[index:size]
print(piece)#输出每次尝试匹配的字符串
if piece in dic:
index=size-1
break
index=index+1
MMresult.append(piece+'--')
print(MMresult)
#逆向最大匹配法
text='研究生命的最初起源'#即将被分词的文本
dic=['研究','研究生','生命','命','的','最','初','起源'] #在这个字典进行匹配
ww=7 #每次取七个字符来匹配
RMMresult=[]
index=len(text)
while index>0:
for size in range(index-ww,index):
print(size,'
jieba分词--正向最大匹配法和逆向最大匹配法
最新推荐文章于 2023-12-12 21:00:14 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)