聊天机器人2(使其支持中文,AIML,C#)

续上一篇博客https://blog.csdn.net/zxy13826134783/article/details/90343419

注意,上一篇博客的聊天机器人是不支持中文的

想要支持中文,在上一篇博客中的类Chatbot中的方法Init()后面添加一句代码

AimlBot.GlobalSettings.addSetting("stripperregex", "[^\u4e00-\u9fa5|a-zA-Z0-9]");

即可支持中文,类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;

            //添加这一句代码就能支持中文
            AimlBot.GlobalSettings.addSetting("stripperregex", "[^\u4e00-\u9fa5|a-zA-Z0-9]");
            
        }

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

修改语料文件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>
     
    
    <!--中文部分-->
    <category>
      <pattern>支持中文吗</pattern>
      <template>
        支持
      </template>
    </category>


    <category>
      <pattern>你吃饭了吗</pattern>
      <template>
        没有,你请我吃吗
      </template>
    </category>

    <category>
      <pattern>老铁你会了吗</pattern>
      <template>
        会了
      </template>
    </category>
    

 </aiml>

运行如下图:

其实,AimlBot.GlobalSettings.addSetting("stripperregex", "[^\u4e00-\u9fa5|a-zA-Z0-9]");

就是全局设置了配置文件Settings.xml中的name="splittersfile"的属性值

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

猜你喜欢

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