iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 30
0

前言

雖然這一篇簡單透過範例介紹如何製作語意分析機器人,但直接回傳語意結果其實意義不大。在前面幾個範例中,我們透過機器人程式分析傳輸訊息中是否包含命令,藉此分辨該使用哪些服務與回傳哪些訊息,雖然程式這樣撰寫容易,但時間一久容易造成程式中無法精準判斷或程式過長難以維護的問題。

透過語意分析訓練,我們能讓機器人越來越精準地得知使用者的意圖,藉此判斷並執行使用者想要的服務,如下圖所示。


介紹

前一篇我們在 Azure Portal 建立 LUIS 服務,與建立其他 cognitive service 建立完成後相同,很貼心的列出相關資訊,我們一樣先點選 keys,記錄下 key 等等撰寫程式的時候使用。

我們透過先前在 LUIS.AI 最下方的 query 進行查詢

我們透過 postman 進行測試,並且複製結果到 json2csharp.com 產生物件,

開啟新的 bot template 專案 → 開啟 RootDialog.cs 貼上物件與 key

private string key = "your_key";


public class TopScoringIntent
{
    public string intent { get; set; }
    public double score { get; set; }
}

public class Intent
{
    public string intent { get; set; }
    public double score { get; set; }
}

public class RootObject
{
    public string query { get; set; }
    public TopScoringIntent topScoringIntent { get; set; }
    public List<Intent> intents { get; set; }
    public List<object> entities { get; set; }
}

修改MessageReceivedAsync 方法如下:

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

    var client = new RestClient("https://westus.api.cognitive.microsoft.com/luis/v2.0/");
    var request = new RestRequest("apps/your_app_id", Method.GET);
    request.AddParameter("subscription-key", key);
    request.AddParameter("verbose", "true");
    request.AddParameter("timezoneOffset", "0");
    request.AddParameter("q", activity.Text);

    var response = await client.ExecuteTaskAsync<RootObject>(request);

    var suggestion = string.Empty;
    foreach (var item in response.Data.intents)
    {
        suggestion += item.intent + ":" + item.score.ToString() + "<BR/>";
    }
    await context.PostAsync($"{suggestion}");

    context.Wait(MessageReceivedAsync);
}

你的RootDialog.cs 應該如下:

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

namespace LUISBotExample.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://westus.api.cognitive.microsoft.com/luis/v2.0/");
            var request = new RestRequest("apps/your_app_id", Method.GET);
            request.AddParameter("subscription-key", key);
            request.AddParameter("verbose", "true");
            request.AddParameter("timezoneOffset", "0");
            request.AddParameter("q", activity.Text);

            var response = await client.ExecuteTaskAsync<RootObject>(request);

            var suggestion = string.Empty;
            foreach (var item in response.Data.intents)
            {
                suggestion += item.intent + ":" + item.score.ToString() + "<BR/>";
            }
            await context.PostAsync($"{suggestion}");

            context.Wait(MessageReceivedAsync);
        }
    }

    public class TopScoringIntent
    {
        public string intent { get; set; }
        public double score { get; set; }
    }

    public class Intent
    {
        public string intent { get; set; }
        public double score { get; set; }
    }

    public class RootObject
    {
        public string query { get; set; }
        public TopScoringIntent topScoringIntent { get; set; }
        public List<Intent> intents { get; set; }
        public List<object> entities { get; set; }
    }
}

啟動專案,並開啟模擬器進行測試,成功!


範例下載

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


上一篇
29. Cognitive Service - Language Understanding Intelligent Service, LUIS(2)增加意圖、訓練、發布與測試
下一篇
31. 總結
系列文
利用 MS Bot framework 與 Cognitive Service 建構自用智慧小秘書31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言