Example #1
0
def get_relations(cursor, document_id, user_id, annotation_id_map,
                  show_predictions):
    from ltnserver.prediction import get_current_prediction_user
    current_prediction_user = get_current_prediction_user(
        user_id, show_predictions)
    cursor.execute(
        'SELECT P.ID, P.E1_ID, P.E2_ID, P.LABEL, T.CODE, TT.LABEL, '
        'T.GROUP_ID, T."GROUP", UD1.USER_ID, TT.ID '
        'FROM LTN_DEVELOP.PAIRS P '
        'LEFT OUTER JOIN LTN_DEVELOP.TASK_TYPES TT ON P.TYPE_ID = TT.ID '
        'JOIN LTN_DEVELOP.TYPES T ON TT.TYPE_ID = T.ID '
        'JOIN LTN_DEVELOP.ENTITIES E1 ON P.E1_ID = E1.ID AND P.DDI = 1 AND P.USER_DOC_ID = E1.USER_DOC_ID '
        'JOIN LTN_DEVELOP.ENTITIES E2 ON P.E2_ID = E2.ID AND P.DDI = 1 AND P.USER_DOC_ID = E2.USER_DOC_ID '
        'JOIN LTN_DEVELOP.USER_DOCUMENTS UD1 ON E1.USER_DOC_ID = UD1.ID AND UD1.DOCUMENT_ID = ? '
        'AND (UD1.USER_ID = ? OR UD1.USER_ID = ? OR UD1.VISIBILITY = 1) '
        'JOIN LTN_DEVELOP.USER_DOCUMENTS UD2 ON E2.USER_DOC_ID = UD2.ID AND UD2.DOCUMENT_ID = ? '
        'AND (UD2.USER_ID = ? OR UD2.USER_ID = ? OR UD2.VISIBILITY = 1)',
        (document_id, user_id, current_prediction_user, document_id, user_id,
         current_prediction_user))
    relations = []
    for result in cursor.fetchall():
        # the bioc library expects all attributes to be strings (and TextAE doesn't care)
        type_info = {
            "id": str(result[9]),
            "code": str(result[4]),
            "name": str(result[5]),
            "groupId": str(result[6]),
            "group": str(result[7]),
            "label": str(result[3])
        }
        relation = {}
        subj = str(result[1])
        obj = str(result[2])
        replacement_subj = annotation_id_map.get(subj)
        replacement_obj = annotation_id_map.get(obj)
        current_user_id = str(result[8])
        if replacement_subj is not None:
            if replacement_subj.get(current_user_id) is not None:
                subj = replacement_subj.get(current_user_id)
        if replacement_obj is not None:
            if replacement_obj.get(current_user_id) is not None:
                obj = replacement_obj.get(current_user_id)
        relation['id'] = str(result[0])
        relation['subj'] = subj
        relation['obj'] = obj
        relation['pred'] = type_info
        relations.append(relation)
    return relations
Example #2
0
def get_denotations_and_users(cursor, document_id, user_id, show_predictions):
    from ltnserver.prediction import get_current_prediction_user
    current_prediction_user = get_current_prediction_user(
        user_id, show_predictions)
    cursor.execute(
        'SELECT E.ID, UD.USER_ID, O."START", O."END", T.CODE, TT."LABEL", T.GROUP_ID, '
        'T."GROUP", E."LABEL", U."NAME", TT.ID '
        'FROM LTN_DEVELOP.ENTITIES E '
        'JOIN LTN_DEVELOP.USER_DOCUMENTS UD ON E.USER_DOC_ID = UD.ID AND UD.DOCUMENT_ID = ? '
        'JOIN LTN_DEVELOP.OFFSETS O ON O.ENTITY_ID = E.ID AND O.USER_DOC_ID = E.USER_DOC_ID '
        'LEFT OUTER JOIN LTN_DEVELOP.USERS U ON UD.USER_ID = U.ID '
        'LEFT OUTER JOIN LTN_DEVELOP.TASK_TYPES TT ON E.TYPE_ID = TT.ID '
        'LEFT OUTER JOIN LTN_DEVELOP.TYPES T ON TT.TYPE_ID = T.ID '
        'WHERE UD.VISIBILITY = 1 OR UD.USER_ID = ? OR UD.USER_ID = ? '
        'ORDER BY E.ID', (document_id, user_id, current_prediction_user))
    denotations = []
    increment = 1
    previous_id = None
    # todo: handle being not logged in
    colors = [
        'blue', 'navy', 'brown', 'chocolate', 'orange', 'maroon', 'turquoise'
    ]
    user_id_mapping = {user_id: 0}
    prediction_engine_info = {'name': 'Prediction Engine', 'color': 'gray'}
    current_user_info = {'name': 'You', 'color': '#55AA55'}
    user_info = {0: current_user_info}
    annotation_id_map = {}
    user_offset = 1
    if current_prediction_user != user_id:
        user_info[-1] = prediction_engine_info
        user_id_mapping[current_prediction_user] = -1
        user_offset = 2
    for result in cursor.fetchall():
        denotation = {}
        current_id = str(result[0])
        creator = str(result[1])
        if current_id == previous_id:
            current_id += "_" + str(increment)
            increment += 1
            if previous_id not in annotation_id_map:
                annotation_id_map[previous_id] = {}
            annotation_id_map[previous_id][creator] = current_id
        else:
            increment = 1
        if creator not in user_id_mapping and creator != current_prediction_user:
            new_id = len(user_id_mapping)
            user_info[new_id] = {
                'name': str(result[9]),
                'color': colors[(new_id - user_offset) % len(colors)]
            }
            user_id_mapping[creator] = new_id

        # the bioc library expects all attributes to be strings (and TextAE doesn't care)
        anno_info = {
            "code": str(result[4]),
            "name": str(result[5]),
            "groupId": str(result[6]),
            "group": str(result[7]),
            "label": str(result[8]),
            "id": str(result[10])
        }
        denotation['id'] = current_id
        denotation['obj'] = anno_info
        denotation['span'] = {}
        denotation['span']['begin'] = result[2]
        denotation['span']['end'] = result[3]
        # necessary for split annotations
        denotation['originalId'] = str(result[0])
        denotation['userId'] = user_id_mapping.get(creator)
        denotations.append(denotation)
        previous_id = str(result[0])
    return denotations, user_info, annotation_id_map