-
Notifications
You must be signed in to change notification settings - Fork 304
Add FindChoiceOptions for recognize_numbers and recognize_ordinals #691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ed02801
Add FindChoiceOptions for recognize_numbers and recognize_ordinals
tracyboehrer d9ebbb4
Corrected if statement
tracyboehrer 3da6e68
Merge branch 'master' into trboehre-find_choice_options
tracyboehrer 53f4cd6
Merge branch 'master' into trboehre-find_choice_options
tracyboehrer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
281 changes: 141 additions & 140 deletions
281
libraries/botbuilder-dialogs/botbuilder/dialogs/choices/choice_recognizers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,140 +1,141 @@ | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
|
|
||
| from typing import List, Union | ||
| from recognizers_number import NumberModel, NumberRecognizer, OrdinalModel | ||
| from recognizers_text import Culture | ||
|
|
||
|
|
||
| from .choice import Choice | ||
| from .find import Find | ||
| from .find_choices_options import FindChoicesOptions | ||
| from .found_choice import FoundChoice | ||
| from .model_result import ModelResult | ||
|
|
||
|
|
||
| class ChoiceRecognizers: | ||
| """ Contains methods for matching user input against a list of choices. """ | ||
|
|
||
| @staticmethod | ||
| def recognize_choices( | ||
| utterance: str, | ||
| choices: List[Union[str, Choice]], | ||
| options: FindChoicesOptions = None, | ||
| ) -> List[ModelResult]: | ||
| """ | ||
| Matches user input against a list of choices. | ||
| This is layered above the `Find.find_choices()` function, and adds logic to let the user specify | ||
| their choice by index (they can say "one" to pick `choice[0]`) or ordinal position | ||
| (they can say "the second one" to pick `choice[1]`.) | ||
| The user's utterance is recognized in the following order: | ||
| - By name using `find_choices()` | ||
| - By 1's based ordinal position. | ||
| - By 1's based index position. | ||
| Parameters: | ||
| ----------- | ||
| utterance: The input. | ||
| choices: The list of choices. | ||
| options: (Optional) Options to control the recognition strategy. | ||
| Returns: | ||
| -------- | ||
| A list of found choices, sorted by most relevant first. | ||
| """ | ||
| if utterance is None: | ||
| utterance = "" | ||
|
|
||
| # Normalize list of choices | ||
| choices_list = [ | ||
| Choice(value=choice) if isinstance(choice, str) else choice | ||
| for choice in choices | ||
| ] | ||
|
|
||
| # Try finding choices by text search first | ||
| # - We only want to use a single strategy for returning results to avoid issues where utterances | ||
| # like the "the third one" or "the red one" or "the first division book" would miss-recognize as | ||
| # a numerical index or ordinal as well. | ||
| locale = options.locale if (options and options.locale) else Culture.English | ||
| matched = Find.find_choices(utterance, choices_list, options) | ||
| if not matched: | ||
| # Next try finding by ordinal | ||
| matches = ChoiceRecognizers._recognize_ordinal(utterance, locale) | ||
|
|
||
| if matches: | ||
| for match in matches: | ||
| ChoiceRecognizers._match_choice_by_index( | ||
| choices_list, matched, match | ||
| ) | ||
| else: | ||
| # Finally try by numerical index | ||
| matches = ChoiceRecognizers._recognize_number(utterance, locale) | ||
|
|
||
| for match in matches: | ||
| ChoiceRecognizers._match_choice_by_index( | ||
| choices_list, matched, match | ||
| ) | ||
|
|
||
| # Sort any found matches by their position within the utterance. | ||
| # - The results from find_choices() are already properly sorted so we just need this | ||
| # for ordinal & numerical lookups. | ||
| matched = sorted(matched, key=lambda model_result: model_result.start) | ||
|
|
||
| return matched | ||
|
|
||
| @staticmethod | ||
| def _recognize_ordinal(utterance: str, culture: str) -> List[ModelResult]: | ||
| model: OrdinalModel = NumberRecognizer(culture).get_ordinal_model(culture) | ||
|
|
||
| return list( | ||
| map(ChoiceRecognizers._found_choice_constructor, model.parse(utterance)) | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _match_choice_by_index( | ||
| choices: List[Choice], matched: List[ModelResult], match: ModelResult | ||
| ): | ||
| try: | ||
| index: int = int(match.resolution.value) - 1 | ||
| if 0 <= index < len(choices): | ||
| choice = choices[index] | ||
|
|
||
| matched.append( | ||
| ModelResult( | ||
| start=match.start, | ||
| end=match.end, | ||
| type_name="choice", | ||
| text=match.text, | ||
| resolution=FoundChoice( | ||
| value=choice.value, index=index, score=1.0 | ||
| ), | ||
| ) | ||
| ) | ||
| except: | ||
| # noop here, as in dotnet/node repos | ||
| pass | ||
|
|
||
| @staticmethod | ||
| def _recognize_number(utterance: str, culture: str) -> List[ModelResult]: | ||
| model: NumberModel = NumberRecognizer(culture).get_number_model(culture) | ||
|
|
||
| return list( | ||
| map(ChoiceRecognizers._found_choice_constructor, model.parse(utterance)) | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _found_choice_constructor(value_model: ModelResult) -> ModelResult: | ||
| return ModelResult( | ||
| start=value_model.start, | ||
| end=value_model.end, | ||
| type_name="choice", | ||
| text=value_model.text, | ||
| resolution=FoundChoice( | ||
| value=value_model.resolution["value"], index=0, score=1.0 | ||
| ), | ||
| ) | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
|
|
||
| from typing import List, Union | ||
| from recognizers_number import NumberModel, NumberRecognizer, OrdinalModel | ||
| from recognizers_text import Culture | ||
|
|
||
|
|
||
| from .choice import Choice | ||
| from .find import Find | ||
| from .find_choices_options import FindChoicesOptions | ||
| from .found_choice import FoundChoice | ||
| from .model_result import ModelResult | ||
|
|
||
|
|
||
| class ChoiceRecognizers: | ||
| """ Contains methods for matching user input against a list of choices. """ | ||
|
|
||
| @staticmethod | ||
| def recognize_choices( | ||
| utterance: str, | ||
| choices: List[Union[str, Choice]], | ||
| options: FindChoicesOptions = None, | ||
| ) -> List[ModelResult]: | ||
| """ | ||
| Matches user input against a list of choices. | ||
| This is layered above the `Find.find_choices()` function, and adds logic to let the user specify | ||
| their choice by index (they can say "one" to pick `choice[0]`) or ordinal position | ||
| (they can say "the second one" to pick `choice[1]`.) | ||
| The user's utterance is recognized in the following order: | ||
| - By name using `find_choices()` | ||
| - By 1's based ordinal position. | ||
| - By 1's based index position. | ||
| Parameters: | ||
| ----------- | ||
| utterance: The input. | ||
| choices: The list of choices. | ||
| options: (Optional) Options to control the recognition strategy. | ||
| Returns: | ||
| -------- | ||
| A list of found choices, sorted by most relevant first. | ||
| """ | ||
| if utterance is None: | ||
| utterance = "" | ||
|
|
||
| # Normalize list of choices | ||
| choices_list = [ | ||
| Choice(value=choice) if isinstance(choice, str) else choice | ||
| for choice in choices | ||
| ] | ||
|
|
||
| # Try finding choices by text search first | ||
| # - We only want to use a single strategy for returning results to avoid issues where utterances | ||
| # like the "the third one" or "the red one" or "the first division book" would miss-recognize as | ||
| # a numerical index or ordinal as well. | ||
| locale = options.locale if (options and options.locale) else Culture.English | ||
| matched = Find.find_choices(utterance, choices_list, options) | ||
| if not matched: | ||
| matches = [] | ||
|
|
||
| if not options or options.recognize_ordinals: | ||
| # Next try finding by ordinal | ||
| matches = ChoiceRecognizers._recognize_ordinal(utterance, locale) | ||
| for match in matches: | ||
| ChoiceRecognizers._match_choice_by_index( | ||
| choices_list, matched, match | ||
| ) | ||
|
|
||
| if not matches and (not options or options.recognize_ordinals): | ||
| # Then try by numerical index | ||
| matches = ChoiceRecognizers._recognize_number(utterance, locale) | ||
| for match in matches: | ||
| ChoiceRecognizers._match_choice_by_index( | ||
| choices_list, matched, match | ||
| ) | ||
|
|
||
| # Sort any found matches by their position within the utterance. | ||
| # - The results from find_choices() are already properly sorted so we just need this | ||
| # for ordinal & numerical lookups. | ||
| matched = sorted(matched, key=lambda model_result: model_result.start) | ||
|
|
||
| return matched | ||
|
|
||
| @staticmethod | ||
| def _recognize_ordinal(utterance: str, culture: str) -> List[ModelResult]: | ||
| model: OrdinalModel = NumberRecognizer(culture).get_ordinal_model(culture) | ||
|
|
||
| return list( | ||
| map(ChoiceRecognizers._found_choice_constructor, model.parse(utterance)) | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _match_choice_by_index( | ||
| choices: List[Choice], matched: List[ModelResult], match: ModelResult | ||
| ): | ||
| try: | ||
| index: int = int(match.resolution.value) - 1 | ||
| if 0 <= index < len(choices): | ||
| choice = choices[index] | ||
|
|
||
| matched.append( | ||
| ModelResult( | ||
| start=match.start, | ||
| end=match.end, | ||
| type_name="choice", | ||
| text=match.text, | ||
| resolution=FoundChoice( | ||
| value=choice.value, index=index, score=1.0 | ||
| ), | ||
| ) | ||
| ) | ||
| except: | ||
| # noop here, as in dotnet/node repos | ||
| pass | ||
|
|
||
| @staticmethod | ||
| def _recognize_number(utterance: str, culture: str) -> List[ModelResult]: | ||
| model: NumberModel = NumberRecognizer(culture).get_number_model(culture) | ||
|
|
||
| return list( | ||
| map(ChoiceRecognizers._found_choice_constructor, model.parse(utterance)) | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _found_choice_constructor(value_model: ModelResult) -> ModelResult: | ||
| return ModelResult( | ||
| start=value_model.start, | ||
| end=value_model.end, | ||
| type_name="choice", | ||
| text=value_model.text, | ||
| resolution=FoundChoice( | ||
| value=value_model.resolution["value"], index=0, score=1.0 | ||
| ), | ||
| ) | ||
61 changes: 38 additions & 23 deletions
61
libraries/botbuilder-dialogs/botbuilder/dialogs/choices/find_choices_options.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,38 @@ | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
|
|
||
| from .find_values_options import FindValuesOptions | ||
|
|
||
|
|
||
| class FindChoicesOptions(FindValuesOptions): | ||
| """ Contains options to control how input is matched against a list of choices """ | ||
|
|
||
| def __init__(self, no_value: bool = None, no_action: bool = None, **kwargs): | ||
| """ | ||
| Parameters: | ||
| ----------- | ||
|
|
||
| no_value: (Optional) If `True`, the choices `value` field will NOT be search over. Defaults to `False`. | ||
|
|
||
| no_action: (Optional) If `True`, the choices `action.title` field will NOT be searched over. | ||
| Defaults to `False`. | ||
| """ | ||
|
|
||
| super().__init__(**kwargs) | ||
| self.no_value = no_value | ||
| self.no_action = no_action | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
|
|
||
| from .find_values_options import FindValuesOptions | ||
|
|
||
|
|
||
| class FindChoicesOptions(FindValuesOptions): | ||
| """ Contains options to control how input is matched against a list of choices """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| no_value: bool = None, | ||
| no_action: bool = None, | ||
| recognize_numbers: bool = True, | ||
| recognize_ordinals: bool = True, | ||
| **kwargs, | ||
| ): | ||
| """ | ||
| Parameters: | ||
| ----------- | ||
|
|
||
| no_value: (Optional) If `True`, the choices `value` field will NOT be search over. Defaults to `False`. | ||
|
|
||
| no_action: (Optional) If `True`, the choices `action.title` field will NOT be searched over. | ||
| Defaults to `False`. | ||
|
|
||
| recognize_numbers: (Optional) Indicates whether the recognizer should check for Numbers using the | ||
| NumberRecognizer's NumberModel. | ||
|
|
||
| recognize_ordinals: (Options) Indicates whether the recognizer should check for Ordinal Numbers using | ||
| the NumberRecognizer's OrdinalModel. | ||
| """ | ||
|
|
||
| super().__init__(**kwargs) | ||
| self.no_value = no_value | ||
| self.no_action = no_action | ||
| self.recognize_numbers = recognize_numbers | ||
| self.recognize_ordinals = recognize_ordinals |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.