52 FRACTION_STRING_IT = {
75 LONG_SCALE_IT = collections.OrderedDict([
82 (1e24,
"quadrilioni"),
83 (1e30,
"quintilioni"),
88 (1e60,
"decemillioni"),
89 (1e66,
"undicilione"),
90 (1e72,
"dodicilione"),
91 (1e78,
"tredicilione"),
92 (1e84,
"quattordicilione"),
93 (1e90,
"quindicilione"),
94 (1e96,
"sedicilione"),
95 (1e102,
"diciasettilione"),
96 (1e108,
"diciottilione"),
97 (1e114,
"dicianovilione"),
98 (1e120,
"vintilione"),
99 (1e306,
"unquinquagintilione"),
100 (1e312,
"duoquinquagintilione"),
101 (1e336,
"sesquinquagintilione"),
102 (1e366,
"unsexagintilione")
106 SHORT_SCALE_IT = collections.OrderedDict([
109 (1000000,
'milioni'),
115 (1e24,
"quadrilioni"),
116 (1e27,
"quadriliardi"),
117 (1e30,
"quintilioni"),
118 (1e33,
"quintiliardi"),
119 (1e36,
"sestilioni"),
120 (1e39,
"sestiliardi"),
121 (1e42,
"settilioni"),
122 (1e45,
"settiliardi"),
124 (1e51,
"ottiliardi"),
126 (1e57,
"noniliardi"),
128 (1e63,
"deciliardi"),
129 (1e66,
"undicilioni"),
130 (1e69,
"undiciliardi"),
131 (1e72,
"dodicilioni"),
132 (1e75,
"dodiciliardi"),
133 (1e78,
"tredicilioni"),
134 (1e81,
"trediciliardi"),
135 (1e84,
"quattordicilioni"),
136 (1e87,
"quattordiciliardi"),
137 (1e90,
"quindicilioni"),
138 (1e93,
"quindiciliardi"),
139 (1e96,
"sedicilioni"),
140 (1e99,
"sediciliardi"),
141 (1e102,
"diciassettilioni"),
142 (1e105,
"diciassettiliardi"),
143 (1e108,
"diciottilioni"),
144 (1e111,
"diciottiliardi"),
145 (1e114,
"dicianovilioni"),
146 (1e117,
"dicianoviliardi"),
147 (1e120,
"vintilioni"),
148 (1e123,
"vintiliardi"),
149 (1e153,
"quinquagintillion"),
150 (1e183,
"sexagintillion"),
151 (1e213,
"septuagintillion"),
152 (1e243,
"ottogintilioni"),
153 (1e273,
"nonigintillioni"),
154 (1e303,
"centilioni"),
155 (1e306,
"uncentilioni"),
156 (1e309,
"duocentilioni"),
157 (1e312,
"trecentilioni"),
158 (1e333,
"decicentilioni"),
159 (1e336,
"undicicentilioni"),
160 (1e363,
"viginticentilioni"),
161 (1e366,
"unviginticentilioni"),
162 (1e393,
"trigintacentilioni"),
163 (1e423,
"quadragintacentillion"),
164 (1e453,
"quinquagintacentillion"),
165 (1e483,
"sexagintacentillion"),
166 (1e513,
"septuagintacentillion"),
167 (1e543,
"ctogintacentillion"),
168 (1e573,
"nonagintacentillion"),
169 (1e603,
"ducentillion"),
170 (1e903,
"trecentillion"),
171 (1e1203,
"quadringentillion"),
172 (1e1503,
"quingentillion"),
173 (1e1803,
"sescentillion"),
174 (1e2103,
"septingentillion"),
175 (1e2403,
"octingentillion"),
176 (1e2703,
"nongentillion"),
177 (1e3003,
"millinillion")
182 """ Italian helper for nice_number 184 This function formats a float to human understandable functions. Like 185 4.5 becomes "4 e un mezz" for speech and "4 1/2" for text 188 number (int or float): the float to format 189 speech (bool): format for speech (True) or display (False) 190 denominators (iter of ints): denominators to use, default [1 .. 20] 192 (str): The formatted string. 198 return str(round(number, 3))
200 whole, num, den = result
206 return '{} {}/{}'.format(whole, num, den)
211 den_str = FRACTION_STRING_IT[den]
216 return_string =
'un {}'.format(den_str)
219 return_string =
'{} {}'.format(num, den_str)
223 return_string =
'{} e un {}'.format(whole, den_str)
227 return_string =
'{} e {} {}'.format(whole, num, den_str)
240 Convert a number to it's spoken equivalent 241 adapted to italian fron en version 243 For example, '5.2' would return 'cinque virgola due' 246 num(float or int): the number to pronounce (under 100) 247 places(int): maximum decimal places to speak 248 short_scale (bool) : use short (True) or long scale (False) 249 https://en.wikipedia.org/wiki/Names_of_large_numbers 250 scientific (bool): pronounce in scientific notation 252 (str): The pronounced number 255 if num == float(
"inf"):
257 elif num == float(
"-inf"):
258 return "meno infinito" 262 n, power = number.replace(
"+",
"").split(
"E")
265 return '{}{} per dieci elevato alla {}{}'.format(
266 'meno ' if float(n) < 0
else '',
268 'meno ' if power < 0
else '',
272 number_names = NUM_STRING_IT.copy()
273 number_names.update(SHORT_SCALE_IT)
275 number_names = NUM_STRING_IT.copy()
276 number_names.update(LONG_SCALE_IT)
278 digits = [number_names[n]
for n
in range(0, 20)]
280 tens = [number_names[n]
for n
in range(10, 100, 10)]
283 hundreds = [SHORT_SCALE_IT[n]
for n
in SHORT_SCALE_IT.keys()]
285 hundreds = [LONG_SCALE_IT[n]
for n
in LONG_SCALE_IT.keys()]
294 if num
in number_names:
297 result += number_names[num]
299 def _sub_thousand(n):
309 if _unit == 1
or _unit == 8:
310 _partial = _partial[:-1]
311 _partial += number_names[_unit]
314 q, r = divmod(n, 100)
318 _partial = digits[q] +
"cento" 320 " " + _sub_thousand(r)
if r
else "")
324 if n >= max(SHORT_SCALE_IT.keys()):
325 return "numero davvero enorme" 329 for i, z
in enumerate(_split_by(n, 1000)):
332 number = _sub_thousand(z)
335 number += hundreds[i]
338 return ", ".join(reversed(res))
340 def _split_by(n, split=1000):
344 n, r = divmod(n, split)
349 if n >= max(LONG_SCALE_IT.keys()):
350 return "numero davvero enorme" 354 for i, z
in enumerate(_split_by(n, 1000000)):
362 number = number.replace(
',',
'')
363 number +=
" " + hundreds[i+1]
365 return ", ".join(reversed(res))
368 result += _short_scale(num)
370 result += _long_scale(num)
375 if result ==
'milioni':
376 result =
'un milione' 377 if result ==
'miliardi':
378 result =
'un miliardo' 379 if result[0:7] ==
'unomila':
380 result = result.replace(
'unomila',
'mille', 1)
381 if result[0:10] ==
'unomilioni':
382 result = result.replace(
'unomilioni',
'un milione', 1)
387 if not num == int(num)
and places > 0:
390 while int(num * place) % 10 > 0
and places > 0:
391 result +=
" " + number_names[int(num * place) % 10]
399 Format a time to a comfortable human format 400 adapted to italian fron en version 402 For example, generate 'cinque e trenta' for speech or '5:30' for 406 dt (datetime): date to format (assumes already in local timezone) 407 speech (bool): format for speech (default/True) or display (False)=Fal 408 use_24hour (bool): output in 24-hour/military or 12-hour format 409 use_ampm (bool): include the am/pm for 12-hour format 411 (str): The formatted time string 415 string = dt.strftime(
"%H:%M")
419 string = dt.strftime(
"%I:%M %p")
422 string = dt.strftime(
"%I:%M")
433 if string[0:2] ==
'00':
435 elif string[0] ==
'0':
437 if int(string[1]) == 1:
447 if string[3:5] ==
'00':
457 if dt.hour == 0
and dt.minute == 0:
459 if dt.hour == 12
and dt.minute == 0:
465 elif dt.hour == 1
or dt.hour == 13:
477 elif dt.minute == 15:
478 speak +=
" un quarto" 479 elif dt.minute == 45:
480 speak +=
" tre quarti" 491 speak +=
" della notte" 493 speak +=
" della sera" 495 speak +=
" del pomeriggio" 497 speak +=
" della mattina"