def reconcile_op(query): """Reconcile operation for a single query.""" name = query.get('query', '').strip() size = int(query.get('limit', '5')) schemas = implied_schemas(query.get('type')) types = get_freebase_types() # TODO: jurisdiction_code etc. # for p in query.get('properties', []): # q[p.get('pid')] = p.get('v') matches = [] suggested = suggest_entities(name, schemas=schemas, size=size) for entity in suggested.get('results'): types = [t for t in types if entity['$schema'] == t['id']] matches.append({ 'id': entity.get('id'), 'name': entity.get('name'), 'type': types, 'score': entity.get('score') * 10, 'uri': entity_link(entity.get('id')), 'match': entity['match'] }) log.info("Reconciled: %r -> %d matches", name, len(matches)) return { 'result': matches, 'num': len(matches) }
def suggest_entity(): """Suggest API, emulates Google Refine API.""" prefix = request.args.get('prefix', '').lower() schemas = request.args.getlist('type') types = get_freebase_types() matches = [] suggested = suggest_entities(prefix, request.authz, schemas=schemas) for entity in suggested.get('results'): types_ = [t for t in types if entity['$schema'] == t['id']] matches.append({ 'quid': entity.get('id'), 'id': entity.get('id'), 'name': entity.get('name'), 'n:type': types_[0], 'type': [types_[0]['name']], 'r:score': entity.get('score'), }) return jsonify({ "code": "/api/status/ok", "status": "200 OK", "prefix": request.args.get('prefix', ''), "result": matches })
def suggest_entity(): """Suggest API, emulates Google Refine API.""" # See: https://github.com/OpenRefine/OpenRefine/wiki/Reconciliation-Service-API prefix = request.args.get('prefix', '').lower() schemas = request.args.getlist('type') schemas = list(set([implied_schemas(s) for s in schemas])) types = get_freebase_types() matches = [] for entity in suggest_entities(prefix, schemas=schemas).get('results'): types_ = [t for t in types if entity['$schema'] == t['id']] matches.append({ 'quid': entity.get('id'), 'id': entity.get('id'), 'name': entity.get('name'), 'n:type': types_[0], 'type': [types[0]['name']], 'r:score': entity.get('score'), }) return jsonify({ "code": "/api/status/ok", "status": "200 OK", "prefix": request.args.get('prefix', ''), "result": matches })