-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrdf_graph_utils.py
56 lines (44 loc) · 1.42 KB
/
rdf_graph_utils.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
48
49
50
51
52
53
54
55
56
import os
from time import time
import networkx as nx
import matplotlib.pyplot as plt
def rdf_to_plot(graph):
nx_graph = nx.DiGraph()
plt.figure(figsize=(30, 30))
for (s, p, o) in graph:
s_n = s.split('/')[-1]
p_n = p.split('/')[-1]
o_n = o.split('/')[-1]
nx_graph.add_node(s_n, name=s_n, pred=False)
nx_graph.add_node(o_n, name=o_n, pred=False)
nx_graph.add_edge(s_n, o_n, name=p_n)
pos = nx.spring_layout(nx_graph)
edge_labels = nx.get_edge_attributes(nx_graph, 'name')
nx.draw_networkx_nodes(nx_graph, pos=pos)
nx.draw_networkx_edges(nx_graph, pos=pos)
nx.draw_networkx_labels(nx_graph, pos=pos)
nx.draw_networkx_edge_labels(nx_graph, pos, edge_labels=edge_labels)
plt.show()
# funzione per stampare file
def rdf_to_text(graph, path, format):
file = "output_" + str(int(time())) + '.' + format
path = os.path.join(path, file)
strings = graph.serialize(format=format)
print(strings)
with open(path, 'wb+') as f:
f.write(strings)
# estrae root node dal graph rdflib
def root_node(graph):
ss = set()
oo = set()
for (s, p, o) in graph:
ss.add(s)
oo.add(p)
oo.add(o)
ss = ss.difference(oo)
if len(ss) == 0:
raise Exception("Error: no root node found.")
elif len(ss) == 1:
return ss.pop()
else:
raise Exception("Error: multiple root nodes found.")