d3 ontology

This commit is contained in:
j 2018-05-31 16:59:17 +02:00
commit 6a8450e773
6 changed files with 341 additions and 0 deletions

57
ontology/ontology.json Normal file
View file

@ -0,0 +1,57 @@
{
"architecture": {
"rhythmanalysis": {
"circadian": {
"season": {},
"time of day": {},
"body rhythm": {},
"weather": {}
},
"the everyday": {
"activity": {},
"animal": {},
"architectural atmosphere": {},
"architectural element": {},
"architectural element action": {},
"architectural element state": {},
"bio": {},
"building material": {},
"building type": {},
"camera height and angle": {},
"camera movement": {},
"clothing": {},
"container": {},
"cookware": {},
"decor": {},
"dramatic context": {},
"element": {},
"filming direction": {},
"food and drink": {},
"furniture": {},
"homeware": {},
"lived space": {},
"location": {},
"miscellaneous": {},
"occupancy": {},
"place": {},
"scene tone": {},
"tableware": {},
"type": {}
}
}
},
"cinema": {
"mise en scene": {
"dramatic mood": {},
"character": {}
},
"mise en cadre": {
"shoot": {},
"shot type": {},
"line": {},
"shape": {},
"space": {}
}
},
"other": {}
}

68
ontology/update.py Executable file
View file

@ -0,0 +1,68 @@
#!/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)

46
ontology/update_keywords.py Executable file
View file

@ -0,0 +1,46 @@
#!/usr/bin/python3
import getpass
import json
import sys
import ox
import ox.web.auth
site = 'pandora.cinemusespace.com'
api = ox.API('https://%s/api/' % site)
update = False
try:
credentials = ox.web.auth.get(site)
except:
credentials = {}
print('Please provide your username and password for %s:' % site)
credentials['username'] = input('Username: ')
credentials['password'] = getpass.getpass('Password: ')
update = True
r = api.signin(**credentials)
if 'errors' in r.get('data', {}):
for kv in r['data']['errors'].items():
print('%s: %s' % kv)
sys.exit(1)
if update:
ox.web.auth.update(site, credentials)
keywords = set()
for annotation in api.findAnnotations({
'query': {
'conditions': [{
'key': 'layer',
'value': 'keywords',
'operator': '=='
}],
'operator': '&'
},
'keys': ['id', 'in', 'out', 'value', 'user', 'created'],
'range': [0, 500000]
})['data']['items']:
keywords.add(annotation['value'])
with open('keywords.json', 'w') as fd:
json.dump(list(sorted(keywords)), fd, indent=4)