-
Notifications
You must be signed in to change notification settings - Fork 5
/
korean2num.py
203 lines (182 loc) · 4.53 KB
/
korean2num.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
199
200
201
202
203
import math
"""
Developed by Junseong Kim, Atlas Guide
[email protected] / github.com/codertimo
Korean to number
"""
numbers = [
("0", 0),
("1", 1),
("2", 2),
("3", 3),
("4", 4),
("5", 5),
("6", 6),
("7", 7),
("8", 8),
("9", 9),
("스물", 20),
("서른", 30),
("마흔", 40),
("쉰", 50),
("예순", 60),
("일흔", 70),
("여든", 80),
("아흔", 90),
("하나", 1),
("한", 1),
("두", 2),
("둘", 2),
("세", 3),
("셋", 3),
("네", 4),
("넷", 4),
("다섯", 5),
("여섯", 6),
("일곱", 7),
("여덟", 8),
("여덜", 8),
("아홉", 9),
("일", 1),
("이", 2),
("삼", 3),
("사", 4),
("오", 5),
("육", 6),
("칠", 7),
("팔", 8),
("구", 9),
("열", 10),
("십", 10),
("백", 100),
("천", 1000),
("만", 10000),
("억", 100000000),
("조", 1000000000000),
("경", 10000000000000000),
("해", 100000000000000000000),
]
number_types = {
"키로": "kg",
"키로그램": "kg",
"킬로": "kg",
"킬로그램": "kg",
'킬로그람': "kg",
"그램": "g",
"그람": "g",
"리터": "L",
"밀리리터": "mL",
"미리리터": "mL",
"미리": "mL",
"밀리": "mL",
"센치미터": "cm",
"센티미터": "cm",
"밀리미터": "mm",
"미터": "m",
"제곱미터": "㎡",
"개입": "개입",
"개": "개",
"명": "명",
"원": "원",
"묶음": "묶음",
"단": "단",
"모": "모",
"세트": "세트",
"병": "병",
"장": "장",
"박스": "박스",
"봉지": "봉지",
"팩": "팩",
"줄": "줄",
"망": "망",
"포": "포",
"말": "말",
"캔": "캔",
"판": "판",
"자루": "자루",
"가마니": "가마니",
"통": "통",
}
float_nums = [
("일", 1),
("이", 2),
("삼", 3),
("사", 4),
("오", 5),
("육", 6),
("칠", 7),
("팔", 8),
("구", 9)
]
def decode(korean_num):
decode_result = []
result = 0
temp_result = 0
index = 0
number_type = None
for word in korean_num.split():
if word in number_types:
number_type = number_types.get(word)
elif word.isdigit():
result = int(word)
if result > 0:
if number_type is not None:
return str(result) + number_type
else:
return result
float_dividing = korean_num.split("점")
float_result = ""
if len(float_dividing) == 2:
korean_num = float_dividing[0]
float_num = float_dividing[1]
for c in float_num:
for float_num, float_value in float_nums:
if c == float_num:
float_result += str(float_value)
break
if len(float_result) == 0:
float_result = 0.0
else:
float_result = float("0." + float_result)
else:
float_result = 0.0
while index < len(korean_num):
for number, true_value in numbers:
if index + len(number) <= len(korean_num):
if korean_num[index:index + len(number)] == number:
decode_result.append((true_value, math.log10(true_value).is_integer()))
if len(number) == 2:
index += 1
break
index += 1
for index, (number, is_natural) in enumerate(decode_result):
if is_natural:
if math.log10(number) > 3 and (math.log10(number) - 4) % 4 == 0:
result += temp_result * number
temp_result = 0
elif index - 1 >= 0:
if not decode_result[index - 1][1]:
temp_result += number * decode_result[index - 1][0]
else:
temp_result += number
else:
temp_result += number
else:
if index + 1 == len(decode_result):
temp_result += number
elif not decode_result[index + 1][1]:
temp_result += number
elif math.log10(decode_result[index + 1][0]) > 3 and (
math.log10(decode_result[index + 1][0]) - 4) % 4 == 0:
temp_result += number
result += temp_result
if float_result != 0.0:
result += float_result
if number_type is not None:
result = str(result) + number_type
return result
def run_test():
result = decode("3175만3235")
print(result)
if __name__ == "__main__":
run_test()