Ragheb Alghezi
Ragheb Alghezi

Reputation: 45

Allowing Duplicate Nodes in Networkx / Preventing a node from having two Parents in a Tree graph

I am trying to represent a constituency parse tree as networkx DiGraph. I have a list of binary tuples showing the edge between the nodes:

[('S', 'NP'), ('S', 'VP'), ('NP', "'I'"), ('VP', 'V'), ('VP', 'NP'), ('V', "'saw'"), ('NP', "'him'")]

So far, I have managed to represent it as: Here's what I have

using the following commands:

tree2graph = nx.DiGraph()
tree2graph.add_edges_from(parsed_tree)
p= nx.drawing.nx_pydot.to_pydot(tree2graph)

However, networkx removes the duplicate nodes, and that made an undesirable effect. I would like to be like: This

i.e. How would I prevent the node NP from having two parents by allow two copies?

Upvotes: 2

Views: 1214

Answers (1)

zohar.kom
zohar.kom

Reputation: 1845

The Node identifiers in your graph are the strings (e.g., 'NP'). Since you can't have two different nodes with the same identifier, different instances of the same string collide. You can solve it by separating the node's identifier and node's string. Keep a unique integer to identify each node, and map it to the appropriate string. For example:

import networkx as nx
import matplotlib.pyplot as plt

parsed_tree = [(0,1), (0,2), (1,3), (2,4), (2,5), (4,6), (5,7)]
id_to_str = {0: 'S', 1: 'NP', 2: 'VP', 3: 'I', 4: 'V', 5: 'NP', 6: "'saw'", 7: "'him'"}

tree2graph = nx.DiGraph()
tree2graph.add_edges_from(parsed_tree)
for node in tree2graph.nodes():
    tree2graph.nodes[node]['label'] = id_to_str[node]
pos = nx.nx_pydot.pydot_layout(tree2graph, prog='dot')
nx.draw_networkx(tree2graph, pos=pos, arrows= True, with_labels=True, labels=id_to_str)
plt.show()

This results with:

enter image description here

Upvotes: 4

Related Questions