mycroft.util.parse

The mycroft.util.parse module provides various parsing functions for things like numbers, times, durations etc. It’s intention is to convert naturally expressed concepts into standard computer readable formats. Doing this also enables localization.

It also provides some useful associated functions like basic fuzzy matching.

The module uses lingua-franca (https://github.com/mycroftai/lingua-franca) to do most of the actual parsing. However methods may be wrapped specifically for use in Mycroft Skills.

mycroft.util.parse.extract_datetime(text, anchorDate='DEFAULT', lang=None, default_time=None)[source]

Extracts date and time information from a sentence.

Parses many of the common ways that humans express dates and times, including relative dates like “5 days from today”, “tomorrow’, and “Tuesday”.

Vague terminology are given arbitrary values, like:

  • morning = 8 AM

  • afternoon = 3 PM

  • evening = 7 PM

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

Parameters
  • text (str) – the text to be interpreted

  • anchorDate (datetime, optional) – the date to be used for relative dating (for example, what does “tomorrow” mean?). Defaults to the current local date/time.

  • lang (str) – the BCP-47 code for the language to use, None uses default

  • default_time (datetime.time) – time to use if none was found in the input string.

Returns

‘datetime’ is the extracted date

as a datetime object in the user’s local timezone. ‘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 or time related text is found.

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']
>>> extract_datetime(
... "Set up an appointment",
... datetime(2016, 02, 19, 00, 00)
... )
None