-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
198 lines (179 loc) · 5.6 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
all = {
"one" : 1,
"two" : 2,
"three" : 3,
"four" : 4,
"five" : 5,
"six" : 6,
"seven" : 7,
"eight" : 8,
"nine" : 9,
"ten" : 10,
"eleven": 11,
"twelve": 12,
"thirteen": 13,
"fourteen": 14,
"fifteen": 15,
"sixteen": 16,
"seventeen": 17,
"eighteen": 18,
"nineteen": 19,
"twenty" : 20,
"thirty" : 30,
"forty" : 40,
"fifty" : 50,
"sixty" : 60,
"seventy" : 70,
"eighty" : 80,
"ninety" : 90,
"hundred" : 100,
"thousand" : 1000,
"million" : 1000000,
"billion" : 1000000000,
"trillion" : 1000000000000,
"quadrillion" : 1000000000000000,
"quintillion" : 1000000000000000000,
"sextillion" : 1000000000000000000000,
"septillion" : 1000000000000000000000000,
"octillion" : 1000000000000000000000000000,
"nonillion" : 1000000000000000000000000000000
};
spliter = {
"thousand" : 1000,
"million" : 1000000,
"billion" : 1000000000,
"trillion" : 1000000000000,
"quadrillion" : 1000000000000000,
"quintillion" : 1000000000000000000,
"sextillion" : 1000000000000000000000,
"septillion" : 1000000000000000000000000,
"octillion" : 1000000000000000000000000000,
"nonillion" : 1000000000000000000000000000000
};
# 数字映射
number_map = {
"零": 0,
"一": 1,
"二": 2,
"两": 2,
"三": 3,
"四": 4,
"五": 5,
"六": 6,
"七": 7,
"八": 8,
"九": 9
}
# 单位映射
unit_map = {
"十": 10,
"百": 100,
"千": 1000,
"万": 10000,
"亿": 100000000
}
ChineseNumberWords = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "百", "千", "两"]
allowNumberWordsCh = ["1","2","3","4","5","6","7","8","9","0","一","二","三","四","五","六","七","八","九","十","零","几","百"]
def judgeBelongToChineseNumbers(inputnumber):
for s in inputnumber:
if s not in ChineseNumberWords:
return False
return True
def convertWordsToNumber_Chinese(inputnumber):
if len(inputnumber) == 0:
return -1
try:
return int(inputnumber)
except Exception as e:
if "十几" in inputnumber:
return 15
if "几十" in inputnumber:
return 20
if "几百" in inputnumber:
return 150
if not judgeBelongToChineseNumbers(inputnumber):
return -1
if "十" == inputnumber:
return 10
output = 0
num = 0
for index, cn_num in enumerate(inputnumber):
if cn_num in number_map:
# 数字
num = number_map[cn_num]
# 最后的个位数字
if index == len(inputnumber) - 1:
output = output + num
elif cn_num in unit_map:
# 单位
unit = unit_map[cn_num]
# 累加
output = output + num * unit
num = 0
else:
return -1
return output
def convertWordsToNumber(inputnumber):
try:
return int(inputnumber)
except Exception as e:
try:
inputnumber = inputnumber.lower()
inputnumber = inputnumber.replace("-"," ")
tokens = inputnumber.split(" ")
result = 0
partial_result = 0
for index in range(len(tokens)):
if tokens[index] in spliter:
if partial_result == 0:
partial_result = 1
partial_result *= all[tokens[index]]
result += partial_result
partial_result = 0
else:
if tokens[index] == "hundred":
if partial_result == 0:
partial_result = 1
partial_result *= all[tokens[index]]
else:
partial_result += all[tokens[index]]
result += partial_result
return result
except Exception as e:
if "dozens" in inputnumber:
return 20
elif "hundreds" in inputnumber:
return 150
else:
raise e
def solveTranslationProblem(s_ch):
s_ch = s_ch.replace(" ", "")
if s_ch[-1] != "。":
s_ch += "。"
s_ch = s_ch[:-1].replace("。", ",") + "。"
# 为了解决翻译bug,处理人
indexList = []
for i, w in enumerate(s_ch):
if s_ch[i] != '人':
continue
if i > 0:
if s_ch[i-1] in allowNumberWordsCh:
indexList.append([i,0])
if i > 1:
if s_ch[i-1] == '个' and s_ch[i-2] in allowNumberWordsCh:
indexList.append([i,1])
res = ""
lastIndex = 0
for index in indexList:
res += s_ch[lastIndex:index[0]]
lastIndex = index[0] + 1
if index[1] == 0:
res += "个人类"
else:
res += "人类"
res += s_ch[lastIndex:]
return res
if __name__=="__main__":
print(convertWordsToNumber("thirty-five"))
print(convertWordsToNumber_Chinese("一百二十三"))
print(solveTranslationProblem("一共有20人,一共有30个人,你说呢"))