我是python的新手,面对下面定义的这个问题。 我正在从这个名为“Node”的类中创建几个图表
class Node(object):
label = ""
nexts = ()
prevs = ()
pos_tag = ""
visited = False # black = False (unexplored), white = True (explored)
score = 0
我的图表生成功能是
# Generating the graph
def generate_graph(self, text, startnode, endnode):
sentences = self.convert_to_sentences(text)
sentences = [sentence.replace(",","") for sentence in sentences]
while(" ." in sentences):
sentences.remove(" .")
length = len(sentences)
self.START = startnode
self.END = endnode
G = nx.Graph()
G.add_node(self.START)
G.add_node(self.END)
for i in range(0, length):
words = sentences[i].split()
sent_size = len(words)
v = [Node() for i in range(sent_size)]
for j in range(0, sent_size):
label = words[j]
tag = nltk.pos_tag(nltk.word_tokenize(label))[0][1]
if self.exists_node(self.START, label, tag):
v[j] = self.get_existing_node(self.START, label, tag)
else:
v[j] = self.create_new_node(label, tag)
G.add_node(v[j])
if j==0:
tup = (v[j],)
self.START.nexts = self.START.nexts + tup
G.add_edge(self.START, v[j])
if v[j].label == ".":
self.add_edge(v[j], self.END)
G.add_edge(v[j], self.END)
if not self.exists_edge(v[j-1], v[j]):
self.add_edge(v[j-1], v[j])
G.add_edge(v[j-1], v[j])
Nodes = []
graph_size, Nodes = self.graph_size(self.START)
for i in range(0, graph_size):
print(Nodes[i].label, Nodes[i].pos_tag)
leng = len(Nodes[i].nexts)
for x in range(0, leng):
print(Nodes[i].nexts[x].label, Nodes[i].nexts[x].pos_tag)
print(" ")
#nx.draw(G)
paths = self.find_paths(G, self.START, self.END)
return paths
我多次调用此图形生成函数来生成多个图形,第一个图形生成非常精细,但是第二个和后续图形包含前面图形的边缘,即它已经包含前一个图形的状态,只是添加了新的边缘它
考虑从句子生成单词级别图的例子: -
句子-1: - 梅林是一只大猫。
句子1创建的图表边缘来自 Merlyn-> ,这非常好。
句子-2: - 梅林以前喝牛奶。句子2创建的图表有两条边缘来自 Merlyn->使用而 Merlyn->
句子2应该只有单边 Merlyn->使用 提前致谢
答案 0 :(得分:3)
所有节点变量都应该是实例级变量而不是类级变量。
class Node(object):
def __init__(self):
self.label = ""
self.nexts = ()
self.prevs = ()
self.pos_tag = ""
self.visited = False
self.score = 0