聊天机器人1(C#,AIML)

本文章使用AIML实现聊天机器人

步骤:

1 新建C#控制台程序,工程名称为:ChatbotDemo

2  AIML下载地址:

http://sourceforge.net/projects/aimlbot/files/aimlbot/2.5/ConsoleBot2.5.zip/download 

备用地址:链接:https://pan.baidu.com/s/1sJhX6fcFfIau8xFkh5guMQ    提取码:usnm 
 

解压下载的文件,把里面的bin/Debug目录下的文件  config和AIMLbot.dll文件拷贝到C#工程的Debug目录下

拷贝到工程Debug目录下后的结果如下图:

3  在工程的Debug目录下新建一个文件夹Myaiml,如下图:

在Myaiml添加一个文本文件,编码格式最好为utf-8(注意,window系统中的记事本默认保存的格式不是utf-8,需要另存为保存,再选择保存格式),名称为my.aiml并编辑如下

<?xml version="1.0" encoding="utf-8"?>
  <aiml>

    <category>
      <pattern>Who are you</pattern>
      <template>
        zxy
      </template>
    </category>

    <category>
      <pattern>Hello</pattern>
      <template>
        Hi
      </template>
    </category>
    

 </aiml>

4  把刚才的Myaiml文件夹添加到配置文件中,打开工程Debug目录下config目录下的Settings.xml,并在Settings.xml做修改如下图:

5 把AIMLbot.dll引用添加到工程中

6  添加一个类,名称为Chatbot,并编辑如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AIMLbot;            //添加的命名空间

namespace ChatbotDemo
{
    internal class Chatbot
    {
        const string UserId = "zxy";
        private Bot AimlBot;
        private User myUser;
       
        public Chatbot() {
            AimlBot = new Bot();
            myUser = new User(UserId,AimlBot);
            Init();
        }

        private void Init() {
            AimlBot.loadSettings();
            AimlBot.isAcceptingUserInput = false;
            AimlBot.loadAIMLFromFiles();
            AimlBot.isAcceptingUserInput = true;
            
        }

        public string getOutput(string input) {
            Request request = new Request(input,myUser,AimlBot);
            Result res = AimlBot.Chat(request);
            return (res.Output);
        }
    }
}

7 编辑主程序如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChatbotDemo
{
    class Program
    {
        static Chatbot chatbot = new Chatbot();
        static void Main(string[] args)
        {
            string input=Console.ReadLine();
            while(input!="q"){
                string output = chatbot.getOutput(input);
                Console.WriteLine("Robot: "+output);
                input = Console.ReadLine();
            }

            Console.ReadKey();
        }
    }
}

运行如下图:

编辑my.aiml,可以自己添加语料,新添加如下:

<?xml version="1.0" encoding="utf-8"?>
  <aiml>

    <category>
      <pattern>Who are you</pattern>
      <template>
        zxy
      </template>
    </category>

    <category>
      <pattern>Hello</pattern>
      <template>
        Hi
      </template>
    </category>

     
    <!--新添加-->
    <category>
      <pattern>How old are you</pattern>
      <template>
        20
      </template>
    </category>

    <category>
      <pattern>How are you</pattern>
      <template>
        Fine,Thank you
      </template>
    </category>
    

 </aiml>

再一次运行如下图:

当然,再添加一个后缀名为aiml的语料文件(名称可以随便起),并添加语料,放到Myaiml文件夹内即可,其它不用修改,这样你新添加的语料文件也起作用了。

发布了66 篇原创文章 · 获赞 48 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/zxy13826134783/article/details/90343419