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
總結
以上是生活随笔為你收集整理的python实现双向最大匹配法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 词法分析-中文分词技术-正向最大匹配法与
- 下一篇: 机器学习笔记:为什么要对数据进行归一化处