def merge_suggestions(request): # TODO: test req = request.json entity_type_primary = req.get("entity_type_primary") or "Transcription" entity_type_secondary = req.get("entity_type_secondary") or "Translation" threshold = req["threshold"] or 0.2 levenstein = req["levenstein"] or 1 client_id = req["client_id"] object_id = req["object_id"] lexes = list( DBSession.query(LexicalEntry) .filter_by(parent_client_id=client_id, parent_object_id=object_id, marked_for_deletion=False) .all() ) if not lexes: return json.dumps([]) # first_persp = json.loads(lexes[0].additional_metadata)['came_from'] lexes_1 = [o.track(False) for o in lexes] remove_deleted(lexes_1) lexes_2 = list(lexes_1) def parse_response(elem): words = filter( lambda x: x["entity_type"] == entity_type_primary and not x["marked_for_deletion"], elem["contains"] ) words = map(lambda x: x["content"], words) trans = filter( lambda x: x["entity_type"] == entity_type_secondary and not x["marked_for_deletion"], elem["contains"] ) trans = map(lambda x: x["content"], trans) tuples_res = [ (i_word, i_trans, (elem["client_id"], elem["object_id"])) for i_word in words for i_trans in trans ] return tuples_res tuples_1 = [parse_response(i) for i in lexes_1] tuples_1 = [item for sublist in tuples_1 for item in sublist] tuples_2 = [parse_response(i) for i in lexes_2] tuples_2 = [item for sublist in tuples_2 for item in sublist] def get_dict(elem): return { "suggestion": [ {"lexical_entry_client_id": elem[0][0], "lexical_entry_object_id": elem[0][1]}, {"lexical_entry_client_id": elem[1][0], "lexical_entry_object_id": elem[1][1]}, ], "confidence": elem[2], } if (not tuples_1) or (not tuples_2): return {} results = [get_dict(i) for i in mergeDicts(tuples_1, tuples_2, float(threshold), int(levenstein))] results = sorted(results, key=lambda k: k["confidence"]) return results
def merge_suggestions_old(request): subreq = Request.blank('/dictionary/' + request.matchdict.get('dictionary_client_id_1') + '/' + request.matchdict.get('dictionary_object_id_1') + '/perspective/' + request.matchdict.get('perspective_client_id_1') + '/' + request.matchdict.get('perspective_object_id_1') + '/all') subreq.method = 'GET' response_1 = request.invoke_subrequest(subreq).json subreq = Request.blank('/dictionary/' + request.matchdict.get('dictionary_client_id_2') + '/' + request.matchdict.get('dictionary_object_id_2') + '/perspective/' + request.matchdict.get('perspective_client_id_2') + '/' + request.matchdict.get('perspective_object_id_2') + '/all') subreq.method = 'GET' response_2 = request.invoke_subrequest(subreq).json #entity_type_primary = 'Word' #entity_type_secondary = 'Transcription' #threshold = 0.2 #levenstein = 2 entity_type_primary = request.matchdict.get('entity_type_primary') entity_type_secondary = request.matchdict.get('entity_type_secondary') threshold = request.matchdict.get('threshold') levenstein = request.matchdict.get('levenstein') def parse_response(elem): words = filter(lambda x: x['entity_type'] == entity_type_primary and not x['marked_for_deletion'], elem['contains']) words = map(lambda x: x['content'], words) trans = filter(lambda x: x['entity_type'] == entity_type_secondary and not x['marked_for_deletion'], elem['contains']) trans = map(lambda x: x['content'], trans) tuples_res = [(i_word, i_trans, (elem['client_id'], elem['object_id'])) for i_word in words for i_trans in trans] return tuples_res tuples_1 = [parse_response(i) for i in response_1['lexical_entries']] tuples_1 = [item for sublist in tuples_1 for item in sublist] tuples_2 = [parse_response(i) for i in response_2['lexical_entries']] tuples_2 = [item for sublist in tuples_2 for item in sublist] def get_dict(elem): return {'suggestion': [ {'lexical_entry_client_id': elem[0][0], 'lexical_entry_object_id': elem[0][1]}, {'lexical_entry_client_id': elem[1][0], 'lexical_entry_object_id': elem[1][1]} ], 'confidence': elem[2]} results = [get_dict(i) for i in mergeDicts(tuples_1, tuples_2, float(threshold), int(levenstein))] return json.dumps(results)
def merge_suggestions_old(request): subreq = Request.blank( "/dictionary/" + request.matchdict.get("dictionary_client_id_1") + "/" + request.matchdict.get("dictionary_object_id_1") + "/perspective/" + request.matchdict.get("perspective_client_id_1") + "/" + request.matchdict.get("perspective_object_id_1") + "/all" ) subreq.method = "GET" response_1 = request.invoke_subrequest(subreq).json subreq = Request.blank( "/dictionary/" + request.matchdict.get("dictionary_client_id_2") + "/" + request.matchdict.get("dictionary_object_id_2") + "/perspective/" + request.matchdict.get("perspective_client_id_2") + "/" + request.matchdict.get("perspective_object_id_2") + "/all" ) subreq.method = "GET" response_2 = request.invoke_subrequest(subreq).json # entity_type_primary = 'Word' # entity_type_secondary = 'Transcription' # threshold = 0.2 # levenstein = 2 entity_type_primary = request.matchdict.get("entity_type_primary") entity_type_secondary = request.matchdict.get("entity_type_secondary") threshold = request.matchdict.get("threshold") levenstein = request.matchdict.get("levenstein") def parse_response(elem): words = filter( lambda x: x["entity_type"] == entity_type_primary and not x["marked_for_deletion"], elem["contains"] ) words = map(lambda x: x["content"], words) trans = filter( lambda x: x["entity_type"] == entity_type_secondary and not x["marked_for_deletion"], elem["contains"] ) trans = map(lambda x: x["content"], trans) tuples_res = [ (i_word, i_trans, (elem["client_id"], elem["object_id"])) for i_word in words for i_trans in trans ] return tuples_res tuples_1 = [parse_response(i) for i in response_1["lexical_entries"]] tuples_1 = [item for sublist in tuples_1 for item in sublist] tuples_2 = [parse_response(i) for i in response_2["lexical_entries"]] tuples_2 = [item for sublist in tuples_2 for item in sublist] def get_dict(elem): return { "suggestion": [ {"lexical_entry_client_id": elem[0][0], "lexical_entry_object_id": elem[0][1]}, {"lexical_entry_client_id": elem[1][0], "lexical_entry_object_id": elem[1][1]}, ], "confidence": elem[2], } results = [get_dict(i) for i in mergeDicts(tuples_1, tuples_2, float(threshold), int(levenstein))] return json.dumps(results)