Sclite-backed WER/CER evaluation engine for ASR outputs.
Project description
prama
prama 是一个基于 sclite 的语音识别评估引擎,用于在 Python 中计算 ASR 结果的 WER(Word Error Rate)和 CER(Character Error Rate),默认返回轻量汇总统计,也可以按需返回可继续分析的对齐结果和 sclite 报告文本。
当前包的定位是轻量评估库:高层 API 面向文本列表评估,底层保留 ScliteClient 以便直接调用 sclite 对齐能力。
功能特性
- 计算 WER 和 CER。
- 支持多条 utterance 批量评估。
- 默认只返回总体统计,降低大批量评估时的内存占用。
- 可选返回分组统计、逐 token 对齐结果和 PRA 文本报告。
- 封装
libsclite.so,默认从包内src/prama/lib/libsclite.so查找动态库。 - 支持通过
SCLITE_LIB_PATH或初始化参数指定自定义libsclite.so路径。
环境要求
- Python
>=3.10, <3.13 - Linux 环境
- Poetry
安装依赖:
poetry install
运行测试:
poetry run pytest
快速开始
计算 WER:
from prama.evaluator import get_wer
result = get_wer(
references=["the quick brown fox jumps over the lazy dog"],
hypotheses=["the quick brown fox jumped over lazy dog"],
utterance_ids=["sample-a"],
)
print(result.wer)
print(result.summary.substitutions)
如果需要逐 utterance 的 token 对齐结果和 PRA 报告,可以显式开启:
from prama.evaluator import get_wer
result = get_wer(
references=["the quick brown fox jumps over the lazy dog"],
hypotheses=["the quick brown fox jumped over lazy dog"],
utterance_ids=["sample-a"],
include_details=True,
include_report=True,
)
print(result.utterances[0].tokens)
print(result.report)
计算 CER:
from prama.evaluator import get_cer
result = get_cer(
references=["你好世界"],
hypotheses=["你好世"],
)
print(result.cer)
print(result.summary.deletions)
复用评估器:
from prama.evaluator import Evaluator
with Evaluator() as evaluator:
wer_result = evaluator.get_wer(["hello world"], ["hello word"])
cer_result = evaluator.get_cer(["hello"], ["hallo"])
print(wer_result.wer)
print(cer_result.cer)
高层 API
get_wer
get_wer(
references: list[str],
hypotheses: list[str],
utterance_ids: list[str] | None = None,
*,
include_details: bool = False,
include_report: bool = False,
batch_size: int | None = 1000,
) -> WerResult
get_cer
get_cer(
references: list[str],
hypotheses: list[str],
utterance_ids: list[str] | None = None,
*,
include_details: bool = False,
include_report: bool = False,
batch_size: int | None = 1000,
) -> WerResult
参数说明:
references:参考文本列表。hypotheses:识别结果文本列表。utterance_ids:可选的 utterance ID 列表。未传入时自动生成utt0001、utt0002等 ID。include_details:是否返回每个 utterance 的分组统计和 token 对齐结果。默认False。include_report:是否生成 PRA 文本报告。默认False。batch_size:轻量汇总模式下的自动分批大小,默认1000。传入None时关闭分批,按整批调用sclite。
当 include_details=False 且 include_report=False 时,高层 API 会按 batch_size 分批评估并累加汇总计数,以避免一次性生成所有样本的 native 对齐结果。开启明细或报告后会使用整批评估,以保持返回内容完整。
输入约束:
references、hypotheses和utterance_ids必须是list[str]。references和hypotheses允许长度不一致,缺失的一侧会按空文本参与评估。utterance_ids如果传入,长度必须等于实际评估对数。utterance_ids不能包含空字符串、括号或换行符。
返回结果
get_wer 和 get_cer 返回 WerResult:
@dataclass(frozen=True, slots=True)
class WerResult:
summary: ScliteCounts
groups: list[ScliteGroup]
utterances: list[WerUtterance]
report: str
metric: str
常用字段:
result.wer:WER 数值。result.cer:CER 数值。CER 复用sclite的错误率字段。result.accuracy:准确率。result.summary:总体统计,包含correct、substitutions、deletions、insertions等字段。result.groups:sclite分组统计。仅在include_details=True时填充,否则为空列表。result.utterances:逐 utterance 的 token 对齐结果。仅在include_details=True时填充,否则为空列表。result.report:sclite生成的 PRA 文本报告。仅在include_report=True时填充,否则为空字符串。
底层 sclite 封装
如果需要直接评估 TRN、STM、CTM 等格式文件,可以使用 ScliteClient:
from prama.sclite import Format, IdType, ScliteClient, ScliteOptions
with ScliteClient() as client:
with client.align_files(
"ref.trn",
"hyp.trn",
ref_format=Format.TRN,
hyp_format=Format.TRN,
options=ScliteOptions(id_type=IdType.SP),
) as result:
print(result.summary().wer)
print(result.report_text())
动态库查找顺序:
ScliteClient(lib_path=...)显式传入的路径。- 环境变量
SCLITE_LIB_PATH。 - 包内
prama/lib/libsclite.so。 - 系统库搜索结果。
开发说明
本项目使用 Poetry 管理依赖和命令:
poetry install
poetry run pytest
测试用例位于 tests/test_sclite,覆盖高层评估 API 和底层 sclite wrapper。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file prama-0.1.1.tar.gz.
File metadata
- Download URL: prama-0.1.1.tar.gz
- Upload date:
- Size: 188.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.12.3 Linux/6.17.0-22-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
88b18f7e604261c4af9faa1836b3261dd3f1403a4f786eaf2f8e8189af29f96b
|
|
| MD5 |
aca73bd073e66c6ba62595c4a054bbca
|
|
| BLAKE2b-256 |
629f31c77aa6e43023e47de1d594164fc938f332a1865affa2a5ea6521ec7430
|
File details
Details for the file prama-0.1.1-py3-none-any.whl.
File metadata
- Download URL: prama-0.1.1-py3-none-any.whl
- Upload date:
- Size: 188.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.12.3 Linux/6.17.0-22-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c7524b0e0228fe48f680782adc12316897b5cf069972c7011b13164d9061bf8
|
|
| MD5 |
ab45d81ad538e290cdd53bc85ee4ea9f
|
|
| BLAKE2b-256 |
3be54fe6b65cde4b61bff2de9b0e95cfba26c9adf78f61de9a50f1ab33dd81c9
|