Artificial Intelligence: Using aiml to write a robot

Source: Internet
Author: User
Tags dateformat what date

I recently developed ai. It seems that aiml (Artificial Intelligence mark-up language) is really a good thing, special note.
Aiml Overview:

Http://www.pandorabots.com/pandora/pics/wallaceaimltutorial.html

A Java engine of aiml:
Http://www.geocities.com/phelio/chatterbean? 200931 # bots

1: aiml Overview

First, let's see what aiml looks like:
<Aiml> <br/> <Category> <pattern> who wants to know </pattern> <template> Alice wants to know. </template> </Category> <br/> <Category> <pattern> Why are you called </pattern> <template> <SRAI> what does Alice stand for </SRAI> </template> </Category> <br/> <Category> <pattern> Why are you named * </pattern> <template> <SRAI> what does Alice stand for </SRAI> </template> </Category> <br/> <Category> <pattern> Why do you dream * </pattern> <template> I dream about adding new code alice. </template> </Category> <br/> <Category> <pattern> Why silver </pattern> <template> Alice is competing for the loebner silver medal. </template> </Category> <br/> <Category> <pattern> why were you named Alice </pattern> <template> <SRAI> what does Alice stand for </ SRAI> </template> </Category> <br/> <Category> <pattern> why were you named </pattern> <template> <SRAI> what does Alice stand for </SRAI> </template> </Category> <br/> <Category> <pattern> Why </pattern> <that> I am smarter * </that> <template> alice won an award for being the "most human" robot. </template> </Category> <br/> <Category> <pattern> wocould Alice * </pattern> <template> <SRAI> wocould you <star/> </SRAI> </template> </Category> <br/> </aiml>
A brief description of common aiml labels:
1: Pattern Tag: supports pattern matching (Regular Expression, fuzzy match) and template-based return.
2: Random Tag: supports random answers (one-to-many)
4: Think, system Tag: supports simple logical memory and user-defined functions (originally intended to expand aiml and create a tag that supports the groovy language, the result shows its <system> tag,)
5: javascript Tag: supports embedding JavaScript scripts (applicable to Web chat development, such as emotion-based changes, etc ).
6: SRAI Tag: supports multiple-to-one answers.
For more information, see the official aiml documentation:
Http://alicebot.org/TR/2005/WD-aiml/WD-aiml-1.0.1-008.html

2: According to aiml, I organized a simple demo, extended the date tag of aiml, supported the Java time mask, and had a self-learning function.

The robot Alice is shown as follows, showing how smart it is:
Alice> Hi ya! Welcome! <Br/> you say> what's your name <br/> Alice> sorry, what? <Br/> you say> This is not a good answer // start learning function <br/> Alice> sorry. What wocould be a good answer? <Br/> you say> my name is Alice <br/> Alice> alright! You can ask me again to see if I got it. <br/> you say> what's your name <br/> Alice> my name is Alice <br/> you say> what is your name <br/> Alice> my name is Alice <br/> you say> my name is lichunlei <br/> Alice> hello, lichunlei. <br/> you say> do you remember me? <Br/> Alice> your name is lichunlei, Seeker. // Alice's memory function <br/> you say> What's time now? <Br/> Alice> it is 10: 59 A. M. <br/> you say> what date is today? <Br/> Alice> Monday.

If Alice is dissatisfied with the answer, she only needs to input sentences that contain not and good answer. Under your guidance, Alice can start to learn new knowledge.

The reason for making it so intelligent is the aiml file, which is the brain of the robot.
The following is the aiml file of Alice:
<? XML version = "1.0" encoding = "ISO-8859-1"?> <Br/> <aiml> <br/> <! -- Copyright (c) 2007 Alice a. I. Foundation, Inc. --> <br/> <! -- Last modified Seo 21,200 9, by lichunlei --> <br/> <Category> <pattern> what is time * </pattern> <template> It is <date format = "H: mm a"/>. </template> </Category> <br/> <Category> <pattern> what day is today </pattern> <template> <date format = "E"/>. </template> </Category> <br/> <Category> <pattern> what is today * </pattern> <template> <date format = "eee"/>. </template> </Category> <br/> <Category> <pattern> my name is * </Pattern> <template> <think> <set name = "name"> <star/> </set> </think> hello, <get name = "name"/>. </template> </Category> <br/> <Category> <pattern> do you remember me </pattern> <template> your name is <get name = "name "/ >, seeker. </template> </Category> <br/> <Category> <pattern> I can not * </pattern> <template> why can't you do <set name =" it "> <person/> </set>? </Template> </Category> <br/> <Category> <pattern> my input </pattern> <template> 1: <input index = "1"/> 2: <input index = "2"/> 3: <input index = "3"/> </template> </Category> <br/> <Category> <pattern> * </pattern> <template> sorry, what? </Template> </Category> <br/> <! -- Greeting categories. --> <br/> <Category> <br/> <pattern> welcome </pattern> <br/> <template> <br/> <think> <br/> <system> <! -- Defines a method to create new categories from user input at run-time. --> <br/> Import bitoflife. chatterbean. alicebot; <br/> Import bitoflife. chatterbean. context; <br/> Import bitoflife. chatterbean. graphmaster; <br/> Import bitoflife. chatterbean. aiml. category; <br/> Import bitoflife. chatterbean. text. transformations; </P> <p> void learn (string pattern, string template) <br/>{< br/>/* The "match" Variable represents the current matching context. */<br/> alicebot bot = match. getcallback (); <br/> context = bot. getcontext (); <br/> transformations = context. gettransformations (); </P> <p> pattern = transformations. normalization (pattern); <br/> category Category = new category (pattern, new string [] {template}); <br/> graphmaster brain = bot. getgraphmaster (); <br/> brain. A Ppend (category); <br/>}< br/> </system> <br/> </think> <br/> Hi ya! Welcome! <Br/> </template> <br/> </Category> <br/> <! -- A category set to learn simple user-fed categories. --> <br/> <Category> <br/> <pattern> ** not * Good answer </pattern> <br/> <template> <br/> sorry. what wocould be a good answer? <Br/> </template> <br/> </Category> <br/> <pattern >_</pattern> <br/> <that> what wocould be a good answer </that> <br/> <template> <br/> <system> learn ("<input index =" 3 "/> ", "<input index =" 1 "/>") </system> <br/> alright! You can ask me again to see if I got it. <br/> </template> <br/> </Category> <br/> </aiml>

