Skip to content

Commit

Permalink
add kannada language support (#243)
Browse files Browse the repository at this point in the history
* add kannada support

* add Kannada Language support

* add tests for kannada

* reformatted

* change lang param

* fix tests

* add condition

* add modifier

* add few more test cases

* fix test

* fix test

* modify test

* fix flake8

* fix isort

* add more tests

* add unicode prefix

* edit tests for float numbers

* add more tests

* add unicode prefix in tests

* add Kannada language
  • Loading branch information
akki2825 authored and erozqba committed May 12, 2019
1 parent f6dc5a3 commit cbfc8fd
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Besides the numerical argument, there are two main optional arguments.
* ``id`` (Indonesian)
* ``it`` (Italian)
* ``ja`` (Japanese)
* ``kn`` (Kannada)
* ``ko`` (Korean)
* ``lt`` (Lithuanian)
* ``lv`` (Latvian)
Expand Down
9 changes: 6 additions & 3 deletions num2words/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
from . import (lang_AR, lang_CZ, lang_DE, lang_DK, lang_EN, lang_EN_IN,
lang_ES, lang_ES_CO, lang_ES_VE, lang_FI, lang_FR, lang_FR_BE,
lang_FR_CH, lang_FR_DZ, lang_HE, lang_ID, lang_IT, lang_JA,
lang_KO, lang_LT, lang_LV, lang_NL, lang_NO, lang_PL, lang_PT,
lang_PT_BR, lang_RO, lang_RU, lang_SL, lang_SR, lang_TH,
lang_TR, lang_UK, lang_VI)
lang_KN, lang_KO, lang_LT, lang_LV, lang_NL, lang_NO, lang_PL,
lang_PT, lang_PT_BR, lang_RO, lang_RU, lang_SL, lang_SR,
lang_TH, lang_TR, lang_UK, lang_VI)

