孫廣東 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