From 1edffbf2b79c2ccc0b98cc59554bc951e666066a Mon Sep 17 00:00:00 2001 From: Sasha Mitchell Date: Tue, 29 Sep 2026 02:31:40 +0700 Subject: [PATCH] Keep one space when two numbers are separated twice. replace_blank dropped every space in a run, so a doubled gap turned 12 and 34 into 1234. --- cosyvoice/utils/frontend_utils.py | 23 ++++++++++++++++------- test_frontend_utils.py | 24 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 test_frontend_utils.py diff --git a/cosyvoice/utils/frontend_utils.py b/cosyvoice/utils/frontend_utils.py index ea1c9fc8c..2289bd006 100644 --- a/cosyvoice/utils/frontend_utils.py +++ b/cosyvoice/utils/frontend_utils.py @@ -120,13 +120,22 @@ def should_merge(_text: str): # remove blank between chinese character def replace_blank(text: str): out_str = [] - for i, c in enumerate(text): - if c == " ": - if ((text[i + 1].isascii() and text[i + 1] != " ") and - (text[i - 1].isascii() and text[i - 1] != " ")): - out_str.append(c) - else: - out_str.append(c) + i = 0 + n = len(text) + while i < n: + if text[i] != " ": + out_str.append(text[i]) + i += 1 + continue + j = i + while j < n and text[j] == " ": + j += 1 + left = text[i - 1] if i else "" + right = text[j] if j < n else "" + if (left and right and left.isascii() and left != " " and + right.isascii() and right != " "): + out_str.append(" ") + i = j return "".join(out_str) diff --git a/test_frontend_utils.py b/test_frontend_utils.py new file mode 100644 index 000000000..7eb54d2d7 --- /dev/null +++ b/test_frontend_utils.py @@ -0,0 +1,24 @@ +import unittest + +from cosyvoice.utils.frontend_utils import replace_blank + + +class ReplaceBlankTests(unittest.TestCase): + def test_doubled_space_between_numbers_stays_a_space(self): + self.assertEqual(replace_blank("版本 12 34"), "版本12 34") + + def test_single_space_between_numbers_stays(self): + self.assertEqual(replace_blank("版本 12 34"), "版本12 34") + + def test_space_beside_chinese_is_still_removed(self): + self.assertEqual(replace_blank("你 a"), "你a") + + def test_single_space_between_ascii_words_stays(self): + self.assertEqual(replace_blank("a b"), "a b") + + def test_doubled_space_between_ascii_words_collapses_to_one(self): + self.assertEqual(replace_blank("a b"), "a b") + + +if __name__ == "__main__": + unittest.main()