Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add percentage support to _num2chinese function #3651

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 11 additions & 2 deletions TTS/tts/utils/text/chinese_mandarin/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ def _num2chinese(num: str, big=False, simp=True, o=False, twoalt=False) -> str:
str: converted number as hanzi characters
"""

# handling percentage
is_percentage = num.endswith("%")
if is_percentage:
num = num[:-1]

# check num first
nd = str(num)
if abs(float(nd)) >= 1e48:
Expand Down Expand Up @@ -98,6 +103,10 @@ def _num2chinese(num: str, big=False, simp=True, o=False, twoalt=False) -> str:
if remainder:
result.append(c_symbol[2])
result.append("".join(c_basic[int(ch)] for ch in remainder))

# Append percentage symbol if applicable
if is_percentage:
result = ["百分之"] + result
return "".join(result)


Expand All @@ -110,7 +119,7 @@ def _number_replace(match) -> str:
Returns:
str: replaced characters for the numbers
"""
match_str: str = match.group()
match_str: str = match.group().replace(",", "")
return _num2chinese(match_str)


Expand All @@ -123,5 +132,5 @@ def replace_numbers_to_characters_in_text(text: str) -> str:
Returns:
str: output text
"""
text = re.sub(r"[0-9]+", _number_replace, text)
text = re.sub(r"[0-9]{1,3}(?:,[0-9]{3})*(?:\.[0-9]+)?%?", _number_replace, text)
return text
Loading