(self, obs)
return (prob, path[state])
特比算法 摘自wiki 维特比算法
特比算法 摘自wiki 维特比算法
特比算法 摘自wiki 维特比算法
def viterbi(self, obs):
        '''
        特比算法 摘自wiki 维特比算法
        '''
        V = [{}]
        path = {}
        for y in self.model.states:
            V[0][y] = self.model.start_states[y] * \
                self.model.emission_probability[y][obs[0]]
            path[y] = [y]
        for t in range(1, len(obs)):
            V.append({})
            newpath = {}
            for y in self.model.states:
                (prob, state) = max(
                    [(V[t - 1][y0] * self.model.transition_probability[y0][y] * self.model.emission_probability[y][obs[t]], y0) for y0 in self.model.states])
                V[t][y] = prob
                newpath[y] = path[state] + [y]
            path = newpath
        (prob, state) = max([(V[len(obs) - 1][y], y)
                             for y in self.model.states])
        return (prob, path[state])
def viterbi ( self , obs ) : V = [ { } ] path = { } for y in self . model . states : V [ 0 ] [ y ] = self . model . start_states [ y ] * self . model . emission_probability [ y ] [ obs [ 0 ] ] path [ y ] = [ y ] for t in range ( 1 , len ( obs ) ) : V . append ( { } ) newpath = { } for y in self . model . states : ( prob , state ) = max ( [ ( V [ t - 1 ] [ y0 ] * self . model . transition_probability [ y0 ] [ y ] * self . model . emission_probability [ y ] [ obs [ t ] ] , y0 ) for y0 in self . model . states ] ) V [ t ] [ y ] = prob newpath [ y ] = path [ state ] + [ y ] path = newpath ( prob , state ) = max ( [ ( V [ len ( obs ) - 1 ] [ y ] , y ) for y in self . model . states ] ) return ( prob , path [ state ] )