Ejemplo n.º 1
0
def import_document():
    user_id = current_user.get_id()
    if user_id is None:
        return "No user is logged in", 401

    req = request.get_json()
    doc_type = req.get('type', TYPE_PLAINTEXT)
    task = req['task']

    documents = []
    if doc_type == TYPE_PLAINTEXT:
        documents.append(req)
    elif doc_type == TYPE_BIOC:
        documents = extract_documents_from_bioc(req['text'],
                                                req['document_id'], task)
    else:
        return "Document type not supported", 400

    for document in documents:
        document_id = document['document_id']
        message, code = create_document_in_database(
            document_id, document['text'], int(document.get('visibility', 1)),
            task)
        if code == 201 and doc_type == TYPE_BIOC:
            save_document(document, load_user_doc_id(document_id, user_id),
                          document_id, user_id,
                          int(document.get('visibility', 1)))
        if code != 201:
            return message, code

    return "Successfully imported", 201
Ejemplo n.º 2
0
def import_document():
    user_id = current_user.get_id()
    if user_id is None:
        return "No user is logged in", 401

    req = request.get_json()
    doc_type = req.get('type', TYPE_PLAINTEXT)
    task = req['task']

    documents = []
    if doc_type == TYPE_PLAINTEXT:
        documents.append(req)
    elif doc_type == TYPE_BIOC:
        documents = extract_documents_from_bioc(req['text'], req['document_id'], task)
    else:
        return "Document type not supported", 400

    for document in documents:
        document_id = document['document_id']
        message, code = create_document_in_database(document_id,
                                                    document['text'],
                                                    int(document.get('visibility', 1)),
                                                    task)
        if code == 201 and doc_type == TYPE_BIOC:
            save_document(document,
                          load_user_doc_id(document_id, user_id),
                          document_id,
                          user_id,
                          int(document.get('visibility', 1)))
        if code != 201:
            return message, code

    return "Successfully imported", 201
Ejemplo n.º 3
0
def predict():
    data = request.get_json()
    task_id = data['task_id']
    jobs = data.get('jobs', [PREDICT_ENTITIES])
    document_id = data['document_id']
    user_id = data.get('user_id', current_user.get_id())
    current_prediction_user = prediction_user_for_user(user_id)
    prediction_user_doc_id = load_user_doc_id(document_id,
                                              current_prediction_user)
    delete_user_document(prediction_user_doc_id)

    document_data = json.loads(data.get('current_state', None))
    if document_data is None:
        document_data = load_document(document_id, user_id)
    else:
        # the current status has to be saved first in order to disambiguate the ids of the annotations
        user_doc_id = load_user_doc_id(document_id, current_user.get_id())
        successful = save_document(document_data, user_doc_id, document_id,
                                   current_user.get_id(), task_id)
        if not successful:
            return "Could not save the document", 500

    if PREDICT_ENTITIES in jobs:
        cursor = get_connection().cursor()
        cursor.execute(
            'INSERT INTO "LTN_DEVELOP"."USER_DOCUMENTS" '
            'VALUES (?, ?, ?, 0, current_timestamp, current_timestamp)', (
                prediction_user_doc_id,
                current_prediction_user,
                document_id,
            ))
        cursor.close()
        get_connection().commit()
        predict_entities(document_id, task_id, prediction_user_doc_id)
    if PREDICT_RELATIONS in jobs:
        if PREDICT_ENTITIES not in jobs:
            save_document(document_data, prediction_user_doc_id, document_id,
                          current_prediction_user, task_id, False)
        predicted_pairs = predict_relations(prediction_user_doc_id, task_id)
        if PREDICT_ENTITIES not in jobs:
            remove_entities_without_relations(predicted_pairs, document_data,
                                              prediction_user_doc_id)

    document_data = load_document(document_id, current_user.get_id(), True)
    return respond_with(document_data)
Ejemplo n.º 4
0
def predict():
    data = request.get_json()
    task_id = data['task_id']
    jobs = data.get('jobs', [PREDICT_ENTITIES])
    document_id = data['document_id']
    user_id = data.get('user_id', current_user.get_id())
    current_prediction_user = prediction_user_for_user(user_id)
    prediction_user_doc_id = load_user_doc_id(document_id, current_prediction_user)
    delete_user_document(prediction_user_doc_id)

    document_data = json.loads(data.get('current_state', None))
    if document_data is None:
        document_data = load_document(document_id, user_id)
    else:
        # the current status has to be saved first in order to disambiguate the ids of the annotations
        user_doc_id = load_user_doc_id(document_id, current_user.get_id())
        successful = save_document(document_data, user_doc_id, document_id, current_user.get_id(), task_id)
        if not successful:
            return "Could not save the document", 500

    if PREDICT_ENTITIES in jobs:
        cursor = get_connection().cursor()
        cursor.execute('INSERT INTO "LTN_DEVELOP"."USER_DOCUMENTS" '
                       'VALUES (?, ?, ?, 0, current_timestamp, current_timestamp)',
                       (prediction_user_doc_id, current_prediction_user, document_id,))
        cursor.close()
        get_connection().commit()
        predict_entities(document_id, task_id, prediction_user_doc_id)
    if PREDICT_RELATIONS in jobs:
        if PREDICT_ENTITIES not in jobs:
            save_document(document_data, prediction_user_doc_id, document_id, current_prediction_user, task_id, False)
        predicted_pairs = predict_relations(prediction_user_doc_id, task_id)
        if PREDICT_ENTITIES not in jobs:
            remove_entities_without_relations(predicted_pairs, document_data, prediction_user_doc_id)

    document_data = load_document(document_id, current_user.get_id(), True)
    return respond_with(document_data)