Пример #1
0
    async def execute_luis_query(
            luis_recognizer: LuisRecognizer,
            turn_context: TurnContext) -> (Intent, object):
        """
        Returns an object with preformatted LUIS results for the bot's dialogs to consume.
        """
        result = None
        intent = None

        try:
            recognizer_result = await luis_recognizer.recognize(turn_context)

            intent = (sorted(
                recognizer_result.intents,
                key=recognizer_result.intents.get,
                reverse=True,
            )[:1][0] if recognizer_result.intents else None)

            if intent == Intent.BOOK_FLIGHT.value:
                result = BookingDetails()

                # We need to get the result from the LUIS JSON which at every level returns an array.
                to_entities = recognizer_result.entities.get("$instance",
                                                             {}).get("To", [])
                if len(to_entities) > 0:
                    if recognizer_result.entities.get("To", [{
                            "$instance": {}
                    }])[0]["$instance"]:
                        result.destination = to_entities[0]["text"].capitalize(
                        )
                    else:
                        result.unsupported_airports.append(
                            to_entities[0]["text"].capitalize())

                from_entities = recognizer_result.entities.get(
                    "$instance", {}).get("From", [])
                if len(from_entities) > 0:
                    if recognizer_result.entities.get("From", [{
                            "$instance": {}
                    }])[0]["$instance"]:
                        result.origin = from_entities[0]["text"].capitalize()
                    else:
                        result.unsupported_airports.append(
                            from_entities[0]["text"].capitalize())

                budget_entities = recognizer_result.entities.get(
                    "$instance", {}).get("budget", [])
                print("luis_helper.py :", budget_entities)
                print("Voici le budget :",
                      budget_entities[0]["text"].capitalize())
                if len(budget_entities) > 0:
                    if budget_entities[0].get("text", ''):
                        result.budget = budget_entities[0]["text"].capitalize()
                    else:
                        result.budget = ""

                # This value will be a TIMEX. And we are only interested in a Date so grab the first result and drop
                # the Time part. TIMEX is a format that represents DateTime expressions that include some ambiguity.
                # e.g. missing a Year.
                date_entities = recognizer_result.entities.get("datetime", [])
                if date_entities:
                    timex = date_entities[0]["timex"]

                    if timex:
                        datetime = timex[0].split("T")[0]

                        result.from_date = datetime

                else:
                    result.from_date = None

        except Exception as exception:
            print(exception)

        return intent, result
Пример #2
0
    async def execute_luis_query(
            luis_recognizer: LuisRecognizer,
            turn_context: TurnContext) -> (Intent, object):
        """
        Returns an object with preformatted LUIS results for the bot's dialogs to consume.
        """
        result = None
        intent = None

        try:
            recognizer_result = await luis_recognizer.recognize(turn_context)
            intent = LuisRecognizer.top_intent(recognizer_result)
            #ajout ---
            #retult = recognizer_result.properties["recognizerResult"]
            #await turn_context.send_activity(f" Luis Result {recognizer_result.entities}")
            #-------------------------

            #intent = (
            #sorted(
            #recognizer_result.intents,
            #key=recognizer_result.intents.get,
            #reverse=True,
            #)[:1][0]
            #if recognizer_result.intents
            #else None
            #)

            if intent == Intent.BOOK_FLIGHT.value:
                result = BookingDetails()

                # We need to get the result from the LUIS JSON which at every level returns an array.
                to_entities = recognizer_result.entities.get("$instance",
                                                             {}).get("To", [])

                if len(to_entities) > 0:
                    result.destination = to_entities[0]["text"].capitalize()

                from_entities = recognizer_result.entities.get(
                    "$instance", {}).get("From", [])
                if len(from_entities) > 0:
                    result.origin = from_entities[0]["text"].capitalize()

                budget_entities = recognizer_result.entities.get(
                    "$instance", {}).get("budget", [])
                if len(budget_entities) > 0:
                    result.budget = budget_entities[0]["text"]

                # This value will be a TIMEX. And we are only interested in a Date so grab the first result and drop
                # the Time part. TIMEX is a format that represents DateTime expressions that include some ambiguity.
                # e.g. missing a Year.

                date_entities = recognizer_result.entities.get("str_date", [])
                if date_entities:
                    timex = date_entities[0]["datetime"][0]["timex"]
                    if timex:
                        datetime = timex[0].split("T")[0]
                        result.travel_date = datetime
                    else:
                        result.travel_date = None

                date_return_entities = recognizer_result.entities.get(
                    "$instance", {}).get("end_date", [])
                if date_return_entities:
                    timex = date_return_entities[0]["datetime"][0]["timex"]
                    if timex:
                        datetime = timex[0].split("T")[0]
                        result.return_date = datetime
                    else:
                        result.return_date = None

        except Exception as exception:
            print(exception)

        return intent, result