-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproduce_graph_libs.py
47 lines (38 loc) · 1.68 KB
/
produce_graph_libs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import sng_parser
import dgl
import networkx as nx
import matplotlib.pyplot as plt
# Function that takes a sentence and produce the dgl graph of it
def sentence_to_dgl(sentence, visualize=True):
# Create the graph of the sentence
graph = sng_parser.parse(sentence)
# Create the networkx graph to visualize it
G = nx.Graph()
for rel in graph['relations']:
# Add the nodes (Just two because in each relation there is only one object and one subject)
G.add_node(graph['entities'][rel['subject']]['head'])
G.add_node(graph['entities'][rel['object']]['head'])
# Add the relation between the two
G.add_edge(graph['entities'][rel['subject']]['head'],graph['entities'][rel['object']]['head'], relation=rel['relation'])
if visualize:
pos = nx.spring_layout(G)
# Add the edge label
nx.draw_networkx_edge_labels(
G, pos,
edge_labels={(graph['entities'][rel['subject']]['head'], graph['entities'][rel['object']]['head']): rel['relation'] },
font_color='red'
)
if visualize:
nx.draw(G, with_labels= True)
plt.show()
# Return the dgl network
return dgl.from_networkx(G)
if __name__ == "__main__":
# Define the sentence
sentence = 'a river goes through this area with a forest on one bank and a road on the other side'
# Check the table to see the resulting graph for the sentence
print("Right relations: ")
# Parse the sentence
graph = sng_parser.parse(sentence)
sng_parser.tprint(graph)
result = sentence_to_dgl(sentence, visualize=True)