优化WER(字错误率)代码?

时间:2018-05-07 10:05:24

标签: python performance text nlp

我正在尝试计算WER以评估ASR系统,但是得分的计算需要花费很多时间(因为我想在其上执行一些引导以获得置信区间以更加稳健地评估系统)。

以下是我到目前为止提出的代码,有没有人看到更有效的方法(更快,如果你有想法让它更有效,那也是受欢迎的)。

def modify_text(text):
    """
    Function to modify a clean text to add some errors in it.
    """
    modified_text = []
    for word in true_text:
        action = np.random.choice(['deletion','addition','subsitution','nothing'],
                                   p = [0.1,0.1,0.1,0.7])
        if action in ['addition','substitution']:
            modified_text.append(random.choice(voca))
        if action in ['addition','nothing']:
            modified_text.append(word)
    return modified_text

def wer(s1,s2):

    d = np.zeros([len(s1)+1,len(s2)+1])
    d[:,0] = np.arange(len(s1)+1)
    d[0,:] = np.arange(len(s2)+1)

    for j in range(1,len(s2)+1):
        for i in range(1,len(s1)+1):
            if s1[i-1] == s2[j-1]:
                d[i,j] = d[i-1,j-1]
            else:
                d[i,j] = min(d[i-1,j]+1, d[i,j-1]+1, d[i-1,j-1]+1)

    return d[-1,-1]/len(s1)

text = """I am happy to join with you today in what will go down in history as
the greatest demonstration for freedom in the history of our nation.
Five score years ago, a great American, in whose symbolic shadow
we stand today, signed the Emancipation Proclamation. This momentous
decree came as a great beacon light of hope to millions of Negro slaves
who had been seared in the flames of withering injustice. It came as a
joyous daybreak to end the long night of their captivity. """

true_text = list(tokenize(text))
modified_text = modify_text(true_text)
%timeit wer(true_text,modified_text)

输出:

7.04 ms ± 49.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

好的,这看起来并不太糟糕,但我有成千上万的文本要评估,使用bootstrap,文本更长。因此,我想找到一种更快的方法来执行wer功能。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

大多数语音识别评估首先将语音分成句子或话语。然后通过对齐每个话语来计算WER。这可以给你一个很大的加速,因为WER计算是O(n ^ 2)。