From 17e2c8511baaa3c8ee638061c6caa5c8672d7a56 Mon Sep 17 00:00:00 2001 From: Dongmin Jang Date: Fri, 5 Jan 2024 17:58:29 +0900 Subject: [PATCH 1/6] Fix error about Korean ordinal conversion, only errors on issue #555 --- num2words/lang_KO.py | 5 ++++- tests/test_ko.py | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/num2words/lang_KO.py b/num2words/lang_KO.py index 65f4531e..b59aceda 100644 --- a/num2words/lang_KO.py +++ b/num2words/lang_KO.py @@ -95,6 +95,8 @@ def to_ordinal(self, value): self.verify_ordinal(value) if value == 1: return "첫 번째" + if value == 100: + return "백 번째" outwords = self.to_cardinal(value).split(" ") lastwords = outwords[-1].split("백") if "십" in lastwords[-1]: @@ -107,7 +109,8 @@ def to_ordinal(self, value): pass lastwords[-1] = ''.join(ten_one) else: - lastwords[-1] = self.ords[lastwords[-1]] + if lastwords[-1] in self.ords: + lastwords[-1] = self.ords[lastwords[-1]] outwords[-1] = "백 ".join(lastwords) return " ".join(outwords) + " 번째" diff --git a/tests/test_ko.py b/tests/test_ko.py index c98ad881..085d0c09 100644 --- a/tests/test_ko.py +++ b/tests/test_ko.py @@ -83,7 +83,8 @@ def test_currency(self): def test_ordinal(self): cases = [(1, "첫 번째"), (101, "백 한 번째"), (2, "두 번째"), (5, "다섯 번째"), - (10, "열 번째"), (25, "스물다섯 번째"), (137, "백 서른일곱 번째")] + (10, "열 번째"), (100, "백 번째"), (1000, "천 번째"), (10000, "만 번째"), + (25, "스물다섯 번째"), (137, "백 서른일곱 번째")] for num, out in cases: self.assertEqual(n2k(num, to="ordinal"), out) From d867574f3c8758ddb14ba3b985b87f3dd11f1b67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dongmin=20Jang=20=28=EC=9E=A5=EB=8F=99=EB=AF=BC=29?= <37837785+dogzz9445@users.noreply.github.com> Date: Sun, 7 Jan 2024 14:39:02 +0900 Subject: [PATCH 2/6] Editing ko support.. --- .vscode/settings.json | 13 +++++++++++++ num2words/lang_KO.py | 21 ++++++++------------- tests/test_ko.py | 8 ++++++-- 3 files changed, 27 insertions(+), 15 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..97f0210d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,13 @@ +{ + "cSpell.words": [ + "lastwords", + "lpair", + "ltext", + "negword", + "ords", + "outwords", + "rpair", + "rtext", + "valtext" + ] +} \ No newline at end of file diff --git a/num2words/lang_KO.py b/num2words/lang_KO.py index b59aceda..ce8e1734 100644 --- a/num2words/lang_KO.py +++ b/num2words/lang_KO.py @@ -78,7 +78,7 @@ def setup(self): "칠십": "일흔", "팔십": "여든", "구십": "아흔"} - + def merge(self, lpair, rpair): ltext, lnum = lpair rtext, rnum = rpair @@ -93,25 +93,20 @@ def merge(self, lpair, rpair): def to_ordinal(self, value): self.verify_ordinal(value) - if value == 1: - return "첫 번째" - if value == 100: - 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 + ten_one[1] = self.ords.get(ten_one[1], ten_one[1]) + ten_one[0] = ten_one[0].replace("스무", "스물") lastwords[-1] = ''.join(ten_one) else: - if lastwords[-1] in self.ords: - lastwords[-1] = self.ords[lastwords[-1]] - outwords[-1] = "백 ".join(lastwords) + lastwords[-1] = self.ords.get(lastwords[-1], lastwords[-1]) + if (lastwords[-1] == '' and len(lastwords) > 1): + outwords[-1] = '백'.join(lastwords) + else: + outwords[-1] = "백 ".join(lastwords) return " ".join(outwords) + " 번째" def to_ordinal_num(self, value): diff --git a/tests/test_ko.py b/tests/test_ko.py index 085d0c09..93520ce6 100644 --- a/tests/test_ko.py +++ b/tests/test_ko.py @@ -82,8 +82,12 @@ def test_currency(self): n2k(4, to="currency", currency="EUR") def test_ordinal(self): - cases = [(1, "첫 번째"), (101, "백 한 번째"), (2, "두 번째"), (5, "다섯 번째"), - (10, "열 번째"), (100, "백 번째"), (1000, "천 번째"), (10000, "만 번째"), + cases = [(1, "한 번째"), (101, "백 한 번째"), (2, "두 번째"), (5, "다섯 번째"), + (10, "열 번째"), (20, "스물 번째"), (100, "백 번째"), (1000, "천 번째"), + (10000, "만 번째"), (100000, "십만 번째"), (1000000, "백만 번째"), + (10000000, "천만 번째"), (100000000, "일억 번째"), (1000000000, "십억 번째"), + (10000000000, "백억 번째"), (100000000000, "천억 번째"), + (1000000000000, "일조 번째"), (10000000000000, "십조 번째"), (25, "스물다섯 번째"), (137, "백 서른일곱 번째")] for num, out in cases: self.assertEqual(n2k(num, to="ordinal"), out) From 9807d4a05fc90460d864a4b349e3bc3adc4fa66e Mon Sep 17 00:00:00 2001 From: Dongmin Jang Date: Mon, 8 Jan 2024 14:24:38 +0900 Subject: [PATCH 3/6] =?UTF-8?q?Edit=20Korean=20ordinal=20with=20removing?= =?UTF-8?q?=20'=EB=B2=88=EC=A7=B8'=20and=20add=20testcases?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- num2words/lang_KO.py | 31 ++++++++++++++++--------------- tests/test_ko.py | 16 ++++++++-------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/num2words/lang_KO.py b/num2words/lang_KO.py index ce8e1734..575c3e2e 100644 --- a/num2words/lang_KO.py +++ b/num2words/lang_KO.py @@ -20,6 +20,7 @@ from .base import Num2Word_Base from .currency import parse_currency_parts +import re class Num2Word_KO(Num2Word_Base): CURRENCY_FORMS = { @@ -94,24 +95,24 @@ def merge(self, lpair, rpair): def to_ordinal(self, value): self.verify_ordinal(value) 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] + "십"] - ten_one[1] = self.ords.get(ten_one[1], 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]) - if (lastwords[-1] == '' and len(lastwords) > 1): - outwords[-1] = '백'.join(lastwords) - else: - 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 93520ce6..e6bbdc0d 100644 --- a/tests/test_ko.py +++ b/tests/test_ko.py @@ -82,17 +82,17 @@ def test_currency(self): n2k(4, to="currency", currency="EUR") def test_ordinal(self): - cases = [(1, "한 번째"), (101, "백 한 번째"), (2, "두 번째"), (5, "다섯 번째"), - (10, "열 번째"), (20, "스물 번째"), (100, "백 번째"), (1000, "천 번째"), - (10000, "만 번째"), (100000, "십만 번째"), (1000000, "백만 번째"), - (10000000, "천만 번째"), (100000000, "일억 번째"), (1000000000, "십억 번째"), - (10000000000, "백억 번째"), (100000000000, "천억 번째"), - (1000000000000, "일조 번째"), (10000000000000, "십조 번째"), - (25, "스물다섯 번째"), (137, "백 서른일곱 번째")] + cases = [(1, "한"), (101, "백한"), (2, "두"), (5, "다섯"), (1050, "천쉰"), + (10, "열"), (20, "스무"), (21, "스물한"), (100, "백"), (1000, "천"), + (10000, "만"), (100000, "십만"), (1000000, "백만"), + (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) From abbb90bb6bfd20ef1bf7d809d8fc3f8c349b0334 Mon Sep 17 00:00:00 2001 From: Dongmin Jang Date: Mon, 8 Jan 2024 14:25:58 +0900 Subject: [PATCH 4/6] Add testcase for Korean ordinal --- tests/test_ko.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_ko.py b/tests/test_ko.py index e6bbdc0d..3e53f89f 100644 --- a/tests/test_ko.py +++ b/tests/test_ko.py @@ -84,7 +84,7 @@ def test_currency(self): def test_ordinal(self): cases = [(1, "한"), (101, "백한"), (2, "두"), (5, "다섯"), (1050, "천쉰"), (10, "열"), (20, "스무"), (21, "스물한"), (100, "백"), (1000, "천"), - (10000, "만"), (100000, "십만"), (1000000, "백만"), + (10000, "만"), (100000, "십만"), (1000000, "백만"), (100001, "십만 한"), (10000000, "천만"), (100000000, "일억"), (1000000000, "십억"), (10000000000, "백억"), (100000000000, "천억"), (1000000000000, "일조"), (10000000000000, "십조"), From 5670f76541fa3fa4a45c964d616ceff3256c248c Mon Sep 17 00:00:00 2001 From: Dongmin Jang Date: Mon, 8 Jan 2024 16:36:42 +0900 Subject: [PATCH 5/6] Remove .vscode --- .vscode/settings.json | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 97f0210d..00000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "cSpell.words": [ - "lastwords", - "lpair", - "ltext", - "negword", - "ords", - "outwords", - "rpair", - "rtext", - "valtext" - ] -} \ No newline at end of file From 0ac94f0b261dbd6935a3cd1ea73c67e9b4449598 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dongmin=20Jang=20=28=EC=9E=A5=EB=8F=99=EB=AF=BC=29?= <37837785+dogzz9445@users.noreply.github.com> Date: Sat, 3 Feb 2024 17:04:38 +0900 Subject: [PATCH 6/6] Edit pass flake8 requirements for PR #555, Fix error Korean ordinal conversion --- num2words/lang_KO.py | 6 ++++-- tests/test_ko.py | 12 ++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/num2words/lang_KO.py b/num2words/lang_KO.py index 575c3e2e..4664533f 100644 --- a/num2words/lang_KO.py +++ b/num2words/lang_KO.py @@ -22,6 +22,7 @@ import re + class Num2Word_KO(Num2Word_Base): CURRENCY_FORMS = { 'KRW': ('원', None), @@ -79,7 +80,7 @@ def setup(self): "칠십": "일흔", "팔십": "여든", "구십": "아흔"} - + def merge(self, lpair, rpair): ltext, lnum = lpair rtext, rnum = rpair @@ -96,7 +97,8 @@ def to_ordinal(self, value): self.verify_ordinal(value) outwords = self.to_cardinal(value).split(" ") if value % 100 != 0: - re_mid_words = '|'.join('(?<={})'.format(re.escape(delim)) for delim in ['천', '백']) + 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("십") diff --git a/tests/test_ko.py b/tests/test_ko.py index 3e53f89f..357bf13e 100644 --- a/tests/test_ko.py +++ b/tests/test_ko.py @@ -83,12 +83,12 @@ def test_currency(self): def test_ordinal(self): cases = [(1, "한"), (101, "백한"), (2, "두"), (5, "다섯"), (1050, "천쉰"), - (10, "열"), (20, "스무"), (21, "스물한"), (100, "백"), (1000, "천"), - (10000, "만"), (100000, "십만"), (1000000, "백만"), (100001, "십만 한"), - (10000000, "천만"), (100000000, "일억"), (1000000000, "십억"), - (10000000000, "백억"), (100000000000, "천억"), - (1000000000000, "일조"), (10000000000000, "십조"), - (25, "스물다섯"), (137, "백서른일곱")] + (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)