iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 20
0

前言

這一篇使用 Bing Spell Check API 流程比較簡易,只需要組合相關參數、放入Key 與 Content-Type ,呼叫 SpellCheck ,在解析回傳的內容即可。如下圖所示:

https://d2mxuefqeaa7sj.cloudfront.net/s_D24D53EC3854B57EA9349D216A5AA3BC69ECBB78AA9B44C7F2ABFC3A62C3AFD6_1514037633249_image.png


介紹

Step 1. 開啟 Bot Template 新專案,並開啟 RootDialog.cs,先加上於Azure 啟用 Bing Spell Check 成功後,所記錄下來的 key

private string key = "your_key";

Step 2. 將我們在前一篇文章,透過postman 測試成功回傳的 json 檔案,貼上json2csharp,產生對應的物件,貼在 RootDialog.cs 內:
JSON 檔案

   {
        "_type": "SpellCheck",
        "flaggedTokens": [
            {
                "offset": 5,
                "token": "its",
                "type": "UnknownToken",
                "suggestions": [
                    {
                        "suggestion": "it's",
                        "score": 0.817378790203206
                    }
                ]
            }
        ]
    }

https://d2mxuefqeaa7sj.cloudfront.net/s_D24D53EC3854B57EA9349D216A5AA3BC69ECBB78AA9B44C7F2ABFC3A62C3AFD6_1514038818337_image.png

public class Suggestion
{
    public string suggestion { get; set; }
    public double score { get; set; }
}

public class FlaggedToken
{
    public int offset { get; set; }
    public string token { get; set; }
    public string type { get; set; }
    public List<Suggestion> suggestions { get; set; }
}

public class RootObject
{
    public string _type { get; set; }
    public List<FlaggedToken> flaggedTokens { get; set; }
}

Step 3. 我們將 MessageReceivedAsync 方法更改如下:分別在header 加入Ocp-Apim-Subscription-Key 與 Content-Type,將 activity.Text 加入傳遞參數內。

註:Response 內容可能因為錯誤內容不同,請製作些防呆措施。

private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
    var activity = await result as Activity;

    var client = new RestClient("https://api.cognitive.microsoft.com/bing/v7.0/");
    var request = new RestRequest("spellcheck?mode=proof&mkt=en-us", Method.POST);
    request.AddHeader("Ocp-Apim-Subscription-Key", key);
    request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
    request.AddParameter("text", activity.Text);

    var response = await client.ExecuteTaskAsync<RootObject>(request);
    var suggestion = string.Empty;
    foreach(var item in response.Data.flaggedTokens[0].suggestions)
    {
        suggestion += item.suggestion + " ";
    }
    await context.PostAsync($"{suggestion}");

    context.Wait(MessageReceivedAsync);
}

Step 4. 你的 RootDialog.cs 應該長的如下:

using System;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using RestSharp;
using System.Collections.Generic;

namespace BingSpellCheckBotApplication.Dialogs
{
    [Serializable]
    public class RootDialog : IDialog<object>
    {
        public Task StartAsync(IDialogContext context)
        {
            context.Wait(MessageReceivedAsync);

            return Task.CompletedTask;
        }

        private string key = "your_key";

        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
        {
            var activity = await result as Activity;

            var client = new RestClient("https://api.cognitive.microsoft.com/bing/v7.0/");
            var request = new RestRequest("spellcheck?mode=proof&mkt=en-us", Method.POST);
            request.AddHeader("Ocp-Apim-Subscription-Key", key);
            request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
            request.AddParameter("text", activity.Text);

            var response = await client.ExecuteTaskAsync<RootObject>(request);
            var suggestion = string.Empty;
            foreach(var item in response.Data.flaggedTokens[0].suggestions)
            {
                suggestion += item.suggestion + " ";
            }
            await context.PostAsync($"{suggestion}");

            context.Wait(MessageReceivedAsync);
        }
    }

    public class Suggestion
    {
        public string suggestion { get; set; }
        public double score { get; set; }
    }

    public class FlaggedToken
    {
        public int offset { get; set; }
        public string token { get; set; }
        public string type { get; set; }
        public List<Suggestion> suggestions { get; set; }
    }

    public class RootObject
    {
        public string _type { get; set; }
        public List<FlaggedToken> flaggedTokens { get; set; }
    }
}

Step 5. 開啟模擬器進行測試,成功
https://d2mxuefqeaa7sj.cloudfront.net/s_D24D53EC3854B57EA9349D216A5AA3BC69ECBB78AA9B44C7F2ABFC3A62C3AFD6_1514040791564_image.png


範例

https://github.com/matsurigoto/BingSpellCheckBotExample.git


終於到20篇惹,文章與程式地獄阿 /images/emoticon/emoticon01.gif


上一篇
19.Cognitive Service - Bing 拼字檢查服務
下一篇
21.Cognitive Service - Bing 影像搜尋服務
系列文
利用 MS Bot framework 與 Cognitive Service 建構自用智慧小秘書31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言