-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery_generator_service.py
executable file
·91 lines (77 loc) · 2.6 KB
/
query_generator_service.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
83
84
85
86
87
88
89
90
91
##############################################
# Author: -- (--)
# Description:
# Query generator web service
# Include: test web service and query generator service
##############################################
import json
import logging
import flask
from flask_cors import CORS
import concept_identification as CI
import geoparser
from ontology_mapping import MappingPipeline as Mapping
app = flask.Flask(__name__)
CORS(app)
app.config["DEBUG"] = True
typeMapping = Mapping()
propertyMapping = Mapping(type='attribute')
app = flask.Flask(__name__)
CORS(app)
app.config["DEBUG"] = True
@app.route('/testresolve', methods=['GET'])
def test_resolve():
results = {}
results['cafe'] = typeMapping.mapping('cafe')
results['population'] = propertyMapping.mapping('population')
results['London'] = CI.from_SOLR('London')
return results
@app.route('/resolve', methods=['POST'])
def resolve():
req_vals = flask.request.get_json()
print(req_vals)
if 'value' not in req_vals.keys() or 'key' not in req_vals.keys():
flask.abort(400)
key = str(req_vals.get('key'))
value = str(req_vals.get('value'))
result = None
thl = None
thg = None
try:
if key == 'type':
result = list(set(typeMapping.mapping(value, thl=0.55, thg=0.55)))
if len(result) == 0:
result = list(set(typeMapping.mapping(value, thl=0.45, thg=0.40)))
elif key == 'property':
result = list(set(propertyMapping.mapping(value, thl=0.8, thg=0.8)))
if len(result) == 0:
result = list(set(propertyMapping.mapping(value, thl=0.45, thg=0.40)))
elif key == 'toponym':
result = list(set(CI.from_SOLR(value)))
else:
logging.error('The key is invalid, it should "type", "property" or "toponym"')
flask.abort(400)
return json.dumps(result)
except:
flask.abort(400)
@app.route('/test', methods=['GET'])
def test():
result = geoparser.analyze('Where is the highest building in UK?')
return result
@app.route('/question', methods=['POST'])
def query_generator():
try:
req_vals = flask.request.get_json()
print(req_vals)
if not 'question' in req_vals.keys():
flask.abort(400)
question = str(req_vals.get('question'))
if 'execute' in req_vals.keys():
result = geoparser.analyze(question, executable=True)
else:
result = geoparser.analyze(question)
return json.dumps(result)
except:
flask.abort(400)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=1313)