首页 > 解决方案 > 从networkx图中提取边缘数据

问题描述

我需要为显示的每个图形提取边缘信息。数据为 utf-8 格式。

图表显示文档中的每个句子。所以现在必须从图中提取信息。提取出来的句子不会和图中一些句子合并的一样。该图像解释了具有一些图形的输出(每个图形最多具有三个节点)

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

标签: python-2.7matplotlibutf-8networkx

解决方案


您需要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
        )
    ))

推荐阅读