示例#1
0
def wer(hyp: List[str], ref: List[str]) -> Tuple[float]:
    """
    Compute edit distance between two str list
    Args:
        hlist: list[str], hypothesis
        rlist: list[str], reference
    Return:
        float: three error types (sub/ins/del)
    """
    error, match, ops = ed.edit_distance_backpointer(hyp, ref)
    sub_err, ins_err, del_err, equal = 0, 0, 0, 0
    for op in ops:
        if op[0] == "delete":
            del_err += 1
        elif op[0] == "replace":
            sub_err += 1
        elif op[0] == "insert":
            ins_err += 1
        else:
            equal += 1
    if sub_err + del_err + ins_err != error:
        raise RuntimeError("Bugs: sub_err + del_err + ins_err != #error")
    if equal != match:
        raise RuntimeError("Bugs: equal != #match")
    return (sub_err, ins_err, del_err)
示例#2
0
 def test_edit_distance0(self):
     """Test edit distance between 'ab' and 'dab'."""
     a = ['a', 'b']
     b = ['d', 'a', 'b']
     self.assertEqual(edit_distance(a, b), (1, 2))
     bp_expected_result = (1, 2, [['insert', 0, 0, 0, 1],
                                  ['equal', 0, 1, 1, 2],
                                  ['equal', 1, 2, 2, 3]])
     self.assertEqual(edit_distance_backpointer(a, b), bp_expected_result)
示例#3
0
 def test_edit_distance3(self):
     """Test for 'are you at work now'."""
     a = ['are', 'you', 'at', 'work', 'now']
     b = ['i', 'feel', 'are', 'saying']
     bp_expected_result = (5, 0, [['delete', 0, 1, 0, 0],
                                  ['replace', 1, 2, 0, 1],
                                  ['replace', 2, 3, 1, 2],
                                  ['replace', 3, 4, 2, 3],
                                  ['replace', 4, 5, 3, 4]])
     self.assertTrue(edit_distance_backpointer(a, b) == bp_expected_result)
示例#4
0
 def test_edit_distance3(self):
     """Test for 'are you at work now'."""
     a = ['are', 'you', 'at', 'work', 'now']
     b = ['i', 'feel', 'are', 'saying']
     bp_expected_result = (5, 0, [['delete', 0, 1, 0, 0],
                                  ['replace', 1, 2, 0, 1],
                                  ['replace', 2, 3, 1, 2],
                                  ['replace', 3, 4, 2, 3],
                                  ['replace', 4, 5, 3, 4]])
     self.assertTrue(edit_distance_backpointer(a, b) == bp_expected_result)
示例#5
0
 def test_edit_distance0(self):
     """Test edit distance between 'ab' and 'dab'."""
     a = ["a", "b"]
     b = ["d", "a", "b"]
     self.assertEqual(edit_distance(a, b), (1, 2))
     bp_expected_result = (
         1,
         2,
         [["insert", 0, 0, 0, 1], ["equal", 0, 1, 1, 2], ["equal", 1, 2, 2, 3]],
     )
     self.assertEqual(edit_distance_backpointer(a, b), bp_expected_result)
示例#6
0
 def test_edit_distance2(self):
     """Test edit distance for 'hi my name is andy'."""
     a = ['hi', 'my', 'name', 'is', 'andy']
     b = ['hi', "i'm", 'my', "name's", 'sandy']
     self.assertTrue(edit_distance(a, b) == (4, 1))
     bp_expected_result = (4, 1, [['equal', 0, 1, 0, 1],
                                  ['replace', 1, 2, 1, 2],
                                  ['replace', 2, 3, 2, 3],
                                  ['replace', 3, 4, 3, 4],
                                  ['replace', 4, 5, 4, 5]])
     self.assertTrue(edit_distance_backpointer(a, b) == bp_expected_result)
示例#7
0
 def test_edit_distance1(self):
     """Test edit distance between 'ab' and 'acdab'."""
     a = ['a', 'b']
     b = ['a', 'c', 'd', 'a', 'b']
     self.assertTrue(edit_distance(a, b) == (3, 2))
     bp_expected_result = (3, 2, [['insert', 0, 0, 0, 1],
                                  ['insert', 0, 0, 1, 2],
                                  ['insert', 0, 0, 2, 3],
                                  ['equal', 0, 1, 3, 4],
                                  ['equal', 1, 2, 4, 5]])
     self.assertTrue(edit_distance_backpointer(a, b) == bp_expected_result)
示例#8
0
 def test_edit_distance2(self):
     """Test edit distance for 'hi my name is andy'."""
     a = ['hi', 'my', 'name', 'is', 'andy']
     b = ['hi', "i'm", 'my', "name's", 'sandy']
     self.assertTrue(edit_distance(a, b) == (4, 1))
     bp_expected_result = (4, 1, [['equal', 0, 1, 0, 1],
                                  ['replace', 1, 2, 1, 2],
                                  ['replace', 2, 3, 2, 3],
                                  ['replace', 3, 4, 3, 4],
                                  ['replace', 4, 5, 4, 5]])
     self.assertTrue(edit_distance_backpointer(a, b) == bp_expected_result)
