diff --git a/libraries/Microsoft.Bot.Builder.Dialogs/Choices/Find.cs b/libraries/Microsoft.Bot.Builder.Dialogs/Choices/Find.cs index 67e4a14fc4..b16df92fd0 100644 --- a/libraries/Microsoft.Bot.Builder.Dialogs/Choices/Find.cs +++ b/libraries/Microsoft.Bot.Builder.Dialogs/Choices/Find.cs @@ -103,6 +103,11 @@ public static List> FindChoices(string utterance, IList /// A list of found values. public static List> FindValues(string utterance, List values, FindValuesOptions options = null) { + if (FindExactMatch(utterance, values) is ModelResult exactMatch) + { + return new List> { exactMatch }; + } + // Sort values in descending order by length so that the longest value is searched over first. var list = values; list.Sort((a, b) => b.Value.Length - a.Value.Length); @@ -114,10 +119,8 @@ public static List> FindValues(string utterance, List MatchValue(IList sourceTokens, int return result; } + + private static ModelResult FindExactMatch(string utterance, List values) + { + foreach (var entry in values) + { + if (entry.Value.Equals(utterance, StringComparison.OrdinalIgnoreCase)) + { + return new ModelResult + { + Text = utterance, + Start = 0, + End = utterance.Length - 1, + TypeName = "value", + Resolution = new FoundValue + { + Value = entry.Value, + Index = entry.Index, + Score = 1, + }, + }; + } + } + + return null; + } } } diff --git a/tests/Microsoft.Bot.Builder.Dialogs.Tests/ChoicesRecognizersTests.cs b/tests/Microsoft.Bot.Builder.Dialogs.Tests/ChoicesRecognizersTests.cs index d74f28ba8e..60ff80b961 100644 --- a/tests/Microsoft.Bot.Builder.Dialogs.Tests/ChoicesRecognizersTests.cs +++ b/tests/Microsoft.Bot.Builder.Dialogs.Tests/ChoicesRecognizersTests.cs @@ -2,6 +2,7 @@ // Licensed under the MIT License. using System.Collections.Generic; +using System.Linq; using Microsoft.Bot.Builder.Dialogs.Choices; using Xunit; @@ -36,6 +37,13 @@ public class ChoicesRecognizersTests new SortedValue { Value = "option C", Index = 2 }, }; + private static List valuesWithSpecialCharacters = new List + { + new SortedValue { Value = "A < B", Index = 0 }, + new SortedValue { Value = "A >= B", Index = 1 }, + new SortedValue { Value = "A ??? B", Index = 2 }, + }; + // FindValues [Fact] public void ShouldFindASimpleValueInAnSingleWordUtterance() @@ -83,6 +91,16 @@ public void ShouldCorrectlyDisambiguateBetweenVerySimilarValues() AssertValue(found[0], "option B", 1, 1.0f); } + [Fact] + public void ShouldPreferExactMatch() + { + var index = 1; + var utterance = valuesWithSpecialCharacters[index].Value; + var found = Find.FindValues(utterance, valuesWithSpecialCharacters); + + AssertValue(found.Single(), utterance, index, 1); + } + [Fact] public void ShouldFindASingleChoiceInAnUtterance() {