版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/chengyuqiang/article/details/102719876
1、词典
./data/rmm_dic.utf8
代码语言:javascript复制南京市
南京市长
长江大桥
人民解放军
大桥
2、RMM算法
代码语言:javascript复制#逆向最大匹配
class RMM(object):
def __init__(self, dic_path):
self.dictionary = set()
self.maximum = 0
#读取词典
with open(dic_path, 'r', encoding='utf8') as f:
for line in f:
#移除字符串头尾指定的字符(默认为空格或换行符)或字符序列
line = line.strip()
if not line:
continue
self.dictionary.add(line)
if len(line) > self.maximum:
self.maximum = len(line)
def cut(self, text):
result = []
index = len(text)
while index > 0:
word = None
# 语法 range(start, stop[, step])
for size in range(self.maximum, 0, -1):
# 切片开始位置:index - size
if index - size < 0:
continue
#逆向切片
piece = text[(index - size):index]
if piece in self.dictionary:
word = piece
result.append(word)
index -= size
break
if word is None:
index -= 1
return result[::-1]
def main():
text = "南京市长江大桥"
tokenizer = RMM('./data/rmm_dic.utf8')
print(tokenizer.cut(text))
main()
执行结果
代码语言:javascript复制['南京市', '长江大桥']