test_edit_distance_op.py 4.4 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
F
fengjiayi 已提交
2
#
D
dzhwinter 已提交
3 4 5
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
F
fengjiayi 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
F
fengjiayi 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15 16
from __future__ import print_function

Y
Yibing Liu 已提交
17 18
import unittest
import numpy as np
19
from op_test import OpTest
Y
Yibing Liu 已提交
20 21 22 23 24


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

25
    :param hyp: hypothesis string in index
Y
Yibing Liu 已提交
26
    :type hyp: list
27
    :param ref: reference string in index
Y
Yibing Liu 已提交
28 29 30 31 32 33 34 35 36
    :type ref: list
    """
    m = len(hyp)
    n = len(ref)
    if m == 0:
        return n
    if n == 0:
        return m

37
    dist = np.zeros((m + 1, n + 1)).astype("float32")
Y
Yibing Liu 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    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]


53
class TestEditDistanceOp(OpTest):
54 55 56
    def setUp(self):
        self.op_type = "edit_distance"
        normalized = False
57 58
        x1 = np.array([[12, 3, 5, 8, 2]]).astype("int64")
        x2 = np.array([[12, 4, 7, 8]]).astype("int64")
59 60
        x1 = np.transpose(x1)
        x2 = np.transpose(x2)
61 62
        self.x1_lod = [1, 4]
        self.x2_lod = [3, 1]
63

64
        num_strs = len(self.x1_lod)
65
        distance = np.zeros((num_strs, 1)).astype("float32")
66
        sequence_num = np.array(2).astype("int64")
67 68 69

        x1_offset = 0
        x2_offset = 0
70 71
        for i in range(0, num_strs):
            distance[i] = Levenshtein(
72 73 74 75
                hyp=x1[x1_offset:(x1_offset + self.x1_lod[i])],
                ref=x2[x2_offset:(x2_offset + self.x2_lod[i])])
            x1_offset += self.x1_lod[i]
            x2_offset += self.x2_lod[i]
76
            if normalized is True:
77
                len_ref = self.x2_lod[i]
78
                distance[i] = distance[i] / len_ref
79

80
        self.attrs = {'normalized': normalized}
81
        self.inputs = {'Hyps': (x1, [self.x1_lod]), 'Refs': (x2, [self.x2_lod])}
82
        self.outputs = {'Out': distance, 'SequenceNum': sequence_num}
83 84 85 86 87

    def test_check_output(self):
        self.check_output()


88 89 90 91
class TestEditDistanceOpNormalizedCase0(OpTest):
    def reset_config(self):
        pass

Y
Yibing Liu 已提交
92
    def setUp(self):
93
        self.op_type = "edit_distance"
Y
Yibing Liu 已提交
94
        normalized = True
95 96
        x1 = np.array([[10, 3, 6, 5, 8, 2]]).astype("int64")
        x2 = np.array([[10, 4, 6, 7, 8]]).astype("int64")
97 98
        x1 = np.transpose(x1)
        x2 = np.transpose(x2)
99 100 101
        self.x1_lod = [3, 0, 3]
        self.x2_lod = [2, 1, 2]
        self.reset_config()
Y
Yibing Liu 已提交
102

103
        num_strs = len(self.x1_lod)
104
        distance = np.zeros((num_strs, 1)).astype("float32")
105
        sequence_num = np.array(3).astype("int64")
106 107 108

        x1_offset = 0
        x2_offset = 0
109 110
        for i in range(0, num_strs):
            distance[i] = Levenshtein(
111 112 113 114
                hyp=x1[x1_offset:(x1_offset + self.x1_lod[i])],
                ref=x2[x2_offset:(x2_offset + self.x2_lod[i])])
            x1_offset += self.x1_lod[i]
            x2_offset += self.x2_lod[i]
115
            if normalized is True:
116
                len_ref = self.x2_lod[i]
117
                distance[i] = distance[i] / len_ref
118

Y
Yibing Liu 已提交
119
        self.attrs = {'normalized': normalized}
120
        self.inputs = {'Hyps': (x1, [self.x1_lod]), 'Refs': (x2, [self.x2_lod])}
121
        self.outputs = {'Out': distance, 'SequenceNum': sequence_num}
Y
Yibing Liu 已提交
122 123 124 125 126

    def test_check_output(self):
        self.check_output()


127 128 129 130 131 132 133 134 135 136 137 138
class TestEditDistanceOpNormalizedCase1(TestEditDistanceOpNormalizedCase0):
    def reset_config(self):
        self.x1_lod = [0, 6, 0]
        self.x2_lod = [2, 1, 2]


class TestEditDistanceOpNormalizedCase2(TestEditDistanceOpNormalizedCase0):
    def reset_config(self):
        self.x1_lod = [0, 0, 6]
        self.x2_lod = [2, 2, 1]


Y
Yibing Liu 已提交
139 140
if __name__ == '__main__':
    unittest.main()