2011-06-29 40 views
1
import networkx as nx #@UnresolvedImport 
from networkx.algorithms import bipartite #@UnresolvedImport 
from operator import itemgetter 
from random import choice 

corpus = open('/home/abehl/Desktop/Corpus/songs.wx', 'r') 

ALPHA = 1.5 
EPSILON = 0.5 

song_nodes = [] 
word_nodes = []  

edges = zip(song_nodes, word_nodes) 

B = nx.Graph(edges) 
degX,degY = bipartite.degrees(B, word_nodes) 

sortedSongNodesByDegree = sorted(degX.iteritems(), key=itemgetter(1)) 
print sortedSongNodesByDegree 

song_nodes2 = [] 
word_nodes2 = [] 
Vc = list(set(word_nodes)) 

edges2 = zip(song_nodes2, word_nodes2) 
C= nx.Graph(edges2) 

for songDegreeTuple in sortedSongNodesByDegree: 
    for i in range(songDegreeTuple[1]): 
     connectedNodes = C.neighbors(songDegreeTuple[0]) 
     VcDash = [element for element in Vc if element not in connectedNodes] 
     calculateBestNode(VcDash) 

def calculateBestNode(VcDashsR): 
    nodeToProbailityDict = {} 
    for node in VcDashsR: 
     degreeOfNode = bipartite(C, [node])[1][node] 
     probabiltyForNode = (degreeOfNode ** ALPHA) + EPSILON 
     nodeToProbailityDict[node] = probabiltyForNode 

在上述Python程序,python解釋是投擲即使函數「calculateBestNode」在程序被定義下面的錯誤。我在這裏錯過了什麼。Python的投擲奇怪NameError:名稱<FUNCTION_NAME>沒有定義

NameError: name 'calculateBestNode' is not defined

請原諒我在這裏發佈大型項目。

+0

我沒有看到calculateSelectedNode函數 –

+0

calculateSelectedNode不在您的程序中的任何位置 – Gerrat

+0

我已更新錯誤消息,在此處發佈了錯誤消息。 –

回答

9

Python程序是從上到下執行的,因此您需要在使用它之前定義函數。一個常見的替代方案是把所有這一切都在一個main功能自動執行的代碼,並在文件末尾添加:

if __name__ == '__main__': 
    main() 

這具有額外的優勢,你現在已經寫了可以通過導入模塊其他。

+1

解析不相關:在執行任何一個模塊之前,整個模塊都會被解析。有意義的是,在正常的程序流中執行'def'時,Python函數就會被創建。 – Duncan

+0

@Duncan更新。 – phihag

3

在您的程序中定義它之前,您嘗試使用函數calculateBestNode()。所以解釋者不知道它存在。

相關問題