Beispiel #1
0
def compute_files(user1, user2, file_list, dir_pre, start_num):
    """
    Compute the smatch scores for a file list between two users
    Args:
    user1: user 1 name
    user2: user 2 name
    file_list: file list
    dir_pre: the file location prefix
    start_num: the number of restarts in smatch
    Returns:
    smatch f score.

    """
    match_total = 0
    test_total = 0
    gold_total = 0
    for fi in file_list:
        file1 = dir_pre + user1 + "/" + fi + ".txt"
        file2 = dir_pre + user2 + "/" + fi + ".txt"
        if not os.path.exists(file1):
            print("*********Error: ",
                  file1,
                  "does not exist*********",
                  file=ERROR_LOG)
            return -1.00
        if not os.path.exists(file2):
            print("*********Error: ",
                  file2,
                  "does not exist*********",
                  file=ERROR_LOG)
            return -1.00
        try:
            file1_h = open(file1, "r")
            file2_h = open(file2, "r")
        except IOError:
            print("Cannot open the files", file1, file2, file=ERROR_LOG)
            break
        cur_amr1 = smatch.get_amr_line(file1_h)
        cur_amr2 = smatch.get_amr_line(file2_h)
        if cur_amr1 == "":
            print("AMR 1 is empty", file=ERROR_LOG)
            continue
        if cur_amr2 == "":
            print("AMR 2 is empty", file=ERROR_LOG)
            continue
        amr1 = amr.AMR.parse_AMR_line(cur_amr1)
        amr2 = amr.AMR.parse_AMR_line(cur_amr2)
        test_label = "a"
        gold_label = "b"
        amr1.rename_node(test_label)
        amr2.rename_node(gold_label)
        (test_inst, test_rel1, test_rel2) = amr1.get_triples()
        (gold_inst, gold_rel1, gold_rel2) = amr2.get_triples()
        if verbose:
            print("Instance triples of file 1:",
                  len(test_inst),
                  file=DEBUG_LOG)
            print(test_inst, file=DEBUG_LOG)
            print("Attribute triples of file 1:",
                  len(test_rel1),
                  file=DEBUG_LOG)
            print(test_rel1, file=DEBUG_LOG)
            print("Relation triples of file 1:",
                  len(test_rel2),
                  file=DEBUG_LOG)
            print(test_rel2, file=DEBUG_LOG)
            print("Instance triples of file 2:",
                  len(gold_inst),
                  file=DEBUG_LOG)
            print(gold_inst, file=DEBUG_LOG)
            print("Attribute triples of file 2:",
                  len(gold_rel1),
                  file=DEBUG_LOG)
            print(gold_rel1, file=DEBUG_LOG)
            print("Relation triples of file 2:",
                  len(gold_rel2),
                  file=DEBUG_LOG)
            print(gold_rel2, file=DEBUG_LOG)
        (best_match, best_match_num) = smatch.get_best_match(
            test_inst, test_rel1, test_rel2, gold_inst, gold_rel1, gold_rel2,
            test_label, gold_label)
        if verbose:
            print("best match number", best_match_num, file=DEBUG_LOG)
            print("Best Match:",
                  smatch.print_alignment(best_match, test_inst, gold_inst),
                  file=DEBUG_LOG)
        match_total += best_match_num
        test_total += (len(test_inst) + len(test_rel1) + len(test_rel2))
        gold_total += (len(gold_inst) + len(gold_rel1) + len(gold_rel2))
        smatch.match_triple_dict.clear()
    (precision, recall, f_score) = smatch.compute_f(match_total, test_total,
                                                    gold_total)
    return "%.2f" % f_score