示例#9
0
 def test_edit_distance1(self):
     """Test edit distance between 'ab' and 'acdab'."""
     a = ['a', 'b']
     b = ['a', 'c', 'd', 'a', 'b']
     self.assertTrue(edit_distance(a, b) == (3, 2))
     bp_expected_result = (3, 2, [['insert', 0, 0, 0, 1],
                                  ['insert', 0, 0, 1, 2],
                                  ['insert', 0, 0, 2, 3],
                                  ['equal', 0, 1, 3, 4],
                                  ['equal', 1, 2, 4, 5]])
     self.assertTrue(edit_distance_backpointer(a, b) == bp_expected_result)
示例#10
0
 def test_edit_distance_highest_match(self):
     """Test edit distance for 'hi my name is andy', maximizing matches rather than
     minimizing edits."""
     a = ['hi', 'my', 'name', 'is', 'andy']
     b = ['hi', "i'm", 'my', "name's", 'sandy']
     self.assertTrue(edit_distance(a, b, action_function=highest_match_action) == (4, 2))
     bp_expected_result = (4, 2, [['equal', 0, 1, 0, 1],
                                  ['insert', 0, 0, 1, 2],
                                  ['equal', 1, 2, 2, 3],
                                  ['delete', 2, 3, 2, 2],
                                  ['replace', 3, 4, 3, 4],
                                  ['replace', 4, 5, 4, 5]])
     self.assertTrue(edit_distance_backpointer(a, b, action_function=highest_match_action) == bp_expected_result)
示例#11
0
 def test_edit_distance3(self):
     """Test for 'are you at work now'."""
     a = ["are", "you", "at", "work", "now"]
     b = ["i", "feel", "are", "saying"]
     bp_expected_result = (
         5,
         0,
         [
             ["delete", 0, 1, 0, 0],
             ["replace", 1, 2, 0, 1],
             ["replace", 2, 3, 1, 2],
             ["replace", 3, 4, 2, 3],
             ["replace", 4, 5, 3, 4],
         ],
     )
     self.assertEqual(edit_distance_backpointer(a, b), bp_expected_result)
示例#12
0
 def test_edit_distance2(self):
     """Test edit distance for 'hi my name is andy'."""
     a = ["hi", "my", "name", "is", "andy"]
     b = ["hi", "i'm", "my", "name's", "sandy"]
     self.assertEqual(edit_distance(a, b), (4, 1))
     bp_expected_result = (
         4,
         1,
         [
             ["equal", 0, 1, 0, 1],
             ["replace", 1, 2, 1, 2],
             ["replace", 2, 3, 2, 3],
             ["replace", 3, 4, 3, 4],
             ["replace", 4, 5, 4, 5],
         ],
     )
     self.assertEqual(edit_distance_backpointer(a, b), bp_expected_result)
示例#13
0
 def test_edit_distance1(self):
     """Test edit distance between 'ab' and 'acdab'."""
     a = ["a", "b"]
     b = ["a", "c", "d", "a", "b"]
     self.assertEqual(edit_distance(a, b), (3, 2))
     bp_expected_result = (
         3,
         2,
         [
             ["insert", 0, 0, 0, 1],
             ["insert", 0, 0, 1, 2],
             ["insert", 0, 0, 2, 3],
             ["equal", 0, 1, 3, 4],
             ["equal", 1, 2, 4, 5],
         ],
     )
     self.assertEqual(edit_distance_backpointer(a, b), bp_expected_result)
示例#14
0
 def test_edit_distance_highest_match(self):
     """Test edit distance for 'hi my name is andy', maximizing matches rather than
     minimizing edits."""
     a = ['hi', 'my', 'name', 'is', 'andy']
     b = ['hi', "i'm", 'my', "name's", 'sandy']
     self.assertTrue(
         edit_distance(a, b, action_function=highest_match_action) == (4,
                                                                       2))
     bp_expected_result = (4, 2, [['equal', 0, 1, 0, 1],
                                  ['insert', 0, 0, 1, 2],
                                  ['equal', 1, 2, 2, 3],
                                  ['delete', 2, 3, 2, 2],
                                  ['replace', 3, 4, 3, 4],
                                  ['replace', 4, 5, 4, 5]])
     self.assertTrue(
         edit_distance_backpointer(
             a, b, action_function=highest_match_action) ==
         bp_expected_result)
示例#15
0
 def test_edit_distance_highest_match(self):
     """Test edit distance for 'hi my name is andy', maximizing matches rather than
     minimizing edits."""
     a = ["hi", "my", "name", "is", "andy"]
     b = ["hi", "i'm", "my", "name's", "sandy"]
     self.assertEqual(
         edit_distance(a, b, action_function=highest_match_action), (4, 2)
     )
     bp_expected_result = (
         4,
         2,
         [
             ["equal", 0, 1, 0, 1],
             ["insert", 1, 1, 1, 2],
             ["equal", 1, 2, 2, 3],
             ["delete", 2, 3, 2, 2],
             ["replace", 3, 4, 3, 4],
             ["replace", 4, 5, 4, 5],
         ],
     )
     self.assertEqual(
         edit_distance_backpointer(a, b, action_function=highest_match_action),
         bp_expected_result,
     )