本文介绍了从networkx图提取边缘数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要为显示的每个图提取出边缘信息.数据采用utf-8格式.
I need to extract out the edge information for each graph displayed. The data is in utf-8 form.
为文档中的每个句子显示图形.因此,现在必须从图中提取信息.要提取的句子与图中合并的某些句子不同.该图说明了输出,其中包含一些图形(每个图形最多包含三个节点)
The graphs are displayed for each sentence in the document. So now the information from the graph has to be extracted out. The sentences to be extracted out won't be the same as some sentences are merged in the graph. The image explains the output which has some graphs (each graph has a maximum of three nodes)
for s in subject_list:
if s is not "":
graph.add_node(s)
labels[s] = s
for o in object_list:
if o is not "":
graph.add_node(o)
labels[b] = b
for v in verb_list:
if v is not "":
graph.add_node(v)
labels[v] = v
for (s, o, v) in zip(subject_list, object_list, verb_list):
if s and o is not "":
graph.add_edge(s, o)
if o and v is not "":
graph.add_edge(o, v)
pos=nx.spring_layout(graph)
nx.draw(graph, with_labels = True, font_family = "Nirmala UI", node_size = 40, font_size = 9 ,node_color = "darkblue")
pl.show()
g=[]
for component in nx.connected_components(graph):
# Each component is the set of nodes
#print(component)
# Filter all edges in graph: we keep only that are in the component
g=(list(
filter(
lambda x: x[0] in component and x[1] in component,
graph.edges
)
))
print g
ls=[]
for x in g[0]:
ls.append(x)
print (x)
![connected components output][1]
[1]: https://i.stack.imgur.com/iynw1.png
推荐答案
您需要 connected_components 函数:
for component in nx.connected_components(graph):
# Each component is the set of nodes
print(component)
# Filter all edges in graph: we keep only that are in the component
print(list(
filter(
lambda x: x[0] in component and x[1] in component,
graph.edges
)
))
这篇关于从networkx图提取边缘数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!