Skip to content

Commit 3f78ff3

Browse files
authored
NumberPrompt Locale Implementation Fix (#279)
* NumberPrompt.on_recognize() no accepts default locale; recgonizers-text contants now used for English culture * black formatting * added parsing str to float with Babel library; used Culture constants from recognizers-text repo * added babel to setup.py * added culture tests; started testing for retry prompts * fixed pylint errors * completed culture unit tests * removed comment * removed commented out code in NumberPrompt
1 parent 3b2071c commit 3f78ff3

File tree

3 files changed

+361
-20
lines changed

3 files changed

+361
-20
lines changed
Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33

4-
from typing import Dict
4+
from typing import Callable, Dict
5+
6+
from babel.numbers import parse_decimal
57
from recognizers_number import recognize_number
8+
from recognizers_text import Culture, ModelResult
9+
610
from botbuilder.core.turn_context import TurnContext
711
from botbuilder.schema import ActivityTypes
8-
from .prompt import Prompt
12+
13+
from .prompt import Prompt, PromptValidatorContext
914
from .prompt_options import PromptOptions
1015
from .prompt_recognizer_result import PromptRecognizerResult
1116

1217

1318
class NumberPrompt(Prompt):
14-
# TODO: PromptValidator
15-
def __init__(self, dialog_id: str, validator: object, default_locale: str):
19+
# TODO: PromptValidator needs to be fixed
20+
# Does not accept answer as intended (times out)
21+
def __init__(
22+
self,
23+
dialog_id: str,
24+
validator: Callable[[PromptValidatorContext], bool] = None,
25+
default_locale: str = None,
26+
):
1627
super(NumberPrompt, self).__init__(dialog_id, validator)
1728
self.default_locale = default_locale
1829

@@ -30,9 +41,8 @@ async def on_prompt(
3041

3142
if is_retry and options.retry_prompt is not None:
3243
turn_context.send_activity(options.retry_prompt)
33-
else:
34-
if options.prompt is not None:
35-
await turn_context.send_activity(options.prompt)
44+
elif options.prompt is not None:
45+
await turn_context.send_activity(options.prompt)
3646

3747
async def on_recognize(
3848
self,
@@ -46,17 +56,25 @@ async def on_recognize(
4656
result = PromptRecognizerResult()
4757
if turn_context.activity.type == ActivityTypes.message:
4858
message = turn_context.activity
59+
culture = self._get_culture(turn_context)
60+
results: [ModelResult] = recognize_number(message.text, culture)
4961

50-
# TODO: Fix constant English with correct constant from text recognizer
51-
culture = (
52-
turn_context.activity.locale
53-
if turn_context.activity.locale is not None
54-
else "English"
55-
)
56-
57-
results = recognize_number(message.text, culture)
5862
if results:
5963
result.succeeded = True
60-
result.value = results[0].resolution["value"]
64+
result.value = parse_decimal(
65+
results[0].resolution["value"], locale=culture.replace("-", "_")
66+
)
6167

6268
return result
69+
70+
def _get_culture(self, turn_context: TurnContext):
71+
culture = (
72+
turn_context.activity.locale
73+
if turn_context.activity.locale
74+
else self.default_locale
75+
)
76+
77+
if not culture:
78+
culture = Culture.English
79+
80+
return culture

libraries/botbuilder-dialogs/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"recognizers-text-choice>=1.0.1a0",
1313
"grapheme>=0.5.0",
1414
"emoji>=0.5.2",
15+
"babel>=2.7.0",
1516
"botbuilder-schema>=4.4.0b1",
1617
"botframework-connector>=4.4.0b1",
1718
"botbuilder-core>=4.4.0b1",

0 commit comments

Comments
 (0)