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
57from recognizers_number import recognize_number
8+ from recognizers_text import Culture , ModelResult
9+
610from botbuilder .core .turn_context import TurnContext
711from botbuilder .schema import ActivityTypes
8- from .prompt import Prompt
12+
13+ from .prompt import Prompt , PromptValidatorContext
914from .prompt_options import PromptOptions
1015from .prompt_recognizer_result import PromptRecognizerResult
1116
1217
1318class 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
0 commit comments