Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
9752884e
M
models
项目概览
PaddlePaddle
/
models
接近 2 年 前同步成功
通知
230
Star
6828
Fork
2962
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
602
列表
看板
标记
里程碑
合并请求
255
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
602
Issue
602
列表
看板
标记
里程碑
合并请求
255
合并请求
255
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
9752884e
编写于
6月 12, 2017
作者:
Y
yangyaming
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Follow comments.
上级
8e3c26fe
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
45 addition
and
49 deletion
+45
-49
deep_speech_2/error_rate.py
deep_speech_2/error_rate.py
+45
-49
未找到文件。
deep_speech_2/error_rate.py
浏览文件 @
9752884e
# -- * -- 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
.
int
64
)
distance
=
np
.
zeros
((
ref_len
+
1
,
hyp_len
+
1
),
dtype
=
np
.
int
32
)
# initializ
ation
distance matrix
# initializ
e
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
,
hypo
physis
,
delimiter
=
' '
,
filter_none
=
True
):
def
wer
(
reference
,
hypo
thesis
,
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
,
hypo
physis
,
squeeze
=
True
,
ignore_case
=
False
,
strip_char
=
''
):
def
cer
(
reference
,
hypo
thesis
,
ignore_case
=
False
):
"""
"""
Calculate charactor error rate (CER). CER
will compare
reference text and
Calculate charactor error rate (CER). CER
compares
reference text and
hypo
phy
sis text in char-level. CER is defined as:
hypo
the
sis 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 character
s
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录