修复汉语数字问题
This commit is contained in:
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user