We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Should expose the distance in error_rate.py


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `utils/error_rate.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 """This module provides functions to calculate error rate in different level.
3 e.g. wer for word-level, cer for char-level.
4 """
5 from __future__ import absolute_import
6 from __future__ import division
7 from __future__ import print_function
8 
9 import numpy as np
10 
11 
12 def _levenshtein_distance(ref, hyp):
13     """Levenshtein distance is a string metric for measuring the difference
14     between two sequences. Informally, the levenshtein disctance is defined as
15     the minimum number of single-character edits (substitutions, insertions or
16     deletions) required to change one word into the other. We can naturally
17     extend the edits to word level when calculate levenshtein disctance for
18     two sentences.
19     """
20     m = len(ref)
21     n = len(hyp)
22 
23     # special case
24     if ref == hyp:
25         return 0
26     if m == 0:
27         return n
28     if n == 0:
29         return m
30 
31     if m < n:
32         ref, hyp = hyp, ref
33         m, n = n, m
34 
35     # use O(min(m, n)) space
36     distance = np.zeros((2, n + 1), dtype=np.int32)
37 
38     # initialize distance matrix
39     for j in xrange(n + 1):
40         distance[0][j] = j
41 
42     # calculate levenshtein distance
43     for i in xrange(1, m + 1):
44         prev_row_idx = (i - 1) % 2
45         cur_row_idx = i % 2
46         distance[cur_row_idx][0] = i
47         for j in xrange(1, n + 1):
48             if ref[i - 1] == hyp[j - 1]:
49                 distance[cur_row_idx][j] = distance[prev_row_idx][j - 1]
50             else:
51                 s_num = distance[prev_row_idx][j - 1] + 1
52                 i_num = distance[cur_row_idx][j - 1] + 1
53                 d_num = distance[prev_row_idx][j] + 1
54                 distance[cur_row_idx][j] = min(s_num, i_num, d_num)
55 
56     return distance[m % 2][n]
57 
58 
59 def wer(reference, hypothesis, ignore_case=False, delimiter=' '):
60     """Calculate word error rate (WER). WER compares reference text and
61     hypothesis text in word-level. WER is defined as:
62 
63     .. math::
64         WER = (Sw + Dw + Iw) / Nw
65 
66     where
67 
68     .. code-block:: text
69 
70         Sw is the number of words subsituted,
71         Dw is the number of words deleted,
72         Iw is the number of words inserted,
73         Nw is the number of words in the reference
74 
75     We can use levenshtein distance to calculate WER. Please draw an attention
76     that empty items will be removed when splitting sentences by delimiter.
77 
78     :param reference: The reference sentence.
79     :type reference: basestring
80     :param hypothesis: The hypothesis sentence.
81     :type hypothesis: basestring
82     :param ignore_case: Whether case-sensitive or not.
83     :type ignore_case: bool
84     :param delimiter: Delimiter of input sentences.
85     :type delimiter: char
86     :return: Word error rate.
87     :rtype: float
88     :raises ValueError: If the reference length is zero.
89     """
90     if ignore_case == True:
91         reference = reference.lower()
92         hypothesis = hypothesis.lower()
93 
94     ref_words = filter(None, reference.split(delimiter))
95     hyp_words = filter(None, hypothesis.split(delimiter))
96 
97     if len(ref_words) == 0:
98         raise ValueError("Reference's word number should be greater than 0.")
99 
100     edit_distance = _levenshtein_distance(ref_words, hyp_words)
101     wer = float(edit_distance) / len(ref_words)
102     return wer
103 
104 
105 def cer(reference, hypothesis, ignore_case=False, remove_space=False):
106     """Calculate charactor error rate (CER). CER compares reference text and
107     hypothesis text in char-level. CER is defined as:
108 
109     .. math::
110         CER = (Sc + Dc + Ic) / Nc
111 
112     where
113 
114     .. code-block:: text
115 
116         Sc is the number of characters substituted,
117         Dc is the number of characters deleted,
118         Ic is the number of characters inserted
119         Nc is the number of characters in the reference
120 
121     We can use levenshtein distance to calculate CER. Chinese input should be
122     encoded to unicode. Please draw an attention that the leading and tailing
123     space characters will be truncated and multiple consecutive space
124     characters in a sentence will be replaced by one space character.
125 
126     :param reference: The reference sentence.
127     :type reference: basestring
128     :param hypothesis: The hypothesis sentence.
129     :type hypothesis: basestring
130     :param ignore_case: Whether case-sensitive or not.
131     :type ignore_case: bool
132     :param remove_space: Whether remove internal space characters
133     :type remove_space: bool
134     :return: Character error rate.
135     :rtype: float
136     :raises ValueError: If the reference length is zero.
137     """
138     if ignore_case == True:
139         reference = reference.lower()
140         hypothesis = hypothesis.lower()
141 
142     join_char = ' '
143     if remove_space == True:
144         join_char = ''
145 
146     reference = join_char.join(filter(None, reference.split(' ')))
147     hypothesis = join_char.join(filter(None, hypothesis.split(' ')))
148 
149     if len(reference) == 0:
150         raise ValueError("Length of reference should be greater than 0.")
151 
152     edit_distance = _levenshtein_distance(reference, hypothesis)
153     cer = float(edit_distance) / len(reference)
154     return cer
155
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/utils/error_rate.py b/utils/error_rate.py
--- a/utils/error_rate.py
+++ b/utils/error_rate.py
@@ -56,6 +56,62 @@
     return distance[m % 2][n]
 
 
+def word_errors(reference, hypothesis, ignore_case=False, delimiter=' '):
+    """Compute the levenshtein distance between reference sequence and
+    hypothesis sequence in word-level.
+
+    :param reference: The reference sentence.
+    :type reference: basestring
+    :param hypothesis: The hypothesis sentence.
+    :type hypothesis: basestring
+    :param ignore_case: Whether case-sensitive or not.
+    :type ignore_case: bool
+    :param delimiter: Delimiter of input sentences.
+    :type delimiter: char
+    :return: Levenshtein distance and word number of reference sentence.
+    :rtype: list
+    """
+    if ignore_case == True:
+        reference = reference.lower()
+        hypothesis = hypothesis.lower()
+
+    ref_words = filter(None, reference.split(delimiter))
+    hyp_words = filter(None, hypothesis.split(delimiter))
+
+    edit_distance = _levenshtein_distance(ref_words, hyp_words)
+    return float(edit_distance), len(ref_words)
+
+
+def char_errors(reference, hypothesis, ignore_case=False, remove_space=False):
+    """Compute the levenshtein distance between reference sequence and
+    hypothesis sequence in char-level.
+
+    :param reference: The reference sentence.
+    :type reference: basestring
+    :param hypothesis: The hypothesis sentence.
+    :type hypothesis: basestring
+    :param ignore_case: Whether case-sensitive or not.
+    :type ignore_case: bool
+    :param remove_space: Whether remove internal space characters
+    :type remove_space: bool
+    :return: Levenshtein distance and length of reference sentence.
+    :rtype: list
+    """
+    if ignore_case == True:
+        reference = reference.lower()
+        hypothesis = hypothesis.lower()
+
+    join_char = ' '
+    if remove_space == True:
+        join_char = ''
+
+    reference = join_char.join(filter(None, reference.split(' ')))
+    hypothesis = join_char.join(filter(None, hypothesis.split(' ')))
+
+    edit_distance = _levenshtein_distance(reference, hypothesis)
+    return float(edit_distance), len(reference)
+
+
 def wer(reference, hypothesis, ignore_case=False, delimiter=' '):
     """Calculate word error rate (WER). WER compares reference text and
     hypothesis text in word-level. WER is defined as:
@@ -85,20 +141,15 @@
     :type delimiter: char
     :return: Word error rate.
     :rtype: float
-    :raises ValueError: If the reference length is zero.
+    :raises ValueError: If word number of reference is zero.
     """
-    if ignore_case == True:
-        reference = reference.lower()
-        hypothesis = hypothesis.lower()
+    edit_distance, ref_len = word_errors(reference, hypothesis, ignore_case,
+                                         delimiter)
 
-    ref_words = filter(None, reference.split(delimiter))
-    hyp_words = filter(None, hypothesis.split(delimiter))
-
-    if len(ref_words) == 0:
+    if ref_len == 0:
         raise ValueError("Reference's word number should be greater than 0.")
 
-    edit_distance = _levenshtein_distance(ref_words, hyp_words)
-    wer = float(edit_distance) / len(ref_words)
+    wer = float(edit_distance) / ref_len
     return wer
 
 
@@ -135,20 +186,11 @@
     :rtype: float
     :raises ValueError: If the reference length is zero.
     """
-    if ignore_case == True:
-        reference = reference.lower()
-        hypothesis = hypothesis.lower()
+    edit_distance, ref_len = char_errors(reference, hypothesis, ignore_case,
+                                         remove_space)
 
-    join_char = ' '
-    if remove_space == True:
-        join_char = ''
-
-    reference = join_char.join(filter(None, reference.split(' ')))
-    hypothesis = join_char.join(filter(None, hypothesis.split(' ')))
-
-    if len(reference) == 0:
+    if ref_len == 0:
         raise ValueError("Length of reference should be greater than 0.")
 
-    edit_distance = _levenshtein_distance(reference, hypothesis)
-    cer = float(edit_distance) / len(reference)
+    cer = float(edit_distance) / ref_len
     return cer

{"golden_diff": "diff --git a/utils/error_rate.py b/utils/error_rate.py\n--- a/utils/error_rate.py\n+++ b/utils/error_rate.py\n@@ -56,6 +56,62 @@\n     return distance[m % 2][n]\n \n \n+def word_errors(reference, hypothesis, ignore_case=False, delimiter=' '):\n+    \"\"\"Compute the levenshtein distance between reference sequence and\n+    hypothesis sequence in word-level.\n+\n+    :param reference: The reference sentence.\n+    :type reference: basestring\n+    :param hypothesis: The hypothesis sentence.\n+    :type hypothesis: basestring\n+    :param ignore_case: Whether case-sensitive or not.\n+    :type ignore_case: bool\n+    :param delimiter: Delimiter of input sentences.\n+    :type delimiter: char\n+    :return: Levenshtein distance and word number of reference sentence.\n+    :rtype: list\n+    \"\"\"\n+    if ignore_case == True:\n+        reference = reference.lower()\n+        hypothesis = hypothesis.lower()\n+\n+    ref_words = filter(None, reference.split(delimiter))\n+    hyp_words = filter(None, hypothesis.split(delimiter))\n+\n+    edit_distance = _levenshtein_distance(ref_words, hyp_words)\n+    return float(edit_distance), len(ref_words)\n+\n+\n+def char_errors(reference, hypothesis, ignore_case=False, remove_space=False):\n+    \"\"\"Compute the levenshtein distance between reference sequence and\n+    hypothesis sequence in char-level.\n+\n+    :param reference: The reference sentence.\n+    :type reference: basestring\n+    :param hypothesis: The hypothesis sentence.\n+    :type hypothesis: basestring\n+    :param ignore_case: Whether case-sensitive or not.\n+    :type ignore_case: bool\n+    :param remove_space: Whether remove internal space characters\n+    :type remove_space: bool\n+    :return: Levenshtein distance and length of reference sentence.\n+    :rtype: list\n+    \"\"\"\n+    if ignore_case == True:\n+        reference = reference.lower()\n+        hypothesis = hypothesis.lower()\n+\n+    join_char = ' '\n+    if remove_space == True:\n+        join_char = ''\n+\n+    reference = join_char.join(filter(None, reference.split(' ')))\n+    hypothesis = join_char.join(filter(None, hypothesis.split(' ')))\n+\n+    edit_distance = _levenshtein_distance(reference, hypothesis)\n+    return float(edit_distance), len(reference)\n+\n+\n def wer(reference, hypothesis, ignore_case=False, delimiter=' '):\n     \"\"\"Calculate word error rate (WER). WER compares reference text and\n     hypothesis text in word-level. WER is defined as:\n@@ -85,20 +141,15 @@\n     :type delimiter: char\n     :return: Word error rate.\n     :rtype: float\n-    :raises ValueError: If the reference length is zero.\n+    :raises ValueError: If word number of reference is zero.\n     \"\"\"\n-    if ignore_case == True:\n-        reference = reference.lower()\n-        hypothesis = hypothesis.lower()\n+    edit_distance, ref_len = word_errors(reference, hypothesis, ignore_case,\n+                                         delimiter)\n \n-    ref_words = filter(None, reference.split(delimiter))\n-    hyp_words = filter(None, hypothesis.split(delimiter))\n-\n-    if len(ref_words) == 0:\n+    if ref_len == 0:\n         raise ValueError(\"Reference's word number should be greater than 0.\")\n \n-    edit_distance = _levenshtein_distance(ref_words, hyp_words)\n-    wer = float(edit_distance) / len(ref_words)\n+    wer = float(edit_distance) / ref_len\n     return wer\n \n \n@@ -135,20 +186,11 @@\n     :rtype: float\n     :raises ValueError: If the reference length is zero.\n     \"\"\"\n-    if ignore_case == True:\n-        reference = reference.lower()\n-        hypothesis = hypothesis.lower()\n+    edit_distance, ref_len = char_errors(reference, hypothesis, ignore_case,\n+                                         remove_space)\n \n-    join_char = ' '\n-    if remove_space == True:\n-        join_char = ''\n-\n-    reference = join_char.join(filter(None, reference.split(' ')))\n-    hypothesis = join_char.join(filter(None, hypothesis.split(' ')))\n-\n-    if len(reference) == 0:\n+    if ref_len == 0:\n         raise ValueError(\"Length of reference should be greater than 0.\")\n \n-    edit_distance = _levenshtein_distance(reference, hypothesis)\n-    cer = float(edit_distance) / len(reference)\n+    cer = float(edit_distance) / ref_len\n     return cer\n", "issue": "Should expose the distance in error_rate.py\n\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n\"\"\"This module provides functions to calculate error rate in different level.\ne.g. wer for word-level, cer for char-level.\n\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport numpy as np\n\n\ndef _levenshtein_distance(ref, hyp):\n    \"\"\"Levenshtein distance is a string metric for measuring the difference\n    between two sequences. Informally, the levenshtein disctance is defined as\n    the minimum number of single-character edits (substitutions, insertions or\n    deletions) required to change one word into the other. We can naturally\n    extend the edits to word level when calculate levenshtein disctance for\n    two sentences.\n    \"\"\"\n    m = len(ref)\n    n = len(hyp)\n\n    # special case\n    if ref == hyp:\n        return 0\n    if m == 0:\n        return n\n    if n == 0:\n        return m\n\n    if m < n:\n        ref, hyp = hyp, ref\n        m, n = n, m\n\n    # use O(min(m, n)) space\n    distance = np.zeros((2, n + 1), dtype=np.int32)\n\n    # initialize distance matrix\n    for j in xrange(n + 1):\n        distance[0][j] = j\n\n    # calculate levenshtein distance\n    for i in xrange(1, m + 1):\n        prev_row_idx = (i - 1) % 2\n        cur_row_idx = i % 2\n        distance[cur_row_idx][0] = i\n        for j in xrange(1, n + 1):\n            if ref[i - 1] == hyp[j - 1]:\n                distance[cur_row_idx][j] = distance[prev_row_idx][j - 1]\n            else:\n                s_num = distance[prev_row_idx][j - 1] + 1\n                i_num = distance[cur_row_idx][j - 1] + 1\n                d_num = distance[prev_row_idx][j] + 1\n                distance[cur_row_idx][j] = min(s_num, i_num, d_num)\n\n    return distance[m % 2][n]\n\n\ndef wer(reference, hypothesis, ignore_case=False, delimiter=' '):\n    \"\"\"Calculate word error rate (WER). WER compares reference text and\n    hypothesis text in word-level. WER is defined as:\n\n    .. math::\n        WER = (Sw + Dw + Iw) / Nw\n\n    where\n\n    .. code-block:: text\n\n        Sw is the number of words subsituted,\n        Dw is the number of words deleted,\n        Iw is the number of words inserted,\n        Nw is the number of words in the reference\n\n    We can use levenshtein distance to calculate WER. Please draw an attention\n    that empty items will be removed when splitting sentences by delimiter.\n\n    :param reference: The reference sentence.\n    :type reference: basestring\n    :param hypothesis: The hypothesis sentence.\n    :type hypothesis: basestring\n    :param ignore_case: Whether case-sensitive or not.\n    :type ignore_case: bool\n    :param delimiter: Delimiter of input sentences.\n    :type delimiter: char\n    :return: Word error rate.\n    :rtype: float\n    :raises ValueError: If the reference length is zero.\n    \"\"\"\n    if ignore_case == True:\n        reference = reference.lower()\n        hypothesis = hypothesis.lower()\n\n    ref_words = filter(None, reference.split(delimiter))\n    hyp_words = filter(None, hypothesis.split(delimiter))\n\n    if len(ref_words) == 0:\n        raise ValueError(\"Reference's word number should be greater than 0.\")\n\n    edit_distance = _levenshtein_distance(ref_words, hyp_words)\n    wer = float(edit_distance) / len(ref_words)\n    return wer\n\n\ndef cer(reference, hypothesis, ignore_case=False, remove_space=False):\n    \"\"\"Calculate charactor error rate (CER). CER compares reference text and\n    hypothesis text in char-level. CER is defined as:\n\n    .. math::\n        CER = (Sc + Dc + Ic) / Nc\n\n    where\n\n    .. code-block:: text\n\n        Sc is the number of characters substituted,\n        Dc is the number of characters deleted,\n        Ic is the number of characters inserted\n        Nc is the number of characters in the reference\n\n    We can use levenshtein distance to calculate CER. Chinese input should be\n    encoded to unicode. Please draw an attention that the leading and tailing\n    space characters will be truncated and multiple consecutive space\n    characters in a sentence will be replaced by one space character.\n\n    :param reference: The reference sentence.\n    :type reference: basestring\n    :param hypothesis: The hypothesis sentence.\n    :type hypothesis: basestring\n    :param ignore_case: Whether case-sensitive or not.\n    :type ignore_case: bool\n    :param remove_space: Whether remove internal space characters\n    :type remove_space: bool\n    :return: Character error rate.\n    :rtype: float\n    :raises ValueError: If the reference length is zero.\n    \"\"\"\n    if ignore_case == True:\n        reference = reference.lower()\n        hypothesis = hypothesis.lower()\n\n    join_char = ' '\n    if remove_space == True:\n        join_char = ''\n\n    reference = join_char.join(filter(None, reference.split(' ')))\n    hypothesis = join_char.join(filter(None, hypothesis.split(' ')))\n\n    if len(reference) == 0:\n        raise ValueError(\"Length of reference should be greater than 0.\")\n\n    edit_distance = _levenshtein_distance(reference, hypothesis)\n    cer = float(edit_distance) / len(reference)\n    return cer\n", "path": "utils/error_rate.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n\"\"\"This module provides functions to calculate error rate in different level.\ne.g. wer for word-level, cer for char-level.\n\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport numpy as np\n\n\ndef _levenshtein_distance(ref, hyp):\n    \"\"\"Levenshtein distance is a string metric for measuring the difference\n    between two sequences. Informally, the levenshtein disctance is defined as\n    the minimum number of single-character edits (substitutions, insertions or\n    deletions) required to change one word into the other. We can naturally\n    extend the edits to word level when calculate levenshtein disctance for\n    two sentences.\n    \"\"\"\n    m = len(ref)\n    n = len(hyp)\n\n    # special case\n    if ref == hyp:\n        return 0\n    if m == 0:\n        return n\n    if n == 0:\n        return m\n\n    if m < n:\n        ref, hyp = hyp, ref\n        m, n = n, m\n\n    # use O(min(m, n)) space\n    distance = np.zeros((2, n + 1), dtype=np.int32)\n\n    # initialize distance matrix\n    for j in xrange(n + 1):\n        distance[0][j] = j\n\n    # calculate levenshtein distance\n    for i in xrange(1, m + 1):\n        prev_row_idx = (i - 1) % 2\n        cur_row_idx = i % 2\n        distance[cur_row_idx][0] = i\n        for j in xrange(1, n + 1):\n            if ref[i - 1] == hyp[j - 1]:\n                distance[cur_row_idx][j] = distance[prev_row_idx][j - 1]\n            else:\n                s_num = distance[prev_row_idx][j - 1] + 1\n                i_num = distance[cur_row_idx][j - 1] + 1\n                d_num = distance[prev_row_idx][j] + 1\n                distance[cur_row_idx][j] = min(s_num, i_num, d_num)\n\n    return distance[m % 2][n]\n\n\ndef word_errors(reference, hypothesis, ignore_case=False, delimiter=' '):\n    \"\"\"Compute the levenshtein distance between reference sequence and\n    hypothesis sequence in word-level.\n\n    :param reference: The reference sentence.\n    :type reference: basestring\n    :param hypothesis: The hypothesis sentence.\n    :type hypothesis: basestring\n    :param ignore_case: Whether case-sensitive or not.\n    :type ignore_case: bool\n    :param delimiter: Delimiter of input sentences.\n    :type delimiter: char\n    :return: Levenshtein distance and word number of reference sentence.\n    :rtype: list\n    \"\"\"\n    if ignore_case == True:\n        reference = reference.lower()\n        hypothesis = hypothesis.lower()\n\n    ref_words = filter(None, reference.split(delimiter))\n    hyp_words = filter(None, hypothesis.split(delimiter))\n\n    edit_distance = _levenshtein_distance(ref_words, hyp_words)\n    return float(edit_distance), len(ref_words)\n\n\ndef char_errors(reference, hypothesis, ignore_case=False, remove_space=False):\n    \"\"\"Compute the levenshtein distance between reference sequence and\n    hypothesis sequence in char-level.\n\n    :param reference: The reference sentence.\n    :type reference: basestring\n    :param hypothesis: The hypothesis sentence.\n    :type hypothesis: basestring\n    :param ignore_case: Whether case-sensitive or not.\n    :type ignore_case: bool\n    :param remove_space: Whether remove internal space characters\n    :type remove_space: bool\n    :return: Levenshtein distance and length of reference sentence.\n    :rtype: list\n    \"\"\"\n    if ignore_case == True:\n        reference = reference.lower()\n        hypothesis = hypothesis.lower()\n\n    join_char = ' '\n    if remove_space == True:\n        join_char = ''\n\n    reference = join_char.join(filter(None, reference.split(' ')))\n    hypothesis = join_char.join(filter(None, hypothesis.split(' ')))\n\n    edit_distance = _levenshtein_distance(reference, hypothesis)\n    return float(edit_distance), len(reference)\n\n\ndef wer(reference, hypothesis, ignore_case=False, delimiter=' '):\n    \"\"\"Calculate word error rate (WER). WER compares reference text and\n    hypothesis text in word-level. WER is defined as:\n\n    .. math::\n        WER = (Sw + Dw + Iw) / Nw\n\n    where\n\n    .. code-block:: text\n\n        Sw is the number of words subsituted,\n        Dw is the number of words deleted,\n        Iw is the number of words inserted,\n        Nw is the number of words in the reference\n\n    We can use levenshtein distance to calculate WER. Please draw an attention\n    that empty items will be removed when splitting sentences by delimiter.\n\n    :param reference: The reference sentence.\n    :type reference: basestring\n    :param hypothesis: The hypothesis sentence.\n    :type hypothesis: basestring\n    :param ignore_case: Whether case-sensitive or not.\n    :type ignore_case: bool\n    :param delimiter: Delimiter of input sentences.\n    :type delimiter: char\n    :return: Word error rate.\n    :rtype: float\n    :raises ValueError: If word number of reference is zero.\n    \"\"\"\n    edit_distance, ref_len = word_errors(reference, hypothesis, ignore_case,\n                                         delimiter)\n\n    if ref_len == 0:\n        raise ValueError(\"Reference's word number should be greater than 0.\")\n\n    wer = float(edit_distance) / ref_len\n    return wer\n\n\ndef cer(reference, hypothesis, ignore_case=False, remove_space=False):\n    \"\"\"Calculate charactor error rate (CER). CER compares reference text and\n    hypothesis text in char-level. CER is defined as:\n\n    .. math::\n        CER = (Sc + Dc + Ic) / Nc\n\n    where\n\n    .. code-block:: text\n\n        Sc is the number of characters substituted,\n        Dc is the number of characters deleted,\n        Ic is the number of characters inserted\n        Nc is the number of characters in the reference\n\n    We can use levenshtein distance to calculate CER. Chinese input should be\n    encoded to unicode. Please draw an attention that the leading and tailing\n    space characters will be truncated and multiple consecutive space\n    characters in a sentence will be replaced by one space character.\n\n    :param reference: The reference sentence.\n    :type reference: basestring\n    :param hypothesis: The hypothesis sentence.\n    :type hypothesis: basestring\n    :param ignore_case: Whether case-sensitive or not.\n    :type ignore_case: bool\n    :param remove_space: Whether remove internal space characters\n    :type remove_space: bool\n    :return: Character error rate.\n    :rtype: float\n    :raises ValueError: If the reference length is zero.\n    \"\"\"\n    edit_distance, ref_len = char_errors(reference, hypothesis, ignore_case,\n                                         remove_space)\n\n    if ref_len == 0:\n        raise ValueError(\"Length of reference should be greater than 0.\")\n\n    cer = float(edit_distance) / ref_len\n    return cer\n", "path": "utils/error_rate.py"}]}