Beispiel #2
0
def compute_files(user1, user2, file_list, dir_pre, start_num):
   # print file_list
   # print user1, user2
  match_total = 0
  test_total = 0
  gold_total = 0
  for fi in file_list:
    file1 = dir_pre + user1 + "/" + fi + ".txt"
    file2 = dir_pre + user2 + "/" + fi + ".txt"
    # print file1,file2
    if not os.path.exists(file1):
      print >> ERROR_LOG, "*********Error: ", file1, "does not exist*********"
      return -1.00
    if not os.path.exists(file2):
      print >> ERROR_LOG, "*********Error: ", file2, "does not exist*********"
      return -1.00
    try:
      file1_h = open(file1, "r")
      file2_h = open(file2, "r")
    except:
      print >> ERROR_LOG, "Cannot open the files", file1, file2
    cur_amr1 = smatch.get_amr_line(file1_h)
    cur_amr2 = smatch.get_amr_line(file2_h)
    if(cur_amr1 == ""):
      print >> ERROR_LOG, "AMR 1 is empty"
      continue
    if(cur_amr2 == ""):
      print >> ERROR_LOG, "AMR 2 is empty"
      continue
    amr1 = amr.AMR.parse_AMR_line(cur_amr1)
    amr2 = amr.AMR.parse_AMR_line(cur_amr2)
    test_label = "a"
    gold_label = "b"
    amr1.rename_node(test_label)
    amr2.rename_node(gold_label)
    (test_inst, test_rel1, test_rel2) = amr1.get_triples2()
    (gold_inst, gold_rel1, gold_rel2) = amr2.get_triples2()
    if verbose:
      print >> ERROR_LOG, "Instance triples of file 1:", len(test_inst)
      print >> ERROR_LOG, test_inst
      print >> sys.stderr, "Relation triples of file 1:", len(
          test_rel1) + len(test_rel2)
      print >>sys.stderr, test_rel1
      print >> sys.stderr, test_rel2
      print >> ERROR_LOG, "Instance triples of file 2:", len(gold_inst)
      print >> ERROR_LOG, gold_inst
      print >> sys.stderr, "Relation triples of file 2:", len(
          gold_rel1) + len(gold_rel2)
      print >> sys.stderr, gold_rel1
      print >> sys.stderr, gold_rel2
    if len(test_inst) < len(gold_inst):
      (best_match,
       best_match_num) = smatch.get_fh(test_inst,
                                       test_rel1,
                                       test_rel2,
                                       gold_inst,
                                       gold_rel1,
                                       gold_rel2,
                                       test_label,
                                       gold_label)
      if verbose:
        print >> ERROR_LOG, "best match number", best_match_num
        print >>ERROR_LOG, "Best Match:", smatch.print_alignment(
            best_match, test_inst, gold_inst)
    else:
      (best_match,
       best_match_num) = smatch.get_fh(gold_inst,
                                       gold_rel1,
                                       gold_rel2,
                                       test_inst,
                                       test_rel1,
                                       test_rel2,
                                       gold_label,
                                       test_label)
      if verbose:
        print >> ERROR_LOG, "best match number", best_match_num
        print >>ERROR_LOG, "Best Match:", smatch.print_alignment(
            best_match, gold_inst, test_inst, True)
    #(match_num,test_num,gold_num)=smatch.get_match(tmp_filename1,tmp_filename2,start_num)
    # print match_num,test_num,gold_num
  # print best_match_num
  # print len(test_inst)+len(test_rel1)+len(test_rel2)
  # print len(gold_inst)+len(gold_rel1)+len(gold_rel2)
    match_total += best_match_num
    test_total += len(test_inst) + len(test_rel1) + len(test_rel2)
    gold_total += len(gold_inst) + len(gold_rel1) + len(gold_rel2)
    smatch.match_num_dict.clear()
  (precision, recall, f_score) = smatch.compute_f(
      match_total, test_total, gold_total)
  return "%.2f" % f_score
Beispiel #3
0
def compute_files(user1, user2, file_list, dir_pre, start_num):

    """
    Compute the smatch scores for a file list between two users
    Args:
    user1: user 1 name
    user2: user 2 name
    file_list: file list
    dir_pre: the file location prefix
    start_num: the number of restarts in smatch
    Returns:
    smatch f score.

    """
    match_total = 0
    test_total = 0
    gold_total = 0
    for fi in file_list:
        file1 = dir_pre + user1 + "/" + fi + ".txt"
        file2 = dir_pre + user2 + "/" + fi + ".txt"
        if not os.path.exists(file1):
            print >> ERROR_LOG, "*********Error: ", file1, "does not exist*********"
            return -1.00
        if not os.path.exists(file2):
            print >> ERROR_LOG, "*********Error: ", file2, "does not exist*********"
            return -1.00
        try:
            file1_h = open(file1, "r")
            file2_h = open(file2, "r")
        except IOError:
            print >> ERROR_LOG, "Cannot open the files", file1, file2
            break
        cur_amr1 = smatch.get_amr_line(file1_h)
        cur_amr2 = smatch.get_amr_line(file2_h)
        if cur_amr1 == "":
            print >> ERROR_LOG, "AMR 1 is empty"
            continue
        if cur_amr2 == "":
            print >> ERROR_LOG, "AMR 2 is empty"
            continue
        amr1 = amr.AMR.parse_AMR_line(cur_amr1)
        amr2 = amr.AMR.parse_AMR_line(cur_amr2)
        test_label = "a"
        gold_label = "b"
        amr1.rename_node(test_label)
        amr2.rename_node(gold_label)
        (test_inst, test_rel1, test_rel2) = amr1.get_triples()
        (gold_inst, gold_rel1, gold_rel2) = amr2.get_triples()
        if verbose:
            print >> DEBUG_LOG, "Instance triples of file 1:", len(test_inst)
            print >> DEBUG_LOG, test_inst
            print >> DEBUG_LOG, "Attribute triples of file 1:", len(test_rel1)
            print >> DEBUG_LOG, test_rel1
            print >> DEBUG_LOG, "Relation triples of file 1:", len(test_rel2)
            print >> DEBUG_LOG, test_rel2
            print >> DEBUG_LOG, "Instance triples of file 2:", len(gold_inst)
            print >> DEBUG_LOG, gold_inst
            print >> DEBUG_LOG, "Attribute triples of file 2:", len(gold_rel1)
            print >> DEBUG_LOG, gold_rel1
            print >> DEBUG_LOG, "Relation triples of file 2:", len(gold_rel2)
            print >> DEBUG_LOG, gold_rel2
        (best_match, best_match_num) = smatch.get_best_match(test_inst, test_rel1, test_rel2,
                                                             gold_inst, gold_rel1, gold_rel2,
                                                             test_label, gold_label)
        if verbose:
            print >> DEBUG_LOG, "best match number", best_match_num
            print >> DEBUG_LOG, "Best Match:", smatch.print_alignment(best_match, test_inst, gold_inst)
        match_total += best_match_num
        test_total += (len(test_inst) + len(test_rel1) + len(test_rel2))
        gold_total += (len(gold_inst) + len(gold_rel1) + len(gold_rel2))
        smatch.match_triple_dict.clear()
    (precision, recall, f_score) = smatch.compute_f(match_total, test_total, gold_total)
    return "%.2f" % f_score
