#!/usr/bin/python3 import json import os from collections import defaultdict def find_path(parent, root=None, path=None): if root is None: root = ontology if path == None: path = [] for key in root: if key == parent: return path + [key] elif root[key]: r = find_path(parent, root[key], path + [key]) if r: return r def get_node(name, children): node = { "size": len(children) + 100, "name": name, "children": [get_node(child, children[child]) for child in children] } if not node['children']: del node['children'] return node if __name__ == '__main__': base = os.path.abspath(os.path.dirname(__file__)) os.chdir(base) keywords = json.load(open('keywords.json')) ontology = json.load(open('ontology.json')) tree = defaultdict(dict) for keyword in keywords: if ': ' not in keyword: parent = 'other' child = keyword else: parent, child = keyword.split(': ') path = find_path(parent) if path: p = tree for part in path: if part not in p: p[part] = {} p = p[part] p[child] = {} else: print('missing root - %s: %s' % (parent, child)) #print(json.dumps(tree, indent=4, sort_keys=True)) sized_ontology = { "size": len(tree), "name": "CineMuseSpace", "children": [] } for name in tree: children = tree[name] child = get_node(name, tree[name]) sized_ontology['children'].append(child) with open('../static/ontology/sized_ontology.json', 'w') as fd: json.dump(sized_ontology, fd, indent=4, sort_keys=True)