Programming AI with Leaf/Communicating With Outside Programs

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Having Leaf exchange data with other programs can be challenging. This author, Thomas Messerschmidt, working with his robot Robot Betty9 (see http://www.myspace.com/robotbetty9), has created a very simple but quite effective way of allowing Leaf to chat with just about any external (external to LISP) program.

The methodology consists of passing text message between Leaf/LISP and the outside world by creating and destroying small text (.txt) files. It was originally written so that Leaf could expand his AI using a standard ALICE-BASED (ProgramD) chatbot.

The first part consists of the function "sendchatout2chatbot". It writes data out to the "in" file "in.txt".

The second part consists of the function "readchatbotfile ". It reads text returned from an application, in this case from a chatbot using a file called "out.txt".

Start of Chatbot Interface Code -----------------------------------------
Start of Chatbot Interface Code -----------------------------------------
Start of Chatbot Interface Code -----------------------------------------
Code by Thomas Messerschmidt (Please credit me. A link back to Betty9 would be nice.)

(defun sendchatout2chatbot ()

 (print *text-phrase*)
       (setf mytext *text-phrase* )
       (setf mytext (subseq mytext 6))
       (with-open-file 
           (textin "C:/in.txt" :direction :output
                                        :if-exists :supersede)
           (print mytext textin)
       )
       (setf *text-phrase* nil)  )

(defun readchatbotfile ()

(with-open-file (stream "C:/out.txt")
  (do ((line (read-line stream nil)
              (read-line stream nil)))
   ((null line))
   (setf x1 line))) 
   (sapi-tts x1)
   (delete-file "C:/out.txt")   ); and then delete the file

(defun getchatin () ; tts what came back from chatbot and then delete the file

 (if (probe-file "C:/out.txt") (readchatbotfile))  ) ; Read the file if there is a file to be read.


End of Chatbot Interface Code -------------------------------------------
End of Chatbot Interface Code -------------------------------------------
End of Chatbot Interface Code -------------------------------------------


So to sum it up: 1. A user asks a question of Leaf 2. Leaf sends the question to the chatbot via the sendchatout2chatbot function which writes to "C:/in.txt" 3. The chatbot reads in the question asked in "C:/in.txt" and then deletes the file. 4. The chatbot writes out its response to the question in a file "C:/out.txt" 5. When leaf sees that there exists a C:/out.txt file, he reads it in with the readchatbotfile function which also reads it aloud using Microsoft's standard TTS (Text to Speech) SAPI 5.x. 6. The file "C:/out.txt" is then deleted by Leaf.