From f53c9e971cbb22cadfa312d79fe8eab25039087d Mon Sep 17 00:00:00 2001 From: CeciliaAvila Date: Fri, 17 Dec 2021 17:56:21 -0300 Subject: [PATCH 1/2] Add findExactMatch function for choices --- .../src/choices/findValues.ts | 21 +++++++++++++++++++ .../tests/choices_recognizers.test.js | 14 +++++++++++++ 2 files changed, 35 insertions(+) diff --git a/libraries/botbuilder-dialogs/src/choices/findValues.ts b/libraries/botbuilder-dialogs/src/choices/findValues.ts index 0909eb2b89..1915c12cb2 100644 --- a/libraries/botbuilder-dialogs/src/choices/findValues.ts +++ b/libraries/botbuilder-dialogs/src/choices/findValues.ts @@ -98,6 +98,27 @@ export function findValues( return -1; } + function findExactMatch(utterance: string, values: SortedValue[]) { + return values + .filter((entry) => entry.value.toLowerCase() === utterance.toLowerCase()) + .map((entry) => ({ + text: utterance, + start: 0, + end: utterance.length - 1, + typeName: 'value', + resolution: { + value: entry.value, + index: entry.index, + score: 1, + }, + })); + } + + const exactMatch = findExactMatch(utterance, values); + if (exactMatch?.length) { + return exactMatch; + } + function matchValue( index: number, value: string, diff --git a/libraries/botbuilder-dialogs/tests/choices_recognizers.test.js b/libraries/botbuilder-dialogs/tests/choices_recognizers.test.js index a118430c2d..bddea279c6 100644 --- a/libraries/botbuilder-dialogs/tests/choices_recognizers.test.js +++ b/libraries/botbuilder-dialogs/tests/choices_recognizers.test.js @@ -53,6 +53,12 @@ const similarValues = [ { value: 'option C', index: 2 }, ]; +const valuesWithSpecialCharacters = [ + { value: 'A < B', index: 0 }, + { value: 'A >= B', index: 1 }, + { value: 'A ??? B', index: 2 }, +]; + describe('findValues()', function () { this.timeout(5000); @@ -91,6 +97,14 @@ describe('findValues()', function () { assert(found.length === 1, `Invalid token count of '${found.length}' returned.`); assertValue(found[0], 'option B', 1, 1.0); }); + + it('should prefer exact match.', function () { + const index = 1; + const utterance = valuesWithSpecialCharacters[index].value; + const found = findValues(utterance, valuesWithSpecialCharacters); + assert(found.length === 1, `Invalid token count of '${found.length}' returned.`); + assertValue(found[0], utterance, index, 1); + }); }); //============================================================================= From eb364eeeb60b3beb11cf3b7b8d987cbe897c5034 Mon Sep 17 00:00:00 2001 From: CeciliaAvila Date: Mon, 20 Dec 2021 10:39:50 -0300 Subject: [PATCH 2/2] Update findExactMatch function to match .net impl. --- .../src/choices/findValues.ts | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/libraries/botbuilder-dialogs/src/choices/findValues.ts b/libraries/botbuilder-dialogs/src/choices/findValues.ts index 1915c12cb2..2945e315f7 100644 --- a/libraries/botbuilder-dialogs/src/choices/findValues.ts +++ b/libraries/botbuilder-dialogs/src/choices/findValues.ts @@ -98,25 +98,26 @@ export function findValues( return -1; } - function findExactMatch(utterance: string, values: SortedValue[]) { - return values - .filter((entry) => entry.value.toLowerCase() === utterance.toLowerCase()) - .map((entry) => ({ - text: utterance, - start: 0, - end: utterance.length - 1, - typeName: 'value', - resolution: { - value: entry.value, - index: entry.index, - score: 1, - }, - })); + function findExactMatch(utterance: string, values: SortedValue[]): ModelResult { + const entry = values.find(({ value }) => value.toLowerCase() === utterance.toLowerCase()); + if (!entry) { + return null; + } + return { + text: utterance, + start: 0, + end: utterance.length - 1, + typeName: 'value', + resolution: { + value: entry.value, + index: entry.index, + score: 1, + }, + }; } - const exactMatch = findExactMatch(utterance, values); - if (exactMatch?.length) { - return exactMatch; + if (exactMatch) { + return [exactMatch]; } function matchValue(