From 1a85c7189241d6bce4d212e88adfe281ef4d5296 Mon Sep 17 00:00:00 2001 From: Sasha Mitchell Date: Tue, 29 Sep 2026 01:07:23 +0700 Subject: [PATCH] Read the end minute of a time range on its own, test=tts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 8:30-9:15 was read as 八点半至九点半. The end of the range used the start minute when it decided half past. --- .../frontend/zh_normalization/chronology.py | 2 +- tests/unit/t2s/test_time_range.py | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tests/unit/t2s/test_time_range.py diff --git a/paddlespeech/t2s/frontend/zh_normalization/chronology.py b/paddlespeech/t2s/frontend/zh_normalization/chronology.py index ea4558e2a..c130c679e 100644 --- a/paddlespeech/t2s/frontend/zh_normalization/chronology.py +++ b/paddlespeech/t2s/frontend/zh_normalization/chronology.py @@ -74,7 +74,7 @@ def replace_time(match) -> str: result += "至" result += f"{num2str(hour_2)}点" if minute_2.lstrip('0'): - if int(minute) == 30: + if int(minute_2) == 30: result += "半" else: result += f"{_time_num2str(minute_2)}分" diff --git a/tests/unit/t2s/test_time_range.py b/tests/unit/t2s/test_time_range.py new file mode 100644 index 000000000..e29c4aa95 --- /dev/null +++ b/tests/unit/t2s/test_time_range.py @@ -0,0 +1,43 @@ +import importlib.util +import sys +import types +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[3] +NORM = ROOT / "paddlespeech" / "t2s" / "frontend" / "zh_normalization" + + +def load_chronology(): + pkg_name = "ps_zh_norm_under_test" + pkg = types.ModuleType(pkg_name) + pkg.__path__ = [str(NORM)] + sys.modules[pkg_name] = pkg + + def load(name, filename): + spec = importlib.util.spec_from_file_location( + name, NORM / filename) + module = importlib.util.module_from_spec(spec) + sys.modules[name] = module + spec.loader.exec_module(module) + return module + + load(pkg_name + ".num", "num.py") + return load(pkg_name + ".chronology", "chronology.py") + + +def spoken(text): + chronology = load_chronology() + match = chronology.RE_TIME_RANGE.fullmatch(text) + if match is None: + match = chronology.RE_TIME.fullmatch(text) + return chronology.replace_time(match) + + +def test_range_end_uses_its_own_minutes(): + assert spoken("8:30-9:15") == "八点半至九点十五分" + assert spoken("8:30-9:45") == "八点半至九点四十五分" + assert spoken("8:30~9:15") == "八点半至九点十五分" + assert spoken("8:15-9:30") == "八点十五分至九点半" + assert spoken("8:30-9:30") == "八点半至九点半" + assert spoken("9:30") == "九点半" + assert spoken("9:15") == "九点十五分"