提交 9752884e 编写于 作者: Y yangyaming

Follow comments.

上级 8e3c26fe
# -- * -- coding: utf-8 -- * -- # -*- coding: utf-8 -*-
"""
This module provides functions to calculate error rate in different level.
e.g. wer for word-level, cer for char-level.
"""
import numpy as np import numpy as np
...@@ -14,9 +19,9 @@ def levenshtein_distance(ref, hyp): ...@@ -14,9 +19,9 @@ def levenshtein_distance(ref, hyp):
if hyp_len == 0: if hyp_len == 0:
return ref_len return ref_len
distance = np.zeros((ref_len + 1, hyp_len + 1), dtype=np.int64) distance = np.zeros((ref_len + 1, hyp_len + 1), dtype=np.int32)
# initialization distance matrix # initialize distance matrix
for j in xrange(hyp_len + 1): for j in xrange(hyp_len + 1):
distance[0][j] = j distance[0][j] = j
for i in xrange(ref_len + 1): for i in xrange(ref_len + 1):
...@@ -36,11 +41,10 @@ def levenshtein_distance(ref, hyp): ...@@ -36,11 +41,10 @@ def levenshtein_distance(ref, hyp):
return distance[ref_len][hyp_len] return distance[ref_len][hyp_len]
def wer(reference, hypophysis, delimiter=' ', filter_none=True): def wer(reference, hypothesis, ignore_case=False, delimiter=' '):
""" """
Calculate word error rate (WER). WER is a popular evaluation metric used Calculate word error rate (WER). WER compares reference text and
in speech recognition. It compares a reference with an hypophysis and hypothesis text in word-level. WER is defined as:
is defined like this:
.. math:: .. math::
WER = (Sw + Dw + Iw) / Nw WER = (Sw + Dw + Iw) / Nw
...@@ -54,41 +58,39 @@ def wer(reference, hypophysis, delimiter=' ', filter_none=True): ...@@ -54,41 +58,39 @@ def wer(reference, hypophysis, delimiter=' ', filter_none=True):
Iw is the number of words inserted, Iw is the number of words inserted,
Nw is the number of words in the reference Nw is the number of words in the reference
We can use levenshtein distance to calculate WER. Please draw an attention We can use levenshtein distance to calculate WER. Please draw an attention that
that this function will truncate the beginning and ending delimiter for empty items will be removed when splitting sentences by delimiter.
reference and hypophysis sentences before calculating WER.
:param reference: The reference sentence. :param reference: The reference sentence.
:type reference: str :type reference: basestring
:param hypophysis: The hypophysis sentence. :param hypothesis: The hypothesis sentence.
:type reference: str :type hypothesis: basestring
:param ignore_case: Whether case-sensitive or not.
:type ignore_case: bool
:param delimiter: Delimiter of input sentences. :param delimiter: Delimiter of input sentences.
:type delimiter: char :type delimiter: char
:param filter_none: Whether to remove None value when splitting sentence. :return: Word error rate.
:type filter_none: bool
:return: WER
:rtype: float :rtype: float
""" """
if ignore_case == True:
reference = reference.lower()
hypothesis = hypothesis.lower()
if len(reference.strip(delimiter)) == 0: ref_words = filter(None, reference.split(delimiter))
raise ValueError("Reference's word number should be greater than 0.") hyp_words = filter(None, hypothesis.split(delimiter))
if filter_none == True: if len(ref_words) == 0:
ref_words = filter(None, reference.strip(delimiter).split(delimiter)) raise ValueError("Reference's word number should be greater than 0.")
hyp_words = filter(None, hypophysis.strip(delimiter).split(delimiter))
else:
ref_words = reference.strip(delimiter).split(delimiter)
hyp_words = reference.strip(delimiter).split(delimiter)
edit_distance = levenshtein_distance(ref_words, hyp_words) edit_distance = levenshtein_distance(ref_words, hyp_words)
wer = float(edit_distance) / len(ref_words) wer = float(edit_distance) / len(ref_words)
return wer return wer
def cer(reference, hypophysis, squeeze=True, ignore_case=False, strip_char=''): def cer(reference, hypothesis, ignore_case=False):
""" """
Calculate charactor error rate (CER). CER will compare reference text and Calculate charactor error rate (CER). CER compares reference text and
hypophysis text in char-level. CER is defined as: hypothesis text in char-level. CER is defined as:
.. math:: .. math::
CER = (Sc + Dc + Ic) / Nc CER = (Sc + Dc + Ic) / Nc
...@@ -97,41 +99,35 @@ def cer(reference, hypophysis, squeeze=True, ignore_case=False, strip_char=''): ...@@ -97,41 +99,35 @@ def cer(reference, hypophysis, squeeze=True, ignore_case=False, strip_char=''):
.. code-block:: text .. code-block:: text
Sc is the number of character substituted, Sc is the number of characters substituted,
Dc is the number of deleted, Dc is the number of characters deleted,
Ic is the number of inserted Ic is the number of characters inserted
Nc is the number of characters in the reference Nc is the number of characters in the reference
We can use levenshtein distance to calculate CER. Chinese input should be We can use levenshtein distance to calculate CER. Chinese input should be
encoded to unicode. encoded to unicode. Please draw an attention that the leading and tailing
white space characters will be truncated and multiple consecutive white
space characters in a sentence will be replaced by one white space character.
:param reference: The reference sentence. :param reference: The reference sentence.
:type reference: str :type reference: basestring
:param hypophysis: The hypophysis sentence. :param hypothesis: The hypothesis sentence.
:type reference: str :type hypothesis: basestring
:param squeeze: If set true, consecutive space character
will be squeezed to one
:type squeeze: bool
:param ignore_case: Whether case-sensitive or not. :param ignore_case: Whether case-sensitive or not.
:type ignore_case: bool :type ignore_case: bool
:param strip_char: If not set to '', strip_char in beginning and ending of :return: Character error rate.
sentence will be truncated.
:type strip_char: char
:return: CER
:rtype: float :rtype: float
""" """
if ignore_case == True: if ignore_case == True:
reference = reference.lower() reference = reference.lower()
hypophysis = hypophysis.lower() hypothesis = hypothesis.lower()
if strip_char != '':
reference = reference.strip(strip_char) reference = ' '.join(filter(None, reference.split(' ')))
hypophysis = hypophysis.strip(strip_char) hypothesis = ' '.join(filter(None, hypothesis.split(' ')))
if squeeze == True:
reference = ' '.join(filter(None, reference.split(' ')))
hypophysis = ' '.join(filter(None, hypophysis.split(' ')))
if len(reference) == 0: if len(reference) == 0:
raise ValueError("Length of reference should be greater than 0.") raise ValueError("Length of reference should be greater than 0.")
edit_distance = levenshtein_distance(reference, hypophysis)
edit_distance = levenshtein_distance(reference, hypothesis)
cer = float(edit_distance) / len(reference) cer = float(edit_distance) / len(reference)
return cer return cer
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册