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) 
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值