-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest.py
executable file
·105 lines (72 loc) · 2.21 KB
/
test.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
#!/usr/bin/env python
# encoding: utf-8
import json
import networkx as nx
import os
import rdflib
import sys
import tempfile
######################################################################
## NetworkX
LABEL = {}
def get_item_index (item):
global LABEL
item = str(item)
if item not in LABEL:
index = len(LABEL)
LABEL[item] = index
else:
index = LABEL[item]
return index
def make_nxgraph (graph):
g = nx.Graph()
for s, p, o in graph:
s_idx = get_item_index(s)
o_idx = get_item_index(o)
print(s_idx, str(s))
print(o_idx, str(o))
g.graph[s_idx] = str(s)
g.graph[o_idx] = str(o)
e = (s_idx, o_idx)
g.add_edge(*e)
g[s_idx][o_idx]["label"] = str(p)
print(g.graph)
def wrap_token (token):
if token.startswith("http"):
return "<{}>".format(token)
else:
return "\"{}\"".format(token)
PREAMBLE = """
@base <https://github.com/Coleridge-Initiative/adrf-onto/wiki/Vocabulary> .
@prefix cito: <http://purl.org/spar/cito/> .
@prefix dct: <http://purl.org/dc/terms/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
"""
if __name__ == "__main__":
# load the graph
filename = sys.argv[1]
graph = rdflib.Graph().parse(filename, format="n3")
# enumerate all of the relations
term = "dataset-11a95bfc951f7d23206a"
out_triples = set([])
for s, p, o in graph:
if s.endswith(term):
out_triples.add((s, p, o,))
elif o.endswith(term):
out_triples.add((s, p, o,))
## write to in-memory file
f = tempfile.NamedTemporaryFile(delete=False)
f.write(PREAMBLE.encode("utf-8"))
for s, p, o in out_triples:
line = "{} {} {} .\n".format(wrap_token(s), wrap_token(p), wrap_token(o))
f.write(line.encode("utf-8"))
f.close()
# serialize the graph as JSON-LD
with open("vocab.json", "r") as v:
context = json.load(v)
graph = rdflib.Graph().parse(f.name, format="n3")
os.unlink(f.name)
buf = graph.serialize(format="json-ld", context=context, indent=None)
print(buf)