Ejemplo n.º 1
0
def cal_tag(result_path):
    '''
    Calculate evaluation for each tag separately
    :param result_path: output file path
    :return: dictionary of evaluation results for each tag
    '''
    evaluation_result = {}
    data = get_full(result_path)

    for tag in tags:
        TP = cal_tp(data, tag)
        FN = cal_fn(data, tag)
        FP = cal_fp(data, tag)
        TN = cal_tn(data, tag)
        precision = TP / (TP + FP)
        recall = TP / (TP + FN)
        accuracy = (TP + TN) / (TP + TN + FN + FP)
        f1 = 2 * precision * recall / (precision + recall)
        print('tag: ' + tag)
        print('precision: ' + str(precision) + '; recall: ' + str(recall) +
              '; accurary: ' + str(accuracy) + '; f1: ' + str(f1))
        evaluation_result[tag] = {
            'precision': precision,
            'recall': recall,
            'accuracy': accuracy,
            'f1': f1
        }

    return evaluation_result
Ejemplo n.º 2
0
def cal_label(result_path):
    '''
    Calculate evaluation on whole label set. Results for each label.
    :param result_path: output file path
    :return:
    '''

    data = get_full(result_path)
    evaluation_result = {}
    for label in labels:
        tags = ['B-' + label, 'I-' + label]
        TP = cal_tp(data, tags[0]) + cal_tp(data, tags[1])
        FN = cal_fn(data, tags[0]) + cal_fn(data, tags[1])
        FP = cal_fp(data, tags[0]) + cal_fp(data, tags[1])
        TN = cal_tn(data, tags[0]) + cal_tn(data, tags[1])
        print(tags)
        print(TP, FN, FP, TN)
        if TP + FP != 0:
            precision = TP / (TP + FP)
        else:
            precision = 0
        if TP + FN != 0:
            recall = TP / (TP + FN)
        else:
            recall = 0
        if (TP + TN + FN + FP) != 0:
            accuracy = (TP + TN) / (TP + TN + FN + FP)
        else:
            accuracy = 0
        if (precision + recall) != 0:
            f1 = 2 * precision * recall / (precision + recall)
        else:
            f1 = 0
        print(label)
        print('precision: ' + str(precision) + '; recall: ' + str(recall) +
              '; accurary: ' + str(accuracy) + '; f1: ' + str(f1))
        evaluation_result[label] = {
            'precision': precision,
            'recall': recall,
            'accuracy': accuracy,
            'f1': f1
        }
    return evaluation_result
Ejemplo n.º 3
0
def cal_total(result_path):
    '''
    Calculate evaluation on whole set
    :param result_path: output file path
    :return:
    '''

    data = get_full(result_path)
    TP = 0
    FN = 0
    FP = 0
    TN = 0

    for tag in tags:
        _TP = cal_tp(data, tag)
        _FN = cal_fn(data, tag)
        _FP = cal_fp(data, tag)
        _TN = cal_tn(data, tag)
        TP += _TP
        FN += _FN
        FP += _FP
        TN += _TN

    precision = TP / (TP + FP)
    recall = TP / (TP + FN)
    accuracy = (TP + TN) / (TP + TN + FN + FP)
    f1 = 2 * precision * recall / (precision + recall)
    print('precision: ' + str(precision) + '; recall: ' + str(recall) +
          '; accurary: ' + str(accuracy) + '; f1: ' + str(f1))
    evaluation_result = {
        'precision': precision,
        'recall': recall,
        'accuracy': accuracy,
        'f1': f1
    }
    return evaluation_result
Ejemplo n.º 4
0
def get_prediction_data(prediction_result_path):
    data = get_full(prediction_result_path)
    return data[2]