Upload: digit2Text file
Browse files- TextUtil/digit2text.py +134 -0
TextUtil/digit2text.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#-*- coding:utf-8 -*-
|
2 |
+
# https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=chandong83&logNo=221144077279
|
3 |
+
# μ μ½λμμ μΆκ° μμ
|
4 |
+
|
5 |
+
# λ§ λ¨μ μλ¦Ώμ
|
6 |
+
tenThousandPos = 4
|
7 |
+
# μ΅ λ¨μ μλ¦Ώμ
|
8 |
+
hundredMillionPos = 9
|
9 |
+
txtDigit = ['', 'μ', 'λ°±', 'μ²', 'λ§', 'μ΅']
|
10 |
+
txtNumber = ['', 'μΌ', 'μ΄', 'μΌ', 'μ¬', 'μ€', 'μ‘', 'μΉ ', 'ν', 'ꡬ']
|
11 |
+
txtPoint = 'μ© '
|
12 |
+
|
13 |
+
def digit2txt(strNum):
|
14 |
+
resultStr = ''
|
15 |
+
digitCount = 0
|
16 |
+
# print(strNum)
|
17 |
+
#μλ¦Ώμ μΉ΄μ΄νΈ
|
18 |
+
for ch in strNum:
|
19 |
+
# ',' 무μ
|
20 |
+
if ch == ',':
|
21 |
+
continue
|
22 |
+
#μμ«μ κΉμ§
|
23 |
+
elif ch == '.':
|
24 |
+
break
|
25 |
+
digitCount = digitCount + 1
|
26 |
+
|
27 |
+
|
28 |
+
digitCount = digitCount-1
|
29 |
+
index = 0
|
30 |
+
|
31 |
+
while True:
|
32 |
+
notShowDigit = False
|
33 |
+
ch = strNum[index]
|
34 |
+
#print(str(index) + ' ' + ch + ' ' +str(digitCount))
|
35 |
+
# ',' 무μ
|
36 |
+
if ch == ',':
|
37 |
+
index = index + 1
|
38 |
+
if index >= len(strNum):
|
39 |
+
break;
|
40 |
+
continue
|
41 |
+
|
42 |
+
if ch == '.':
|
43 |
+
# [μμ ] 0.13 μ²λΌ 1μ΄νμ κ°μ λν μ²λ¦¬ μΆκ°
|
44 |
+
if strNum[index - 1] == '0' and not resultStr:
|
45 |
+
resultStr = 'μ'
|
46 |
+
resultStr += txtPoint
|
47 |
+
else:
|
48 |
+
# μλ¦Ώμκ° 2μ리μ΄κ³ 1μ΄λ©΄ 'μΌ'μ νμ μν¨.
|
49 |
+
# λ¨ 'λ§' 'μ΅'μμλ νμ ν¨
|
50 |
+
# [μμ ] digitCount >= 1μΌλ‘ μ€μ νμ¬ 'μ' λ¨μμμλ νμ
|
51 |
+
if(digitCount >= 1) and (digitCount != tenThousandPos) and (digitCount != hundredMillionPos) and int(ch) == 1:
|
52 |
+
resultStr = resultStr + ''
|
53 |
+
elif int(ch) == 0:
|
54 |
+
resultStr = resultStr + ''
|
55 |
+
# λ¨ 'λ§' 'μ΅'μμλ νμ ν¨
|
56 |
+
if (digitCount != tenThousandPos) and (digitCount != hundredMillionPos):
|
57 |
+
notShowDigit = True
|
58 |
+
else:
|
59 |
+
resultStr = resultStr + txtNumber[int(ch)]
|
60 |
+
|
61 |
+
|
62 |
+
# 1μ΅ μ΄μ
|
63 |
+
if digitCount > hundredMillionPos:
|
64 |
+
if not notShowDigit:
|
65 |
+
resultStr = resultStr + txtDigit[digitCount-hundredMillionPos]
|
66 |
+
# 1λ§ μ΄μ
|
67 |
+
elif digitCount > tenThousandPos:
|
68 |
+
if not notShowDigit:
|
69 |
+
resultStr = resultStr + txtDigit[digitCount-tenThousandPos]
|
70 |
+
else:
|
71 |
+
if not notShowDigit:
|
72 |
+
resultStr = resultStr + txtDigit[digitCount]
|
73 |
+
|
74 |
+
if digitCount <= 0:
|
75 |
+
digitCount = 0
|
76 |
+
else:
|
77 |
+
digitCount = digitCount - 1
|
78 |
+
index = index + 1
|
79 |
+
if index >= len(strNum):
|
80 |
+
break;
|
81 |
+
return resultStr
|
82 |
+
|
83 |
+
|
84 |
+
NATIVE_MAP_ONES = {
|
85 |
+
# "νλ": 1, "λ": 2, "μ
": 3, "λ·": 4, "λ€μ―": 5,
|
86 |
+
"ν": 1, "λ": 2, "μΈ": 3, "λ·": 4, "λ€μ―": 5,
|
87 |
+
"μ¬μ―": 6, "μΌκ³±": 7, "μ¬λ": 8, "μν": 9,
|
88 |
+
# "ν": 1, "λ": 2, "μΈ": 3, "μ": 3, "μ": 3, "λ€": 4, "λ": 4, "λ": 4,
|
89 |
+
# "λ·": 5, "μΏ": 6
|
90 |
+
}
|
91 |
+
|
92 |
+
MAP_TENS = {
|
93 |
+
"μ΄": 10, "μ€λ¬Ό": 20, "μλ₯Έ": 30, "λ§ν": 40, "μ°": 50,
|
94 |
+
"μμ": 60, "μΌν": 70, "μ¬λ ": 80, "μν": 90
|
95 |
+
}
|
96 |
+
|
97 |
+
|
98 |
+
def NNGdigit2txt(number):
|
99 |
+
# μ΄ ν¨μλ μ£Όμ΄μ§ μ«μλ₯Ό νκ΅μ΄ λ°μμΌλ‘ λ³νν©λλ€.
|
100 |
+
# μ: 25 -> "μ€λ¬Όλ€μ―", 91 -> "μννλ"
|
101 |
+
# μ¬κΈ°μλ μ λ¨μμ κΈ°λ³Έ μ«μμ μ‘°ν©λ§μ κ³ λ €ν©λλ€.
|
102 |
+
korean_number = ""
|
103 |
+
number = int(number)
|
104 |
+
|
105 |
+
if number >= 100:
|
106 |
+
korean_number = digit2txt(str(number))
|
107 |
+
elif number < 10:
|
108 |
+
for key, value in NATIVE_MAP_ONES.items():
|
109 |
+
if value == number:
|
110 |
+
return key
|
111 |
+
else:
|
112 |
+
tens = number // 10
|
113 |
+
ones = number % 10
|
114 |
+
|
115 |
+
for key, value in MAP_TENS.items():
|
116 |
+
if value == tens * 10:
|
117 |
+
korean_number += key
|
118 |
+
|
119 |
+
for key, value in NATIVE_MAP_ONES.items():
|
120 |
+
if value == ones:
|
121 |
+
korean_number += key
|
122 |
+
|
123 |
+
return korean_number
|
124 |
+
|
125 |
+
def CSign2txt(csign):
|
126 |
+
currency_symbols = {
|
127 |
+
"$": "λ¬λ¬",
|
128 |
+
"β¬": "μ λ‘",
|
129 |
+
"Β£": "νμ΄λ",
|
130 |
+
"Β₯": "μ",
|
131 |
+
"οΏ¦": "μ"
|
132 |
+
}
|
133 |
+
|
134 |
+
return currency_symbols.get(csign, "")
|