19 from math
import floor
21 months = [
'januari',
'februari',
'maart',
'april',
'mei',
'juni',
22 'juli',
'augustus',
'september',
'oktober',
'november',
63 '',
'duizend',
'miljoen',
'miljard',
'biljoen',
'biljard',
'triljoen',
67 FRACTION_STRING_NL = {
101 """ Dutch 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_NL[den]
127 return_string =
u'één {}'.format(den_str)
129 return_string =
'{} {}'.format(num, den_str)
131 return_string =
u'{} en één {}'.format(whole, den_str)
133 return_string =
'{} en {} {}'.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_nl(num):
154 hundreds = floor(num / 100)
156 result += NUM_STRING_NL[
157 hundreds] + EXTRA_SPACE +
'honderd' + EXTRA_SPACE
158 num -= hundreds * 100
162 result += NUM_STRING_NL[num]
167 result += NUM_STRING_NL[ones] + EXTRA_SPACE
169 result +=
'en' + EXTRA_SPACE
171 result += NUM_STRING_NL[tens] + EXTRA_SPACE
174 def pronounce_fractional_nl(num,
181 result +=
" " + NUM_STRING_NL[int(num * place) % 10]
182 if int(num * place) % 10 == 1:
189 def pronounce_whole_number_nl(num, scale_level=0):
195 last_triplet = num % 1000
197 if last_triplet == 1:
200 result +=
'' +
u'één' 203 elif scale_level == 1:
204 result +=
u'één' + EXTRA_SPACE +
'duizend' + EXTRA_SPACE
206 result +=
u"één " + NUM_POWERS_OF_TEN[scale_level] +
' ' 207 elif last_triplet > 1:
208 result += pronounce_triplet_nl(last_triplet)
211 result +=
'duizend' + EXTRA_SPACE
215 result +=
" " + NUM_POWERS_OF_TEN[scale_level] +
' ' 217 if scale_level % 2 == 0:
221 num = floor(num / 1000)
223 return pronounce_whole_number_nl(num,
224 scale_level) + result +
'' 227 if abs(num) >= 1000000000000000000000000:
230 return str(NUM_STRING_NL[0])
235 return pronounce_whole_number_nl(num)
237 whole_number_part = floor(num)
238 fractional_part = num - whole_number_part
239 result += pronounce_whole_number_nl(whole_number_part)
242 result += pronounce_fractional_nl(fractional_part, places)
247 ordinals = [
"nulste",
"eerste",
"tweede",
"derde",
"vierde",
"vijfde",
248 "zesde",
"zevende",
"achtste"]
251 if num < 0
or num != int(num):
266 Format a time to a comfortable human format 268 For example, generate 'five thirty' for speech or '5:30' for 272 dt (datetime): date to format (assumes already in local timezone) 273 speech (bool): format for speech (default/True) or display (False)=Fal 274 use_24hour (bool): output in 24-hour/military or 12-hour format 275 use_ampm (bool): include the am/pm for 12-hour format 277 (str): The formatted time string 281 string = dt.strftime(
"%H:%M")
285 string = dt.strftime(
"%I:%M %p")
288 string = dt.strftime(
"%I:%M")
300 if not dt.minute == 0:
305 if dt.hour == 0
and dt.minute == 0:
312 elif dt.minute == 30:
317 elif dt.minute == 15:
318 speak +=
"kwart over " 321 elif dt.minute == 45:
322 speak +=
"kwart voor " 355 return " 's ochtends" 360 raise Exception(
'dt.hour is bigger than 24')
368 for idx, word
in enumerate(words):
369 if word.lower()
in months:
373 wordNext = words[idx + 1]
if idx + 1 < len(words)
else "" 374 if wordNext.isnumeric():
375 words[idx] =
"tot de macht" 376 text =
" ".join(words)
383 normalized_text = text
385 for idx, word
in enumerate(words):
386 wordNext = words[idx + 1]
if idx + 1 < len(words)
else "" 387 wordPrev = words[idx - 1]
if idx > 0
else "" 388 if word[:-1].isdecimal():
389 if wordNext.lower()
in months:
395 normalized_text =
" ".join(words)
396 return normalized_text