Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning Robotics using Python

You're reading from  Learning Robotics using Python

Product type Book
Published in May 2015
Publisher Packt
ISBN-13 9781783287536
Pages 330 pages
Edition 1st Edition
Languages
Concepts

Table of Contents (19) Chapters

Learning Robotics Using Python
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Introduction to Robotics Mechanical Design of a Service Robot Working with Robot Simulation Using ROS and Gazebo Designing ChefBot Hardware Working with Robotic Actuators and Wheel Encoders Working with Robotic Sensors Programming Vision Sensors Using Python and ROS Working with Speech Recognition and Synthesis Using Python and ROS Applying Artificial Intelligence to ChefBot Using Python Integration of ChefBot Hardware and Interfacing it into ROS, Using Python Designing a GUI for a Robot Using Qt and Python The Calibration and Testing of ChefBot Index

Chapter 9. Applying Artificial Intelligence to ChefBot Using Python

In the previous chapter, we have discussed and implemented speech recognition and speech synthesis using Python and ROS. In this chapter, we will discuss how to apply AI to ChefBot to communicate with people intelligently, like a human. Actually, these features are add-ons in ChefBot, which can increase human-robot interaction and make the robot resemble a human food supplier. In this chapter, we will mainly cover the following topics:

  • Block diagram of ChefBot's communication system

  • Introduction to AIML and PyAIML

  • Interfacing ChefBot's AI module to ROS

AI (Artificial Intelligence) can be defined as the intelligent behavior exhibited by computers or machines. Using AI, we can create a virtual intelligence in machines to perform a specific task like a human. In this chapter, we will use a simple method to apply AI to the robot. This intelligence will work using pattern matching and searching algorithms. The input-output dialog...

Block diagram of the communication system in ChefBot


The following block diagram shows how ChefBot communicates and interacts with humans using speech:

Robot communication block diagram

The robot can convert human speech to text using the speech recognition system and can convert textual data to speech using speech synthesis. We have already discussed these processes in the previous chapter. The AI we will discuss here is contained in between these two blocks. After receiving the text data from a speech to text conversion stage, it is sent to the AIML interpreter. The AIML interpreter retrieves the most meaningful reply from the AIML dataset. The dataset of the robot can be anything, such as food details, casual talks, and so on. The user can write any kind of pattern in AIML files. In the case of ChefBot, the user can ask about food details or can command the robot to do something. The robot command system checks whether the converted text is a command to the robot. If it's a command, it...

Introduction to AIML


