mycroft.util package

LOG

mycroft.util.LOG()[source]

Custom logger class that acts like logging.Logger The logger name is automatically generated by the module of the caller

Usage:
>>> LOG.debug('My message: %s', debug_str)
13:12:43.673 - :<module>:1 - DEBUG - My message: hi
>>> LOG('custom_name').debug('Another message')
13:13:10.462 - custom_name - DEBUG - Another message

wait_while_speaking

mycroft.util.wait_while_speaking()[source]

Pause as long as Text to Speech is still happening

Pause while Text to Speech is still happening. This always pauses briefly to ensure that any preceeding request to speak has time to begin.

play_wav

mycroft.util.play_wav(uri)[source]

play_mp3

mycroft.util.play_mp3(uri)[source]

extract_datetime

mycroft.util.extract_datetime(text, anchorDate=None, lang='en-us')[source]

Parsing function that extracts date and time information from sentences. Parses many of the common ways that humans express dates and times. Includes relative dates like “5 days from today”.

Vague terminology are given arbitrary values, like:
  • morning = 8 AM
  • afternoon = 3 PM
  • evening = 7 PM

If a time isn’t supplied, the function defaults to 12 AM

Parameters:
  • str (string) – the text to be normalized
  • anchortDate (datetime, optional) – the date to be used for relative dating (for example, what does “tomorrow” mean?). Defaults to the current date (acquired with datetime.datetime.now())
  • lang (string) – the language of the sentence(s)
Returns:

‘datetime’ is the extracted date

as a datetime object. Times are represented in 24 hour notation. ‘leftover_string’ is the original phrase with all date and time related keywords stripped out. See examples for further clarification

Returns ‘None’ if no date was extracted.

Return type:

[datetime, str]

Examples

>>> extract_datetime(
... "What is the weather like the day after tomorrow?",
... datetime(2017, 06, 30, 00, 00)
... )
[datetime.datetime(2017, 7, 2, 0, 0), 'what is weather like']
>>> extract_datetime(
... "Set up an appointment 2 weeks from Sunday at 5 pm",
... datetime(2016, 02, 19, 00, 00)
... )
[datetime.datetime(2016, 3, 6, 17, 0), 'set up appointment']

extractnumber

mycroft.util.extractnumber(text, lang='en-us')[source]

Takes in a string and extracts a number. :param text: the string to extract a number from :type text: str :param lang: the code for the language text is in :type lang: str

Returns:The number extracted or the original text.
Return type:(str)

normalize

mycroft.util.normalize(text, lang='en-us', remove_articles=True)[source]

Prepare a string for parsing

This function prepares the given text for parsing by making numbers consistent, getting rid of contractions, etc. :param text: the string to normalize :type text: str :param lang: the code for the language text is in :type lang: str :param remove_articles: whether to remove articles (like ‘a’, or ‘the’) :type remove_articles: bool

Returns:The normalized string.
Return type:(str)

nice_number

mycroft.util.nice_number(number, lang='en-us', speech=True, denominators=None)[source]

Format a float to human readable functions

This function formats a float to human understandable functions. Like 4.5 becomes 4 and a half for speech and 4 1/2 for text :param number: the float to format :type number: int or float :param lang: code for the language to use :type lang: str :param speech: format for speech (True) or display (False) :type speech: bool :param denominators: denominators to use, default [1 .. 20] :type denominators: iter of ints

Returns:The formatted string.
Return type:(str)

convert_number

resolve_resource_file

mycroft.util.resolve_resource_file(res_name)[source]

Convert a resource into an absolute filename.

Resource names are in the form: ‘filename.ext’ or ‘path/filename.ext’

The system wil look for ~/.mycroft/res_name first, and if not found will look at /opt/mycroft/res_name, then finally it will look for res_name in the ‘mycroft/res’ folder of the source code package.

Example: With mycroft running as the user ‘bob’, if you called

resolve_resource_file(‘snd/beep.wav’)

it would return either ‘/home/bob/.mycroft/snd/beep.wav’ or ‘/opt/mycroft/snd/beep.wav’ or ‘…/mycroft/res/snd/beep.wav’, where the ‘…’ is replaced by the path where the package has been installed.

Parameters:res_name (str) – a resource path/name

get_cache_directory

mycroft.util.get_cache_directory(domain=None)[source]

Get a directory for caching data

This directory can be used to hold temporary caches of data to speed up performance. This directory will likely be part of a small RAM disk and may be cleared at any time. So code that uses these cached files must be able to fallback and regenerate the file.

Parameters:domain (str) – The cache domain. Basically just a subdirectory.
Returns:a path to the directory where you can cache data
Return type:str