num2words2 0.1.0.dev1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- num2words2/README.md +69 -0
- num2words2/__init__.py +196 -0
- num2words2/_version.py +3 -0
- num2words2/base.py +377 -0
- num2words2/compat.py +29 -0
- num2words2/currency.py +60 -0
- num2words2/lang_AF.py +202 -0
- num2words2/lang_AM.py +193 -0
- num2words2/lang_AR.py +435 -0
- num2words2/lang_AS.py +119 -0
- num2words2/lang_AZ.py +208 -0
- num2words2/lang_BA.py +119 -0
- num2words2/lang_BE.py +356 -0
- num2words2/lang_BG.py +393 -0
- num2words2/lang_BN.py +242 -0
- num2words2/lang_BO.py +122 -0
- num2words2/lang_BR.py +135 -0
- num2words2/lang_BS.py +189 -0
- num2words2/lang_CA.py +476 -0
- num2words2/lang_CE.py +546 -0
- num2words2/lang_CS.py +260 -0
- num2words2/lang_CY.py +574 -0
- num2words2/lang_DA.py +170 -0
- num2words2/lang_DE.py +180 -0
- num2words2/lang_EL.py +338 -0
- num2words2/lang_EN.py +118 -0
- num2words2/lang_EN_IN.py +26 -0
- num2words2/lang_EN_NG.py +45 -0
- num2words2/lang_EO.py +130 -0
- num2words2/lang_ES.py +399 -0
- num2words2/lang_ES_CO.py +37 -0
- num2words2/lang_ES_CR.py +38 -0
- num2words2/lang_ES_GT.py +37 -0
- num2words2/lang_ES_NI.py +35 -0
- num2words2/lang_ES_VE.py +43 -0
- num2words2/lang_ET.py +393 -0
- num2words2/lang_EU.py +158 -0
- num2words2/lang_EUR.py +104 -0
- num2words2/lang_FA.py +187 -0
- num2words2/lang_FI.py +759 -0
- num2words2/lang_FO.py +136 -0
- num2words2/lang_FR.py +133 -0
- num2words2/lang_FR_BE.py +49 -0
- num2words2/lang_FR_CH.py +48 -0
- num2words2/lang_FR_DZ.py +35 -0
- num2words2/lang_GL.py +135 -0
- num2words2/lang_GU.py +172 -0
- num2words2/lang_HA.py +293 -0
- num2words2/lang_HAW.py +144 -0
- num2words2/lang_HE.py +314 -0
- num2words2/lang_HI.py +243 -0
- num2words2/lang_HR.py +304 -0
- num2words2/lang_HT.py +136 -0
- num2words2/lang_HU.py +189 -0
- num2words2/lang_HY.py +364 -0
- num2words2/lang_ID.py +242 -0
- num2words2/lang_IS.py +208 -0
- num2words2/lang_IT.py +265 -0
- num2words2/lang_JA.py +615 -0
- num2words2/lang_JW.py +136 -0
- num2words2/lang_KA.py +169 -0
- num2words2/lang_KK.py +136 -0
- num2words2/lang_KM.py +173 -0
- num2words2/lang_KN.py +182 -0
- num2words2/lang_KO.py +175 -0
- num2words2/lang_KZ.py +161 -0
- num2words2/lang_LA.py +135 -0
- num2words2/lang_LB.py +135 -0
- num2words2/lang_LN.py +136 -0
- num2words2/lang_LO.py +149 -0
- num2words2/lang_LT.py +283 -0
- num2words2/lang_LV.py +266 -0
- num2words2/lang_MG.py +143 -0
- num2words2/lang_MI.py +151 -0
- num2words2/lang_MK.py +136 -0
- num2words2/lang_ML.py +159 -0
- num2words2/lang_MN.py +319 -0
- num2words2/lang_MR.py +187 -0
- num2words2/lang_MS.py +293 -0
- num2words2/lang_MT.py +159 -0
- num2words2/lang_MY.py +152 -0
- num2words2/lang_NE.py +157 -0
- num2words2/lang_NL.py +204 -0
- num2words2/lang_NN.py +136 -0
- num2words2/lang_NO.py +143 -0
- num2words2/lang_OC.py +135 -0
- num2words2/lang_PA.py +159 -0
- num2words2/lang_PL.py +336 -0
- num2words2/lang_PS.py +136 -0
- num2words2/lang_PT.py +280 -0
- num2words2/lang_PT_BR.py +224 -0
- num2words2/lang_RO.py +183 -0
- num2words2/lang_RU.py +570 -0
- num2words2/lang_SA.py +136 -0
- num2words2/lang_SD.py +136 -0
- num2words2/lang_SI.py +152 -0
- num2words2/lang_SK.py +250 -0
- num2words2/lang_SL.py +306 -0
- num2words2/lang_SN.py +447 -0
- num2words2/lang_SO.py +136 -0
- num2words2/lang_SQ.py +252 -0
- num2words2/lang_SR.py +254 -0
- num2words2/lang_SU.py +136 -0
- num2words2/lang_SV.py +169 -0
- num2words2/lang_SW.py +219 -0
- num2words2/lang_TA.py +303 -0
- num2words2/lang_TE.py +169 -0
- num2words2/lang_TET.py +285 -0
- num2words2/lang_TG.py +149 -0
- num2words2/lang_TH.py +214 -0
- num2words2/lang_TK.py +136 -0
- num2words2/lang_TL.py +145 -0
- num2words2/lang_TR.py +882 -0
- num2words2/lang_TT.py +136 -0
- num2words2/lang_UK.py +1101 -0
- num2words2/lang_UR.py +168 -0
- num2words2/lang_UZ.py +136 -0
- num2words2/lang_VI.py +143 -0
- num2words2/lang_WO.py +136 -0
- num2words2/lang_YI.py +135 -0
- num2words2/lang_YO.py +136 -0
- num2words2/lang_ZH.py +296 -0
- num2words2/lang_ZH_CN.py +85 -0
- num2words2/lang_ZH_HK.py +48 -0
- num2words2/lang_ZH_TW.py +198 -0
- num2words2/utils.py +35 -0
- num2words2-0.1.0.dev1.data/scripts/num2words2 +98 -0
- num2words2-0.1.0.dev1.dist-info/METADATA +244 -0
- num2words2-0.1.0.dev1.dist-info/RECORD +133 -0
- num2words2-0.1.0.dev1.dist-info/WHEEL +5 -0
- num2words2-0.1.0.dev1.dist-info/entry_points.txt +2 -0
- num2words2-0.1.0.dev1.dist-info/licenses/COPYING +145 -0
- num2words2-0.1.0.dev1.dist-info/top_level.txt +1 -0
num2words2/lang_IS.py
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
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_EUR
|
|
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_EUR.Num2Word_EUR):
|
|
40
|
+
|
|
41
|
+
CURRENCY_FORMS = {
|
|
42
|
+
'ISK': (('króna', 'krónur'), ('eyrir', 'aurar')),
|
|
43
|
+
'EUR': (('evra', 'evrur'), ('sent', 'sent')),
|
|
44
|
+
'USD': (('dalur', 'dalir'), ('sent', 'sent')),
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
GIGA_SUFFIX = "illjarður"
|
|
48
|
+
MEGA_SUFFIX = "illjón"
|
|
49
|
+
|
|
50
|
+
def setup(self):
|
|
51
|
+
lows = ["okt", "sept", "sext", "kvint", "kvaðr", "tr", "b", "m"]
|
|
52
|
+
self.high_numwords = self.gen_high_numwords([], [], lows)
|
|
53
|
+
|
|
54
|
+
self.negword = "mínus "
|
|
55
|
+
self.pointword = "komma"
|
|
56
|
+
|
|
57
|
+
# All words should be excluded, title case is not used in Icelandic
|
|
58
|
+
self.exclude_title = ["og", "komma", "mínus"]
|
|
59
|
+
|
|
60
|
+
self.mid_numwords = [(1000, "þúsund"), (100, "hundrað"),
|
|
61
|
+
(90, "níutíu"), (80, "áttatíu"), (70, "sjötíu"),
|
|
62
|
+
(60, "sextíu"), (50, "fimmtíu"), (40, "fjörutíu"),
|
|
63
|
+
(30, "þrjátíu")]
|
|
64
|
+
self.low_numwords = ["tuttugu", "nítján", "átján", "sautján",
|
|
65
|
+
"sextán", "fimmtán", "fjórtán", "þrettán",
|
|
66
|
+
"tólf", "ellefu", "tíu", "níu", "átta",
|
|
67
|
+
"sjö", "sex", "fimm", "fjórir", "þrír",
|
|
68
|
+
"tveir", "einn", "núll"]
|
|
69
|
+
self.ords = {"einn": "fyrsti",
|
|
70
|
+
"tveir": "annar",
|
|
71
|
+
"þrír": "þriðji",
|
|
72
|
+
"fjórir": "fjórði",
|
|
73
|
+
"fimm": "fimmti",
|
|
74
|
+
"sex": "sjötti",
|
|
75
|
+
"sjö": "sjöundi",
|
|
76
|
+
"átta": "áttundi",
|
|
77
|
+
"níu": "níundi",
|
|
78
|
+
"tíu": "tíundi",
|
|
79
|
+
"ellefu": "ellefti",
|
|
80
|
+
"tólf": "tólfti"}
|
|
81
|
+
|
|
82
|
+
def pluralize(self, n, noun):
|
|
83
|
+
form = 0 if (n % 10 == 1 and n % 100 != 11) else 1
|
|
84
|
+
if form == 0:
|
|
85
|
+
return noun
|
|
86
|
+
elif self.GIGA_SUFFIX in noun:
|
|
87
|
+
return noun.replace(self.GIGA_SUFFIX, "illjarðar")
|
|
88
|
+
elif self.MEGA_SUFFIX in noun:
|
|
89
|
+
return noun.replace(self.MEGA_SUFFIX, "illjónir")
|
|
90
|
+
elif noun not in PLURALS:
|
|
91
|
+
return noun
|
|
92
|
+
return PLURALS[noun][form]
|
|
93
|
+
|
|
94
|
+
def genderize(self, adj, noun):
|
|
95
|
+
last = adj.split()[-1]
|
|
96
|
+
if last not in GENDERS:
|
|
97
|
+
return adj
|
|
98
|
+
gender = KK
|
|
99
|
+
if "hund" in noun or "þús" in noun:
|
|
100
|
+
gender = HK
|
|
101
|
+
elif "illjarð" in noun:
|
|
102
|
+
gender = KK
|
|
103
|
+
elif "illjón" in noun:
|
|
104
|
+
gender = KVK
|
|
105
|
+
return adj.replace(last, GENDERS[last][gender])
|
|
106
|
+
|
|
107
|
+
def merge(self, lpair, rpair):
|
|
108
|
+
ltext, lnum = lpair
|
|
109
|
+
rtext, rnum = rpair
|
|
110
|
+
|
|
111
|
+
if lnum == 1 and rnum < 100:
|
|
112
|
+
return (rtext, rnum)
|
|
113
|
+
elif lnum < rnum:
|
|
114
|
+
rtext = self.pluralize(lnum, rtext)
|
|
115
|
+
ltext = self.genderize(ltext, rtext)
|
|
116
|
+
return ("%s %s" % (ltext, rtext), lnum * rnum)
|
|
117
|
+
elif lnum > rnum and rnum in self.cards:
|
|
118
|
+
rtext = self.pluralize(lnum, rtext)
|
|
119
|
+
ltext = self.genderize(ltext, rtext)
|
|
120
|
+
return ("%s og %s" % (ltext, rtext), lnum + rnum)
|
|
121
|
+
return ("%s %s" % (ltext, rtext), lnum + rnum)
|
|
122
|
+
|
|
123
|
+
def to_ordinal(self, value):
|
|
124
|
+
# Use the ordinal dictionary defined in setup
|
|
125
|
+
try:
|
|
126
|
+
number = int(value)
|
|
127
|
+
except (ValueError, TypeError):
|
|
128
|
+
return str(value)
|
|
129
|
+
|
|
130
|
+
# Get cardinal form first
|
|
131
|
+
cardinal = self.to_cardinal(number)
|
|
132
|
+
|
|
133
|
+
# Check if we have a direct mapping
|
|
134
|
+
if cardinal in self.ords:
|
|
135
|
+
return self.ords[cardinal]
|
|
136
|
+
|
|
137
|
+
# For numbers beyond our mapping, use generic suffix
|
|
138
|
+
# In Icelandic, ordinals typically end with -asti, -undi, etc.
|
|
139
|
+
if number == 20:
|
|
140
|
+
return "tuttugasti"
|
|
141
|
+
elif 20 < number < 30:
|
|
142
|
+
return "tuttugasti og " + self.ords.get(self.to_cardinal(number % 10), self.to_cardinal(number % 10))
|
|
143
|
+
elif number == 30:
|
|
144
|
+
return "þrítugasti"
|
|
145
|
+
elif number == 40:
|
|
146
|
+
return "fertugasti"
|
|
147
|
+
elif number == 50:
|
|
148
|
+
return "fimmtugasti"
|
|
149
|
+
elif number == 60:
|
|
150
|
+
return "sextugasti"
|
|
151
|
+
elif number == 70:
|
|
152
|
+
return "sjötugasti"
|
|
153
|
+
elif number == 80:
|
|
154
|
+
return "áttugasti"
|
|
155
|
+
elif number == 90:
|
|
156
|
+
return "nítugasti"
|
|
157
|
+
elif number == 100:
|
|
158
|
+
return "hundraðasti"
|
|
159
|
+
elif number == 1000:
|
|
160
|
+
return "þúsundasti"
|
|
161
|
+
else:
|
|
162
|
+
# For composite numbers, just append 'asti' to the cardinal
|
|
163
|
+
# This is a simplified implementation
|
|
164
|
+
return cardinal + "asti"
|
|
165
|
+
|
|
166
|
+
def to_ordinal_num(self, value):
|
|
167
|
+
# Just add a period after the number for ordinal numbers in Icelandic
|
|
168
|
+
return str(value) + "."
|
|
169
|
+
|
|
170
|
+
def to_year(self, val, suffix=None, longval=True):
|
|
171
|
+
# For Icelandic, years are typically read as cardinal numbers
|
|
172
|
+
# For example: 2024 would be "tvö þúsund tuttugu og fjórir"
|
|
173
|
+
return self.to_cardinal(val)
|
|
174
|
+
|
|
175
|
+
def pluralize_currency(self, count, forms):
|
|
176
|
+
"""Return the proper plural form for Icelandic currency"""
|
|
177
|
+
if count == 1:
|
|
178
|
+
return forms[0]
|
|
179
|
+
return forms[1] if len(forms) > 1 else forms[0]
|
|
180
|
+
|
|
181
|
+
def to_currency(self, val, currency='ISK', cents=True, separator=',',
|
|
182
|
+
adjective=False):
|
|
183
|
+
# Handle integers specially - just add currency name without cents
|
|
184
|
+
if isinstance(val, int):
|
|
185
|
+
try:
|
|
186
|
+
cr1, cr2 = self.CURRENCY_FORMS[currency]
|
|
187
|
+
except (KeyError, AttributeError):
|
|
188
|
+
# Fallback to base implementation for unknown currency
|
|
189
|
+
return super(Num2Word_IS, self).to_currency(
|
|
190
|
+
val, currency=currency, cents=cents, separator=separator,
|
|
191
|
+
adjective=adjective)
|
|
192
|
+
|
|
193
|
+
minus_str = self.negword if val < 0 else ""
|
|
194
|
+
abs_val = abs(val)
|
|
195
|
+
money_str = self.to_cardinal(abs_val)
|
|
196
|
+
|
|
197
|
+
# Proper pluralization for currency
|
|
198
|
+
if abs_val == 1:
|
|
199
|
+
currency_str = cr1[0] if isinstance(cr1, tuple) else cr1
|
|
200
|
+
else:
|
|
201
|
+
currency_str = cr1[1] if isinstance(cr1, tuple) and len(cr1) > 1 else (cr1[0] if isinstance(cr1, tuple) else cr1)
|
|
202
|
+
|
|
203
|
+
return (u'%s %s %s' % (minus_str, money_str, currency_str)).strip()
|
|
204
|
+
|
|
205
|
+
# For floats, use the parent class implementation
|
|
206
|
+
return super(Num2Word_IS, self).to_currency(
|
|
207
|
+
val, currency=currency, cents=cents, separator=separator,
|
|
208
|
+
adjective=adjective)
|
num2words2/lang_IT.py
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# Copyright (c) 2018-2019, Filippo Costa. All Rights Reserved.
|
|
3
|
+
|
|
4
|
+
# This library is free software; you can redistribute it and/or
|
|
5
|
+
# modify it under the terms of the GNU Lesser General Public
|
|
6
|
+
# License as published by the Free Software Foundation; either
|
|
7
|
+
# version 2.1 of the License, or (at your option) any later version.
|
|
8
|
+
# This library is distributed in the hope that it will be useful,
|
|
9
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
11
|
+
# Lesser General Public License for more details.
|
|
12
|
+
# You should have received a copy of the GNU Lesser General Public
|
|
13
|
+
# License along with this library; if not, write to the Free Software
|
|
14
|
+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
15
|
+
# MA 02110-1301 USA
|
|
16
|
+
|
|
17
|
+
from __future__ import unicode_literals
|
|
18
|
+
|
|
19
|
+
from .lang_EUR import Num2Word_EUR
|
|
20
|
+
|
|
21
|
+
# Globals
|
|
22
|
+
# -------
|
|
23
|
+
|
|
24
|
+
ZERO = "zero"
|
|
25
|
+
|
|
26
|
+
CARDINAL_WORDS = [
|
|
27
|
+
ZERO, "uno", "due", "tre", "quattro", "cinque", "sei", "sette", "otto",
|
|
28
|
+
"nove", "dieci", "undici", "dodici", "tredici", "quattordici", "quindici",
|
|
29
|
+
"sedici", "diciassette", "diciotto", "diciannove"
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
ORDINAL_WORDS = [
|
|
33
|
+
ZERO, "primo", "secondo", "terzo", "quarto", "quinto", "sesto", "settimo",
|
|
34
|
+
"ottavo", "nono", "decimo", "undicesimo", "dodicesimo", "tredicesimo",
|
|
35
|
+
"quattordicesimo", "quindicesimo", "sedicesimo", "diciassettesimo",
|
|
36
|
+
"diciottesimo", "diciannovesimo"
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
# The script can extrapolate the missing numbers from the base forms.
|
|
40
|
+
STR_TENS = {2: "venti", 3: "trenta", 4: "quaranta", 6: "sessanta"}
|
|
41
|
+
|
|
42
|
+
# These prefixes are used for extremely big numbers.
|
|
43
|
+
EXPONENT_PREFIXES = [
|
|
44
|
+
ZERO, "m", "b", "tr", "quadr", "quint", "sest", "sett", "ott", "nov", "dec"
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
EXPONENT = EXPONENT_PREFIXES # Alias for backward compatibility
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
GENERIC_DOLLARS = ('dollaro', 'dollari')
|
|
51
|
+
GENERIC_CENTS = ('centesimo', 'centesimi')
|
|
52
|
+
CURRENCIES_UNA = ('GBP')
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
# Helper functions
|
|
56
|
+
def omitt_if_zero(number_to_string):
|
|
57
|
+
return "" if number_to_string == ZERO else number_to_string
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def accentuate(string):
|
|
61
|
+
# This is inefficient: it may do several rewritings when deleting
|
|
62
|
+
# half-sentence accents. However, it is the easiest method and speed is
|
|
63
|
+
# not crucial (duh), so...
|
|
64
|
+
return " ".join(
|
|
65
|
+
# Deletes half-sentence accents and accentuates the last "tre"
|
|
66
|
+
[w.replace("tré", "tre")[:-3] + "tré"
|
|
67
|
+
# We shouldn't accentuate a single "tre": is has to be a composite
|
|
68
|
+
# word. ~~~~~~~~~~
|
|
69
|
+
if w[-3:] == "tre" and len(w) > 3
|
|
70
|
+
# Deletes half-sentence accents anyway
|
|
71
|
+
# ~~~~~~~~~~~~~~~~~~~~~~
|
|
72
|
+
else w.replace("tré", "tre")
|
|
73
|
+
for w in string.split()
|
|
74
|
+
])
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def phonetic_contraction(string):
|
|
78
|
+
return (string
|
|
79
|
+
.replace("oo", "o") # ex. "centootto"
|
|
80
|
+
.replace("ao", "o") # ex. "settantaotto"
|
|
81
|
+
.replace("io", "o") # ex. "ventiotto"
|
|
82
|
+
.replace("au", "u") # ex. "trentauno"
|
|
83
|
+
.replace("iu", "u") # ex. "ventiunesimo"
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def exponent_length_to_string(exponent_length):
|
|
88
|
+
# We always get an exponent length of the form 3n, n>=1
|
|
89
|
+
prefix = EXPONENT[(exponent_length // 6) % 5]
|
|
90
|
+
|
|
91
|
+
if exponent_length == 3:
|
|
92
|
+
return "mila"
|
|
93
|
+
elif exponent_length % 6 == 0:
|
|
94
|
+
return prefix + "ilione"
|
|
95
|
+
else:
|
|
96
|
+
return prefix + "iliardo"
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
# Main class
|
|
100
|
+
# ==========
|
|
101
|
+
|
|
102
|
+
class Num2Word_IT(Num2Word_EUR):
|
|
103
|
+
CURRENCY_FORMS = {
|
|
104
|
+
'EUR': (('euro', 'euro'), GENERIC_CENTS),
|
|
105
|
+
'USD': (GENERIC_DOLLARS, GENERIC_CENTS),
|
|
106
|
+
'GBP': (('sterlina', 'sterline'), ('penny', 'penny')),
|
|
107
|
+
'CNY': (('yuan', 'yuan'), ('fen', 'fen')),
|
|
108
|
+
'CHF': (('franco', 'franchi'), GENERIC_CENTS),
|
|
109
|
+
}
|
|
110
|
+
MINUS_PREFIX_WORD = "meno "
|
|
111
|
+
FLOAT_INFIX_WORD = " virgola "
|
|
112
|
+
|
|
113
|
+
def setup(self):
|
|
114
|
+
Num2Word_EUR.setup(self)
|
|
115
|
+
self.negword = "meno"
|
|
116
|
+
|
|
117
|
+
def __init__(self):
|
|
118
|
+
self.setup()
|
|
119
|
+
|
|
120
|
+
def float_to_words(self, float_number, ordinal=False):
|
|
121
|
+
if ordinal:
|
|
122
|
+
prefix = self.to_ordinal(int(float_number))
|
|
123
|
+
else:
|
|
124
|
+
prefix = self.to_cardinal(int(float_number))
|
|
125
|
+
float_part = str(float_number).split('.')[1]
|
|
126
|
+
postfix = " ".join(
|
|
127
|
+
# Drops the trailing zero and comma
|
|
128
|
+
[self.to_cardinal(int(c)) for c in float_part]
|
|
129
|
+
)
|
|
130
|
+
return prefix + Num2Word_IT.FLOAT_INFIX_WORD + postfix
|
|
131
|
+
|
|
132
|
+
def tens_to_cardinal(self, number):
|
|
133
|
+
tens = number // 10
|
|
134
|
+
units = number % 10
|
|
135
|
+
if tens in STR_TENS:
|
|
136
|
+
prefix = STR_TENS[tens]
|
|
137
|
+
else:
|
|
138
|
+
prefix = CARDINAL_WORDS[tens][:-1] + "anta"
|
|
139
|
+
postfix = omitt_if_zero(CARDINAL_WORDS[units])
|
|
140
|
+
return phonetic_contraction(prefix + postfix)
|
|
141
|
+
|
|
142
|
+
def hundreds_to_cardinal(self, number):
|
|
143
|
+
hundreds = number // 100
|
|
144
|
+
prefix = "cento"
|
|
145
|
+
if hundreds != 1:
|
|
146
|
+
prefix = CARDINAL_WORDS[hundreds] + prefix
|
|
147
|
+
postfix = omitt_if_zero(self.to_cardinal(number % 100))
|
|
148
|
+
return phonetic_contraction(prefix + postfix)
|
|
149
|
+
|
|
150
|
+
def thousands_to_cardinal(self, number):
|
|
151
|
+
thousands = number // 1000
|
|
152
|
+
if thousands == 1:
|
|
153
|
+
prefix = "mille"
|
|
154
|
+
else:
|
|
155
|
+
prefix = self.to_cardinal(thousands) + "mila"
|
|
156
|
+
postfix = omitt_if_zero(self.to_cardinal(number % 1000))
|
|
157
|
+
# "mille" and "mila" don't need any phonetic contractions
|
|
158
|
+
return prefix + postfix
|
|
159
|
+
|
|
160
|
+
def big_number_to_cardinal(self, number):
|
|
161
|
+
digits = [c for c in str(number)]
|
|
162
|
+
length = len(digits)
|
|
163
|
+
if length >= 66:
|
|
164
|
+
raise NotImplementedError("The given number is too large.")
|
|
165
|
+
# This is how many digits come before the "illion" term.
|
|
166
|
+
# cento miliardi => 3
|
|
167
|
+
# dieci milioni => 2
|
|
168
|
+
# un miliardo => 1
|
|
169
|
+
predigits = length % 3 or 3
|
|
170
|
+
multiplier = digits[:predigits]
|
|
171
|
+
exponent = digits[predigits:]
|
|
172
|
+
# Default infix string: "milione", "biliardo", "sestilione", ecc.
|
|
173
|
+
infix = exponent_length_to_string(len(exponent))
|
|
174
|
+
if multiplier == ["1"]:
|
|
175
|
+
prefix = "un "
|
|
176
|
+
else:
|
|
177
|
+
prefix = self.to_cardinal(int("".join(multiplier)))
|
|
178
|
+
# Plural form ~~~~~~~~~~~
|
|
179
|
+
infix = " " + infix[:-1] + "i"
|
|
180
|
+
# Read as: Does the value of exponent equal 0?
|
|
181
|
+
if set(exponent) != set("0"):
|
|
182
|
+
postfix = self.to_cardinal(int("".join(exponent)))
|
|
183
|
+
if " e " in postfix:
|
|
184
|
+
infix += ", "
|
|
185
|
+
else:
|
|
186
|
+
infix += " e "
|
|
187
|
+
else:
|
|
188
|
+
postfix = ""
|
|
189
|
+
return prefix + infix + postfix
|
|
190
|
+
|
|
191
|
+
def to_cardinal(self, number):
|
|
192
|
+
if number < 0:
|
|
193
|
+
# Apply accentuation to the positive part, then add minus prefix
|
|
194
|
+
positive_part = self.to_cardinal(-number)
|
|
195
|
+
string = Num2Word_IT.MINUS_PREFIX_WORD + positive_part
|
|
196
|
+
elif int(number) != number:
|
|
197
|
+
string = self.float_to_words(number)
|
|
198
|
+
elif number < 20:
|
|
199
|
+
string = CARDINAL_WORDS[int(number)]
|
|
200
|
+
elif number < 100:
|
|
201
|
+
string = self.tens_to_cardinal(int(number))
|
|
202
|
+
elif number < 1000:
|
|
203
|
+
string = self.hundreds_to_cardinal(int(number))
|
|
204
|
+
elif number < 1000000:
|
|
205
|
+
string = self.thousands_to_cardinal(int(number))
|
|
206
|
+
else:
|
|
207
|
+
string = self.big_number_to_cardinal(number)
|
|
208
|
+
# Only apply accentuate for non-negative numbers (already done for negative)
|
|
209
|
+
return string if number < 0 else accentuate(string)
|
|
210
|
+
|
|
211
|
+
def to_ordinal(self, number):
|
|
212
|
+
tens = number % 100
|
|
213
|
+
# Italian grammar is poorly defined here ¯\_(ツ)_/¯:
|
|
214
|
+
# centodecimo VS centodieciesimo VS centesimo decimo?
|
|
215
|
+
is_outside_teens = not 10 < tens < 20
|
|
216
|
+
if number < 0:
|
|
217
|
+
return Num2Word_IT.MINUS_PREFIX_WORD + self.to_ordinal(-number)
|
|
218
|
+
elif number % 1 != 0:
|
|
219
|
+
return self.float_to_words(number, ordinal=True)
|
|
220
|
+
elif number < 20:
|
|
221
|
+
return ORDINAL_WORDS[int(number)]
|
|
222
|
+
elif is_outside_teens and tens % 10 == 3:
|
|
223
|
+
# Gets rid of the accent
|
|
224
|
+
return self.to_cardinal(number)[:-1] + "eesimo"
|
|
225
|
+
elif is_outside_teens and tens % 10 == 6:
|
|
226
|
+
return self.to_cardinal(number) + "esimo"
|
|
227
|
+
else:
|
|
228
|
+
string = self.to_cardinal(number)[:-1]
|
|
229
|
+
if string[-3:] == "mil":
|
|
230
|
+
string += "l"
|
|
231
|
+
return string + "esimo"
|
|
232
|
+
|
|
233
|
+
def to_currency(self, val, currency='EUR', cents=True, separator=' e',
|
|
234
|
+
adjective=False):
|
|
235
|
+
# Handle integers specially - just add currency name without cents
|
|
236
|
+
if isinstance(val, int):
|
|
237
|
+
try:
|
|
238
|
+
cr1, cr2 = self.CURRENCY_FORMS[currency]
|
|
239
|
+
except (KeyError, AttributeError):
|
|
240
|
+
# Fallback to base implementation for unknown currency
|
|
241
|
+
return super(Num2Word_IT, self).to_currency(
|
|
242
|
+
val, currency=currency, cents=cents, separator=separator,
|
|
243
|
+
adjective=adjective)
|
|
244
|
+
|
|
245
|
+
minus_str = "meno" if val < 0 else ""
|
|
246
|
+
abs_val = abs(val)
|
|
247
|
+
money_str = self.to_cardinal(abs_val)
|
|
248
|
+
|
|
249
|
+
# Proper pluralization for currency
|
|
250
|
+
if abs_val == 1:
|
|
251
|
+
currency_str = cr1[0] if isinstance(cr1, tuple) else cr1
|
|
252
|
+
else:
|
|
253
|
+
currency_str = cr1[1] if isinstance(cr1, tuple) and len(cr1) > 1 else (cr1[0] if isinstance(cr1, tuple) else cr1)
|
|
254
|
+
|
|
255
|
+
# Build result with proper spacing
|
|
256
|
+
if minus_str:
|
|
257
|
+
result = "%s %s %s" % (minus_str, money_str, currency_str)
|
|
258
|
+
else:
|
|
259
|
+
result = "%s %s" % (money_str, currency_str)
|
|
260
|
+
return result.strip()
|
|
261
|
+
|
|
262
|
+
# For floats, use the parent class implementation
|
|
263
|
+
return super(Num2Word_IT, self).to_currency(
|
|
264
|
+
val, currency=currency, cents=cents, separator=separator,
|
|
265
|
+
adjective=adjective)
|