Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error about Korean ordinal conversion, only errors on issue #555 #556

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions num2words/lang_KO.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from .base import Num2Word_Base
from .currency import parse_currency_parts

import re


class Num2Word_KO(Num2Word_Base):
CURRENCY_FORMS = {
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 8 additions & 3 deletions tests/test_ko.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading