File size: 5,399 Bytes
fe858e7
 
 
 
 
 
 
 
 
 
 
 
 
02e56fa
fe858e7
 
 
02e56fa
fe858e7
 
 
02e56fa
 
 
 
 
 
fe858e7
 
 
 
02e56fa
 
 
 
 
 
 
fe858e7
02e56fa
 
 
 
 
 
 
 
 
 
 
 
 
fe858e7
 
02e56fa
 
fe858e7
02e56fa
 
 
 
fe858e7
02e56fa
 
fe858e7
 
02e56fa
 
 
 
 
 
fe858e7
 
 
 
 
 
 
 
 
 
 
02e56fa
 
 
 
 
 
 
 
 
 
fe858e7
 
 
 
 
 
02e56fa
fe858e7
02e56fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This is the same as WER, but it also returns detailed information about the erros (insertions, deletions, substitutions)"""

import evaluate
import datasets
from jiwer import compute_measures


_CITATION = """\
@inproceedings{inproceedings,
    author = {Morris, Andrew and Maier, Viktoria and Green, Phil},
    year = {2004},
    month = {01},
    pages = {},
    title = {From WER and RIL to MER and WIL: improved evaluation measures for connected speech recognition.}
}
"""

_DESCRIPTION = """\
Word error rate (WER) is a common metric of the performance of an automatic speech recognition system.

The general difficulty of measuring performance lies in the fact that the recognized word sequence can have a different length from the reference word sequence (supposedly the correct one). The WER is derived from the Levenshtein distance, working at the word level instead of the phoneme level. The WER is a valuable tool for comparing different systems as well as for evaluating improvements within one system. This kind of measurement, however, provides no details on the nature of translation errors and further work is therefore required to identify the main source(s) of error and to focus any research effort.

This problem is solved by first aligning the recognized word sequence with the reference (spoken) word sequence using dynamic string alignment. Examination of this issue is seen through a theory called the power law that states the correlation between perplexity and word error rate.

Word error rate can then be computed as:

WER = (S + D + I) / N = (S + D + I) / (S + D + C)

where

S is the number of substitutions,
D is the number of deletions,
I is the number of insertions,
C is the number of correct words,
N is the number of words in the reference (N=S+D+C).

This value indicates the average number of errors per reference word. The lower the value, the better the
performance of the ASR system with a WER of 0 being a perfect score.
"""

_KWARGS_DESCRIPTION = """
Compute WER score of transcribed segments against references.

Args:
    references: List of references for each speech input.
    predictions: List of transcriptions to score.
    detailed (bool, default=False): Whether to also return normalized substitutions, deletions and insertions.

Returns:
    (float): the word error rate

Examples:

    >>> predictions = ["this is the prediction", "there is an other sample"]
    >>> references = ["this is the reference", "there is another one"]
    >>> wer = evaluate.load("EduardoPacheco/argwer")
    >>> wer_score = wer.compute(predictions=predictions, references=references)
    >>> print(wer_score)
    0.5
"""

@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
class ArgWER(evaluate.Metric):
    """TODO: Short description of my evaluation module."""

    def _info(self):
        return evaluate.MetricInfo(
            description=_DESCRIPTION,
            citation=_CITATION,
            inputs_description=_KWARGS_DESCRIPTION,
            features=datasets.Features(
                {
                    "predictions": datasets.Value("string", id="sequence"),
                    "references": datasets.Value("string", id="sequence"),
                }
            ),
            codebase_urls=["https://github.com/jitsi/jiwer/"],
            reference_urls=[
                "https://en.wikipedia.org/wiki/Word_error_rate",
            ],
        )

    def _download_and_prepare(self, dl_manager):
        """Optional: download external resources useful to compute the scores"""
        pass

    def _compute(self, predictions, references, detailed=False) -> float | dict[str, float]:
        """Returns the scores"""
        num_substitutions = 0
        num_deletions = 0
        num_insertions = 0
        num_hits = 0
        for prediction, reference in zip(predictions, references):
            measures = compute_measures(reference, prediction)
            num_substitutions += measures["substitutions"]
            num_deletions += measures["deletions"]
            num_insertions += measures["insertions"]
            num_hits += measures["hits"]

        total = num_substitutions + num_deletions + num_hits
        incorrect = num_substitutions + num_deletions + num_insertions

        if detailed:
            return dict(
                wer=incorrect / total,
                substitution_rate=num_substitutions / total,
                deletion_rate=num_deletions / total,
                insertion_rate=num_insertions / total,
                num_substitutions=num_substitutions,
                num_deletions=num_deletions,
                num_insertions=num_insertions,
                num_hits=num_hits,
            )
        return incorrect / total