|
| 1 | +# -- * -- coding: utf-8 -- * -- |
| 2 | +import numpy as np |
| 3 | + |
| 4 | + |
| 5 | +def levenshtein_distance(ref, hyp): |
| 6 | + ref_len = len(ref) |
| 7 | + hyp_len = len(hyp) |
| 8 | + |
| 9 | + # special case |
| 10 | + if ref == hyp: |
| 11 | + return 0 |
| 12 | + if ref_len == 0: |
| 13 | + return hyp_len |
| 14 | + if hyp_len == 0: |
| 15 | + return ref_len |
| 16 | + |
| 17 | + distance = np.zeros((ref_len + 1) * (hyp_len + 1), dtype=np.int64) |
| 18 | + distance = distance.reshape((ref_len + 1, hyp_len + 1)) |
| 19 | + |
| 20 | + # initialization distance matrix |
| 21 | + for j in xrange(hyp_len + 1): |
| 22 | + distance[0][j] = j |
| 23 | + for i in xrange(ref_len + 1): |
| 24 | + distance[i][0] = i |
| 25 | + |
| 26 | + # calculate levenshtein distance |
| 27 | + for i in xrange(1, ref_len + 1): |
| 28 | + for j in xrange(1, hyp_len + 1): |
| 29 | + if ref[i - 1] == hyp[j - 1]: |
| 30 | + distance[i][j] = distance[i - 1][j - 1] |
| 31 | + else: |
| 32 | + s_num = distance[i - 1][j - 1] + 1 |
| 33 | + i_num = distance[i][j - 1] + 1 |
| 34 | + d_num = distance[i - 1][j] + 1 |
| 35 | + distance[i][j] = min(s_num, i_num, d_num) |
| 36 | + |
| 37 | + return distance[ref_len][hyp_len] |
| 38 | + |
| 39 | + |
| 40 | +def wer(reference, hypophysis, delimiter=' ', filter_none=True): |
| 41 | + """ |
| 42 | + Calculate word error rate (WER). WER is a popular evaluation metric used |
| 43 | + in speech recognition. It compares a reference to an hypophysis and |
| 44 | + is defined like this: |
| 45 | +
|
| 46 | + .. math:: |
| 47 | + WER = (Sw + Dw + Iw) / Nw |
| 48 | +
|
| 49 | + where |
| 50 | +
|
| 51 | + .. code-block:: text |
| 52 | +
|
| 53 | + Sw is the number of words subsituted, |
| 54 | + Dw is the number of words deleted, |
| 55 | + Iw is the number of words inserted, |
| 56 | + Nw is the number of words in the reference |
| 57 | +
|
| 58 | + We can use levenshtein distance to calculate WER. Take an attention that |
| 59 | + this function will truncate the beginning and ending delimiter for |
| 60 | + reference and hypophysis sentences before calculating WER. |
| 61 | +
|
| 62 | + :param reference: The reference sentence. |
| 63 | + :type reference: str |
| 64 | + :param hypophysis: The hypophysis sentence. |
| 65 | + :type reference: str |
| 66 | + :param delimiter: Delimiter of input sentences. |
| 67 | + :type delimiter: char |
| 68 | + :param filter_none: Whether to remove None value when splitting sentence. |
| 69 | + :type filter_none: bool |
| 70 | + :return: WER |
| 71 | + :rtype: float |
| 72 | + """ |
| 73 | + |
| 74 | + if len(reference.strip(delimiter)) == 0: |
| 75 | + raise ValueError("Reference's word number should be greater than 0.") |
| 76 | + |
| 77 | + if filter_none == True: |
| 78 | + ref_words = filter(None, reference.strip(delimiter).split(delimiter)) |
| 79 | + hyp_words = filter(None, hypophysis.strip(delimiter).split(delimiter)) |
| 80 | + else: |
| 81 | + ref_words = reference.strip(delimiter).split(delimiter) |
| 82 | + hyp_words = reference.strip(delimiter).split(delimiter) |
| 83 | + |
| 84 | + edit_distance = levenshtein_distance(ref_words, hyp_words) |
| 85 | + wer = float(edit_distance) / len(ref_words) |
| 86 | + return wer |
| 87 | + |
| 88 | + |
| 89 | +def cer(reference, hypophysis, squeeze=True, ignore_case=False, strip_char=''): |
| 90 | + """ |
| 91 | + Calculate charactor error rate (CER). CER will compare reference text and |
| 92 | + hypophysis text in char-level. CER is defined as: |
| 93 | +
|
| 94 | + .. math:: |
| 95 | + CER = (Sc + Dc + Ic) / Nc |
| 96 | +
|
| 97 | + where |
| 98 | +
|
| 99 | + .. code-block:: text |
| 100 | +
|
| 101 | + Sc is the number of character substituted, |
| 102 | + Dc is the number of deleted, |
| 103 | + Ic is the number of inserted |
| 104 | + Nc is the number of characters in the reference |
| 105 | +
|
| 106 | + We can use levenshtein distance to calculate CER. Chinese input should be |
| 107 | + encoded to unicode. |
| 108 | +
|
| 109 | + :param reference: The reference sentence. |
| 110 | + :type reference: str |
| 111 | + :param hypophysis: The hypophysis sentence. |
| 112 | + :type reference: str |
| 113 | + :param squeeze: If set true, consecutive space character |
| 114 | + will be squeezed to one |
| 115 | + :type squeezed: bool |
| 116 | + :param ignore_case: Whether ignoring character case. |
| 117 | + :type ignore_case: bool |
| 118 | + :param strip_char: If not set to '', strip_char in beginning and ending of |
| 119 | + sentence will be truncated. |
| 120 | + :type strip_char: char |
| 121 | + :return: CER |
| 122 | + :rtype: float |
| 123 | + """ |
| 124 | + if ignore_case == True: |
| 125 | + reference = reference.lower() |
| 126 | + hypophysis = hypophysis.lower() |
| 127 | + if strip_char != '': |
| 128 | + reference = reference.strip(strip_char) |
| 129 | + hypophysis = hypophysis.strip(strip_char) |
| 130 | + if squeeze == True: |
| 131 | + reference = ' '.join(filter(None, reference.split(' '))) |
| 132 | + hypophysis = ' '.join(filter(None, hypophysis.split(' '))) |
| 133 | + |
| 134 | + if len(reference) == 0: |
| 135 | + raise ValueError("Length of reference should be greater than 0.") |
| 136 | + edit_distance = levenshtein_distance(reference, hypophysis) |
| 137 | + cer = float(edit_distance) / len(reference) |
| 138 | + return cer |
0 commit comments