propagation algorithm

63 views
Skip to first unread message

Goog Cheng

unread,
Sep 12, 2012, 9:24:54 AM9/12/12
to networkx...@googlegroups.com
Hi,pal! I wanna use networkx to calculate the sentiment strength of a wordnet by a propagation algorithm(it seems to be belief propagation),but didnt get a good result. Is there a problem of my implement?Any guidance would be greatly appreciated.

here is my code :
#init the strength
for n in DG:
    if DG.in_degree(n)==0:
        if dictpos.get(n.encode('utf8')):
            DG.node[n]['s'] = 1
        else:
            DG.node[n]['s'] = -1
    else:
        DG.node[n]['s']=0
#iter 12 times
for i in range(12):
    maxs=0
    for n in DG:
        if DG.in_degree(n)==0:
            continue
        else:
            neighbors=DG.predecessors(n)
            for nb in neighbors:  
                w=DG[nb][n]['weight'] / DG.out_degree(nb,weight='weight')
                DG.node[n]['s']+=w*DG.node[nb]['s']
            if abs(DG.node[n]['s'])>maxs:
                maxs=abs(DG.node[n]['s'])
    #normalize the strength
    for n in DG:
        DG.node[n]['s']/=maxs
Reply all
Reply to author
Forward
0 new messages