|
|
@ -1,5 +1,6 @@
|
|
|
|
import operator
|
|
|
|
import operator
|
|
|
|
MIN_FLOAT=-3.14e100
|
|
|
|
MIN_FLOAT=-3.14e100
|
|
|
|
|
|
|
|
MIN_INF=float("-inf")
|
|
|
|
|
|
|
|
|
|
|
|
def get_top_states(t_state_v,K=4):
|
|
|
|
def get_top_states(t_state_v,K=4):
|
|
|
|
items = t_state_v.items()
|
|
|
|
items = t_state_v.items()
|
|
|
@ -27,7 +28,7 @@ def viterbi(obs, states, start_p, trans_p, emit_p):
|
|
|
|
if len(obs_states)==0: obs_states = all_states
|
|
|
|
if len(obs_states)==0: obs_states = all_states
|
|
|
|
|
|
|
|
|
|
|
|
for y in obs_states:
|
|
|
|
for y in obs_states:
|
|
|
|
(prob,state ) = max([(V[t-1][y0] + trans_p[y0].get(y,MIN_FLOAT) + emit_p[y].get(obs[t],MIN_FLOAT) ,y0) for y0 in prev_states])
|
|
|
|
(prob,state ) = max([(V[t-1][y0] + trans_p[y0].get(y,MIN_INF) + emit_p[y].get(obs[t],MIN_FLOAT) ,y0) for y0 in prev_states])
|
|
|
|
V[t][y] =prob
|
|
|
|
V[t][y] =prob
|
|
|
|
mem_path[t][y] = state
|
|
|
|
mem_path[t][y] = state
|
|
|
|
|
|
|
|
|
|
|
|