维特比分词算法(纯循环)

def viterbi_split_word(sentence,dictionaries):
    math = __import__('math')
    lenht_of_seq = len(sentence)
    def directed_acyclic_graph(sentence):
        DAG = {} 
        for end in range(lenht_of_seq):
            tmp = [] 
            start = end
            point = sentence[end]
            for start in range(start,lenht_of_seq):
                if point in word_dict:
                    tmp.append(start)  
                point = sentence[end:start + 2] 
            if not tmp:
                tmp.append(end)
            DAG[end] = tmp
        return DAG
    route={}
    route[lenht_of_seq] = (0, 0)
    DAG = directed_acyclic_graph(sentence)
    for reverse_start in range(lenht_of_seq - 1, -1, -1):
        distance = (((word_dict.get(sentence[reverse_start:start + 1]) or 0) + route[start + 1][0], start) for start in DAG[reverse_start])
        route[reverse_start] = max(distance)                   
    start = 0
    words = []
    while start < lenht_of_seq:
        init_start = route[start][1] + 1
        word = sentence[start:init_start]
        words.append(word)
        start = init_start
    return (sum(i[0]==0 and 10**(-8) or -math.log(i[0]) for i in route.values()),words,DAG)
viterbi_split_word(sentence ="我们经常有意见分歧,这是没有办法的意见分歧",dictionaries=word_dict)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值