Alice source code analysis

Source: Internet
Author: User
Preface

Alice is the abbreviation of "artificial language Computer Entity. It once won three times in the lebrna artificial intelligence Award Competition in previous years (2000, 2001, and 2004. He also achieved remarkable results in other years. It is an open-source software that can be downloaded to multiple languages at http://www.alicebot.org/downloads/programs.html.

Here, the Java version is used as an example to explain Alice's chat principles and code implementation. The implementation mentioned below refers to the Java version.

Principle

Alice's chat principle is still relatively simple. It has a dialog library. After a user asks a question, Alice finds the answer to the same question in the dialog library. The Java version supports wildcard matching, which can greatly reduce the size of the Q & A database.

The principle is very simple, but if the Q & A database is rich enough, it will still be amazing.

Q & A Database Management

The Q & A library is stored in the aiml language. Aiml is a markup language similar to HTML, in XML format. The following are a pair of questions and answers in the aiml language:

<Category>

<Pattern> who are you </pattern>

<Template> Iam Alice, nice to meet you! </Template>

</Category>

When the user inputs who are you, Alice will answer I am Alice, nice to meet you! That's simple.

When Alice starts, the dialog database is loaded into the memory, and the source code is graphmaster (GM ). GM is actually a trie tree, but a node is a word. Alice divides the topic (that) into a word list and stores it in the memory as a trie tree. During the query, Alice queries the topic based on the trie tree. If a matching problem is found, then, return the corresponding answer to the user. Take who are you as an example. The method in memory should be as follows:

GM :( who)

\

GM :( are)

\

GM :( you) --> category {<pattern>: Who are you; <template>: I am Alice, nice to meetyou! }

Of course, some special tag methods are also supported in the implementation, such as <set> setting context, <SRAI> calling the SRAI class to process some special tag conversions.

Start

Alice's main program is bitoflife. chatterbean. chatterbean, which accepts 1 ~ The first parameter is the path of the configuration file bots/properties. xml. If there is a second parameter and it is 'GU', the GUI is started; otherwise, the console interface is started.

The chatterbean constructor loads context and Splitter, loads the Q & A database, and passes the reference of the Q & A database to alicebot.

Answer questions

The specific implementation is alicebot. Respond ()

Private void respond (sentence, sentence that, sentence topic, response ){

If (sentence. Length ()> 0)

{

Match match = New Match (this, sentence, that, topic); // construct a matching string

CATEGORY Category = graphmaster. Match (MATCH); // search for matching strings and matching methods in the database

// Trie search algorithm supporting wildcard characters

Response. append (category. Process (MATCH); // matches the string to process the answer

}

}

Because Alice has a * matching string, all issues that cannot be matched will be returned by this mode:

<Category>

<Pattern> * </pattern>

<Template> I am sorry, my answers arelimited -- you must provide the right questions. </template>

</Category>

Here, category. Process is an operation for parsing according to the aiml language. Some default operations are set in the aiml language for reference, such as get, set, and SRAI. through reflection, these labels are converted into actual Java classes, then, call the process method of these classes. These aiml elements are inherited from templateelement and are a composite + template design mode.

Public String process (match ){

Stringbuilder value = new stringbuilder ();

For (templateelement I: Children)

Value. append (I. Process (MATCH ));

Return Value. tostring ();

}

Postscript

The principle and code implementation of Alice's chat machine are relatively simple, and there is still a lot of fun after loading Enough Q & A databases, but it is far from the real AI. Recently, many of the children around me are learning to talk. At the beginning, they are learning words repeatedly. After repeating the n-sides, they learned the terms mom, dad, and so on, and then learned other verbs, then combine these phrases. I think this entire process is exactly the path that ai should take. Naming Entity Recognition/ ing, rule learning and application, and even reasoning. Chatbots that truly learn Rule self-learning and reasoning are artificial intelligence robots in the true sense.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.