在python中的一个公共节点上合并两个点图 [英] Merge two dot graphs at a common node in python

查看:150
本文介绍了在python中的一个公共节点上合并两个点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下两个句子的依存关系分析输出(使用Stanford Parser)如下.

The dependency-parsed output (using Stanford Parser) of the following two sentences are as follows.

句子1-约翰是计算机科学家

Sentence 1 - John is a computer scientist

点格式-

digraph G{
edge [dir=forward]
node [shape=plaintext]

0 [label="0 (None)"]
0 -> 5 [label="root"]
1 [label="1 (John)"]
2 [label="2 (is)"]
3 [label="3 (a)"]
4 [label="4 (computer)"]
5 [label="5 (scientist)"]
5 -> 2 [label="cop"]
5 -> 4 [label="compound"]
5 -> 3 [label="det"]
5 -> 1 [label="nsubj"]
}

图-

句子2-约翰有一个姐姐叫玛丽.

Sentence 2 - John has an elder sister named Mary.

点格式-

digraph G{
edge [dir=forward]
node [shape=plaintext]

0 [label="0 (None)"]
0 -> 2 [label="root"]
1 [label="1 (John)"]
2 [label="2 (has)"]
2 -> 5 [label="dobj"]
2 -> 1 [label="nsubj"]
3 [label="3 (an)"]
4 [label="4 (elder)"]
5 [label="5 (sister)"]
5 -> 6 [label="acl"]
5 -> 3 [label="det"]
5 -> 4 [label="amod"]
6 [label="6 (named)"]
6 -> 7 [label="dobj"]
7 [label="7 (Mary)"]
}

图-

现在,我想在公共节点John上合并这些图.我目前正在使用graphviz像这样导入dot图,

Now I want to merge these graphs at a common node, John. I am currently using graphviz to import dot graph like this,

from graphviz import Source
s = Source(dotGraph, filename=filepath, format="png")

但是似乎没有功能可以合并GraphvizNetworkx中的图形.那怎么办呢?

But there seems to be no functionality to merge graphs in Graphviz, or Networkx. So how can this be done?

推荐答案

合并两个图的方法是定义一个具有两个子图的单个图.

The way to merge the two graphs would be defining a single digraph having two subgraphs.

from graphviz import Source

clusters = """
digraph G{

subgraph cluster0 {
    edge [dir=forward]
    node [shape=plaintext]

    0 [label="0 (None)"]
    0 -> 5 [label="root"]
    1 [label="1 (John)"]
    2 [label="2 (is)"]
    3 [label="3 (a)"]
    4 [label="4 (computer)"]
    5 [label="5 (scientist)"]
    5 -> 2 [label="cop"]
    5 -> 4 [label="compound"]
    5 -> 3 [label="det"]
    5 -> 1 [label="nsubj"]
}

subgraph cluster1 {
edge [dir=forward]
node [shape=plaintext]

0 [label="0 (None)"]
0 -> 2 [label="root"]
1 [label="1 (John)"]
2 [label="2 (has)"]
2 -> 5 [label="dobj"]
2 -> 1 [label="nsubj"]
3 [label="3 (an)"]
4 [label="4 (elder)"]
5 [label="5 (sister)"]
5 -> 6 [label="acl"]
5 -> 3 [label="det"]
5 -> 4 [label="amod"]
6 [label="6 (named)"]
6 -> 7 [label="dobj"]
7 [label="7 (Mary)"]
}
}
"""



src = Source(clusters, format='png')
src.render("graphing1", view=True)

这篇关于在python中的一个公共节点上合并两个点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