19 from math
import floor
21 months = [
'januar',
'februar',
'märz',
'april',
'mai',
'juni',
22 'juli',
'august',
'september',
'oktober',
'november',
63 '',
'tausend',
'Million',
'Milliarde',
'Billion',
'Billiarde',
'Trillion',
67 FRACTION_STRING_DE = {
101 """ German helper for nice_number 102 This function formats a float to human understandable functions. Like 103 4.5 becomes "4 einhalb" for speech and "4 1/2" for text 105 number (int or float): the float to format 106 speech (bool): format for speech (True) or display (False) 107 denominators (iter of ints): denominators to use, default [1 .. 20] 109 (str): The formatted string. 114 return str(round(number, 3)).replace(
".",
",")
115 whole, num, den = result
121 return '{} {}/{}'.format(whole, num, den)
124 den_str = FRACTION_STRING_DE[den]
127 return_string =
'ein {}'.format(den_str)
129 return_string =
'{} {}'.format(num, den_str)
131 return_string =
'{} und ein {}'.format(whole, den_str)
133 return_string =
'{} und {} {}'.format(whole, num, den_str)
140 Convert a number to its spoken equivalent 141 For example, '5.2' would return 'five point two' 143 num(float or int): the number to pronounce (set limit below) 144 places(int): maximum decimal places to speak 146 (str): The pronounced number 150 def pronounce_triplet_de(num):
154 hundreds = floor(num / 100)
156 result += NUM_STRING_DE[
157 hundreds] + EXTRA_SPACE +
'hundert' + EXTRA_SPACE
158 num -= hundreds * 100
164 result += NUM_STRING_DE[num]
169 result += NUM_STRING_DE[ones] + EXTRA_SPACE
171 result +=
'und' + EXTRA_SPACE
173 result += NUM_STRING_DE[tens] + EXTRA_SPACE
176 def pronounce_fractional_de(num,
183 result +=
" " + NUM_STRING_DE[int(num * place) % 10]
184 if int(num * place) % 10 == 1:
191 def pronounce_whole_number_de(num, scale_level=0):
197 last_triplet = num % 1000
199 if last_triplet == 1:
202 result +=
'' +
'eins' 205 elif scale_level == 1:
206 result +=
'ein' + EXTRA_SPACE +
'tausend' + EXTRA_SPACE
208 result +=
"eine " + NUM_POWERS_OF_TEN[scale_level] +
' ' 209 elif last_triplet > 1:
210 result += pronounce_triplet_de(last_triplet)
213 result +=
'tausend' + EXTRA_SPACE
217 result +=
" " + NUM_POWERS_OF_TEN[scale_level]
219 if scale_level % 2 == 0:
223 num = floor(num / 1000)
225 return pronounce_whole_number_de(num,
226 scale_level) + result
229 if abs(num) >= 1000000000000000000000000:
232 return str(NUM_STRING_DE[0])
237 return pronounce_whole_number_de(num)
239 whole_number_part = floor(num)
240 fractional_part = num - whole_number_part
241 result += pronounce_whole_number_de(whole_number_part)
244 result += pronounce_fractional_de(fractional_part, places)
253 ordinals = [
"nullte",
"erste",
"zweite",
"dritte",
"vierte",
u"fünfte",
254 "sechste",
"siebte",
"achte"]
257 if num < 0
or num != int(num):
269 Format a time to a comfortable human format 271 For example, generate 'five thirty' for speech or '5:30' for 275 dt (datetime): date to format (assumes already in local timezone) 276 speech (bool): format for speech (default/True) or display (False)=Fal 277 use_24hour (bool): output in 24-hour/military or 12-hour format 278 use_ampm (bool): include the am/pm for 12-hour format 280 (str): The formatted time string 284 string = dt.strftime(
"%H:%M")
288 string = dt.strftime(
"%I:%M %p")
291 string = dt.strftime(
"%I:%M")
306 if not dt.minute == 0:
312 if dt.hour == 0
and dt.minute == 0:
314 if dt.hour == 12
and dt.minute == 0:
321 if dt.hour == 1
or dt.hour == 13:
331 if not dt.minute == 0:
337 speak +=
" nachmittags" 356 for idx, word
in enumerate(words):
357 if word.lower()
in months:
361 wordNext = words[idx + 1]
if idx + 1 < len(words)
else "" 362 if wordNext.isnumeric():
364 text =
" ".join(words)
371 normalized_text = text
374 for idx, word
in enumerate(words):
375 wordNext = words[idx + 1]
if idx + 1 < len(words)
else "" 376 wordPrev = words[idx - 1]
if idx > 0
else "" 378 if word[:-1].isdecimal():
379 if wordNext.lower()
in months:
381 if wordPrev.lower()
in [
"am",
"dem",
"vom",
"zum",
382 "(vom",
"(am",
"zum"]:
384 elif wordPrev.lower()
not in [
"der",
"die",
"das"]:
387 normalized_text = " ".join(words)
388 return normalized_text