Intelligent Customer Service Dialogue realizes--python AIML package

Source: Internet
Author: User
Tags session id

Using Python's AIML package to answer

What is AIML?

AIML was developed by Richard Wallace. He developed a robot called A.L.I.C.E (Artificial linguistics Internet computer Entity) and won several awards for AI. Interestingly, one of the Turing tests was to have a person talk to a robot in a text interface for a few minutes to see if people thought it was a human being. AIML is an XML that defines the rules for matching patterns and determining responses.

To see the complete AIML primer, you can look at the Alice Bot's AIML Primer. You can learn more about AIML on the AIML wiki page and know what it can do. Let's start by writing some AIML files and giving it a little life with Python.

Write standard boot file

The standard is to write a startup file called Std-startup.xml as the main entry point for loading the Aiml file. In this example, we'll write a basic file that matches a pattern and makes an action. We want to match the pattern load AIML band then let it load into our aiml brain. We'll spend a minute writing a basic_chat aiml file.

<AIMLversion= "1.0.1"encoding= "UTF-8">    <!--Std-startup.xml -    <!--Category is an atomic AIML unit -    <category>        <!--Pattern to match in user input -        <!--If user enters "LOAD AIML B" -        <pattern>LOAD AIML B</pattern>        <!--Template is the response to the pattern -        <!--This learn an AIML file -        <Template>            <Learn>Basic_chat.aiml</Learn>            <!--You can add more AIML files here -            <!--<learn>more_aiml.aiml</learn> -        </Template>            </category></AIML>

Write Aiml file

Above we wrote a AIML file that only handles one pattern,load AIML b. When we enter that command to the robot, it will load the BASIC_CHAT.AIML. We don't work when we don't write this file. Here's what you can put into the BASIC_CHAT.AIML. We will match two basic patterns and responses.

<AIMLversion= "1.0.1"encoding= "UTF-8"><!--BASIC_CHAT.AIML -    <category>        <pattern>HELLO</pattern>        <Template>Well , hello! </Template>    </category>        <category>        <pattern>What is You</pattern>        <Template>I ' m a bot, silly! </Template>    </category>    </AIML>

Random reply

We can also add some random replies. This is a random response when it receives a message that starts with "one time I". * is a placeholder representing the ability to match any character.

<category>    <pattern>One time I *</pattern>    <Template>        <Random>            <Li>Go on.</Li>            <Li>How is old is you?</Li>            <Li>be more specific.</Li>            <Li>I did not know that.</Li>            <Li>Is you telling the truth?</Li>            <Li>I don ' t know what's that means.</Li>            <Li>Try to tell me the another.</Li>            <Li>Is talking about an animal, vegetable or mineral?</Li>            <Li>What is it?</Li>        </Random>    </Template></category>

Use ready-made AIML files

It's interesting to write your own AIML file, but it will be a lot of work. I think it takes about 10,000 of the pattern to look real. Fortunately, the Alice Foundation offers a lot of free aiml files. You can look at the Aiml file on the Alice Bot website. There's a std-65-percent.xml file that includes the general 65% scenario. There's also a robot that lets you play 21 points (BlackJack).

Write Python

So far, all things are aiml XML files. These are important parts of the robot's brain that can be constructed, but these are just some information. Robots need to live. You need to use some language to implement the AIML specification, but some good people have already done it in Python.

First, the AIML package is loaded with PIP.

pip install aiml

Remember that the AIML package can only be used under Python 2. Python 3 can be replaced with the PY3KAIML on GitHub.

Simple Python Program

This is the simplest program that we can start. It builds a Aiml object, learns the boot file, and loads the remaining AIML files. After that, we can chat, then we enter an infinite loop and continue to let the user enter the message. You need to enter a pattern that the robot can recognize. Pattern recognition relies on the Aiml file you previously loaded.

We write a separate boot file so that we can load more AIML files without moving the program source code. We can learn by adding more files to the startup XML file.

ImportAIML#Create the kernel and learn AIML filesKernel =AIML. Kernel () Kernel.learn ("Std-startup.xml") Kernel.respond ("load AIML b")#Press Ctrl-c to break this loop whileTrue:PrintKernel.respond (Raw_input ("Enter your message >>"))

Accelerates brain activation

When you have a lot of aiml files, it takes a long time to learn. This depends on the robot's brain file. After learning all the AIML files, the robot can store the brain directly in a file so that it can be accelerated at the next boot.

ImportAIMLImportOskernel=AIML. Kernel ()ifOs.path.isfile ("BOT_BRAIN.BRN"): Kernel.bootstrap (Brainfile="BOT_BRAIN.BRN")Else: Kernel.bootstrap (learnfiles="Std-startup.xml", commands ="load AIML b") Kernel.savebrain ("BOT_BRAIN.BRN")#kernel now ready for use whileTrue:PrintKernel.respond (Raw_input ("Enter your message >>"))

Overloaded AIML in operation

You can overload the Aiml file by sending overloaded messages to the robot at run time. Remember that if you use a previously written brain method, overloading does not save you from just altering your brain. You need to delete the brain files again the next time you start to rebuild, or you need to change the code to allow the robot to save the brain at a point after the next reload. Look at the next section and write the Python command to the robot.

load aiml b
Add Python command

If you want to add some commands to your robot to run the Python function, you need to capture the robot's input and process it before sending it to Kernel.respond (). In the last example we got the user's input from Raw_input. We can get input from many places. such as a TCP socket, or a source of voice-to-text. Deal with it before it is sent to AIML. You need to skip AIML on certain messages.

 whileTrue:message= Raw_input ("Enter your message to the bot:")    ifMessage = ="quit": Exit ()elifMessage = ="Save": Kernel.savebrain ("BOT_BRAIN.BRN")    Else: Bot_response=kernel.respond (message)#Do something with Bot_response

Sessions and assertions

Through a conversation, AIML can distinguish between different people's different conversations. For example, if a person tells the robot that her name is Alice, and another person tells the robot that his name is Bob, the robot can differentiate them. To specify which session you are using, you can pass the second argument to respond ().

SessionId = 12345kernel.respond (raw_input (">>>"), SessionId)

So every customer has a personalized chat, very good. You can generate your own session ID (conversation ID) and track them. Remember that saving a brain file does not save all session values.

SessionId = 12345#Get Session info as dictionary. Contains the input#and output history as well as any predicates knownSessiondata =Kernel.getsessiondata (sessionId)#Each session ID needs to be a unique value#The predicate name is the name of Something/someone#That the bot knows about in your session with the bot#The bot might know as "Billy" and that your "dog" is named "Brandy"Kernel.setpredicate ("Dog","Brandy", sessionId) clients_dogs_name= Kernel.getpredicate ("Dog", SessionId) kernel.setbotpredicate ("Hometown","127.0.0.1") Bot_hometown= Kernel.getbotpredicate ("Hometown")

In AIML we can set response in the template to set the assertion.

&LT;AIML version="1.0.1"encoding="UTF-8"> <category> <pattern>my DOGS NAME is *</pattern> <template> that isInteresting that you had a dog named <set name="Dog"><star/></set> </template> </category> <category> <pattern>what I S MY DOGS name</pattern> <template>Your Dog's name is <get name= "dog"/>.</template> </category> </aiml>

With the AIML above you can tell the robot:

My dogs name is Max

The robot will then reply:

That is interesting that you have a dog named Max

If you ask:

What is my dogs name?

The robot will answer:

Your dog‘s name is Max.

原文:http://www.devdungeon.com/con...

Intelligent Customer Service Dialogue realizes--python AIML package

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.