Hyeonseo's picture
Upload: digit2Text file
3a7b4f7 verified
#-*- coding:utf-8 -*-
# https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=chandong83&logNo=221144077279
# μœ„ μ½”λ“œμ—μ„œ μΆ”κ°€ μˆ˜μ •
# 만 λ‹¨μœ„ 자릿수
tenThousandPos = 4
# μ–΅ λ‹¨μœ„ 자릿수
hundredMillionPos = 9
txtDigit = ['', 'μ‹­', 'λ°±', '천', '만', 'μ–΅']
txtNumber = ['', '일', '이', 'μ‚Ό', '사', '였', '윑', 'μΉ ', 'νŒ”', 'ꡬ']
txtPoint = '쩜 '
def digit2txt(strNum):
resultStr = ''
digitCount = 0
# print(strNum)
#자릿수 카운트
for ch in strNum:
# ',' λ¬΄μ‹œ
if ch == ',':
continue
#μ†Œμˆ«μ  κΉŒμ§€
elif ch == '.':
break
digitCount = digitCount + 1
digitCount = digitCount-1
index = 0
while True:
notShowDigit = False
ch = strNum[index]
#print(str(index) + ' ' + ch + ' ' +str(digitCount))
# ',' λ¬΄μ‹œ
if ch == ',':
index = index + 1
if index >= len(strNum):
break;
continue
if ch == '.':
# [μˆ˜μ •] 0.13 처럼 1μ΄ν•˜μ˜ 값에 λŒ€ν•œ 처리 μΆ”κ°€
if strNum[index - 1] == '0' and not resultStr:
resultStr = '영'
resultStr += txtPoint
else:
# μžλ¦Ώμˆ˜κ°€ 2자리이고 1이면 '일'은 ν‘œμ‹œ μ•ˆν•¨.
# 단 '만' 'μ–΅'μ—μ„œλŠ” ν‘œμ‹œ 함
# [μˆ˜μ •] digitCount >= 1으둜 μ„€μ •ν•˜μ—¬ 'μ‹­' λ‹¨μœ„μ—μ„œλ„ ν‘œμ‹œ
if(digitCount >= 1) and (digitCount != tenThousandPos) and (digitCount != hundredMillionPos) and int(ch) == 1:
resultStr = resultStr + ''
elif int(ch) == 0:
resultStr = resultStr + ''
# 단 '만' 'μ–΅'μ—μ„œλŠ” ν‘œμ‹œ 함
if (digitCount != tenThousandPos) and (digitCount != hundredMillionPos):
notShowDigit = True
else:
resultStr = resultStr + txtNumber[int(ch)]
# 1μ–΅ 이상
if digitCount > hundredMillionPos:
if not notShowDigit:
resultStr = resultStr + txtDigit[digitCount-hundredMillionPos]
# 1만 이상
elif digitCount > tenThousandPos:
if not notShowDigit:
resultStr = resultStr + txtDigit[digitCount-tenThousandPos]
else:
if not notShowDigit:
resultStr = resultStr + txtDigit[digitCount]
if digitCount <= 0:
digitCount = 0
else:
digitCount = digitCount - 1
index = index + 1
if index >= len(strNum):
break;
return resultStr
NATIVE_MAP_ONES = {
# "ν•˜λ‚˜": 1, "λ‘˜": 2, "μ…‹": 3, "λ„·": 4, "λ‹€μ„―": 5,
"ν•œ": 1, "두": 2, "μ„Έ": 3, "λ„·": 4, "λ‹€μ„―": 5,
"μ—¬μ„―": 6, "일곱": 7, "μ—¬λŸ": 8, "아홉": 9,
# "ν•œ": 1, "두": 2, "μ„Έ": 3, "석": 3, "μ„œ": 3, "λ„€": 4, "넉": 4, "λ„ˆ": 4,
# "λ‹·": 5, "μ—Ώ": 6
}
MAP_TENS = {
"μ—΄": 10, "슀물": 20, "μ„œλ₯Έ": 30, "λ§ˆν”": 40, "μ‰°": 50,
"예순": 60, "일흔": 70, "μ—¬λ“ ": 80, "아흔": 90
}
def NNGdigit2txt(number):
# 이 ν•¨μˆ˜λŠ” 주어진 숫자λ₯Ό ν•œκ΅­μ–΄ 발음으둜 λ³€ν™˜ν•©λ‹ˆλ‹€.
# 예: 25 -> "μŠ€λ¬Όλ‹€μ„―", 91 -> "μ•„ν”ν•˜λ‚˜"
# μ—¬κΈ°μ„œλŠ” μ‹­ λ‹¨μœ„μ™€ κΈ°λ³Έ 숫자의 μ‘°ν•©λ§Œμ„ κ³ λ €ν•©λ‹ˆλ‹€.
korean_number = ""
number = int(number)
if number >= 100:
korean_number = digit2txt(str(number))
elif number < 10:
for key, value in NATIVE_MAP_ONES.items():
if value == number:
return key
else:
tens = number // 10
ones = number % 10
for key, value in MAP_TENS.items():
if value == tens * 10:
korean_number += key
for key, value in NATIVE_MAP_ONES.items():
if value == ones:
korean_number += key
return korean_number
def CSign2txt(csign):
currency_symbols = {
"$": "λ‹¬λŸ¬",
"€": "유둜",
"Β£": "νŒŒμš΄λ“œ",
"Β₯": "μ—”",
"οΏ¦": "원"
}
return currency_symbols.get(csign, "")