AIML files are a subset of Extensible Mark-up Language (XML) that can store different text patterns in the form of tags. AIML was developed by the Alicebot free software community (http://www.alicebot.org/). AIML is mainly used to implement Chatbots, a natural language software agent in which a user can ask questions to the robot and it can give an intelligent reply. This same technique is used in ChefBot. Using speech recognition, the robot gets input text from the user and an AIML interpreter; a software program that can interpret AIML files and retrieve an intelligent reply from the AIML dataset. The reply will be converted to speech. AIML files mainly consist of various tags. Here are a set of commonly used AIML tags.

Introduction to AIML tags

AIML files consist of a set of commonly used AIML tags. Let's take a look at them.

The <aiml> tag: Each AIML code begins with this tag and is closed using the </aiml> tag. This tag also consists of attributes such...

Introduction to PyAIML


PyAIML is an open source Python AIML interpreter written completely in pure Python without using any third-party dependencies. The module will read all the patterns of AIML from memory and build a directed pattern tree. The backtracking depth-first search algorithm is implemented in this module for pattern matching.

Now, we can check whether we can install PyAIML on our system. The PyAIML module can be installed on Linux, Windows, and Mac OS X. There are prebuilt binaries of PyAIML available on Ubuntu and the source code of this module is also available on GitHub. Currently, we are working with Python version 2.7, or anything less than 2.8, to install PyAIML.

Installing PyAIML on Ubuntu 14.04.2

PyAIML can be installed on Ubuntu using the apt-get command. The binaries are available on the Ubuntu package repositories. The Python version we are working with is 2.7.6 and the PyAIML version we are installing is 0.86. The following command will install PyAIML on Ubuntu 14.04...

Working with AIML and Python


To check whether the module is properly installed on your machine, open a Python IDLE and import the aiml module:

>>> import aiml

If the module is imported correctly, it will not show any error and comes to the next line. Then, we can confirm that the installation is correct.

The most important class we are handling in the aiml module is Kernel(). We are mainly using this class to learn from AIML files and get a response from the AIML dataset to user input. The following line will create an object of the aiml.Kernel() class:

>>> mybot = aiml.Kernel()

After creating the Kernel() object, we can assign the robot name using the following command. We will assign Chefbot as the name for this robot:

>>> mybot.setBotPredicate("name","Chefbot")

The next step is to learn AIML files. We can load either a single AIML file or a group of AIML files. To load a single AIML file, we can use the following command. Note that the sample.aiml file must be...

Working with A.L.I.C.E. AIML files


The AIML files of A.L.I.C.E. chatter are freely available at https://code.google.com/p/aiml-en-us-foundation-alice/.

Extract the AIML files to a folder on the desktop or in the home directory and copy startup.xml to these AIML files. This startup.xml file will load all the other AIML files into memory. The following code is an example of a typical startup.xml file:

<aiml version="1.0">
<category>
  <pattern>LOAD AIML B</pattern>
  <template>
          <!-- Load standard AIML set -->
          <learn>*.aiml</learn>

  </template>
</category>
</aiml>

The preceding XML file will learn all the AIML files when we call the LOAD AIML B pattern.

Loading AIML files into memory

The following code will load all the AIML files into memory:

#!/usr/bin/env python

import aiml
import sys
import os

#Change the current path to your aiml files path
os.chdir('/home/lentin/Desktop/aiml-files')
mybot = aiml.Kernel...

Integrating PyAIML into ROS


In this section, we are going to develop ROS Python nodes which can handle AIML files. We are using the Python code that we developed in the preceding section. The ROS version we are going to use is Indigo and the Ubuntu version we will use is 14.04.2. We already discussed the interfacing of speech recognition and speech synthesis in ROS and also discussed the Python code to interface AIML files. In this section, we will make a package in ROS to handle AIML files. Currently, there are no active packages in ROS repositories that can handle AIML files. We will build our own package using the code that we develop.

Create a ROS package using the following dependencies. Here the sound_play package is used for speech synthesis:

$ catkin_create_pkg ros_aiml rospy std_msgs sound_play

Create a scripts folder inside the ros_aiml package and create the following Python files in it. Create a folder called data and copy the ALICE AIML dataset we have already downloaded to this...

Questions


  1. What is Artificial Intelligence?

  2. What is the use of an AIML file?

  3. Which are the most commonly used AIML tags?

  4. What is the use of the PyAIML module?

Summary


In this chapter, we discussed how to add Artificial Intelligence to ChefBot in order to interact with people. This function is an add-on to ChefBot to increase the interactivity of the robot. We used simple AI techniques such as pattern matching and searching in ChefBot. The pattern datasets are stored in a special type of file called AIML. The Python interpreter module is called PyAIML. We used this to decode AIML files. The user can store the pattern data in an AIML format and PyAIML can interpret this pattern. This method is similar to a stimulus-response system. The user has to give a stimulus in the form of text data and from the AIML pattern, the module finds the appropriate reply to the user input. We saw the entire communication system of the robot and how the robot communicates with people. It includes speech recognition and synthesis along with AI. We already discussed speech in the previous chapter. We also saw useful tags used in AIML and the PyAIML installation, how...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Learning Robotics using Python
Published in: May 2015 Publisher: Packt ISBN-13: 9781783287536
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}