python实现双向最大匹配法
优快云小马哥
于 2019-01-08 21:01:29 发布
2776
收藏 3
文章标签: python 中文分词技术 双向最大匹配法 自然语言处理
版权
-- coding: utf-8 --
“”"
Created on Sat Jan 5 15:53:18 2019
@author: 86199
“”"
class MM():
def init(self):
self.window_size = 3
def cut(self,text):
result = []
index = 0
text_length = len(text)
dic = [‘研究’,‘研究生’,‘生命’,‘命’,‘的’,‘起源’]
while text_length > index:
for size in range(self.window_size+index,index,-1):
piece = text[index:size]
if piece in dic:
index = size - 1
break
index = index + 1
result.append(piece+’----’)
return(result)
class RMM():
def init(self):
self.window_size = 3
def cut(self,text):
result = []
index = 0
index = len(text)
dic = [‘研究’,‘研究生’,‘生命’,‘命’,‘的’,‘起源’]
while index > 0:
for size in range(index - self.window_size,index,):
piece = text[size:index]
if piece in dic:
index = size + 1
break
index = index - 1
result.append(piece+’----’)
result.reverse()
return(result)
if name == ‘main’:
text = ‘研究生命的起源’
count1 = 0
count2 = 0
First = MM()
Second = RMM()
a = First.cut(text)
b = Second.cut(text)
if a == b:
print(a)
lena = len(a)
lenb = len(b)
if lena == lenb:
for DY1 in a:
if len(DY1) == 5:
count1 = count1 + 1
for DY2 in b:
if len(DY2) == 5:
count2 = count2 + 1
if count1 > count2:
print(b)
else:
print(a)
if lena > lenb:
print(b)
if lena < lenb:
print(a)
————————————————
版权声明:本文为优快云博主「优快云小马哥」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/weixin_43256799/article/details/86098695