【问题标题】:Use Bing Spell Check before Microsoft Translate which is called before call to LUIS在调用 LUIS 之前调用 Microsoft 翻译之前使用 Bing 拼写检查
【发布时间】:2017-12-05 14:06:56
【问题描述】:

所以... 我正在尝试在瑞典语中使用带有 LUIS 的 Bot 框架。 使用示例,我实现了将输入从瑞典语翻译成英语,然后调用了 LUIS 功能。 在我们从 LUIS 得到一些非常奇怪的意图命中之前,它一直运行良好。 我们发现,一个非常小的拼写错误(瑞典语)导致翻译创建了一条触发错误意图的消息。 我们可以通过检查接收到的意图的分数来解决问题,但是返回给用户的“我不明白”的消息并不是特别有用。 通过 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());
}

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

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

有没有人接受这个?

(我的另一个解决方案是创建一个从 Bing 拼写检查触发的 Intent “SpellCheckError”,并在 Intent 中将带有更正消息的消息发送回机器人(即使我不知道我这是可行的以适当的方式)) //汤米

【问题讨论】:

  • 但是你想在控制器中这样做?
  • 是的,它在 MessagesController.cs 中
  • 如果你直接从对话框中做会更好。 MessageController 启动一个 RootDialog 并且 RootDialog 执行您的逻辑/调用/转发到另一个对话框

标签: botframework azure-language-understanding


【解决方案1】:

要在您的机器人中启用Bing Spell Check API,您将首先从必应服务获取密钥,然后在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 Spell Api,更多信息可以参考Quickstart for Bing Spell Check API with C#。在这里,您可以在应用中提供服务,例如 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();
    }
}

【讨论】:

  • 正是我需要的!非常感谢你!
  • @TommyEinarsson,如果这个答案有帮助,你能标记这个答案吗?
  • 冯。我很抱歉很晚才更新答案。
猜你喜欢
  • 2014-02-02
  • 1970-01-01
  • 1970-01-01
  • 2012-05-02
  • 2010-12-27
  • 1970-01-01
  • 2012-06-09
  • 2019-01-24
  • 1970-01-01
相关资源
最近更新 更多