示例#1
0
文件: api.py 项目: AlongWY/AMR
def smatch(cur_amr1, cur_amr2, n_iter=5):
    best_match_num, test_triple_num, gold_triple_num = _smatch(
        cur_amr1, cur_amr2, n_iter)
    precision, recall, best_f_score = compute_f(best_match_num,
                                                test_triple_num,
                                                gold_triple_num)
    return best_f_score
def main(arguments):
    """
    Main function of smatch score calculation

    """
    global verbose
    global iteration_num
    global single_score
    global pr_flag
    # set the iteration number
    # total iteration number = restart number + 1
    iteration_num = arguments.r + 1
    if arguments.ms:
        single_score = False
    if arguments.v:
        verbose = True
    if arguments.pr:
        pr_flag = True
    # matching triple number
    total_match_num = 0
    # triple number in test file
    total_test_num = 0
    # triple number in gold file
    total_gold_num = 0
    # sentence number
    sent_num = 1
    # Read amr pairs from two files
    while True:
        cur_amr1 = get_amr_line(args.f[0])
        cur_amr2 = get_amr_line(args.f[1])
        if cur_amr1 == "" and cur_amr2 == "":
            break
        if cur_amr1 == "":
            print >> ERROR_LOG, "Error: File 1 has less AMRs than file 2"
            print >> ERROR_LOG, "Ignoring remaining AMRs"
            break
        if cur_amr2 == "":
            print >> ERROR_LOG, "Error: File 2 has less AMRs than file 1"
            print >> ERROR_LOG, "Ignoring remaining AMRs"
            break
        amr1 = amr.AMR.parse_AMR_line(cur_amr1)
        amr2 = amr.AMR.parse_AMR_line(cur_amr2)
        prefix1 = "a"
        prefix2 = "b"
        # Rename node to "a1", "a2", .etc
        amr1.rename_node(prefix1)
        # Renaming node to "b1", "b2", .etc
        amr2.rename_node(prefix2)
        (instance1, attributes1, relation1) = amr1.get_triples()
        (instance2, attributes2, relation2) = amr2.get_triples()
        if verbose:
            # print parse results of two AMRs
            print >> DEBUG_LOG, "AMR pair", sent_num
            print >> DEBUG_LOG, "============================================"
            print >> DEBUG_LOG, "AMR 1 (one-line):", cur_amr1
            print >> DEBUG_LOG, "AMR 2 (one-line):", cur_amr2
            print >> DEBUG_LOG, "Instance triples of AMR 1:", len(instance1)
            print >> DEBUG_LOG, instance1
            print >> DEBUG_LOG, "Attribute triples of AMR 1:", len(attributes1)
            print >> DEBUG_LOG, attributes1
            print >> DEBUG_LOG, "Relation triples of AMR 1:", len(relation1)
            print >> DEBUG_LOG, relation1
            print >> DEBUG_LOG, "Instance triples of AMR 2:", len(instance2)
            print >> DEBUG_LOG, instance2
            print >> DEBUG_LOG, "Attribute triples of AMR 2:", len(attributes2)
            print >> DEBUG_LOG, attributes2
            print >> DEBUG_LOG, "Relation triples of AMR 2:", len(relation2)
            print >> DEBUG_LOG, relation2
        (best_mapping, best_match_num) = get_best_match(instance1,
                                                        attributes1,
                                                        relation1,
                                                        instance2,
                                                        attributes2,
                                                        relation2,
                                                        prefix1,
                                                        prefix2,
                                                        verbose=verbose)
        if verbose:
            print >> DEBUG_LOG, "best match number", best_match_num
            print >> DEBUG_LOG, "best node mapping", best_mapping
            print >> DEBUG_LOG, "Best node mapping alignment:", print_alignment(
                best_mapping, instance1, instance2)
        test_triple_num = len(instance1) + len(attributes1) + len(relation1)
        gold_triple_num = len(instance2) + len(attributes2) + len(relation2)
        #print_errors(best_mapping, (instance1, attributes1, relation1), (instance2, attributes2, relation2), prefix1, prefix2)
        if not single_score:
            # if each AMR pair should have a score, compute and output it here
            (precision, recall,
             best_f_score) = compute_f(best_match_num, test_triple_num,
                                       gold_triple_num, verbose)
            #print "Sentence", sent_num
            if pr_flag:
                print "Precision: %.2f" % precision
                print "Recall: %.2f" % recall
            print "F1: %.3f" % best_f_score
        total_match_num += best_match_num
        total_test_num += test_triple_num
        total_gold_num += gold_triple_num
        sent_num += 1
    if verbose:
        print >> DEBUG_LOG, "Total match number, total triple number in AMR 1, and total triple number in AMR 2:"
        print >> DEBUG_LOG, total_match_num, total_test_num, total_gold_num
        print >> DEBUG_LOG, "---------------------------------------------------------------------------------"
    # output document-level smatch score (a single f-score for all AMR pairs in two files)
    if single_score:
        (precision, recall, best_f_score) = compute_f(total_match_num,
                                                      total_test_num,
                                                      total_gold_num)
        if pr_flag:
            print "Precision: %.4f" % precision
            print "Recall: %.4f" % recall
        print "F-score: %.4f" % best_f_score
    args.f[0].close()
    args.f[1].close()
示例#3
0
文件: api.py 项目: AlongWY/AMR
 def last_f_score(self):
     return compute_f(self.last_match_num, self.last_test_num,
                      self.last_gold_num)[2]
示例#4
0
文件: api.py 项目: AlongWY/AMR
 def f_score(self):
     return compute_f(self.total_match_num, self.total_test_num,
                      self.total_gold_num)[2]