在调用 LUIS 之前调用 Microsoft Translate 之前使用 Bing 拼写检查

Use Bing Spell Check before Microsoft Translate which is called before call to LUIS

所以... 我正在尝试在瑞典语中将 Bot Framwork 与 LUIS 结合使用。 我使用这些示例将输入从瑞典语翻译成英语,然后调用 LUIS 功能。 在我们从 LUIS 获得一些非常奇怪的意向命中之前,它工作得很好。 我们发现,一个非常小的拼写错误(瑞典语)导致翻译创建了一条触发错误意图的消息。 我们可以通过检查接收到的意图的分数来解决问题,但是返回给用户的消息 "I didn't understand that" 并不是特别有用。 运行 通过 Bing 拼写检查并用正确文本替换错误文本将产生正确的行为(大部分)。

我想做的是使用拼写检查的结果来询问用户文本 he/she 是否应该替换为 Bing.

的结果

现在的问题是:我找不到合适的方法来实现对用户的对话。 (如果可能的话,我想避免使用 PromptDialog.Confirm 因为它很难本地化)

我现在拥有的(不起作用)大致是:

if (activity.Type == ActivityTypes.Message)
{
    correctedText = await sc.BingSpellCheck(activity.Text, spellValues);

    if (spellValues.Count > 0)
    {
      // Ask the client if the correction is ok
      await Conversation.SendAsync(activity, () => new CorrectSpellingDialog());
    }
    Translate.Current.ToEnglish(activity.Text, "en");
    await Conversation.SendAsync(activity, () => new MeBotLuisDialog());
}

我想在这里创建一个 CorrectSpellingDialog() 只是 returns true 或 false,如果它是 true,我将调用 ...MeBotLuisDialog()。

抱歉所有文字,但这是一个很长的问题:-)

有人接受吗?

(我的另一个解决方案是创建一个 Intent "SpellCheckError",该 Intent "SpellCheckError" 从 Bing 拼写检查中触发,并在 Intent 中将包含更正消息的消息发送回机器人(即使我不知道我能以正确的方式做到这一点)) // 汤米

要在您的机器人中启用 Bing Spell Check API,您将首先从 Bing 服务获取密钥,然后在 Web.config 文件中添加 BingSpellCheckApiKey 并一起启用 IsSpellCorrectionEnabled 例如:

<appSettings>
  <!-- update these with your BotId, Microsoft App Id and your Microsoft App Password-->
  <add key="BotId" value="YourBotId" />
  <add key="MicrosoftAppId" value="" />
  <add key="MicrosoftAppPassword" value="" />
  <add key="BingSpellCheckApiKey" value="YourBingSpellApiKey" />
  <add key="IsSpellCorrectionEnabled" value="true" />
</appSettings>

然后你可以为你的机器人创建一个组件来使用Bing拼写Api,更多信息,你可以参考Quickstart for Bing Spell Check API with C#. Here you can a service in your app for example like this Service

然后在MessagesController中,在将消息发送到对话框之前先检查拼写。如果文本应替换为 Bing 的结果,您希望向用户显示确认对话框,因此您可以先发送 FormFlow 让用户进行选择。例如:

MessagesController代码:

private static readonly bool IsSpellCorrectionEnabled = bool.Parse(WebConfigurationManager.AppSettings["IsSpellCorrectionEnabled"]);
private BingSpellCheckService spellService = new BingSpellCheckService();

/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
   if (activity.Type == ActivityTypes.Message)
    {
        if (IsSpellCorrectionEnabled)
        {
            try
            {
                var text = await this.spellService.GetCorrectedTextAsync(activity.Text);
                if(text != activity.Text)
                {
                    //if spelling is wrong, go to rootdialog
                    await Conversation.SendAsync(activity, () => new Dialogs.RootDialog(activity.Text, text));
                }
                else
                {
                    //if right, direct go to luisdialog

                    await Conversation.SendAsync(activity, () => new Dialogs.MyLuisDialog());

                }
            }
            catch(Exception ex)
            {
                Trace.TraceError(ex.ToString());
            }
        }
        else
        {                    
            await Conversation.SendAsync(activity, () => new Dialogs.MyLuisDialog());
        }

    }
    else
    {
        HandleSystemMessage(activity);
    }
    var response = Request.CreateResponse(HttpStatusCode.OK);
    return response;
}

我的代码 RootDialog:

[Serializable]
public class RootDialog : IDialog<object>
{
    private string otext;
    private string ntext;
    public RootDialog(string oldtext, string newtext)
    {
        otext = oldtext;
        ntext = newtext;
    }

    public async Task StartAsync(IDialogContext context)
    {
        context.Wait(MessageReceivedAsync);
    }

    public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
    {
        var message = await result;
        var form = new FormDialog<Confirmation>(new Confirmation(otext, ntext),
                Confirmation.BuildForm, FormOptions.PromptInStart, null);
        context.Call(form, this.GetResultAsync);
    }

    private async Task GetResultAsync(IDialogContext context, IAwaitable<Confirmation> result)
    {
        var state = await result;
        await context.Forward(new MyLuisDialog(), null, context.Activity, System.Threading.CancellationToken.None);
    }

}

[Serializable]
public class Confirmation
{

    public string Text { get; set; }
    private static string otext;
    private static string ntext;
    public Confirmation(string oldtext, string newtext)
    {
        otext = oldtext;
        ntext = newtext;
    }
    public static IForm<Confirmation> BuildForm()
    {
        return new FormBuilder<Confirmation>()
            .Field(new FieldReflector<Confirmation>(nameof(Text))
            .SetType(null)
            .SetDefine(async(state, field) =>
            {
                field
                .AddDescription(otext, otext)
                .AddTerms(otext, otext)
                .AddDescription(ntext,ntext)
                .AddTerms(ntext, ntext);

                return true;
            }))
            .Build();
    }
}