CONVERTER_CLASSES = {
'ar': lang_AR.Num2Word_AR(),
Expand All @@ -40,6 +40,7 @@
'es_VE': lang_ES_VE.Num2Word_ES_VE(),
'id': lang_ID.Num2Word_ID(),
'ja': lang_JA.Num2Word_JA(),
'kn': lang_KN.Num2Word_KN(),
'ko': lang_KO.Num2Word_KO(),
'lt': lang_LT.Num2Word_LT(),
'lv': lang_LV.Num2Word_LV(),
Expand Down Expand Up @@ -73,8 +74,10 @@ def num2words(number, ordinal=False, lang='en', to='cardinal', **kwargs):
if lang not in CONVERTER_CLASSES:
raise NotImplementedError()
converter = CONVERTER_CLASSES[lang]

if isinstance(number, str):
number = converter.str_to_number(number)

# backwards compatible
if ordinal:
return converter.to_ordinal(number)
Expand Down
182 changes: 182 additions & 0 deletions num2words/lang_KN.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
# -*- encoding: utf-8 -*-
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA

from __future__ import unicode_literals

from .base import Num2Word_Base


class Num2Word_KN(Num2Word_Base):
def set_high_numwords(self, high):
for n, word in self.high_numwords:
self.cards[10 ** n] = word

def setup(self):
self.low_numwords = [
"ತೊಂಬತ್ತೊಂಬತ್ತು",
"ತೊಂಬತ್ತೆಂಟು",
"ತೊಂಬತ್ತೇಳು",
"ತೊಂಬತ್ತಾರು",
"ತೊಂಬತ್ತೈದು",
"ತೊಂಬತ್ತ ನಾಲ್ಕು",
"ತೊಂಬತ್ತ ಮೂರು",
"ತೊಂಬತ್ತೆರಡು",
"ತೊಂಬತ್ತೊಂದು",
"ತೊಂಬತ್ತು",
"ಎಂಬತ್ತೊಂಬತ್ತು",
"ಎಂಬತ್ತೆಂಟು",
"ಎಂಬತ್ತೇಳು",
"ಎಂಬತ್ತಾರು",
"ಎಂಬತ್ತೈದು",
"ಎಂಬತ್ತ್ ನಾಲ್ಕು",
"ಎಂಬತ್ತ್ ಮೂರು",
"ಎಂಬತ್ತೆರಡು",
"ಎಂಬತ್ತೊಂದು",
"ಎಂಬತ್ತು",
"ಎಪ್ಪತ್ತೊಂಬತ್ತು",
"ಎಪ್ಪತ್ತೆಂಟು",
"ಎಪ್ಪತ್ತೇಳು",
"ಎಪ್ಪತ್ತಾರು",
"ಎಪ್ಪತ್ತೈದು",
"ಎಪ್ಪತ್ತ್ ನಾಲ್ಕು",
"ಎಪ್ಪತ್ತ್ ಮೂರು",
"ಎಪ್ಪತ್ತೆರಡು",
"ಎಪ್ಪತ್ತೊಂದು",
"ಎಪ್ಪತ್ತು",
"ಅರವತ್ತೊಂಬತ್ತು",
"ಅರವತ್ತೆಂಟು",
"ಅರವತ್ತೇಳು",
"ಅರವತ್ತಾರು",
"ಅರವತ್ತೈದು",
"ಅರವತ್ತ್ ನಾಲ್ಕು",
"ಅರವತ್ತ್ ಮೂರು",
"ಅರವತ್ತೆರಡು",
"ಅರವತ್ತೊಂದು",
"ಅರವತ್ತು",
"ಐವತ್ತೊಂಬತ್ತು",
"ಐವತ್ತೆಂಟು",
"ಐವತ್ತೇಳು",
"ಐವತ್ತಾರು",
"ಐವತ್ತೈದು",
"ಐವತ್ತ್ನಾಲ್ಕು",
"ಐವತ್ತಮೂರು",
"ಐವತ್ತೆರಡು",
"ಐವತ್ತೊಂದು",
"ಐವತ್ತು",
"ನಲವತ್ತೊಂಬತ್ತು",
"ನಲವತ್ತೆಂಟು",
"ನಲವತ್ತೇಳು",
"ನಲವತ್ತಾರು",
"ನಲವತ್ತೈದು",
"ನಲವತ್ತ್ ನಾಲ್ಕು",
"ನಲವತ್ತ್ ಮೂರು",
"ನಲವತ್ತ್ ಎರಡು",
"ನಲವತ್ತೊಂದು",
"ನಲವತ್ತು",
"ಮೂವತ್ತ್ ಒಂಬತ್ತು",
"ಮೂವತ್ಎಂಟು",
"ಮೂವತ್ಏಳು",
"ಮೂವತ್ಆರು",
"ಮೂವತ್ತ್ ಐದು",
"ಮೂವತ್ತ್ ನಾಲ್ಕು",
"ಮೂವತ್ತ್ ಮೂರು",
"ಮೂವತ್ತ್ಎರಡು",
"ಮೂವತ್ತ್ಒಂದು",
"ಮೂವತ್ತು",
"ಇಪ್ಪತ್ತ್ಒಂಬತ್ತು",
"ಇಪ್ಪತ್ತ್ಎಂಟು",
"ಇಪ್ಪತ್ತ್ಏಳು",
"ಇಪ್ಪತ್ತ್ಆರು",
"ಇಪ್ಪತ್ತ್ ಐದು",
"ಇಪ್ಪತ್ತ್ ನಾಲ್ಕು",
"ಇಪ್ಪತ್ತ್ ಮೂರು",
"ಇಪ್ಪತ್ತ್ ಎರಡು",
"ಇಪ್ಪತ್ತ್ ಒಂದು",
"ಇಪ್ಪತ್ತು",
"ಹತ್ತೊಂಬತ್ತು",
"ಹದಿನೆಂಟು",
"ಹದಿನೇಳು",
"ಹದಿನಾರು",
"ಹದಿನೈದು",
"ಹದಿನಾಲ್ಕು",
"ಹದಿಮೂರು",
"ಹನ್ನೆರಡು",
"ಹನ್ನೊಂದು",
"ಹತ್ತು",
"ಒಂಬತ್ತು",
"ಎಂಟು",
"ಏಳು",
"ಆರು",
"ಐದು",
"ನಾಲ್ಕು",
"ಮೂರು",
"ಎರಡು",
"ಒಂದು",
"ಸೊನ್ನೆ",
]

self.mid_numwords = [(100, "ನೂರು")]

self.high_numwords = [(7, "ಕೋಟಿ"), (5, "ಒಂದು ಲಕ್ಷ"), (3, "ಸಾವಿರ")]

self.pointword = "ಬಿಂದು"

self.modifiers = [
"್",
"ಾ",
"ಿ",
"ೀ",
"ೀ",
"ು",
"ೂ",
"ೃ",
"ೆ",
"ೇ",
"ೈ",
"ೊ",
"ೋ",
"ೌ",
"ೕ",
]

def merge(self, lpair, rpair):
ltext, lnum = lpair
rtext, rnum = rpair
if lnum == 1 and rnum < 100:
return (rtext, rnum)
elif 100 > lnum > rnum:
return ("%s-%s" % (ltext, rtext), lnum + rnum)
elif lnum >= 100 > rnum:
if ltext[-1] in self.modifiers:
return ("%s %s" % (ltext[:-1], rtext), lnum + rnum)
else:
return ("%s %s" % (ltext + "ದ", rtext), lnum + rnum)
elif rnum > lnum:
return ("%s %s" % (ltext, rtext), lnum * rnum)
return ("%s %s" % (ltext, rtext), lnum + rnum)

def to_ordinal_num(self, value):
self.verify_ordinal(value)
return "%s%s" % (value, self.to_ordinal(value))

def to_ordinal(self, value):
self.verify_ordinal(value)
outwords = self.to_cardinal(value)
if outwords[-1] in self.modifiers:
outwords = outwords[:-1]
ordinal_num = outwords + "ನೇ"
return ordinal_num
67 changes: 67 additions & 0 deletions tests/test_kn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA


from unittest import TestCase

from num2words import num2words


class Num2WordsKNTest(TestCase):
def test_numbers(self):
self.assertEqual(num2words(42, lang="kn"), u"ನಲವತ್ತ್ ಎರಡು")
self.assertEqual(num2words(893, lang="kn"), u"ಎಂಟು ನೂರ ತೊಂಬತ್ತ ಮೂರು")
self.assertEqual(
num2words(1729, lang="kn"), u"ಒಂದು ಸಾವಿರ ಏಳು ನೂರ ಇಪ್ಪತ್ತ್ಒಂಬತ್ತು"
)
self.assertEqual(num2words(123, lang="kn"), u"ಒಂದು ನೂರ ಇಪ್ಪತ್ತ್ ಮೂರು")
self.assertEqual(num2words(32211, lang="kn"),
u"ಮೂವತ್ತ್ಎರಡು ಸಾವಿರ ಎರಡು ನೂರ ಹನ್ನೊಂದು")

def test_cardinal_for_float_number(self):
self.assertEqual(num2words(3.14, lang="kn"), u"ಮೂರು ಬಿಂದು ಒಂದು ನಾಲ್ಕು")
self.assertEqual(num2words(1.61803, lang="kn"),
u"ಒಂದು ಬಿಂದು ಆರು ಒಂದು ಎಂಟು ಸೊನ್ನೆ ಮೂರು")

def test_ordinal(self):
self.assertEqual(
num2words(1, lang='kn', to='ordinal'),
u'ಒಂದನೇ'
)
self.assertEqual(
num2words(22, lang='kn', to='ordinal'),
u'ಇಪ್ಪತ್ತ್ ಎರಡನೇ'
)
self.assertEqual(
num2words(12, lang='kn', to='ordinal'),
u'ಹನ್ನೆರಡನೇ'
)
self.assertEqual(
num2words(130, lang='kn', to='ordinal'),
u'ಒಂದು ನೂರ ಮೂವತ್ತನೇ'
)
self.assertEqual(
num2words(1003, lang='kn', to='ordinal'),
u'ಒಂದು ಸಾವಿರದ ಮೂರನೇ'
)

def test_ordinal_num(self):
self.assertEqual(num2words(2, lang="kn", ordinal=True), u"ಎರಡನೇ")
self.assertEqual(num2words(5, lang="kn", ordinal=True), u"ಐದನೇ")
self.assertEqual(num2words(16, lang="kn", ordinal=True), u"ಹದಿನಾರನೇ")
self.assertEqual(num2words(113, lang="kn", ordinal=True),
u"ಒಂದು ನೂರ ಹದಿಮೂರನೇ")

0 comments on commit cbfc8fd

Please sign in to comment.