Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/coin/zhNum2Int.py
2023-04-30 11:30:59 +08:00

52 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

ZH_NUM_CHAR_DICT = {
'1': 1, '': 1, '': 1, '': 1,
'2': 2, '': 2, '': 2, '': 2, '': 2,
'3': 3, '': 3, '': 3, '': 3,
'4': 4, '': 4, '': 4, '': 4,
'5': 5, '': 5, '': 5,
'6': 6, '': 6, '': 6,
'7': 7, '': 7, '': 7, '': 7,
'8': 8, '': 8, '': 8,
'9': 9, '': 9, '': 9, '': 9,
'0': 0, '': 0, '': 0, '': 0, 'O': 0, 'o': 0,
}
ZH_NUM_CHAR_DICT2 = {
'亿': (100000000, True),
'': (10000, True),
'': (1000, False), '': (1000, False),
'': (100, False), '': (100, False),
'': (10, False), '': (10, False),
'': (1, True)
}
def Zh2Int(chars):
result = 0
buffer = 0
buffer2 = 0
prev_is_num = False
if chars == '':
return 1
chars = chars.replace('廿', '二十')
for c in chars:
if c in ZH_NUM_CHAR_DICT.keys():
if prev_is_num:
buffer *= 10
buffer += ZH_NUM_CHAR_DICT[c]
prev_is_num = True
elif c in ZH_NUM_CHAR_DICT2.keys():
if ZH_NUM_CHAR_DICT2[c][1]:
buffer2 += buffer
result += buffer2 * ZH_NUM_CHAR_DICT2[c][0]
buffer2 = 0
else:
if buffer == 0:
buffer = 1
buffer2 += buffer * ZH_NUM_CHAR_DICT2[c][0]
buffer = 0
prev_is_num = False
else:
raise ValueError(f"存在无法识别的字符:{c}")
result += buffer2 + buffer
return result