get keyword size
This commit is contained in:
parent
f81fa3a7d6
commit
de5368e47a
2 changed files with 20 additions and 20 deletions
|
@ -3,10 +3,15 @@ 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 == None:
|
||||
if path is None:
|
||||
path = []
|
||||
for key in root:
|
||||
if key == parent:
|
||||
|
@ -16,31 +21,26 @@ def find_path(parent, root=None, path=None):
|
|||
if r:
|
||||
return r
|
||||
|
||||
def get_node(name, children):
|
||||
def get_node(name, children, parent=None):
|
||||
node = {
|
||||
"size": len(children) + 100,
|
||||
"name": name,
|
||||
"children": [get_node(child, children[child]) for child in children]
|
||||
"children": [get_node(child, children[child], name) for child in children]
|
||||
}
|
||||
if not node['children']:
|
||||
del node['children']
|
||||
key = '%s: %s' % (parent, name)
|
||||
if key in keywords:
|
||||
node['size'] = keywords[key]
|
||||
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:
|
||||
|
@ -61,7 +61,7 @@ if __name__ == '__main__':
|
|||
}
|
||||
for name in tree:
|
||||
children = tree[name]
|
||||
child = get_node(name, tree[name])
|
||||
child = get_node(name, tree[name], name)
|
||||
sized_ontology['children'].append(child)
|
||||
|
||||
with open('../static/ontology/sized_ontology.json', 'w') as fd:
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
import getpass
|
||||
import collections
|
||||
import json
|
||||
import sys
|
||||
|
||||
import ox
|
||||
import ox.api
|
||||
|
@ -10,7 +8,7 @@ import ox.api
|
|||
site = 'pandora.cinemusespace.com'
|
||||
api = ox.api.signin('https://%s/api/' % site)
|
||||
|
||||
keywords = set()
|
||||
keywords = collections.Counter()
|
||||
for annotation in api.findAnnotations({
|
||||
'query': {
|
||||
'conditions': [{
|
||||
|
@ -23,8 +21,10 @@ for annotation in api.findAnnotations({
|
|||
'keys': ['id', 'in', 'out', 'value', 'user', 'created'],
|
||||
'range': [0, 500000]
|
||||
})['data']['items']:
|
||||
keywords.add(annotation['value'])
|
||||
|
||||
keyword = annotation['value']
|
||||
if ': ' not in keyword:
|
||||
keyword = 'other: ' + keyword
|
||||
keywords[keyword] += 1
|
||||
|
||||
with open('keywords.json', 'w') as fd:
|
||||
json.dump(list(sorted(keywords)), fd, indent=4)
|
||||
json.dump(keywords, fd, indent=4, ensure_ascii=False, sort_keys=True)
|
||||
|
|
Loading…
Reference in a new issue