-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkg-generator.py
33 lines (26 loc) · 1.24 KB
/
kg-generator.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
from pyld import jsonld
import json
from argparse import ArgumentParser
argparser = ArgumentParser()
argparser.add_argument('--modelfile', '-M', type=str, help='Relative path to a model\'s filled-out metadata JSON file')
argparser.add_argument('--format_output', '-F', default='jsonld', type=str, help='Output format: "jsonld" (default) or "nquads"')
args = argparser.parse_args()
assert args.format_output in ['jsonld', 'nquads'], 'The supplied format ('+args.format_output+') should be "jsonld" (default) or "nquads"!'
with open(args.modelfile) as file:
modeljson = file.read()
with open('jsonld-context.json') as file:
contextjson = file.read()
context = json.loads(contextjson)
model = json.loads(modeljson)
merge = dict(context, **model)
expanded = jsonld.expand(merge)
if args.format_output == 'nquads':
normalized = jsonld.normalize(expanded, {'algorithm': 'URDNA2015', 'format': 'application/n-quads'})
with open('graph-out.nq', 'w') as outfile:
outfile.write(normalized)
print('Wrote output to: "graph-out.nq"')
else: #default: args.output_format == 'jsonld'
with open('graph-out.jsonld', 'w') as outfile:
outfile.write(json.dumps(expanded))
print('Wrote output to: "graph-out.jsonld"')
print('fin')