【问题标题】:Optimizing WER (Word Error Rate) code?优化 WER(字错误率)代码?
【发布时间】:2018-05-07 10:05:24
【问题描述】:

我正在尝试计算 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)

好吧,这似乎并不算太​​糟糕,但我有数以万计的文本要评估,带有引导程序,而且文本更长。因此,我想找到一种更快的方法来执行 wer 功能。有什么想法吗?

【问题讨论】:

    标签: python performance text nlp


    【解决方案1】:

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

    【讨论】:

    • 我在论文中看到过这个,但我看到了两个主要问题:如果句子不是独立的会怎样(例如在句子末尾添加对应于第一个单词下面的句子)?如果 ASR 输出中的分段没有正确完成怎么办? (例如,一个原始句子通过 ASR 产生两个句子)
    • 如果你的分句模型不好,那显然会影响你的单词错误率。
    • 您对稳健的分割模型有想法吗?因为仅仅因为 ASR 无法正确分割句子,WER 飙升似乎是不对的。
    • 如果你想让你的评估不依赖于句子分割模型,你可以使用ground truth segmentation。
    • 这对参考有效,但我怎样才能使这个基本事实分割与假设保持一致呢?如果对齐不匹配,我最终会得到一个有偏见的 WER,不是吗?
    【解决方案2】:

    很高兴尝试 jiwer 包。它还负责一些数据清理和转换(请参阅https://github.com/jitsi/jiwer)。它适用于中等大小的数据;需要检查大数据。

    一些计算误差的代码:

        import jitwer 
        ground_truth = "ground truth text"
        modified_text = "modified text or output of model to ground truth text" 
        
        # list of transformations you can apply on your text data
        transformation = jiwer.Compose([
            jiwer.ToLowerCase(),
            jiwer.Strip(),
            jiwer.RemoveEmptyStrings(),
            jiwer.RemoveMultipleSpaces(),
            jiwer.RemoveWhiteSpace(replace_by_space=False),
            jiwer.SentencesToListOfWords(word_delimiter=" "),
            jiwer.ExpandCommonEnglishContractions(),
            jiwer.RemoveKaldiNonWords()
        ]) 
    
        wer = jiwer.wer(
            ground_truth, 
            modified_text, 
            truth_transform=transformation, 
            hypothesis_transform=transformation
        )
        mer = jiwer.mer(
            ground_truth, 
            modified_text, 
            truth_transform=transformation, 
            hypothesis_transform=transformation
        )
        wil = jiwer.wil(
            ground_truth, 
            modified_text, 
            truth_transform=transformation, 
            hypothesis_transform=transformation
        )
        measures = jiwer.compute_measures(ground_truth, modified_text)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-24
      • 2023-03-19
      • 2012-04-26
      • 2016-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      相关资源
      最近更新 更多