IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]C# 使用AIML 弄 聊天机器人

    u010019717发表于 2016-12-21 21:46:21
    love 0

    孙广东  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 

     

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

    • 下载  AIMLbot.dll   http://sourceforge.net/projects/aimlbot/files/aimlbot/2.5/AIMLbot2.5.zip/download    到项目中
    • 下载  aiml and config   http://sourceforge.net/projects/aimlbot/files/aimlbot/2.5/ConsoleBot2.5.zip/download   两个文件夹,并导入到项目中

    下载到是压缩包, 内容在

     

    把上面的下载解压的文件拷贝到  我们新建的 库项目  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










沪ICP备19023445号-2号
友情链接