def merge_node(path,new_path):
g = nx.read_gml(path)
nodes = [n for n,d in g.out_degree().items() if d==1]
for node in nodes:
if not node in g.nodes():
continue
if g.in_degree(node) != 1:
continue
p = g.successors(node)
#print p
#dict = g.in_degree(p)
#print dict[p]
#print g.in_degree(p)[p[0]]
if g.in_degree(p)[p[0]] == 1:
text1 = g.node[node]["text"]
text1 = remove_last_jump(text1)
text2 = g.node[p[0]]["text"]
#print text1
#print text2
new_text = text1 + ',' + text2
#print new_text
nns = g.successors(p[0])
g.node[node]["text"] = new_text
for n in nns:
g.add_edge(node, n)
g.remove_node(p[0])
nx.write_gml(g, new_path)
return nx.number_of_nodes(g)
评论列表
文章目录