zoukankan      html  css  js  c++  java
  • Syn Bot /OSCOVA 基础教程(2)

    在vs中使用.NET 4.5创建一个控制台应用

    使用nuget安装Syn.Bot

    然后创建一个对话类

    class AppDialog: Dialog
    {
    
    }
    然后创建一个意图
    [Expression("open {calc}")]
    [Entity("app")]
    public void OpenApp(Context context, Result result)
    {
        var appEntity = result.Entities.OfType("app");
        Process.Start(appEntity.Value);
        result.SendResponse("App opened");
    }
    result.Entities属性包含了所有的全局的和从已知实体识别器解析到的参数传入的实体对象

    现在,你的类就是这个样子了
    using System.Diagnostics;
    using Syn.Bot.Oscova;
    using Syn.Bot.Oscova.Attributes;
    
    namespace OscovaConsoleBot
    {
        internal class AppDialog : Dialog
        {
            [Expression("open {calc}")]
            [Entity("app")]
            public void OpenApp(Context context, Result result)
            {
                var entity = result.Entities.OfType("app");
                Process.Start(entity.Value);
                result.SendResponse("App opened");
            }
        }
    }
    

     添加到机器人并且训练它

    在main方法中添加一下代码

    var bot = new OscovaBot();
    bot.Dialogs.Add(new AppDialog());
    bot.Trainer.StartTraining();

    注意,StartTraining必须放在添加dialog或entity之后

    然后我们测试一下,执行一个请求
    while (true)
    {
        var request = Console.ReadLine();
        var evaluationResult = bot.Evaluate(request);
        evaluationResult.Invoke();
    }

    这个请求会在MainUser的上下文执行, 也就是bot.MainUser。
    如果需要获得执行结果,你需要订阅MainUser的ResponseReceived事件。

    当执行完成,Oscova将返回评分最高的Intents,并包含了识别到的实体和会话上下文,再次校准后的意图是按评分高低排列的。

    需要注意的是,有时候并不是返回全局中评分最高的意图,因为还需要考虑会话上下文的限制。

    看一下代码现在的样子:
    using System;
    using Syn.Bot.Oscova;
    
    namespace OscovaConsoleBot
    {
        internal class Program
        {
            [STAThread]
            private static void Main(string[] args)
            {
                var bot = new OscovaBot();
                bot.Dialogs.Add(new AppDialog());
                bot.Trainer.StartTraining();
    
                bot.MainUser.ResponseReceived += (sender, eventArgs) =>
                {
                    Console.WriteLine(eventArgs.Response.Text);
                };
    
                while (true)
                {
                    var request = Console.ReadLine();
                    var evaluationResult = bot.Evaluate(request);
                    evaluationResult.Invoke();
                }
            }
        }
    }
    F5运行试试吧!你已经创建了你的第一个Oscova机器人。
     
  • 相关阅读:
    处处留心皆学问,世事如棋局局新…
    【转载】2017 软件测试行业现状调查报告_From_51testing_On_20180625
    Hello World In Go ...
    C# 易错题整理
    C# 函数
    C# 哈希表,队列,栈
    C# 数组,集合,泛型集合
    C# 如何生成验证码
    C# 年月日时间题+Timespan
    闰年的一个BUG
  • 原文地址:https://www.cnblogs.com/mrtiny/p/9081584.html
Copyright © 2011-2022 走看看