From 6b3972302972c41853c4f65b81652d9ba9c99c66 Mon Sep 17 00:00:00 2001 From: Bifang <915779419@qq.com> Date: Mon, 12 Oct 2026 14:30:22 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=95=B0=E5=AD=97=E5=BD=92?= =?UTF-8?q?=E4=B8=80=E5=8C=96=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/utils/text_processing.py | 45 +++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/app/utils/text_processing.py b/app/utils/text_processing.py index 68b6ab3..4f7cee3 100644 --- a/app/utils/text_processing.py +++ b/app/utils/text_processing.py @@ -5,12 +5,33 @@ """ import logging +import re logger = logging.getLogger(__name__) # itntext导入 - 延迟导入以避免初始化问题 _itntext_normalizer = None +_CHINESE_DECIMAL_SCALE_PATTERN = re.compile( + r"(?P[零〇○一二两三四五六七八九十百千万亿]+)点" + r"(?P[零〇○一二两三四五六七八九]+)(?P万|亿)" +) +_CHINESE_DIGITS = { + "零": "0", + "〇": "0", + "○": "0", + "一": "1", + "二": "2", + "两": "2", + "三": "3", + "四": "4", + "五": "5", + "六": "6", + "七": "7", + "八": "8", + "九": "9", +} + def _get_normalizer(): """获取itntext标准化器实例(单例模式)""" @@ -45,7 +66,29 @@ def apply_itn_to_text(text: str) -> str: try: normalizer = _get_normalizer() - result = normalizer.normalize(text) + # Protect Chinese decimal quantities with 万/亿: generic ITN rules may + # interpret the spoken fractional digits as separate magnitude groups. + # Normalize surrounding text normally, and keep the fraction digit-by-digit. + matches = list(_CHINESE_DECIMAL_SCALE_PATTERN.finditer(text)) + if matches: + parts = [] + cursor = 0 + for match in matches: + parts.append(normalizer.normalize(text[cursor:match.start()])) + integer = str(normalizer.normalize(match.group("integer"))).strip() + if not re.fullmatch(r"\d+", integer): + # If this ITN version cannot normalize the integer component, + # fall back to its regular whole-text path rather than guess. + result = normalizer.normalize(text) + break + fraction = "".join(_CHINESE_DIGITS[digit] for digit in match.group("fraction")) + parts.append(f"{integer}.{fraction}{match.group('scale')}") + cursor = match.end() + else: + parts.append(normalizer.normalize(text[cursor:])) + result = "".join(parts) + else: + result = normalizer.normalize(text) logger.debug(f"ITN处理: '{text}' -> '{result}'") return result except Exception as e: