Skip to content

Commit f510792

Browse files
Merge pull request #380 from biggboss83/icelandic
Icelandic
2 parents 1dc4912 + 3404faa commit f510792

File tree

6 files changed

+218
-6
lines changed

6 files changed

+218
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ dist
55
*.egg-info
66
/.tox
77
.eggs/
8+
/venv/

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Besides the numerical argument, there are two main optional arguments.
9797
* ``he`` (Hebrew)
9898
* ``hu`` (Hungarian)
9999
* ``id`` (Indonesian)
100+
* ``is`` (Icelandic)
100101
* ``it`` (Italian)
101102
* ``ja`` (Japanese)
102103
* ``kn`` (Kannada)

num2words/__init__.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
from . import (lang_AM, lang_AR, lang_AZ, lang_CZ, lang_DE, lang_DK, lang_EN,
2121
lang_EN_IN, lang_EO, lang_ES, lang_ES_CO, lang_ES_NI,
2222
lang_ES_VE, lang_FA, lang_FI, lang_FR, lang_FR_BE, lang_FR_CH,
23-
lang_FR_DZ, lang_HE, lang_HU, lang_ID, lang_IT, lang_JA,
24-
lang_KN, lang_KO, lang_KZ, lang_LT, lang_LV, lang_NL, lang_NO,
25-
lang_PL, lang_PT, lang_PT_BR, lang_RO, lang_RU, lang_SL,
26-
lang_SR, lang_SV, lang_TE, lang_TG, lang_TH, lang_TR, lang_UK,
27-
lang_VI)
23+
lang_FR_DZ, lang_HE, lang_HU, lang_ID, lang_IS, lang_IT,
24+
lang_JA, lang_KN, lang_KO, lang_KZ, lang_LT, lang_LV, lang_NL,
25+
lang_NO, lang_PL, lang_PT, lang_PT_BR, lang_RO, lang_RU,
26+
lang_SL, lang_SR, lang_SV, lang_TE, lang_TG, lang_TH, lang_TR,
27+
lang_UK, lang_VI)
2828

2929
CONVERTER_CLASSES = {
3030
'am': lang_AM.Num2Word_AM(),
@@ -71,7 +71,8 @@
7171
'nl': lang_NL.Num2Word_NL(),
7272
'uk': lang_UK.Num2Word_UK(),
7373
'te': lang_TE.Num2Word_TE(),
74-
'hu': lang_HU.Num2Word_HU()
74+
'hu': lang_HU.Num2Word_HU(),
75+
'is': lang_IS.Num2Word_IS()
7576
}
7677

7778
CONVERTES_TYPES = ['cardinal', 'ordinal', 'ordinal_num', 'year', 'currency']

num2words/lang_EU.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ class Num2Word_EU(Num2Word_Base):
4545
'RON': (('leu', 'lei', 'de lei'), ('ban', 'bani', 'de bani')),
4646
'INR': (('rupee', 'rupees'), ('paisa', 'paise')),
4747
'HUF': (('forint', 'forint'), ('fillér', 'fillér')),
48+
'ISK': (('króna', 'krónur'), ('aur', 'aurar')),
4849
'UZS': (('sum', 'sums'), ('tiyin', 'tiyins'))
50+
4951
}
5052

5153
CURRENCY_ADJECTIVES = {
@@ -60,6 +62,7 @@ class Num2Word_EU(Num2Word_Base):
6062
'RON': 'Romanian',
6163
'INR': 'Indian',
6264
'HUF': 'Hungarian',
65+
'ISK': 'íslenskar',
6366
'UZS': 'Uzbekistan'
6467
}
6568

num2words/lang_IS.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
3+
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
4+
5+
# This library is free software; you can redistribute it and/or
6+
# modify it under the terms of the GNU Lesser General Public
7+
# License as published by the Free Software Foundation; either
8+
# version 2.1 of the License, or (at your option) any later version.
9+
# This library is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
# Lesser General Public License for more details.
13+
# You should have received a copy of the GNU Lesser General Public
14+
# License along with this library; if not, write to the Free Software
15+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16+
# MA 02110-1301 USA
17+
18+
from __future__ import division, print_function, unicode_literals
19+
20+
from . import lang_EU
21+
22+
# Genders
23+
KK = 0 # Karlkyn (male)
24+
KVK = 1 # Kvenkyn (female)
25+
HK = 2 # Hvorugkyn (neuter)
26+
27+
GENDERS = {
28+
"einn": ("einn", "ein", "eitt"),
29+
"tveir": ("tveir", "tvær", "tvö"),
30+
"þrír": ("þrír", "þrjár", "þrjú"),
31+
"fjórir": ("fjórir", "fjórar", "fjögur"),
32+
}
33+
34+
PLURALS = {
35+
"hundrað": ("hundrað", "hundruð"),
36+
}
37+
38+
39+
class Num2Word_IS(lang_EU.Num2Word_EU):
40+
41+
GIGA_SUFFIX = "illjarður"
42+
MEGA_SUFFIX = "illjón"
43+
44+
def setup(self):
45+
lows = ["okt", "sept", "sext", "kvint", "kvaðr", "tr", "b", "m"]
46+
self.high_numwords = self.gen_high_numwords([], [], lows)
47+
48+
self.negword = "mínus "
49+
self.pointword = "komma"
50+
51+
# All words should be excluded, title case is not used in Icelandic
52+
self.exclude_title = ["og", "komma", "mínus"]
53+
54+
self.mid_numwords = [(1000, "þúsund"), (100, "hundrað"),
55+
(90, "níutíu"), (80, "áttatíu"), (70, "sjötíu"),
56+
(60, "sextíu"), (50, "fimmtíu"), (40, "fjörutíu"),
57+
(30, "þrjátíu")]
58+
self.low_numwords = ["tuttugu", "nítján", "átján", "sautján",
59+
"sextán", "fimmtán", "fjórtán", "þrettán",
60+
"tólf", "ellefu", "tíu", "níu", "átta",
61+
"sjö", "sex", "fimm", "fjórir", "þrír",
62+
"tveir", "einn", "núll"]
63+
self.ords = {"einn": "fyrsti",
64+
"tveir": "annar",
65+
"þrír": "þriðji",
66+
"fjórir": "fjórði",
67+
"fimm": "fimmti",
68+
"sex": "sjötti",
69+
"sjö": "sjöundi",
70+
"átta": "áttundi",
71+
"níu": "níundi",
72+
"tíu": "tíundi",
73+
"ellefu": "ellefti",
74+
"tólf": "tólfti"}
75+
76+
def pluralize(self, n, noun):
77+
form = 0 if (n % 10 == 1 and n % 100 != 11) else 1
78+
if form == 0:
79+
return noun
80+
elif self.GIGA_SUFFIX in noun:
81+
return noun.replace(self.GIGA_SUFFIX, "illjarðar")
82+
elif self.MEGA_SUFFIX in noun:
83+
return noun.replace(self.MEGA_SUFFIX, "illjónir")
84+
elif noun not in PLURALS:
85+
return noun
86+
return PLURALS[noun][form]
87+
88+
def genderize(self, adj, noun):
89+
last = adj.split()[-1]
90+
if last not in GENDERS:
91+
return adj
92+
gender = KK
93+
if "hund" in noun or "þús" in noun:
94+
gender = HK
95+
elif "illjarð" in noun:
96+
gender = KK
97+
elif "illjón" in noun:
98+
gender = KVK
99+
return adj.replace(last, GENDERS[last][gender])
100+
101+
def merge(self, lpair, rpair):
102+
ltext, lnum = lpair
103+
rtext, rnum = rpair
104+
105+
if lnum == 1 and rnum < 100:
106+
return (rtext, rnum)
107+
elif lnum < rnum:
108+
rtext = self.pluralize(lnum, rtext)
109+
ltext = self.genderize(ltext, rtext)
110+
return ("%s %s" % (ltext, rtext), lnum * rnum)
111+
elif lnum > rnum and rnum in self.cards:
112+
rtext = self.pluralize(lnum, rtext)
113+
ltext = self.genderize(ltext, rtext)
114+
return ("%s og %s" % (ltext, rtext), lnum + rnum)
115+
return ("%s %s" % (ltext, rtext), lnum + rnum)
116+
117+
def to_ordinal(self, value):
118+
raise NotImplementedError
119+
120+
def to_ordinal_num(self, value):
121+
raise NotImplementedError
122+
123+
def to_year(self, val, suffix=None, longval=True):
124+
raise NotImplementedError
125+
126+
def to_currency(self, val, longval=True):
127+
raise NotImplementedError

