Ejemplo n.º 1
0
def extract_datetime(text, anchorDate=None, lang=None, default_time=None):
    """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
    Args:
        text (str): the text to be interpreted
        anchorDate (:obj:`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:
        [:obj:`datetime`, :obj:`str`]: '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.
    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
    """
    ret = lingua_franca.parse.extract_datetime(text, anchorDate or now_local(),
                                               lang, default_time)

    # TODO: When default skills work with the documented behavour below needs
    # to be removed.
    # This implements the incorrect return value expected by many skills
    lang = lang or get_active_lang()
    if ret is None and lang == 'en-us':
        ret = (anchorDate or now_local().replace(
            hour=0, minute=0, second=0, microsecond=0), text)

    return ret
Ejemplo n.º 2
0
 def setUp(self):
     self.old_lang = get_active_lang()
     set_active_lang("de-de")
Ejemplo n.º 3
0
 def setUp(self):
     self.old_lang = get_active_lang()
     set_active_lang("cs-cz")