add negative numbers and decimals for re range

This commit is contained in:
TianYuan 2021-08-30 06:43:36 +00:00
parent cf26e2932b
commit 06cbdb1ebd
1 changed files with 8 additions and 6 deletions

View File

@ -162,8 +162,9 @@ def replace_number(match) -> str:
# 范围表达式 # 范围表达式
# 12-23, 12~23 # match.group(1) and match.group(8) are copy from RE_NUMBER
RE_RANGE = re.compile(r'(\d+)[-~](\d+)') RE_RANGE = re.compile(
r'((-?)((\d+)(\.\d+)?)|(\.(\d+)))[-~]((-?)((\d+)(\.\d+)?)|(\.(\d+)))')
def replace_range(match) -> str: def replace_range(match) -> str:
@ -175,9 +176,9 @@ def replace_range(match) -> str:
---------- ----------
str str
""" """
first, second = match.group(1), match.group(2) first, second = match.group(1), match.group(8)
first: str = num2str(first) first = RE_NUMBER.sub(replace_number, first)
second: str = num2str(second) second = RE_NUMBER.sub(replace_number, second)
result = f"{first}{second}" result = f"{first}{second}"
return result return result
@ -241,7 +242,8 @@ def num2str(value_string: str) -> str:
decimal = decimal.rstrip('0') decimal = decimal.rstrip('0')
if decimal: if decimal:
# '.22' is verbalized as '点二二' # '.22' is verbalized as '点二二'
# '3.20' is verbalized as '三点二 # '3.20' is verbalized as '三点二
result = result if result else ""
result += '' + verbalize_digit(decimal) result += '' + verbalize_digit(decimal)
return result return result