HMM实现中文分词

链接:https://pan.baidu.com/s/1uBjLC61xm4tQ9raDa_M1wQ提取码:f7l1
推荐:https://blog.csdn.net/longgb123/article/details/78154295

import sys sys.path.append('保存文件的路径')#设置路径
# 下面三个文件在上面
from prob_emit import P as p_emit from prob_start import P as p_start from prob_trans import P as p_transobs = '今天我来到北京清华大学'#观察值 states = 'BMES'V = [{}] path = {}prev_states = { 'B': 'ES',# t时刻:t-1时刻 'M': 'BM', 'E': 'BM', 'S': 'SE' }# 初始化第一个字符作为 'BMES' 的概率 for y in states: V[0][y] = p_start[y] + p_emit[y][obs[0]] path[y] = y#概率: 输出概率(独立性概率:第k个字符状态为y的概率)+ 转换概率(y0 --> y ) + 上一个字符状态为y0的概率(当前状态与上一个字符的状态有关)for k in range(1, len(obs)): t0 = {}# 储存概率 path0 = {}# 储存路径for y in states: em_p = p_emit[y][obs[k]]# 输出概率表示 第 k 个字符状态为 y 时 的输出概率(prob, s0) = max((em_p+p_trans[y0][y]+V[-1][y0], y0) for y0 in prev_states[y])# 动态规划 #上面的probs0就是在下面的每个循环中取tmp_prob tmp_s0 中取得最大值 #for y0 in prev_states[y]: # prev_state[y] 表示 t时刻 状态为 y 时的 t-1时刻可能出现的状态 #tmp_prob = em_p + p_trans[y0][y] + V[-1][y0] #tmp_s0 = y0t0[y] = prob path0[y] = path[s0] + ypath = path0 V.append(t0) path (prob, s0) = max((V[-1][y], y) for y in 'ES')s2 = [] for i, char in enumerate(obs): sign = path[s0][i] if(sign == 'B'): begin = i elif sign == 'E': s2.append(obs[begin: i+1]) elif sign == 'S': s2.append(obs[i]) else : pass s2


【HMM实现中文分词】转载于:https://www.cnblogs.com/DDiamondd/p/11146388.html

    推荐阅读