Copyright (C) 2002 Claudio Scordino


Current version: 4.02



Index :

  1. Introduction
  2. The code
    2.1 License
  3. Installation
    3.1 Files locations
  4. Training
    4.1 Reading a file
    4.2 Showing the content of the memory
  5. Questions
  6. Customization
    6.1 Force an answer
    6.2 Using external applications
    6.3 How to add new verbs
  7. Download
  8. Contacts



1. Introduction

During my studies at University, I learned the C++ language (which I consider a very powerful and expressive language), so I tried to realize a program capable of learning sentences, and capable of answering questions about the sentences given before. It was the first release of PAI (version 1.00).

PAI is a Question Answering System that gains information from a conversation conducted in a restricted form of the English language.

I decided to create a command-line program, just to help people to create their own GUIs. In this way it's also possible to put the program on a Web Server, and use CGI (Common Gateway Interface) to make people interact with it.

This program was developed on Linux, but should work also on other systems (e.g. Windows and FreeBSD).



2. The code

The code is written in C++, and uses a layer provided by the words class. This class provides a lot of operations on strings (C++ lacks of powerful operations on strings) and is widely used by all the other classes.

The code is composed of the following files:

words.h: dichiaration of class words
words.cpp: definitions of functions for the class words
verbs.h: dichiaration of class verb
verbs.cpp: definitions of functions for the class verb
memory.h: dichiaration of class memory
memory.cpp: definitions of functions for the class memory

reading.cpp: code to read an external file

command.h: dichiaration of class command
command.cpp: definitions of functions for the class command
execute.cpp: code to execute an external program

start.cpp: code to initialize the program
main.cpp: code to start the program


2.1 License

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

If you modify this software, please send me a copy of the modified code.


3. Installation on Linux



3.1 Files locations


/usr/local/Pai/memory.ia
/usr/local/Pai/commands.ia
/usr/local/Pai/spdans.ia
/usr/local/Pai/verbs.ia
/usr/bin/pai
/usr/share/doc/Pai4.02/compile
/usr/share/doc/Pai4.02/copying
/usr/share/doc/Pai4.02/copyright
/usr/share/doc/Pai4.02/pai.html



4 Training

To train the system, you have to provide many sentences. The program will learn them and it will be capable of answering to questions regarding these sentences.

The command to make PAI learn a sentence is the following pai [--username yourname] sentence The flag username allows to specify the user name (in particular, it's used when in the sentence there are words like me, my, etc.).
If you prefer, you can decide to use the command without this flag. Anyway it's always better to specify which user is using the program.

For example, to teach PAI that the window is blue, just type:

pai the window is blue You can't use abbreviations for the verbs to be and to have (like 'm instead of am, 're instead of are, etc.), but you should use abbreviations for negative sentences (doesn't for does not and didn't for did not).
Remember that the program is case sensitive! This means that you should not use capital letters.

4.1 Reading a file

Since the training is really a boring work, it's possible to make PAI read a file full of sentences. The command is pai --readfile filename where filename is the text file containing the sentences to be learnt.
This command make PAI read a file, learning everything is written in it.

4.2 Showing the content of the memory

To see what the system learnt during the training period, type pai --show This program will print on the standard output the content of the memory.


5 Questions

After the training phase, it's possible to ask PAI some questions. If you are lucky you obtain the desired answer. The command to ask PAI a question is the following pai [--username yourname] question Again, the flag username allows to specify the user name.

For example, to ask how is the window, type

pai how is the window ?


6 Customization

This is the section for an advanced use of PAI, and explains how customize the program to answer with predefined sentences, and how to use external applications to create the answer.

6.1 Force an answer

Every time the user fills a question, PAI searches its internal memory looking for the proper answer. Anyway, it's possible to force an answer, specifying how to answer to predefined questions.
Using this command, you can teach PAI how to answer when the user types some particular questions (or affirmations).

The command is the following pai --forceanswer sentence -then- answer where answer is the predefined answer that PAI must give when the user types sentence.

For example, if you want PAI to answer "not so good..." when you type "what a nice day!", just type: pai --forceanswer what a nice day! -then- not so good... At this point, try to type pai what a nice day! and see the answer.

You can also use a question, as shown in the following example: pai --forceanswer is the window black ? -then- are you blind ?

6.2 Using external applications

This is the most complex command and, maybe, the most important. This command let PAI to use an external application to create the answer.

Lets' start with a simple example. Suppose that you want PAI to show the content of the current directory (on Unix systems the command is called ls) every time the user types show the content of the directory. Just type pai --externalapplication show the content of the directory -then- ls At this point try to type pai show the content of the directory and see that PAI calls the external program ls.

The syntax of this command is pai --externalapplication sentence -then- command where command is the external program that PAI starts every time the user types sentence.

Let's see a more interesting example. Suppose that you have in your system an external command-line program (say multiplicator) capable of calculating multiplications, and you want PAI answering to questions like give me the result of 45 multiplicated for 3. Obviously, you can't fill the answer for all the possible values of the numbers!
The idea is to call the external application multiplicator, providing the right values. The right command is pai --externalapplication give me the result of -any- multiplicated for -any- -then- multiplicator Using the keyword -any- in sentence, PAI will append any word in that position to the external application specified with command.

6.3 How to add new verbs

PAI has a built-in list of english verbs to split the sentence provided by the user, and "understand" it.
If you want to add new verbs, open the file verbs.ia (/usr/local/Pai/verbs.ia on Unix systems) using a simple editor (such as vi or emacs).
You can see that the file is full of coniugations of verbs followed by strange numbers. To add a new verb open this file and write (before the line &%$-end-$%& , that must be the last one !) a new verb, in all its coniugations. After every coniugation you must write two numbers: the first number is the Type, and the second one is the Time.

The Type should be :
  • 0 =unknown; used for every verb different from To Be and To Have
  • 1 =To Have
  • 2 =To Be


  • The Time should be :
  • 0 =ambiguous; used when a form is used for several coniugations
  • 1 =past or past participle
  • 2 =present
  • 3 =future
  • 4 =gerund


  • For example, for the verb To Cut, we must write cut 0 0 : the type is 0 because the verb is not to be or to have; the time is 0 because this form is used for present, for past and for past participle. Then we must write all the other coniugations: cuts 0 2 and cutting 0 4 .
    As you can see, the type is the same for all the coniugations . Moreover for the 2nd person of present there isn't ambiguity: the word "cuts" can be used only for present, so the time is 2.
    Remember to specify always the 2nd person of present !!.




    8. Contacts

    Report bugs & suggestions here

    Official mailing list p_a_i@yahoogroups.com (to subscribe: p_a_i-subscribe@yahoogroups.com ).