16 from os.path
import join
42 from collections
import namedtuple
43 from padatious.util
import expand_parentheses
51 """ Helper to get word tranlations 54 name (str): Word name. Returned as the default value if not translated. 55 lang (str): Language code, e.g. "en-us" 58 str: translated version of resource name 68 with open(filename,
'r', encoding='utf8') as f:
71 if word.startswith(
"#"):
79 NUMBER_TUPLE = namedtuple(
81 (
'x, xx, x0, x_in_x0, xxx, x00, x_in_x00, xx00, xx_in_xx00, x000, ' +
82 'x_in_x000, x0_in_x000, x_in_0x00'))
94 with open(self.
config_path +
'/' + lang +
'/date_time.json',
95 'r') as lang_config_file: 97 lang_config_file.read()) 98 except FileNotFoundError:
100 with open(self.
config_path +
'/en-us/date_time.json',
101 'r') as lang_config_file: 103 lang_config_file.read()) 105 for x
in [
'decade_format',
'hundreds_format',
'thousand_format',
110 re.compile(self.
lang_config[lang][x][str(i)][
'match']
120 str(int(number % 100 / 10)))
or str(int(number % 100 / 10))
122 str(int(number % 100 / 10) * 10))
or 123 str(int(number % 100 / 10) * 10))
124 xxx = (self.
lang_config[lang][
'number'].
get(str(number % 1000))
or 127 number % 1000 / 100) * 100))
or 128 str(int(number % 1000 / 100) * 100))
130 number % 1000 / 100)))
or str(int(number % 1000 / 100))
132 number % 10000 / 100) * 100))
or str(int(number % 10000 / 100) *
135 number % 10000 / 100)))
or str(int(number % 10000 / 100))
137 number % 10000 / 1000) * 1000))
or 138 str(int(number % 10000 / 1000) * 1000))
140 number % 10000 / 1000)))
or str(int(number % 10000 / 1000))
142 number % 10000 / 1000)*10))
or str(int(number % 10000 / 1000)*10)
144 number % 1000 / 100))
or str(int(number % 1000 / 100)))
147 x, xx, x0, x_in_x0, xxx, x00, x_in_x00, xx00, xx_in_xx00, x000,
148 x_in_x000, x0_in_x000, x_in_0x00)
151 s = self.
lang_config[lang][format_section][
'default']
155 if e[
're'].match(str(number)):
162 return s.format(x=number_tuple.x, xx=number_tuple.xx,
163 x0=number_tuple.x0, x_in_x0=number_tuple.x_in_x0,
164 number=str(number % 100))
169 return s.format(xxx=number_tuple.xxx, x00=number_tuple.x00,
170 x_in_x00=number_tuple.x_in_x00,
171 formatted_decade=formatted_decade,
172 number=str(number % 1000))
175 formatted_decade, formatted_hundreds):
177 return s.format(x_in_x00=number_tuple.x_in_x00,
178 xx00=number_tuple.xx00,
179 xx_in_xx00=number_tuple.xx_in_xx00,
180 x000=number_tuple.x000,
181 x_in_x000=number_tuple.x_in_x000,
182 x0_in_x000=number_tuple.x0_in_x000,
183 x_in_0x00=number_tuple.x_in_0x00,
184 formatted_decade=formatted_decade,
185 formatted_hundreds=formatted_hundreds,
186 number=str(number % 10000))
189 format_str =
'date_full' 191 if dt.year == now.year:
192 format_str =
'date_full_no_year' 193 if dt.month == now.month
and dt.day > now.day:
194 format_str =
'date_full_no_year_month' 196 tomorrow = now + datetime.timedelta(days=1)
197 yesterday = now - datetime.timedelta(days=1)
198 if tomorrow.date() == dt.date():
199 format_str =
'tomorrow' 200 elif now.date() == dt.date():
202 elif yesterday.date() == dt.date():
203 format_str =
'yesterday' 205 return self.
lang_config[lang][
'date_format'][format_str].format(
206 weekday=self.
lang_config[lang][
'weekday'][str(dt.weekday())],
207 month=self.
lang_config[lang][
'month'][str(dt.month)],
213 time_str =
nice_time(dt, lang, use_24hour=use_24hour,
215 return self.
lang_config[lang][
'date_time_format'][
'date_time'].format(
216 formatted_date=date_str, formatted_time=time_str)
221 self.
lang_config[lang][
'year_format'][
'bc']
if bc
else '')
223 dt.year, number_tuple, lang)
225 dt.year, number_tuple, lang, formatted_decade)
227 dt.year, number_tuple, lang, formatted_decade, formatted_hundreds)
231 return re.sub(
' +',
' ',
234 century=str(int(dt.year / 100)),
235 decade=str(dt.year % 100),
236 formatted_hundreds=formatted_hundreds,
237 formatted_decade=formatted_decade,
238 formatted_thousand=formatted_thousand,
239 bc=formatted_bc)).strip()
243 os.path.dirname(os.path.abspath(__file__)) +
'/../res/text')
246 def nice_number(number, lang=None, speech=True, denominators=None):
247 """Format a float to human readable functions 249 This function formats a float to human understandable functions. Like 250 4.5 becomes 4 and a half for speech and 4 1/2 for text 252 number (int or float): the float to format 253 lang (str): code for the language to use 254 speech (bool): format for speech (True) or display (False) 255 denominators (iter of ints): denominators to use, default [1 .. 20] 257 (str): The formatted string. 261 if lang_code ==
"en":
263 elif lang_code ==
"es":
265 elif lang_code ==
"pt":
267 elif lang_code ==
"it":
269 elif lang_code ==
"fr":
271 elif lang_code ==
"sv":
273 elif lang_code ==
"de":
275 elif lang_code ==
"hu":
277 elif lang_code ==
"nl":
279 elif lang_code ==
"da":
287 def nice_time(dt, lang=None, speech=True, use_24hour=False,
290 Format a time to a comfortable human format 292 For example, generate 'five thirty' for speech or '5:30' for 296 dt (datetime): date to format (assumes already in local timezone) 297 lang (str): code for the language to use 298 speech (bool): format for speech (default/True) or display (False) 299 use_24hour (bool): output in 24-hour/military or 12-hour format 300 use_ampm (bool): include the am/pm for 12-hour format 302 (str): The formatted time string 305 if lang_code ==
"en":
307 elif lang_code ==
"es":
309 elif lang_code ==
"it":
311 elif lang_code ==
"fr":
313 elif lang_code ==
"de":
315 elif lang_code ==
"hu":
317 elif lang_code ==
"nl":
319 elif lang_code ==
"da":
321 elif lang_code ==
"pt":
331 Convert a number to it's spoken equivalent 333 For example, '5' would be 'five' 336 number: the number to pronounce 337 short_scale (bool) : use short (True) or long scale (False) 338 https://en.wikipedia.org/wiki/Names_of_large_numbers 339 scientific (bool) : convert and pronounce in scientific notation 341 (str): The pronounced number 344 if lang_code ==
"en":
346 short_scale=short_scale,
347 scientific=scientific)
348 elif lang_code ==
"it":
350 short_scale=short_scale,
351 scientific=scientific)
352 elif lang_code ==
"es":
354 elif lang_code ==
"fr":
356 elif lang_code ==
"de":
358 elif lang_code ==
"hu":
360 elif lang_code ==
"nl":
362 elif lang_code ==
"da":
364 elif lang_code ==
"pt":
373 Format a datetime to a pronounceable date 375 For example, generates 'tuesday, june the fifth, 2018' 377 dt (datetime): date to format (assumes already in local timezone) 378 lang (string): the language to use, use Mycroft default language if not 380 now (datetime): Current date. If provided, the returned date for speech 381 will be shortened accordingly: No year is returned if now is in the 382 same year as td, no month is returned if now is in the same month 383 as td. If now and td is the same day, 'today' is returned. 385 (str): The formatted date string 388 date_time_format.cache(full_code)
390 return date_time_format.date_format(dt, full_code, now)
396 Format a datetime to a pronounceable date and time 398 For example, generate 'tuesday, june the fifth, 2018 at five thirty' 401 dt (datetime): date to format (assumes already in local timezone) 402 lang (string): the language to use, use Mycroft default language if 404 now (datetime): Current date. If provided, the returned date for 405 speech will be shortened accordingly: No year is returned if 406 now is in the same year as td, no month is returned if now is 407 in the same month as td. If now and td is the same day, 'today' 409 use_24hour (bool): output in 24-hour/military or 12-hour format 410 use_ampm (bool): include the am/pm for 12-hour format 412 (str): The formatted date time string 416 date_time_format.cache(full_code)
418 return date_time_format.date_time_format(dt, full_code, now, use_24hour,
424 Format a datetime to a pronounceable year 426 For example, generate 'nineteen-hundred and eighty-four' for year 1984 429 dt (datetime): date to format (assumes already in local timezone) 430 lang (string): the language to use, use Mycroft default language if 432 bc (bool) pust B.C. after the year (python does not support dates 435 (str): The formatted year string 439 date_time_format.cache(full_code)
441 return date_time_format.year_format(dt, full_code, bc)
445 """ Convert duration in seconds to a nice spoken timespan 448 duration = 60 -> "1:00" or "one minute" 449 duration = 163 -> "2:43" or "two minutes forty three seconds" 452 duration: time, in seconds 453 lang (str, optional): a BCP-47 language code, None for default 454 speech (bool): format for speech (True) or display (False) 456 str: timespan as a string 458 if type(duration)
is datetime.timedelta:
459 duration = duration.total_seconds()
466 days = int(duration // 86400)
467 hours = int(duration // 3600 % 24)
468 minutes = int(duration // 60 % 60)
469 seconds = int(duration % 60)
508 out = str(days) +
"d " 509 if hours > 0
or days > 0:
510 out += str(hours) +
":" 511 if minutes < 10
and (hours > 0
or days > 0):
513 out += str(minutes)+
":" 522 """ Join a list into a phrase using the given connector word 525 join_list([1,2,3], "and") -> "1, 2 and 3" 526 join_list([1,2,3], "and", ";") -> "1; 2 and 3" 529 items(array): items to be joined 530 connector(str): connecting word (resource name), like "and" or "or" 531 sep(str, optional): separator character, default = "," 533 str: the connected list phrase 545 return (sep.join(str(item)
for item
in items[:-1]) +
552 Convert 'test (a|b)' -> ['test a', 'test b'] 554 parentheses_line: Input line to expand 556 List of expanded possibilities 559 options = expand_parentheses(re.split(
r'([(|)])', parentheses_line))
560 return [re.sub(
r'\s+',
' ',
' '.join(i)).strip()
for i
in options]
def resolve_resource_file(res_name)
def get_full_lang_code(lang=None)
def get_primary_lang_code(lang=None)
def get(phrase, lang=None, context=None)