python-2.7 - 从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
解决方案
您需要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
)
))
推荐阅读
- jquery - 如果存在未定义的内联背景图像,请使用 jQuery 删除 DIV
- c# - 多个下拉框不拾取新元素(Selenium,C#)
- sql - 仅将某些列从一个表复制到另一个表并插入 defa
- javascript - 我收到错误消息:无法注册 ServiceWorker:获取脚本时收到错误的 HTTP 响应代码 (404)
- libgdx - 当我将代码编译为 HTML5 时,它并不总是接受新的更改
- c++ - do while 中的无限循环
- python - 调整字典中的数据,然后绘制它
- python - 从 Pandas 形式的 iris 数据集到 sk-learn 形式的有效方法?
- postgresql - pgpool2 看门狗在尝试使用 docker-compose 设置相互连接时崩溃
- vba - 从外部 excel VBA 更新电子表格