tests/test_is.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# coding: utf-8
2+
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
3+
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
4+
5+
# This library is free software; you can redistribute it and/or
6+
# modify it under the terms of the GNU Lesser General Public
7+
# License as published by the Free Software Foundation; either
8+
# version 2.1 of the License, or (at your option) any later version.
9+
# This library is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
# Lesser General Public License for more details.
13+
# You should have received a copy of the GNU Lesser General Public
14+
# License along with this library; if not, write to the Free Software
15+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16+
# MA 02110-1301 USA
17+
18+
from __future__ import unicode_literals
19+
20+
from unittest import TestCase
21+
22+
from num2words import num2words
23+
24+
25+
class Num2WordsISTest(TestCase):
26+
27+
def test_cardinal(self):
28+
self.assertEqual(num2words(0, to="cardinal", lang="is"),
29+
"núll")
30+
self.assertEqual(num2words(1, to="cardinal", lang="is"),
31+
"einn")
32+
self.assertEqual(num2words(45, to="cardinal", lang="is"),
33+
"fjörutíu og fimm")
34+
self.assertEqual(num2words(145, to="cardinal", lang="is"),
35+
"eitt hundrað fjörutíu og fimm")
36+
self.assertEqual(num2words(-1245, to="cardinal", lang="is"),
37+
"mínus eitt þúsund tvö hundruð fjörutíu og fimm")
38+
self.assertEqual(num2words(2234045, to="cardinal", lang="is"),
39+
"tvær milljónir tvö hundruð þrjátíu og fjögur þúsund "
40+
"fjörutíu og fimm")
41+
self.assertEqual(num2words(4002234045, to="cardinal", lang="is"),
42+
"fjórir milljarðar tvær milljónir tvö hundruð "
43+
"þrjátíu og fjögur þúsund fjörutíu og fimm")
44+
45+
def test_cardinal_for_float_number(self):
46+
self.assertEqual(num2words(12.5, to="cardinal", lang="is"),
47+
"tólf komma fimm")
48+
self.assertEqual(num2words(12.51, to="cardinal", lang="is"),
49+
"tólf komma fimm einn")
50+
self.assertEqual(num2words(-12.53, to="cardinal", lang="is"),
51+
"mínus tólf komma fimm þrír")
52+
self.assertEqual(num2words(12.59, to="cardinal", lang="is"),
53+
"tólf komma fimm níu")
54+
55+
def test_overflow(self):
56+
with self.assertRaises(OverflowError):
57+
num2words("1000000000000000000000000000000000000000000000000000000"
58+
"0000000000000000000000000000000000000000000000000000000"
59+
"0000000000000000000000000000000000000000000000000000000"
60+
"0000000000000000000000000000000000000000000000000000000"
61+
"0000000000000000000000000000000000000000000000000000000"
62+
"00000000000000000000000000000000", lang="is")
63+
64+
def test_not_implemented(self):
65+
# Ordinals
66+
with self.assertRaises(NotImplementedError):
67+
num2words(1, to="ordinal", lang="is")
68+
69+
# Ordinal num
70+
with self.assertRaises(NotImplementedError):
71+
num2words(1, to="ordinal_num", lang="is")
72+
73+
# Year
74+
with self.assertRaises(NotImplementedError):
75+
num2words(1, to="year", lang="is")
76+
77+
# Currency
78+
with self.assertRaises(NotImplementedError):
79+
num2words(1, to="currency", lang="is")

0 commit comments

Comments
 (0)