25acf9db6e
横向对比云端 gummy 与本地开源模型(faster-whisper/SenseVoice/Paraformer), 重点覆盖中英混说,产出准确率(CER/WER/MER)/速度(延迟/RTF)/资源(cpu/mem/模型大小) 对比报告。公共集(ASCEND/AISHELL/LibriSpeech)统一走 HF 适配器 + 自定义 JSONL manifest。 gummy 引擎对照 server/internal/asr/gummy.go 协议移植。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
"""评分前文本归一化。
|
|
|
|
中英混说不归一化就算分会严重失真(标点、繁简、全半角、大小写都会被算成错)。
|
|
本模块做最小必要归一化;繁→简依赖 opencc,缺失时自动降级(仅告警一次)。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
import sys
|
|
import unicodedata
|
|
|
|
_cc = None
|
|
_cc_warned = False
|
|
|
|
|
|
def _converter():
|
|
global _cc, _cc_warned
|
|
if _cc is not None:
|
|
return _cc
|
|
try:
|
|
from opencc import OpenCC
|
|
|
|
_cc = OpenCC("t2s")
|
|
except Exception:
|
|
_cc = False # 标记尝试过且失败
|
|
if not _cc_warned:
|
|
print("[text_norm] 警告:opencc 不可用,跳过繁→简归一化", file=sys.stderr)
|
|
_cc_warned = True
|
|
return _cc
|
|
|
|
|
|
# 中英文常见标点(评分时整体移除)
|
|
_PUNCT = set(
|
|
",。!?、;:“”‘’()《》【】〔〕…—~·.,!?;:\"'()<>[]{}~`@#$%^&*-_=+|\\/。."
|
|
)
|
|
_WS = re.compile(r"\s+")
|
|
|
|
|
|
def normalize(text: str, *, t2s: bool = True, lower: bool = True) -> str:
|
|
if not text:
|
|
return ""
|
|
# 全角→半角 + Unicode 兼容分解(NFKC 会把 ABC→ABC、123→123)
|
|
text = unicodedata.normalize("NFKC", text)
|
|
if t2s:
|
|
cc = _converter()
|
|
if cc:
|
|
text = cc.convert(text)
|
|
if lower:
|
|
text = text.lower()
|
|
text = "".join(ch for ch in text if ch not in _PUNCT)
|
|
text = _WS.sub(" ", text).strip()
|
|
return text
|