当前位置:   article > 正文

python实现双向最大匹配法_双向最大匹配算法 python

双向最大匹配算法 python

python实现双向最大匹配法

CSDN小马哥

于 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) 
————————————————
版权声明:本文为CSDN博主「CSDN小马哥」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43256799/article/details/86098695

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/374921
推荐阅读
相关标签
  

闽ICP备14008679号