"""评分前文本归一化。 中英混说不归一化就算分会严重失真(标点、繁简、全半角、大小写都会被算成错)。 本模块做最小必要归一化;繁→简依赖 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