The important thing Alice can learn is the <input/> tag, which remembers the chat records of the other party and can be obtained through index (inverted index)

The program is relatively simple. two classes:
Alice Factory: alicebotmother
Package Co. aiml; <br/> Import Java. io. fileinputstream; <br/> Import Java. io. bytearrayoutputstream; <br/> Import bitoflife. chatterbean. alicebot; <br/> Import bitoflife. chatterbean. context; <br/> Import bitoflife. chatterbean. parser. alicebotparser; <br/> Import bitoflife. chatterbean. util. searcher; <br/> public class alicebotmother <br/>{</P> <p> private bytearrayoutputstream gossip; </P> <p> Public void setup () <br/>{< br/> gossip = new bytearrayoutputstream (); <br/>}</P> <p> Public String gossip () <br/>{< br/> return gossip. tostring (); <br/>}< br/> Public alicebot newinstance () throws exception <br/>{< br/> searcher = new searcher (); <br/> alicebotparser parser = new alicebotparser (); <br/> alicebot bot = parser. parse (New fileinputstream ("bots/context. XML "), <br/> New fileinputstream (" bots/splitters. XML "), <br/> New fileinputstream (" bots/substitutions. XML "), <br/> searcher. search ("bots/mydomain ",". *//. aiml "); <br/> context = bot. getcontext (); <br/> context. outputstream (gossip); <br/> return Bot; <br/>}< br/>

Command line chat program:
Package Co. aiml; <br/> Import Java. io. bufferedreader; <br/> Import Java. io. ioexception; <br/> Import Java. io. inputstreamreader; <br/> Import bitoflife. chatterbean. alicebot; <br/> public class chat <br/>{< br/> Public static final string end = "bye "; </P> <p> Public static string input () <br/> {<br/> bufferedreader in = new bufferedreader (New inputstreamreader (system. in); <br/> system. out. println ("You say>"); <br/> string input = ""; <br/> try <br/>{< br/> input = in. readline (); <br/>}catch (ioexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> return input; <br/>}</P> <p> Public static void main (string [] ARGs) throws exception <br/>{< br/> alicebotmother mother = new alicebotmother (); <br/> mother. setup (); <br/> alicebot bot = mother. newinstance (); <br/> system. err. println ("Alice>" + bot. respond ("welcome"); <br/> while (true) <br/>{< br/> string input = chat. input (); <br/> If (chat. end. equalsignorecase (input) <br/> break; </P> <p> system. err. println ("Alice>" + bot. respond (input); <br/>}</P> <p >}< br/>}

Note:
Alicebot bot = parser. parse (New fileinputstream ("bots/context. XML "), <br/> New fileinputstream (" bots/splitters. XML "), <br/> New fileinputstream (" bots/substitutions. XML "), <br/> searcher. search ("bots/mydomain ",". *//. aiml "));

Context. xml: sets variable attributes such as application attributes and time formats.
<Context> <br/> <! -- The ID is a unique string that identifies this context. --> <br/> <bot name = "ID" value = "test_cases"/> <br/> <! -- Bot predicates are set at load time, and cannot be changed at runtime. --> <br/> <bot name = "output" value = "logs/gossip.txt"/> <br/> <bot name = "randomseed" value = "1"/> <br/> <bot name = "series" value = "Alpha"/> <br/> <bot name = "version" value = "0.7.5 Alpha"/> <br/> <bot name = "location" value = "Atlanta"/> <br/> <bot name = "name" value = "Alice"/> <br/> <! -- Default values for predicates, can be changed later at runtime. --> <br/> <set name = "dateformat" value = "yyyy-mm-dd hh: mm: SS "/> <br/> <set name =" name "value =" dear friend "/> <br/> <set name =" me "value =" Alice "/> <br/> <set name = "engine" value = "chatterbean"/> <br/> <set name = "topic" value = "*"/> <br/> </context>

The preceding attributes can be accessed using the <bot> tag and <get> tag of aiml.

Splitters. xml: defines what is a sentence, that is, the end character of a sentence.
<Splitters> <br/> <splitter value = "... "type =" sentence "/> <br/> <splitter value = ". "type =" sentence "/> <br/> <splitter value = "! "Type =" sentence "/> <br/> <splitter value = "? "Type =" sentence "/> <br/> <splitter value ="; "type =" sentence "/> <br/> <splitter value = ", "type =" word "/> <br/> <splitter value =": "type =" word "/> <br/> </splitters>

Substitutions. xml: defines Fault Tolerance rules and special character ing.
<Substitutions> <br/> <! -- Input substitutions correct spelling mistakes and convert "sentence"-ending characters into characters that will not be identified as sentence Enders. --> <br/> <input> <br/> <correction> <! -- Sentence correction --> <br/> <substitute find = "= reply" replace = ""/> <br/> <substitute find = "name = RESET" replace = "" /> <br/> <substitute find = ":-)" replace = "Smile"/> <br/> <substitute find = ":)" replace = "Smile"/> <br /> <substitute find = ",) "Replace =" Smile "/> <br/> <substitute find =";) "replace =" Smile "/> <br/> <substitute find = ";-) "Replace =" Smile "/> <br/> <substitute find =" replace = ""/> <Br/> <substitute find = "/" replace = ""/> <br/> <substitute find = ">" replace = "GT"/> <br/> <substitute find = "<" replace = "LT"/> <br/> <substitute find = "(" replace = ""/> <br/> <substitute find = ") "Replace =" "/> <br/> <substitute find =" U "replace =" you "/> <br/> <substitute find =" ur "replace =" Your "/> <br/> <substitute find =" you 'd "replace =" You wowould "/> <br/> <substitute find =" you're "re Place = "you are"/> <br/> <substitute find = "you re" replace = "you are"/> <br/> <substitute find = "you 've "Replace =" you have "/> <br/> <substitute find =" You ve "replace =" you have "/> <br/> <substitute find =" What' s "replace =" what is "/> <br/>... <br/> </correction> <br/> <protection> <! -- Sentence protection --> <br/> <substitute find = ", what" replace = ". what "/> <br/> <substitute find =", do "replace = ". do "/> <br/> <substitute find =", do "replace = ". do "/> <br/>... <br/> </protection> <br/> </input> <br/> <gender> <br/> <substitute find = "on her" replace = "on him "/> <br/> <substitute find =" in her "replace =" in him "/> <br/> <substitute find =" his "replace =" her "/> <br/> <substitute find = "her" replace = "his"/> <br/> <substitute find = "him" replace = "her"/> <br/ >... <br/> </gender> <br/> <person> <br/> <substitute find = "I was" replace = "he or she was"/> <br/> <substitute find = "mine" replace = "his or hers"/> <br/> </person> <br/> <person2> <br/>... <br/> <substitute find = "your" replace = "my"/> <br/> </person2> <br/> </substitutions>

For example, in the preceding chat demo, if I enter what's your name and what is your name, I can get the correct answer because:
The following settings are available in the substitutions. xml file;
<Substitute find = "what's" replace = "what is"/>

3: Extended aiml labels (aiml-based Java engine: chatterbean ):

Package bitoflife. chatterbean. aiml is the implementation package of chatterbean for aiml labels. Currently, most commonly used aiml labels are implemented.
The date tag has only one simple implementation and does not support Java time masks.
The ideal date tag of the monks should be:
<Category> <pattern> what is time * </pattern> <template> It is <date format = "H: mm a"/>. </template> </Category>
The label class only needs to expand templateelement.
Therefore, modify it as follows:
Public class date extends templateelement <br/>{< br/> private final simpledateformat format = new simpledateformat (); <br/>/** date tag format value, add By LCL **/<br/> private string formatstr = ""; <br/> Public date () <br/>{< br/>}< br/> Public date (attributes) <br/>{< br/> // obtain the time mask <br/> formatstr = attributes. getvalue (0); <br/>}< br/>/* <br/> methods <br/> */</P> <p> Public int hashcode () <br/>{< br/> return 13; <br/>}< br/> Public String process (match) <br/>{< br/> try <br/>{< br/> format. applypattern (formatstr); <br/> return format. format (New Java. util. date (); <br/>}< br/> catch (exception e) <br/>{< br/> return defaultdate (MATCH ); <br/>}</P> <p> private string defaultdate (match) <br/>{< br/> try <br/>{< br/> format. applypattern (string) match. getcallback (). getcontext (). property ("predicate. dateformat "); <br/> return format. format (New Java. util. date (); <br/>}< br/> catch (nullpointerexception e) <br/>{< br/> return ""; <br/>}</P> <p>}

4: To make Alice smart enough, she must have enough aiml. the following address is all her databases:

Http://www.alicebot.org/downloads/sets.html

Alice had almost no idea how to join the program.

5: If you need to become a robot expert in a certain field, it is a good choice to implement it based on aiml.

6: source code download (Eclipse project ):

Http://download.csdn.net/source/1683344

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.