Beispiel #4
0
def compute_files(user1, user2, file_list, dir_pre, start_num):
    #print file_list
    #print user1, user2
    match_total = 0
    test_total = 0
    gold_total = 0
    for fi in file_list:
        file1 = dir_pre + user1 + "/" + fi + ".txt"
        file2 = dir_pre + user2 + "/" + fi + ".txt"
        #print file1,file2
        if not os.path.exists(file1):
            print >> ERROR_LOG, "*********Error: ", file1, "does not exist*********"
            return -1.00
        if not os.path.exists(file2):
            print >> ERROR_LOG, "*********Error: ", file2, "does not exist*********"
            return -1.00
        try:
            file1_h = open(file1, "r")
            file2_h = open(file2, "r")
        except:
            print >> ERROR_LOG, "Cannot open the files", file1, file2
        cur_amr1 = smatch.get_amr_line(file1_h)
        cur_amr2 = smatch.get_amr_line(file2_h)
        if (cur_amr1 == ""):
            print >> ERROR_LOG, "AMR 1 is empty"
            continue
        if (cur_amr2 == ""):
            print >> ERROR_LOG, "AMR 2 is empty"
            continue
        amr1 = amr.AMR.parse_AMR_line(cur_amr1)
        amr2 = amr.AMR.parse_AMR_line(cur_amr2)
        test_label = "a"
        gold_label = "b"
        amr1.rename_node(test_label)
        amr2.rename_node(gold_label)
        (test_inst, test_rel1, test_rel2) = amr1.get_triples2()
        (gold_inst, gold_rel1, gold_rel2) = amr2.get_triples2()
        if verbose:
            print >> ERROR_LOG, "Instance triples of file 1:", len(test_inst)
            print >> ERROR_LOG, test_inst
            print >> sys.stderr, "Relation triples of file 1:", len(
                test_rel1) + len(test_rel2)
            print >> sys.stderr, test_rel1
            print >> sys.stderr, test_rel2
            print >> ERROR_LOG, "Instance triples of file 2:", len(gold_inst)
            print >> ERROR_LOG, gold_inst
            print >> sys.stderr, "Relation triples of file 2:", len(
                gold_rel1) + len(gold_rel2)
            print >> sys.stderr, gold_rel1
            print >> sys.stderr, gold_rel2
        if len(test_inst) < len(gold_inst):
            (best_match,
             best_match_num) = smatch.get_fh(test_inst, test_rel1, test_rel2,
                                             gold_inst, gold_rel1, gold_rel2,
                                             test_label, gold_label)
            if verbose:
                print >> ERROR_LOG, "best match number", best_match_num
                print >> ERROR_LOG, "Best Match:", smatch.print_alignment(
                    best_match, test_inst, gold_inst)
        else:
            (best_match,
             best_match_num) = smatch.get_fh(gold_inst, gold_rel1, gold_rel2,
                                             test_inst, test_rel1, test_rel2,
                                             gold_label, test_label)
            if verbose:
                print >> ERROR_LOG, "best match number", best_match_num
                print >> ERROR_LOG, "Best Match:", smatch.print_alignment(
                    best_match, gold_inst, test_inst, True)
        #(match_num,test_num,gold_num)=smatch.get_match(tmp_filename1,tmp_filename2,start_num)
        #print match_num,test_num,gold_num
    # print best_match_num
    # print len(test_inst)+len(test_rel1)+len(test_rel2)
    # print len(gold_inst)+len(gold_rel1)+len(gold_rel2)
        match_total += best_match_num
        test_total += len(test_inst) + len(test_rel1) + len(test_rel2)
        gold_total += len(gold_inst) + len(gold_rel1) + len(gold_rel2)
        smatch.match_num_dict.clear()
    (precision, recall, f_score) = smatch.compute_f(match_total, test_total,
                                                    gold_total)
    return "%.2f" % f_score