續上一篇部落格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"的屬性值