Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions cosyvoice/utils/frontend_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
24 changes: 24 additions & 0 deletions test_frontend_utils.py
Original file line number Diff line number Diff line change
@@ -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()