test_edit_distance_op.py 1.4 KB
Newer Older
Y
Yibing Liu 已提交
1 2 3 4 5 6 7 8
import unittest
import numpy as np
from op_test import OpTest


def Levenshtein(hyp, ref):
    """ Compute the Levenshtein distance between two strings.

9
    :param hyp: hypothesis string in index
Y
Yibing Liu 已提交
10
    :type hyp: list
11
    :param ref: reference string in index
Y
Yibing Liu 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    :type ref: list
    """
    m = len(hyp)
    n = len(ref)
    if m == 0:
        return n
    if n == 0:
        return m

    dist = np.zeros((m + 1, n + 1))
    for i in range(0, m + 1):
        dist[i][0] = i
    for j in range(0, n + 1):
        dist[0][j] = j

    for i in range(1, m + 1):
        for j in range(1, n + 1):
            cost = 0 if hyp[i - 1] == ref[j - 1] else 1
            deletion = dist[i - 1][j] + 1
            insertion = dist[i][j - 1] + 1
            substitution = dist[i - 1][j - 1] + cost
            dist[i][j] = min(deletion, insertion, substitution)
    return dist[m][n]


class TestCTCEditDistanceOp(OpTest):
    def setUp(self):
39
        self.op_type = "edit_distance"
Y
Yibing Liu 已提交
40 41 42
        normalized = True
        x1 = np.array([0, 12, 3, 5]).astype("int32")
        x2 = np.array([0, 12, 4, 7, 8]).astype("int32")
Y
Yibing Liu 已提交
43 44 45 46 47

        distance = Levenshtein(hyp=x1, ref=x2)
        if normalized is True:
            distance = distance / len(x2)
        self.attrs = {'normalized': normalized}
Y
Yibing Liu 已提交
48
        self.inputs = {'Hyp': x1, 'Ref': x2}
Y
Yibing Liu 已提交
49 50 51 52 53 54 55 56
        self.outputs = {'Out': distance}

    def test_check_output(self):
        self.check_output()


if __name__ == '__main__':
    unittest.main()