diff --git a/num2words/lang_KO.py b/num2words/lang_KO.py index 65f4531e..4664533f 100644 --- a/num2words/lang_KO.py +++ b/num2words/lang_KO.py @@ -20,6 +20,8 @@ from .base import Num2Word_Base from .currency import parse_currency_parts +import re + class Num2Word_KO(Num2Word_Base): CURRENCY_FORMS = { @@ -93,27 +95,26 @@ def merge(self, lpair, rpair): def to_ordinal(self, value): self.verify_ordinal(value) - if value == 1: - return "첫 번째" outwords = self.to_cardinal(value).split(" ") - lastwords = outwords[-1].split("백") - if "십" in lastwords[-1]: - ten_one = lastwords[-1].split("십") - ten_one[0] = self.ords[ten_one[0] + "십"] - try: - ten_one[1] = self.ords[ten_one[1]] - ten_one[0] = ten_one[0].replace("스무", "스물") - except KeyError: - pass - lastwords[-1] = ''.join(ten_one) - else: - lastwords[-1] = self.ords[lastwords[-1]] - outwords[-1] = "백 ".join(lastwords) - return " ".join(outwords) + " 번째" + if value % 100 != 0: + re_mid_words = '|'.join('(?<={})'.format(re.escape(delim)) + for delim in ['천', '백']) + lastwords = re.split(re_mid_words, outwords[-1]) + if "십" in lastwords[-1]: + ten_one = lastwords[-1].split("십") + ten_one[0] = self.ords.get(ten_one[0] + "십", ten_one[0] + "십") + ten_one[1] = self.ords.get(ten_one[1], ten_one[1]) + if not ten_one[1]: + ten_one[0] = ten_one[0].replace("스물", "스무") + lastwords[-1] = ''.join(ten_one) + else: + lastwords[-1] = self.ords.get(lastwords[-1], lastwords[-1]) + outwords[-1] = ''.join(lastwords) + return ' '.join(x.strip() for x in outwords if x.strip()) def to_ordinal_num(self, value): self.verify_ordinal(value) - return "%s 번째" % (value) + return "%s" % (value) def to_year(self, val, suffix=None, longval=True): if val < 0: diff --git a/tests/test_ko.py b/tests/test_ko.py index c98ad881..357bf13e 100644 --- a/tests/test_ko.py +++ b/tests/test_ko.py @@ -82,12 +82,17 @@ def test_currency(self): n2k(4, to="currency", currency="EUR") def test_ordinal(self): - cases = [(1, "첫 번째"), (101, "백 한 번째"), (2, "두 번째"), (5, "다섯 번째"), - (10, "열 번째"), (25, "스물다섯 번째"), (137, "백 서른일곱 번째")] + cases = [(1, "한"), (101, "백한"), (2, "두"), (5, "다섯"), (1050, "천쉰"), + (10, "열"), (20, "스무"), (21, "스물한"), (100, "백"), + (1000, "천"), (10000, "만"), (100000, "십만"), (1000000, "백만"), + (100001, "십만 한"),(10000000, "천만"), (100000000, "일억"), + (1000000000, "십억"),(10000000000, "백억"), + (100000000000, "천억"), (1000000000000, "일조"), + (10000000000000, "십조"), (25, "스물다섯"), (137, "백서른일곱")] for num, out in cases: self.assertEqual(n2k(num, to="ordinal"), out) def test_ordinal_num(self): - cases = [(1, "1 번째"), (101, "101 번째"), (25, "25 번째")] + cases = [(1, "1"), (101, "101"), (25, "25")] for num, out in cases: self.assertEqual(n2k(num, to="ordinal_num"), out)