[译]使用正则表达式引擎构建AI聊天机器人
By robot-v1.0
本文链接 https://www.kyfws.com/ai/building-an-ai-chatbot-using-a-regular-expression-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 10 分钟阅读 - 4895 个词 阅读量 0使用正则表达式引擎构建AI聊天机器人(译文)
原文地址:https://www.codeproject.com/Articles/18109/Building-an-AI-Chatbot-using-a-Regular-Expression
原文作者:MattsterP
译文由本站 robot-v1.0 翻译
前言
This article describes how to build an AI Chatterbot using a popular, Regular Expression-based open source Chatterbot engine: Verbots
本文介绍如何使用流行的基于正则表达式的开源Chatterbot引擎构建AI Chatterbot:Verbots
介绍(Introduction)
本文介绍了如何使用称为开源的基于正则表达式的自然语言处理引擎来构建人工智能(AI)Chatterbot.(This article describes how to build an Artificial Intelligence (AI) Chatterbot using an open source, Regular Expression-based natural language processing engine called) 威宝(Verbot) . Chatterbot技术并不是什么新技术,但是随着人们尝试与计算机交互的新方式而发展起来,Chatterbot技术已经越来越流行.聊天机器人,动画人物,语音识别和语音合成正被用于促进新的人机交互(HCI)渠道.(. Chatterbot technology is not new, but has been growing in popularity and usage as people experiment with new ways of interacting with computers. Chatterbots, Animated Characters, Speech Recognition and Voice Synthesis are being used to facilitate new human–computer interaction (HCI) channels.)
背景(Background)
原始的聊天机器人(The original chatterbot,) 伊丽莎(ELIZA) 由约瑟夫`魏岑鲍姆(Joseph Weizenbaum)于1966年开发.ELIZA是一位虚拟的心理治疗师,他试图通过关注话题并表达对自己问题的关注来吸引用户. 1994年,第一个Verbot Sylvie的创建者Michael Mauldin创造了" ChatterBot"一词.从那时起,Verbots不断发展壮大,发展到目前(was developed by Joseph Weizenbaum in 1966. ELIZA was a virtual psychotherapist that would attempt to engage the user by staying on topic and sounding concerned with his/her problems. In 1994, Michael Mauldin – the creator of the first Verbot, Sylvie – coined the term “ChatterBot.” Since then, Verbots have evolved and matured into the current) Verbot 4(Verbot 4) 创建的版本(version created by) 善于交谈(Conversive) . 2006年,Conversive发布了(. In 2006, Conversive released the) Verbot SDK(Verbot SDK) 作为开放源代码,使人们可以轻松创建自己的"机器人"并将其嵌入到自己的应用程序中.本文介绍了Verbot Engine SDK背后的一些基本知识,以及如何将SDK集成到另一个应用程序中.(as open source to allow people to easily create their own ‘bots and embed them in their own applications. This article describes some of the basics behind the Verbot Engine SDK and how the SDK can be integrated into another application.)
Verbot的工作方式(How the Verbot works)
在深入研究代码之前,我认为从高层次解释Verbot SDK的工作方式将很有用.所有聊天机器人都具有" KnowledgeBase"或脚本的概念,该脚本会告诉’bot’可以识别哪些关键字或短语.找到匹配项后,相应的输出将发送给用户. Verbot知识库由一系列规则组成,这些规则是Verbot对话的基本构建块.规则必须具有一个或多个可以识别用户所说内容的输入.它们还必须具有一个或多个输出,这是当规则中的输入匹配时,自动程序会做出的响应. Verbot Engine加载知识库文件时,会将在脚本中找到的简单文本输入转换为正则表达式,然后可以将其用作匹配的基础.(Before delving into code, I thought it would be useful to explain at a high level how the Verbot SDK works. All chatterbots have an idea of a “KnowledgeBase” or script, which tells the ‘bot what keywords or phrases to recognize. The corresponding output is then sent to the user when a match is found. Verbot KnowledgeBases are made up of a collection of rules that are the fundamental building blocks of Verbot conversations. Rules must have one or more Inputs, which recognize what the user says. They must also have one or more Outputs, which are what the ‘bot responds when an input in the rule is matched. When the Verbot Engine loads a KnowledgeBase file, it turns the simple text inputs found in the script into regular expressions that can then be used as the basis for matching.)
例如,假设我们有一个输入:(For example, let’s say we have an input which is:)
What is your name?
引擎生成的结果正则表达式如下所示:(The resulting regular expression generated by the engine looks like this:)
^(|.*?\b|.*?\s)What\b.+?\bis\b.+?\byour\b.+?\bname(|\b.*?|\s.*?)$
如果您不熟悉正则表达式语法,这可能会有些令人生畏.即使您很熟悉,它似乎仍然有点神秘.基本上,引擎在单词之间以及短语的开头和结尾都放置了通配符.该正则表达式将匹配为:“那么,您的名字叫什么?“但不要说"你叫什么名字?“为此,我们可以使用Verbots中的同义词功能,您可以在其网站上了解更多信息以及其他更高级的脚本主题.另外,在上有一个很好的正则表达式语法参考(This may be a bit daunting if you are unfamiliar with Regular Expression syntax. Even if you are familiar, it may still seem a bit cryptic. Basically, the engine has put wildcards between words, as well as at the start and end of the phrase. This regular expression will match to, “So, what the heck is your name?” but not to, “What’s your name?” To match to this, we could use the Synonym feature in Verbots, which you can learn more about at their website along with other, more advanced scripting topics. Also, there is a good Regular Expression syntax reference over at) 网络编码器(dotnetcoders) 如果您想了解更多有关正则表达式的信息.(if you’d like to learn more about regex.)
Verbot知识库((Verbot KnowledgeBases ()**.vkb(.vkb)扩展名)是定义知识库的规则,输入和输出的XML文件.他们还可以引用其他Verbot 4文件类型,例如同义词,替换配置文件和代码模块. Verbots允许您将VKB文件"编译"为更紧凑的二进制” Compiled KnowledgeBase”((extension) are XML files that define the rules, inputs and outputs of the KnowledgeBase. They may also reference other Verbot 4 file types such as Synonyms, Replacement Profiles and Code Modules. Verbots allows you to “compile” your VKB files into a more compact binary “Compiled KnowledgeBase” ().ckb(.ckb)**扩展名)文件格式.(extension) file format.)
测试应用程序(Testing the application)
执行(Execute the)**VerbotWindowsApplicationSample.exe(VerbotWindowsApplicationSample.exe)**内找到的文件(file found within the)**VerbotWindowsApplicationSample \ bin \ Release(VerbotWindowsApplicationSample\bin\Release)**夹.应用程序运行后,选择"文件”->“加载…“菜单项,然后浏览至(folder. Once the application is running, choose the File -> Load… menu item and browse to the)**VerbotWindowsApplicationSample \ Resources \ sample.ckb(VerbotWindowsApplicationSample\Resources\sample.ckb)**文件以打开示例知识库.将文件加载到引擎后,您可以与’bot进行交互.尝试键入"记事本"以测试示例知识库中包含的规则之一.(file to open the sample KnowledgeBase. Once the file is loaded into the engine, you may interact with the ‘bot. Try typing “notepad” to test out one of the rules included in the sample KnowledgeBase.)
请注意该程序如何启动Windows记事本.这已被编码为示例应用程序中的行为,以解析脚本中的命令.稍后我将详细讨论.(Notice how the program has started Windows Notepad. This is behavior coded into the Sample application to parse commands in the script. I’ll talk more about this later.)
使用代码(Using the code)
要在自己的应用程序中使用Verbot Engine SDK,只需添加(To use the Verbot Engine SDK inside your own application, simply include the) Verbot4Library
在您的项目中引用,创建一个实例(reference in your project, create an instance of the) Verbot4Engine
然后打电话给(and call either) AddCompiledKnowledgeBase
要么(or) AddKnowledgeBase
加载您的"机器人"知识.要在用户输入进入时与"机器人"互动,请创建一个(to load your ‘bot’s knowledge. To interact with the ‘bot when user input comes in, create a) State
对象以唯一地标识您的用户.将路径添加到您要与用户进行交互的知识库.然后致电(object to uniquely identify your user. Add the paths to the KnowledgeBases you want your user to interact with. Then call the) GetReply
上的方法(method on the) Verbot4Engine
对象以在已加载的脚本中找到匹配项.(object to find a match in the loaded script(s).)
让我们看一下(Let’s look at the) getReply()
在我们的功能(function in our)**VerbotWinApp.cs(VerbotWinApp.cs)**文件:(file:)
private void getReply()
{
string stInput = this.inputTextBox.Text.Trim();
this.inputTextBox.Text = "";
Reply reply = this.verbot.GetReply(stInput, this.state);
if(reply != null)
{
this.outputTextBox.Text = reply.Text;
this.parseEmbeddedOutputCommands(reply.AgentText);
this.runProgram(reply.Cmd);
}
else
this.outputTextBox.Text = "No reply found.";
}
此方法将输入文本框中的文本发送到引擎的(This method takes the text in the input text box and sends it to the engine’s) GetReply
方法.它得到一个(method. It gets a) Reply
对象返回,然后我们可以对其进行处理.的(object back, which we can then process. The) Reply
对象具有成员"文本”,我们将其放置在输出框中.它还具有” AgentText"成员,该成员是要发送到文本语音转换(TTS)引擎的Text版本.它也有一个" Cmd"(命令)成员,我们可以根据需要将其用于特殊处理.(object has a member “Text” that we will just put in the output box. It also has an “AgentText” member, which is a version of the Text meant to be sent to a Text-To-Speech (TTS) engine. It has a “Cmd” (Command) member, too, that we can use for special processing as necessary.)
最后,看看(Finally, take a look at the) parseEmbeddedOutputCommands
和(and) runEmbeddedOutputCommand
方法.这些方法根据我们的要求进行定制处理.如上所述,这是"运行"命令打开记事本的地方.(methods. These methods do custom processing based on our requirements. This is where the “run” command opens Notepad, as I mentioned above.)
根据您自己的应用程序的工作方式,您可以对(Depending on how your own application works, you can make customizations to how the) Reply
对象被处理.例如,在在线"机器人引擎"中,您可能希望通过打开弹出窗口等来处理URL.(object is handled. For example, in an online ‘bot engine you may want to process URLs by opening a popup window, etc.)
创造知识(Creating knowledge)
有一些免费提供的工具可帮助您创建Verbot知识库.(There are some freely available tools to help you create a Verbot KnowledgeBase.) Verbot GPL编辑器(Verbot GPL Editor) 和(and) 编译器(VCompiler) 有两种选择可帮助您入门.您也可以使用(are a couple of options to get you started. You can also use the) Verbot编辑器(Verbot Editor) ,使用注册码进行解锁需要花费10美元.如果您想创建自己的知识库创建工具,则代码非常简单.您可能还想看一下我上面提到的Verbot GPL编辑器的源代码,以此作为很好的参考.这是在代码中创建知识库所需的步骤的摘要:(, which costs $10 to unlock with a registration code. If you’d like to create your own KnowledgeBase creation tool, the code is pretty straightforward. You may also want to look at the source code for the Verbot GPL Editor I mentioned above as a good reference. Here is a summary of the steps required to create a KnowledgeBase in code:)
- 首先,创建一个实例(First, create an instance of the)
KnowledgeBase
类:(class:) - 然后,我们要创建一个规则以添加到我们的知识库中:(Then we’ll want to create a rule to add to our KB:)
- 最后,对于您创建的每个规则,您至少要有一个输入和一个输出.我们可以手动创建并添加输入和输出,也可以简单地称为(Finally, for each rule you create you’ll want to have at least one input and one output. We could manually create and add inputs and outputs or we could simply call the)
AddInput
和(and)AddOutput
规则类中的方法:(methods in the rule class:) 现在我们有一个(Now we’ve got a)KnowledgeBase
具有一个规则且具有一个输入和一个输出的对象.当然,在实际的应用程序中,我们不想对"输入文本"和"输出文本"字段进行硬编码,并且可能希望允许用户自己添加它们,但是您明白了.接下来,我们可能想要将KB保存到文件中.为此,我们需要一个(object with one rule that has one input and one output. Of course, in a real application we wouldn’t want to hard-code the “Input Text” and “Output Text” fields and would probably want to allow the user to add these themselves, but you get the idea. Next, we probably want to save our KB to a file. To do this, we need an instance of the)XMLToolbox
类.然后我们简单地称(class. Then we simply call the)SaveXML
保存文件的方法:(method to save the file:)
XMLToolbox xmlToolbox = new XMLToolbox(typeof(KnowledgeBase));
xmlToolbox.SaveXML(kb, @"C:\mykbpath.vkb");
然后,我们希望能够将我们的知识库加载到Verbot Engine中并能够与其进行交互.这是执行此操作的代码:(Then we want to be able to load our KnowledgeBase into the Verbot Engine and be able to interact with it. Here is the code to do that:)
Verbot4Engine engine = new Verbot4Engine();
KnowledgeBaseItem kbi = new KnowledgeBaseItem();
kbi.Fullpath = @"C:\";
kbi.Filename = "mykbpath.vkb";
engine.AddKnowledgeBase(kb, kbi);
State state = new State();
state.CurrentKBs.Add(@"C:\mykbpath.vkb");
最后,要与加载的知识库进行交互,只需如上所述在引擎上调用GetReply方法即可.(Finally, to interact with the loaded knowledge bases just call the GetReply method on the Engine, as discussed above.)
进一步阅读(Further reading)
如果您有兴趣了解有关Verbots的更多信息,请访问(If you are interested in learning more about Verbots, visit the) Verbot社区论坛(Verbot community forums) 或浏览(or browse) Verbots维基(Verbots wiki) 了解有关编写自己的Verbot脚本的更多信息.(to learn more about scripting your own Verbot.)
历史(History)
- 2007年3月22日-文章初稿.(22 March, 2007 – First draft of article.)
- 2007年3月28日-更新了有关SDK 4.1.3.2新版本的文章,其中包括一个简单的控制台应用程序作为示例,以及一个新的KnowledgeBase方法AddRule,该方法返回新规则.此外,文章说明中进行了小的更改.(28 March, 2007 – Updated article for new version of SDK 4.1.3.2, which includes a simple console application as a sample and a new KnowledgeBase method, AddRule, which returns the new rule. Also, small changes in article instructions were made.)
- 2007年6月21日-编辑文章,并将其移至CodeProject.com的主要文章库.(21 June, 2007 – Article edited and moved to the main CodeProject.com article base.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# HTML C#2.0 .NET2.0 Windows .NET1.1 VS2005 VS.NET2003 Dev AI 新闻 翻译