当前位置:网站首页>Forward maximum matching method

Forward maximum matching method

2022-07-06 21:08:00 wx5d786476cd8b2


       
class MM(object):
def __init__(self,dic_path):
self.dictionary=set()
self.maximum=0
# Read the dictionary
with open(dic_path,'r',encoding='utf-8') as f:
for line in f:
line=line.strip()
if not line:
continue
self.dictionary.add(line)
if self.maximum<len(line):
self.maximum=len(line)
def cut(self, text):
print(self.dictionary)
result=[]
index=len(text)
print(index)
n=0
while index>0:
word=None
for size in range(self.maximum,0,-1):
print(size)
if index-size<0:
continue
piece = text[n:n+size]
print('piece',piece)
if piece in self.dictionary:
word=piece
result.append(word)
index-=size
print('ooooop',index)
n+=size
break
if word is None:
n+=1
index-=1
return result[::]
def main():
text=" Nanjing Yangtze River Bridge "
t=MM(r'C:\Users\ljy\Desktop\learning-nlp-master\chapter-3\data\imm_dic.utf8')
print(len(t.cut(text)))
main()
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.



原网站

版权声明
本文为[wx5d786476cd8b2]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131124550644.html