Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions libraries/Microsoft.Bot.Builder.Dialogs/Choices/Find.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public static List<ModelResult<FoundChoice>> FindChoices(string utterance, IList
/// <returns>A list of found values.</returns>
public static List<ModelResult<FoundValue>> FindValues(string utterance, List<SortedValue> values, FindValuesOptions options = null)
{
if (FindExactMatch(utterance, values) is ModelResult<FoundValue> exactMatch)
{
return new List<ModelResult<FoundValue>> { 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);
Expand All @@ -114,10 +119,8 @@ public static List<ModelResult<FoundValue>> FindValues(string utterance, List<So
var tokens = tokenizer(utterance, opt.Locale);
var maxDistance = opt.MaxTokenDistance ?? 2;

for (var index = 0; index < list.Count; index++)
foreach (var entry in list)
{
var entry = list[index];

// Find all matches for a value
// - To match "last one" in "the last time I chose the last one" we need
// to re-search the string starting from the end of the previous match.
Expand Down Expand Up @@ -278,5 +281,30 @@ private static ModelResult<FoundValue> MatchValue(IList<Token> sourceTokens, int

return result;
}

private static ModelResult<FoundValue> FindExactMatch(string utterance, List<SortedValue> values)
{
foreach (var entry in values)
{
if (entry.Value.Equals(utterance, StringComparison.OrdinalIgnoreCase))
{
return new ModelResult<FoundValue>
{
Text = utterance,
Start = 0,
End = utterance.Length - 1,
TypeName = "value",
Resolution = new FoundValue
{
Value = entry.Value,
Index = entry.Index,
Score = 1,
},
};
}
}

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Linq;
using Microsoft.Bot.Builder.Dialogs.Choices;
using Xunit;

Expand Down Expand Up @@ -36,6 +37,13 @@ public class ChoicesRecognizersTests
new SortedValue { Value = "option C", Index = 2 },
};

private static List<SortedValue> valuesWithSpecialCharacters = new List<SortedValue>
{
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()
Expand Down Expand Up @@ -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()
{
Expand Down