From ad0e0e758fb4b8418c6bd2792eb37ae5c4cb2e73 Mon Sep 17 00:00:00 2001 From: b84330808 Date: Tue, 3 Nov 2026 10:41:06 +0900 Subject: [PATCH] =?UTF-8?q?Fix=20negative=20decimals=20being=20read=20with?= =?UTF-8?q?=20an=20extra=20=E9=9B=B6=20(=E4=BF=AE=E5=A4=8D=E8=B4=9F?= =?UTF-8?q?=E5=B0=8F=E6=95=B0=E5=A4=9A=E8=AF=BB=E2=80=9C=E9=9B=B6=E2=80=9D?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit RE_INTEGER ran before RE_DECIMAL_NUM and took the integer part of a negative decimal, so -1.5 was read as 负一零点五 and -0.5 as 负零零点五. Skip integers that are followed by a decimal part. Co-Authored-By: Claude Opus 5.5 --- GPT_SoVITS/text/zh_normalization/num.py | 3 ++- test_zh_normalization_num.py | 26 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 test_zh_normalization_num.py diff --git a/GPT_SoVITS/text/zh_normalization/num.py b/GPT_SoVITS/text/zh_normalization/num.py index 14d602b0..54896b21 100644 --- a/GPT_SoVITS/text/zh_normalization/num.py +++ b/GPT_SoVITS/text/zh_normalization/num.py @@ -75,7 +75,8 @@ def replace_percentage(match) -> str: # 整数表达式 # 带负号的整数 -10 -RE_INTEGER = re.compile(r"(-)" r"(\d+)") +# 不匹配负小数(如 -1.5)的整数部分 +RE_INTEGER = re.compile(r"(-)" r"(\d+)" r"(?!\.?\d)") def replace_negative_num(match) -> str: diff --git a/test_zh_normalization_num.py b/test_zh_normalization_num.py new file mode 100644 index 00000000..e8a12f84 --- /dev/null +++ b/test_zh_normalization_num.py @@ -0,0 +1,26 @@ +"""Regressions for reading numbers in Chinese text normalization, without loading any model.""" + +import sys +import unittest +from pathlib import Path + +sys.path[:0] = [str(Path(__file__).resolve().parent / "GPT_SoVITS")] +from text.zh_normalization.text_normlization import TextNormalizer # noqa: E402 + + +class NegativeNumberTests(unittest.TestCase): + def setUp(self): + self.normalizer = TextNormalizer() + + def test_negative_decimals(self): + self.assertEqual(self.normalizer.normalize_sentence("-1.5"), "负一点五") + self.assertEqual(self.normalizer.normalize_sentence("-0.5"), "负零点五") + self.assertEqual(self.normalizer.normalize_sentence("余额为-3.75元"), "余额为负三点七五元") + + def test_negative_integers(self): + self.assertEqual(self.normalizer.normalize_sentence("-10"), "负十") + self.assertEqual(self.normalizer.normalize_sentence("温度是-1。"), "温度是负一。") + + +if __name__ == "__main__": + unittest.main()