-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.py
82 lines (69 loc) · 2.34 KB
/
app.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from flask import Flask, request
from flask_cors import CORS
from flask_jsonpify import jsonify
from abbrmap import abbrmap as tagmap
import json
import brickschema
import re
app = Flask(__name__)
CORS(app)
metadata = {
"name": "Brick Reconciliation Service",
"defaultTypes": [
{"id": "EquipmentClass", "name": "EquipmentClass"},
{"id": "PointClass", "name": "PointClass"},
{"id": "BrickClass", "name": "BrickClass"}
]
}
inf = brickschema.inference.TagInferenceSession(approximate=True)
def flatten(lol):
"""flatten a list of lists"""
return [x for sl in lol for x in sl]
def resolve(q):
"""
q has fields:
- query: string of the label that needs to be converted to a Brick type
- type: optional list of 'types' (e.g. "PointClass" above)
- limit: optional limit on # of returned candidates (default to 10)
- properties: optional map of property idents to values
- type_strict: [any, all, should] for strictness on the types returned
"""
limit = int(q.get('limit', 10))
# break query up into potential tags
tags = map(str.lower, re.split(r'[.:\-_ ]', q.get('query', '')))
tags = list(tags)
brick_tags = flatten([tagmap.get(tag.lower(), [tag]) for tag in tags])
if q.get('type') == 'PointClass':
brick_tags += ['Point']
elif q.get('type') == 'EquipmentClass':
brick_tags += ['Equipment']
else:
q['type'] = 'BrickClass'
q['id'] = 'BrickClass'
res = []
most_likely, leftover = inf.most_likely_tagsets(brick_tags, limit)
for ml in most_likely:
res.append({
'id': q['query'],
'name': ml,
'score': (len(brick_tags) - len(leftover)) / len(brick_tags),
'match': len(leftover) == 0,
'type': [{"id": q.get("type"), "name": q.get("type")}],
})
print('returning', res)
return res
@app.route("/reconcile", methods=["POST", "GET"])
def reconcile():
if request.method == "GET":
queries = json.loads(request.args.get("queries", "[]"))
else:
queries = json.loads(request.form.get("queries", "[]"))
print(queries)
if queries:
results = {}
for qid, q in queries.items():
results[qid] = {'result': resolve(q)}
return jsonify(results)
return jsonify(metadata)
if __name__ == "__main__":
app.run()