1

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

Sentence 1 - John is a computer scientist

Dot format -

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"]
}

Graph - enter image description here

Sentence 2 - John has an elder sister named Mary.

Dot Format -

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)"]
}

Graph - enter image description here

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")

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

Riken Shah
  • 3,022
  • 5
  • 29
  • 56

1 Answers1

0

enter image description here

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)
SkinnyTok
  • 23
  • 1
  • 6
  • if you want to see a more through discussion on this matter: http://stackoverflow.com/questions/2012036/graphviz-how-to-connect-subgraphs – SkinnyTok Feb 04 '17 at 15:42
  • How do I find the common nodes between two graphs? Is there some traversal technique? And upon merging, reference must be changed so that there are no redundancies , how can this be done? – Riken Shah Feb 05 '17 at 04:15
  • To see the common nodes: look at where you define the link, so lets say we want to see what is linked to node 2, we see cluster0 (5 -> 2) and cluster1 (2 -> 5, 2 -> 1) – SkinnyTok Feb 05 '17 at 08:36
  • Graphviz is a graph visualiser it wasn't made to perform searches or look for commonality. One idea could be using set theory in python; having two sets `a={cluster0}` and `b={cluster1}`. Then you can apply the `a.intersection(b)` method. Before you turn it into this string source code format. – SkinnyTok Feb 05 '17 at 08:47
  • Since I am using Stanford dependency parser, I have no control on the `dot` formatted output. – Riken Shah Feb 05 '17 at 08:49
  • It would be if you had a script to read in the `dot` file, and create a tree structure in the script. Then perform a commonality check that way. Personally, I would use `Python` to convert it to a `Prolog` readable format. Then query with `Prolog`. Why `Prolog`? Because it stores data in a tree-like structure. And when you query it by default performs a depth first search. So minimal amount of algorithms have to be implemented. – SkinnyTok Feb 05 '17 at 09:14