jiwer 库

pip install jiwer

计算准确率例子



from jiwer import compute_measures
from typing import List

# 模拟类,模仿 Hugging Face metric.add_batch 的形式
class JiwerMetric:
    def __init__(self):
        self.preds = []
        self.refs = []

    def add_batch(self, predictions: List[str], references: List[str]):
        self.preds.extend(predictions)
        self.refs.extend(references)

    def compute(self):
        # 使用 jiwer 计算整体评估指标
        return compute_measures(self.refs, self.preds)

# 示例数据
decoded_preds = [
    "hello world",
    "how are you",
    "good morning"
]

decoded_labels = [
    "hello word",      # 少一个 'l'
    "how are you",     # 完全一致
    "good mourning"    # 拼写错误
]

# 使用 JiwerMetric
metric = JiwerMetric()
metric.add_batch(predictions=decoded_preds, references=decoded_labels)

# 计算评估结果
result = metric.compute()

print("评估结果:")
for k, v in result.items():
    if isinstance(v, (float, int)):
        print(f"{k}: {v:.4f}")
    else:
        print(f"{k}: {v}")

更多推荐