-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
137 lines (116 loc) · 4.07 KB
/
demo.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Shuai Wang
# VU Amsterdam
# DEMO for SUBMASSIVE
from hdt import HDTDocument, IdentifierPosition
import sys
import networkx as nx
import matplotlib.pyplot as plt
import argparse
import scipy
import tldextract
import rdflib
subClassOf = "http://www.w3.org/2000/01/rdf-schema#subClassOf"
PATH_LOD = "./refined-subC-SA.hdt"
# PATH_LOD = "./subC-all.hdt"
hdt_file = HDTDocument(PATH_LOD)
query = ''
graph = nx.DiGraph()
def get_domain_and_label(t):
domain = tldextract.extract(t).domain
name1 = t.rsplit('/', 1)[-1]
name2 = t.rsplit('#', 1)[-1]
if len(name2) < len(name1):
return (domain, name2)
else:
return (domain, name1)
def find_all_superclass():
query_entity = query
last_size = len(graph.edges)
# print ('entity = ', query_entity)
# print ('cardi = ', cardinality)
graph.add_node(query_entity)
nodes = list(graph.nodes).copy()
for n in nodes:
triples, cardinality = hdt_file.search_triples(n, subClassOf, '')
for (s, p, o) in triples:
# print ('subClassOf: ', o, '\n')
graph.add_edge(s, o)
# O.append(o)
#do-while loop:
while len(graph.edges) > last_size:
last_size = len(graph.edges)
nodes = list(graph.nodes).copy()
for n in nodes:
triples, cardinality = hdt_file.search_triples(n, subClassOf, '')
for (s, p, o) in triples:
# print ('subClassOf: ', o, '\n')
graph.add_edge(s, o)
def find_immediate_superclass():
query_entity = query
print ('entity = ', query_entity)
triples, cardinality = hdt_file.search_triples(query_entity, subClassOf, '')
print ('cardi = ', cardinality)
O = []
graph.add_node(query_entity)
for (s, p, o) in triples:
print ('\tsubClassOf: ', o, '\n')
graph.add_edge(query_entity, o)
O.append(o)
return O
def plot_graph(file_name='output'):
pos = nx.kamada_kawai_layout(graph)
# pos = nx.spring_layout(graph)
# pos = nx.spectral_layout(graph)
# pos = nx.spiral_layout(graph)
nx.draw_networkx_nodes(graph, pos,
nodelist = graph.nodes,
node_color = 'g',
node_size=5,
alpha=0.8)
nx.draw_networkx_nodes(graph, pos,
nodelist = [query],
node_color = 'b',
node_size=10,
alpha=0.8)
nx.draw_networkx_edges(graph, pos,
edgelist=graph.edges,
width=1,alpha=0.5,edge_color='r')
labels = {}
for n in graph.nodes:
(domain,name) = get_domain_and_label(n)
labels[n] = domain + ':' + name
nx.draw_networkx_labels(graph,pos,labels,font_size=5)
plt.savefig(file_name + '.png')
plt.savefig(file_name + '.svg')
plt.close()
def export():
# create a Graph
g = rdflib.Graph()
for (s, o) in graph.edges:
g.add((rdflib.URIRef(s), rdflib.URIRef(subClassOf), rdflib.URIRef(o)))
# print the number of "triples" in the Graph
# print("graph has {} statements.".format(len(g)))
# print ('should be', len(graph.edges))
# print out the entire Graph in the RDF Turtle format
# print()
file = open("output.ttl", mode="w")
file.write(g.serialize(format="turtle").decode("utf-8"))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--input', action='store', required=True, help='enable the long listing formatxx')
parser.add_argument('--plot', action='store_true', help='enable the long listing format')
parser.add_argument('--all', action='store_true', help='enable the long listing format')
args = parser.parse_args()
# print(args.input)
# print (args.i)
#
query = args.input
if args.all == True:
find_all_superclass()
else:
find_immediate_superclass()
if args.plot == True:
print ('also output the plot')
plot_graph()
# Finally export the graph
export()