async def on_recognize(
        self,
        turn_context: TurnContext,
        state: Dict[str, object],
        options: PromptOptions,
    ) -> PromptRecognizerResult:
        if not turn_context:
            raise TypeError(
                "DateTimePrompt.on_recognize(): turn_context cannot be None.")

        result = PromptRecognizerResult()
        if turn_context.activity.type == ActivityTypes.message:
            # Recognize utterance
            message = turn_context.activity
            # TODO: English constant needs to be ported.
            culture = message.locale if message.locale is not None else "English"

            results = recognize_datetime(message.text, culture)
            if results:
                result.succeeded = True
                result.value = []
                values = results[0].resolution["values"]
                for value in values:
                    result.value.append(self.read_resolution(value))

        return result
Esempio n. 2
0
    def date_ambiguity():
        # Run the recognizer.
        results = recognize_datetime("Either Saturday or Sunday would work.",
                                     Culture.English)

        # We should find two results in this example.
        for result in results:
            # The resolution includes two example values: going backwards and forwards from NOW in the calendar.
            # Each result includes a TIMEX expression that captures the inherent date but not time ambiguity.
            # We are interested in the distinct set of TIMEX expressions.
            # There is also either a "value" property on each value or "start" and "end".
            distinct_timex_expressions = {
                value["timex"]
                for value in result.resolution["values"] if "timex" in value
            }
            print(f"{result.text} ({','.join(distinct_timex_expressions)})")
Esempio n. 3
0
    def time_range():
        # Run the recognizer.
        results = recognize_datetime("Some time between 6pm and 6:30pm.",
                                     Culture.English)

        # We should find a single result in this example.
        for result in results:
            # The resolution includes a single value because there is no ambiguity.
            # We are interested in the distinct set of TIMEX expressions.
            distinct_timex_expressions = {
                value["timex"]
                for value in result.resolution["values"] if "timex" in value
            }

            # The TIMEX expression can also capture the notion of range.
            print(f"{result.text} ({','.join(distinct_timex_expressions)})")
    async def date_prompt_validator(
            prompt_context: PromptValidatorContext) -> bool:
        is_valid = False
        results = recognize_datetime(prompt_context.recognized.value,
                                     Culture.English)
        for result in results:
            for resolution in result.resolution["values"]:
                if "value" in resolution:

                    value = resolution["value"]
                    if resolution["type"] == "date":
                        candidate = datetime.strptime(value, "%Y-%m-%d")
                        is_valid = True

        ret = prompt_context.recognized.succeeded and is_valid

        return ret
Esempio n. 5
0
    def date_time_ambiguity():
        # Run the recognizer.
        results = recognize_datetime(
            "It will be ready Wednesday at 5 o'clock.", Culture.English)

        # We should find a single result in this example.
        for result in results:
            # The resolution includes four example values: backwards and forward in the calendar and then AM and PM.
            # Each result includes a TIMEX expression that captures the inherent date but not time ambiguity.
            # We are interested in the distinct set of TIMEX expressions.
            distinct_timex_expressions = {
                value["timex"]
                for value in result.resolution["values"] if "timex" in value
            }

            # TIMEX expressions don't capture time ambiguity so there will be two distinct expressions for each result.
            print(f"{result.text} ({','.join(distinct_timex_expressions)})")
Esempio n. 6
0
    def time_ambiguity():
        # Run the recognizer.
        results = recognize_datetime(
            "We would like to arrive at 4 o'clock or 5 o'clock.",
            Culture.English)

        # We should find two results in this example.
        for result in results:
            # The resolution includes two example values: one for AM and one for PM.
            # Each result includes a TIMEX expression that captures the inherent date but not time ambiguity.
            #  We are interested in the distinct set of TIMEX expressions.
            distinct_timex_expressions = {
                value["timex"]
                for value in result.resolution["values"] if "timex" in value
            }

            # TIMEX expressions don't capture time ambiguity so there will be two distinct expressions for each result.
            print(f"{result.text} ({','.join(distinct_timex_expressions)})")
Esempio n. 7
0
    def _validate_date(self, user_input: str) -> ValidationResult:
        try:
            # Try to recognize the input as a date-time. This works for responses such as "11/14/2018", "9pm",
            # "tomorrow", "Sunday at 5pm", and so on. The recognizer returns a list of potential recognition results,
            # if any.
            results = recognize_datetime(user_input, Culture.English)
            for result in results:
                for resolution in result.resolution["values"]:
                    if "value" in resolution:
                        now = datetime.now()

                        value = resolution["value"]
                        if resolution["type"] == "date":
                            candidate = datetime.strptime(value, "%Y-%m-%d")
                        elif resolution["type"] == "time":
                            candidate = datetime.strptime(value, "%H:%M:%S")
                            candidate = candidate.replace(year=now.year,
                                                          month=now.month,
                                                          day=now.day)
                        else:
                            candidate = datetime.strptime(
                                value, "%Y-%m-%d %H:%M:%S")

                        # user response must be more than an hour out

                        diff = candidate - now
                        if diff.total_seconds() >= 3600:
                            return ValidationResult(
                                is_valid=True,
                                value=candidate.strftime("%m/%d/%y at %H:%M"),
                            )

            return ValidationResult(
                is_valid=False,
                message="I'm sorry, please enter a valid date.",
            )
        except ValueError:
            return ValidationResult(
                is_valid=False,
                message=
                "I'm sorry, I could not interpret that as an appropriate "
                "date. Please enter a date at least an hour out.",
            )