C# 使用AIML 弄 聊天机器人

本文介绍了如何在C#中使用AIML(Artificial Intelligence Markup Language)创建聊天机器人。首先,需要下载并导入AIMLbot.dll及相关资源文件到C#类库项目。接着,在控制台项目中添加类库项目的引用,并设置配置文件以解决运行时异常。为防止无答案时程序卡死,可以在settings.xml中设置超时时间。最后,通过编辑AIML文件实现问题与答案的匹配,利用<srai>标签实现模糊匹配功能。

孙广东  2016.12.21

http://blog.csdn.net/u010019717


确实有人问在Unity中怎么使用:

http://answers.unity3d.com/questions/826590/does-anyone-know-how-to-embed-aiml.html

           答案:  https://code.google.com/p/aimlbot-for-unity/

 

类似的  插件 AssetStore  : 

Chatbot

 来自 <https://www.assetstore.unity3d.com/en/#!/content/40404>


   最容易理解的就是聊天机器人,  可以看看百度百科的介绍.:  AIML

 

一、

 现在看看C#  中该怎么使用?

            新建一个 C# 的类库项目,  命名为  Chatbot 

 

先做  准备工作,  下载类库 并导入:

下载到是压缩包, 内容在

 

把上面的下载解压的文件拷贝到  我们新建的 库项目  Chatbot 中:

 

添加  文件

using AIMLbot;
using System;
 
namespace Chatbot
{
    public class Chatbot
    {
        const string UserId ="CityU.Scm.David";
        private Bot AimlBot;
        private User myUser;
 
        public Chatbot()
        {
            AimlBot = new Bot();
            myUser = new User(UserId, AimlBot);
            Initialize();
        }
 
        // Loads all the AIML files in the\AIML folder        
        public void Initialize()
        {
            AimlBot.loadSettings();
            AimlBot.isAcceptingUserInput =false;
            AimlBot.loadAIMLFromFiles();
            AimlBot.isAcceptingUserInput =true;
        }
 
        // Given an input string, finds aresponse using AIMLbot lib
        public String getOutput(String input)
        {
            Request r = new Request(input,myUser, AimlBot);
            Result res = AimlBot.Chat(r);
            return (res.Output);
        }
    }
}


 

         然后在  当前解决方案  中  添加  新建项目  C# 控制台项目,  命名为ChatbotConsole  , 并添加引用之前 的库项目 Chatbot 

 

    class Program
    {
        static Chatbot.Chatbot bot;
        static void Main(string[] args)
        {
            bot = new Chatbot.Chatbot();
            string input = "Hello, what isyour name";
            var output = bot.getOutput(input);
            Console.WriteLine(input);
            Console.WriteLine(output);
            Console.ReadKey();
        }
    }


 

 http://blog.csdn.net/u010019717

 

运行的时候会报异常:

 

          就是没有加载到配置文件

将 aiml and config  两个文件夹  拷贝到  控制台项目的  bin 下

 

 

 

              现在  机器人不知道  自己是什么名字?  怎么办 ?

 

 

 

二  、

 

更改  控制台项目  下的配置:

 

改为 :

 

保存  然后执行, OK !  已经改变了

 

 

三、

          如果 问一个问题, 没有找打答案, 会出现卡死现象。

解决办法是  设置超时时间

 settings.xml  文件中 修改 :


          把值改为  "10"  .

在测试, 提示的是:

                ERROR: The request has timed out.

 

            更优雅的处理提示方式:

var output= bot.getOutput(input);
if(!string.IsNullOrEmpty(output))
{
    if(output.StartsWith("ERROR:"))
       HistoryListBox.Items.Add("Robot:\t" + "I don't understand what you mean.");
    else
       HistoryListBox.Items.Add("Robot:\t" + output);
}



四、  配置你的机器人  

     我们在 问这样的问题, 可以得出这样的答案, 怎么弄?

Q:    Whereis City University of Hong Kong?

A:    Itis located in Tat Chee Avenue, Kowloon, Hong Kong SAR.

 

               在  AIML 中 , 问题或输入  在Pattern 标签中 , 回答或输出 在 Template 标签中,  Q-A 对 在  category 标签下

 

因为 主要是要  弄聊天机器人,  更改

         Chatbot\ChatbotConsole\bin\Debug\aiml  路径下的 :    Bot.aiml     

 

 

如果 把问题 改变一点呢?


         怎么让他模糊  匹配之前的问题呢?

 

使用   一个特殊的标记<srai>:    解决这样的需求

   

http://blog.csdn.net/u010019717








评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值