修复汉语数字问题

This commit is contained in:
2025-09-30 00:43:09 +08:00
parent a855c69f61
commit 818f2b64ec

View File

@ -38,32 +38,27 @@ def parse_chinese_or_digit(s: str) -> int:
s = s.replace("", "")
chinese_digits = {
'': 1, '': 2, '': 3, '': 4, '': 5,
'': 6, '': 7, '': 8, '': 9, '': 10
chinese_to_arabic = {
'': 0, '': 1, '': 2, '': 3, '': 4,
'': 5, '': 6, '': 7, '': 8, '': 9,
'': 10
}
if s in chinese_digits:
return chinese_digits[s]
if s in chinese_to_arabic:
return chinese_to_arabic[s]
if len(s) == 2 and s[0] == '':
if s[1] in chinese_digits:
return 10 + chinese_digits[s[1]] - 1 # e.g., "十一" = 10 + 1 = 11
try:
chinese_to_arabic = {
'': 0, '': 1, '': 2, '': 3, '': 4,
'': 5, '': 6, '': 7, '': 8, '': 9,
'': 10
}
if s in chinese_to_arabic:
return chinese_to_arabic[s]
if len(s) == 2 and s[0] == '' and s[1] in chinese_to_arabic:
return 10 + chinese_to_arabic[s[1]]
except (ValueError, KeyError):
pass
if s[1] not in chinese_to_arabic:
return -1
return 10 + chinese_to_arabic.get(s[1], 0)
elif len(s) == 2 and s[1] == '':
if s[0] not in chinese_to_arabic:
return -1
return 10 * chinese_to_arabic.get(s[0], 0)
elif len(s) == 3 and s[1] == '':
if s[0] not in chinese_to_arabic or s[2] not in chinese_to_arabic:
return -1
return 10 * chinese_to_arabic.get(s[0], 0) + chinese_to_arabic.get(s[2], 0)
try:
return int(s)