fugashi_wer / fugashi_wer.py
esnya
:bug: fix: Use tagger.parse
cff653f
raw
history blame contribute delete
No virus
4.35 kB
""" Word Error Rate (WER) metric for Japanese text powered by fugashi"""
from functools import cached_property
import datasets
import evaluate
from fugashi import Tagger
tagger = Tagger("-Owakati")
_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) metric for Japanese text powered by fugashi.
To install dictionaly using unidic:
```bash
pip install fugashi[unidic]
python -m unidic download
```
See documentation for [fugashi](https://github.com/polm/fugashi) and [original wer](https://huggingface.co/spaces/evaluate-metric/wer) for more details.
Here is the original description of the metric:
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.
concatenate_texts (bool, default=False): Whether to concatenate all input texts or compute WER iteratively.
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("wer")
>>> wer_score = wer.compute(predictions=predictions, references=references)
>>> print(wer_score)
0.5
"""
class FugashiWER(evaluate.Metric):
"""Word Error Rate (WER) metric for Japanese text powered by fugashi"""
@cached_property
def _wer(self):
return evaluate.load("wer")
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 _apply_fugashi(self, value):
if isinstance(value, str):
return tagger.parse(value)
elif isinstance(value, list):
return [self._apply_fugashi(v) for v in value]
else:
return value
def _compute(self, predictions=None, references=None, concatenate_texts=False):
return self._wer.compute(
predictions=self._apply_fugashi(predictions),
references=self._apply_fugashi(references),
concatenate_texts=concatenate_texts,
)