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机器人。
 

猜你喜欢

转载自www.cnblogs.com/mrtiny/p/9081584.html