Search code examples
c#botframework

How to use AllowPartialMatches in Microsoft.Bot.Builder.Dialogs.Choices?


I'm using Microsoft BotFramework with Microsoft.Bot.Builder 4.0 library in C#. I want to use Dialogs.Choices, and have been able to get simple ChoicePrompt working. However, the above link does not help much in understanding the namespace in depth. Online demos and samples are very basic, so I have to guess & experiment to understand the functionality.

Specifically, I'm looking at AllowPartialMatches, which appears to support some kind of fuzzy/similarity match. I.e. user types something without exact match, and the prompt finds the 'nearest' match. Is my guess correct?

Can someone explain and provide examples? Thanks?


Solution

  • In the waterfall dialog, create the dialog step as:

    AddDialog(new ChoicePrompt(UNSPSCPrompt){
        RecognizerOptions = new FindChoicesOptions()
        { AllowPartialMatches = true }
    });
    

    In the dialog step itself:

    var choices = new List<Choice>
    {
        new Choice()
        {
            Value = "itm001",
            Synonyms = new List<string> {"hotdog", "hot dog"},
            Action = new CardAction()
            {
                Type = ActionTypes.ImBack,
                Title = "Buy a hotdog",
                Value = "hotdog"
            }
        },
        new Choice()
        {
            Value = "itm002",
            Synonyms = new List<string> {"bulldog", "bull dog"},
            Action = new CardAction()
            {
                Type = ActionTypes.ImBack,
                Title = "Buy a bulldog",
                Value = "bulldog"
            }
        },
    };
    
    return await stepContext.PromptAsync("myPrompt",
        new PromptOptions {
            Prompt = MessageFactory.Text("What can I offer you?"),
            RetryPrompt = MessageFactory.Text("I dont have that"),
            Choices = choices,
            Style = ListStyle.HeroCard
        }, cancellationToken);
    

    This will make utterance "a hot one" match "hot dog".

    However, "hotdogs" will match nothing, i.e. tokens (words) need exact match.

    "dog" will match either of the choices, and it seems that only the 'top' score is returned. (Fully implemented?)