10000 Added timex parsing to luis_helper & corrected ambiguous date logic (… · microsoft/botbuilder-python@bbbb543 · GitHub
[go: up one dir, main page]

Skip to content

Commit bbbb543

Browse files
authored
Added timex parsing to luis_helper & corrected ambiguous date logic (#299)
1 parent c98eb96 commit bbbb543

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

samples/13.core-bot/dialogs/date_resolver_dialog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ async def initial_step(
5656
PromptOptions(prompt=prompt_msg, retry_prompt=reprompt_msg),
5757
)
5858
# We have a Date we just need to check it is unambiguous.
59-
if "definite" in Timex(timex).types:
59+
if "definite" not in Timex(timex).types:
6060
# This is essentially a "reprompt" of the data we were given up front.
6161
return await step_context.prompt(
6262
DateTimePrompt.__name__, PromptOptions(prompt=reprompt_msg)

samples/13.core-bot/dialogs/main_dialog.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33

4-
from datetime import datetime
5-
from typing import Dict
64
from botbuilder.dialogs import (
75
ComponentDialog,
8-
DialogSet,
9-
DialogTurnStatus,
106
WaterfallDialog,
117
WaterfallStepContext,
128
DialogTurnResult,
139
)
14-
from botbuilder.dialogs.prompts import TextPrompt, ConfirmPrompt, PromptOptions
10+
from botbuilder.dialogs.prompts import TextPrompt, PromptOptions
1511
from botbuilder.core import MessageFactory, TurnContext
1612
from botbuilder.schema import InputHints
1713

@@ -76,9 +72,8 @@ async def act_step(self, step_context: WaterfallStepContext) -> DialogTurnResult
7672
self._luis_recognizer, step_context.context
7773
)
7874

79-
# top_intent = cognitive_models_helper.top_intent(luis_result['intents'])
80-
8175
if intent == Intent.BOOK_FLIGHT.value and luis_result:
76+
# Show a warning for Origin and Destination if we can't resolve them.
8277
await MainDialog._show_warning_for_unsupported_cities(
8378
step_context.context, luis_result
8479
)

samples/13.core-bot/helpers/luis_helper.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Licensed under the MIT License.
33
from enum import Enum
44
from typing import Dict
5-
from botbuilder.ai.luis import LuisRecognizer, LuisApplication
5+
from botbuilder.ai.luis import LuisRecognizer
66
from botbuilder.core import IntentScore, TopIntent, TurnContext
77

88
from booking_details import BookingDetails
@@ -32,6 +32,9 @@ class LuisHelper:
3232
async def execute_luis_query(
3333
luis_recognizer: LuisRecognizer, turn_context: TurnContext
3434
) -> (Intent, object):
35+
"""
36+
Returns an object with preformatted LUIS results for the bot's dialogs to consume.
37+
"""
3538
result = None
3639
intent = None
3740

@@ -78,13 +81,20 @@ async def execute_luis_query(
7881
from_entities[0]["text"].capitalize()
7982
)
8083

81-
# TODO: This value will be a TIMEX. And we are only interested in a Date so grab the first result and drop the Time part.
84+
# This value will be a TIMEX. And we are only interested in a Date so grab the first result and drop the Time part.
8285
# TIMEX is a format that represents DateTime expressions that include some ambiguity. e.g. missing a Year.
83-
date_entities = recognizer_result.entities.get("$instance", {}).get(
84-
"datetime", []
85-
)
86-
if len(date_entities) > 0:
87-
result.travel_date = None # TODO: Set when we get a timex format
86+
date_entities = recognizer_result.entities.get("datetime", [])
87+
if date_entities:
88+
timex = date_entities[0]["timex"]
89+
90+
if timex:
91+
datetime = timex[0].split("T")[0]
92+
93+
result.travel_date = datetime
94+
95+
else:
96+
result.travel_date = None
97+
8898
except Exception as e:
8999
print(e)
90100

0 commit comments

Comments
 (0)
0