玩蛇网提供最新Python编程技术信息以及Python资源下载!
您现在的位置: 玩蛇网首页 > Python源码实例_Python程序源代码_网站项目下载 > 正文内容

Python实现viterbi(维特比)算法原理流程是什么样的

Python入门佳作 经典教程的全新修订 10个项目引人入胜
玩蛇网推荐图文教程:python黑客多线程扫描器

本文是关于Python实现viterbi算法原理流程的代码示例讲解,说到viterbi算法自然会想到隐马尔可夫模型。这个算法简单的理解意思就是通过已知可以观察到的序列(python 序列操作方法),结合已知的状态转换之间的概率情况,通过综合状态之间的转移概率和前一个状态的情况,计算得出概率最大的状态转换路径,从结果推断出隐含状态序列情况。

对于viterbi更权威全面的讲解大家可以自行baidu一下,会有更好的解答。

python viterbi算法

Python的viterbi算法代码一如下:

def InitDicForViterbi(nodes,posw,posdi,n):   
    newWordList = []   
    # 解决未登录词   
    for i in nodes:   
        if not posw.has_key(i):   
            newWordList.append(i)   
    maxPosList = NPos(posdi,n)   
    print maxPosList   
    GenerateDicPos(newWordList,maxPosList,posw,posdi)   
       
def InitViterbi(node,posw,posdi):   
    viStatePath = []   
    for i in posw[node]:   
        if i <> "@@@":   
            print "i:",i   
            viStatePath.append([posw[node][i]*posdi[i]["@@@"],[i]])   
    return viStatePath  

Python的viterbi算法代码第二部分如下:

""" nodes 就是分好的词, posw 是词转换为词性的概率,posdi是词性之间的转换概率,n 是n个最大的词性将此用于未登录词中,  
    weightNone是未出现的词性转移的概率  
    nodes format {word1,word2...} , posw format {word1:{pos1:fre,pos2:fre,..."@@@":totalnum},..."@@@":total}  
    posdi format {pos1:{pos2:fre,pos3:fre...."@@@":total},...."@@@":total}  
    """  

def Viterbi(nodes,posw,posdi,n,weightNone):   
    InitDicForViterbi(nodes,posw,posdi,n)   
    viStatePath = InitViterbi(nodes[0],posw,posdi)   
    length = len(nodes)   
    currentNode = 1  
    
    while currentNode < length:   
        currentPosList = posw[nodes[currentNode]]   
        paths = []   
       # print "vstate:",viStatePath   
        
         for k in currentPosList:   
            if k <> "@@@":   
                ajk = weightNone   
                heap = []   
                   
                for j in xrange(len(viStatePath)):   
                    # compute every state j to every state k in ti   
         #          temppath = viStatePath[j]   
         #           print "lastpos:",temppath   
                    lastpos = viStatePath[j][1][-1]   
                    lastweight = viStatePath[j][0]   
                    lastposList = posdi[lastpos]   
                    if lastposList.has_key(k):   
                            ajk =lastposList[k]   
                    currentweight = lastweight * ajk * currentPosList[k]   
        #            print "viStatePath:",viStatePath[j][1]   
                    pathNew = [data for data in viStatePath[j][1]]   
                    pathNew.append(k)   
       #             print "pathNew:",pathNew   
                    heappush(heap,[currentweight,pathNew])   
                   
                 # get the max possibility of state k in ti   
                   
      #          print "path:",path   
                paths.append(nlargest(1,heap)[0])   
        del viStatePath   
        viStatePath = paths   
    #    print "paths:",paths 
#www.iplaypython.com  
        currentNode = currentNode + 1  
    
    heap = []   
    # get the max possibility path   
    for i in viStatePath:   
        heappush(heap,i)   
    return nlargest(1,heap) 

Python的viterbi算法代码第三部分如下:

"""nodes format {word1,word2,...} path is [weight,[pos1,pos2....]]"""  
def Result(nodes,path,edcode="utf-8"):   
    realPath = path[0][1]   
    ResultPrint(nodes,realPath,edcode)   
"""nodes format {word1,word2,...} p
25a2
ath is [pos1,pos2....]"""  

def ResultPrint(nodes,path,edcode="utf-8"):   
    for i in xrange(len(nodes)):   
        print nodes[i].decode(edcode),"/",path[i].decode(edcode)  
aa = ConvertGBKtoUTF("球球")       

bb = ConvertGBKtoUTF("娃娃")   

cc = ConvertGBKtoUTF("吃饭")   

dd = ConvertGBKtoUTF("好")   

ee = ConvertGBKtoUTF("dddwieoewkem")   

dictions = {aa:{bb:1,"@@@":4},bb:{cc:2,aa:3,"@@@":40},"@@@":400}   
posdi = {"n":{"s":3,"v":3,"@@@":40},"s":{"v":2,"e":3,"@@@":33},"v":{"@@@":1},"@@@":100}   
posw = {aa:{"n":1,"v":3,"@@@":29},bb:{"n":1,"s":1,"@@@":19},cc:{"n":1,"@@@":1},"@@@":10002}   
      
nodes = [aa,bb,cc,ee,dd,bb,aa,cc]   
path = Viterbi(nodes,posw,posdi,3,0.01)   
print path   
Result(nodes,path)

玩蛇网文章,转载请注明出处和来源网址:http://www.iplaypython.com/code/c2716.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

在线python培训课程

玩蛇网PythonQQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
出炉日期:2015-12-21 13:22 玩蛇网 www.iplaypython.com

我要分享到:
评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)

必知PYTHON教程 Must Know PYTHON Tutorials

必知PYTHON模块 Must Know PYTHON Modules