【问题标题】:Merge two dot graphs at a common node in python在python中的一个公共节点处合并两个点图
【发布时间】:2017-02-04 23:41:09
【问题描述】:

以下两句的依赖解析输出(使用Stanford Parser)如下。

句子 1 - John 是一名计算机科学家

点格式 -

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 - 约翰有一个名叫玛丽的姐姐。

点格式 -

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这样的图形,

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

但似乎没有在GraphvizNetworkx 中合并图表的功能。那么如何做到这一点呢?

【问题讨论】:

    标签: python graph nlp dot


    【解决方案1】:

    合并两个图的方法是定义一个有两个子图的有向图。

    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)
    

    【讨论】:

    • 如果你想看到更多关于这个问题的讨论:stackoverflow.com/questions/2012036/…
    • 如何找到两个图之间的公共节点?有什么穿越技术吗?并且在合并时,必须更改引用,以便没有冗余,如何做到这一点?
    • 查看公共节点:查看定义链接的位置,假设我们想查看链接到节点 2 的内容,我们看到 cluster0 (5 -> 2) 和 cluster1 (2 - > 5, 2 -> 1)
    • Graphviz 是一个图形可视化器,它不是用来执行搜索或寻找共性的。一个想法可能是在 python 中使用集合论;有两组a={cluster0}b={cluster1}。然后你可以应用a.intersection(b) 方法。在你把它变成这个字符串源代码格式之前。
    • 由于我使用的是斯坦福依赖解析器,我无法控制dot 格式化输出。
    猜你喜欢
    • 2017-05-04
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 2012-07-28
    • 2020-05-28
    相关资源
    最近更新 更多