#!/usr/bin/python3 import json import os from collections import defaultdict base = os.path.abspath(os.path.dirname(__file__)) keywords = json.load(open(os.path.join(base, 'keywords.json'))) ontology = json.load(open(os.path.join(base, 'ontology.json'))) def find_path(parent, root=None, path=None): if root is None: root = ontology if path is None: path = [] if parent == 'location': return ['film', 'architecture', 'rhythmanalysis', 'the everyday', 'environment', 'location'] 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, parent=None): children_ = [get_node(child, children[child], name) for child in children] children_.sort(key=lambda c: c['name']) node = { "size": len(children) + 100, "name": name, "children": children_ } if not node['children']: del node['children'] key = '%s: %s' % (parent, name) if key in keywords: node['size'] = keywords[key] return node def render_children(root, indent=0): txt = ('\t' * indent) + root['name'] if 'children' in root: parts = '' for child in root['children']: parts += '\n' + render_children(child, indent+1) txt += '\n'.join([('\t' * indent) + p for p in parts.split('\n')]) return '\n'.join([l.rstrip() for l in txt.split('\n')]) if __name__ == '__main__': os.chdir(base) tree = defaultdict(dict) ontology_txt = '' for keyword in keywords: if ': ' not in keyword: parent = 'other' child = keyword else: parent, child = keyword.split(': ', 1) 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: if parent not in tree['missing']: tree['missing'][parent] = {} tree['missing'][parent][child] = {} #print('missing root - %s: %s' % (parent, child)) #print(json.dumps(tree, indent=4, sort_keys=True)) sized_ontology = { "size": len(tree), "name": "CineUrban", "children": [] } for name in tree: children = tree[name] child = get_node(name, tree[name], name) sized_ontology['children'].append(child) ontology_txt = render_children(sized_ontology) with open('../static/ontology/sized_ontology.json', 'w') as fd: json.dump(sized_ontology, fd, indent=4, sort_keys=True) with open('../static/ontology/ontology.txt', 'w') as fd: fd.write(ontology_txt)