Friday 1 August 2014

ChatBot 3 - Getting started with ChatScript

A random picture of Alan Turing
for inspiration
Having lost faith in AIML as a platform to capture life history, I've turned to ChatScript by Bruce Wilcox. Amazing.

Bruce explains why his approach (ChatScript) is an improvement on AIML based ChatBots in the Speaker for the Dead paper:

"AIML is based purely on pattern matching literal words. It compares scripted patterns against the input until it finds a match and then presents the prerecorded response. This is incredibly limiting, and AIML has excruciatingly over-precise pattern match capabilities. One can not easily author a lot of content, nor can it get anywhere close to finding the meaning of an input.

I've played with it for a while now and am very impressed!
There are a couple of major functional differences between ChatScript and AIML to note:

  • ChatScript is written in plain text and not XML. It's a computer scripting language and to my mind, much easier to write, test and debug than AIML.
  • It supports the notion of concepts. That is, you can test to see if a word fits the concept rather than simply a text match. It comes with built in set of concepts arranged in a taxonomy and you can simply add your own as well. 
  • The pattern matching is MUCH more flexible that AIML.
  • Groups of rules are arranged in topics. Topics can be selected through a list of keywords or concepts. ChatScript will then select rules in that context. For example, a concept such as my my_dog, may list keywords such as ( dog Sarah puppy ) as triggers to enter this topic.

But first, I'm going to list the basics of how to get started. This section is all covered in Bruce's documentation but is spread across a few documents. What follows is my "cheat sheet" for getting started.

How to get the source:

Go to the sourceforge project here:

Download and unzip the latest zip file, for example: ChatScript-4.5.zip

Running on a Mac?

If you're running on a Windows PC or Linux, then skip this section.

Sadly, if you're running on a Mac then you'll need to compile the code. It's not difficult - follow thee steps:

Download Xcode for Mac from here:

If you're not a Mac developer you'll have to register first.

Run Xcode and download the command line tools. You'll find this option in
Preferences>Downloads.

Browse to the base directory of your ChatScript files and in a command line window and run:
g++ -lpthread -funsigned-char src/*.cpp -O2 -oMacChatScript 2>err.txt

Then try and run the server as follows:
./MacChatScript local


You should see the server start and have a promp on the command line. Press enter and see if it’s working (talking). If it is, you'll be talking to Harry - the default bot.

Note: If you get stuck and just want a working Mac binary let me know and I'll send you mine. 
In the long run it's worthwhile being able to build your own as Bruce releases new versions quite frequently.


Running on a PC?

There is a EXE file provided with the download. Simply run that as follows:
ChatScript.exe local

Running on Linux?

32 bit and 64 bit binaries shipped with the zip file. You'll need to make them executable:
chmod +x ./LinuxChatScript32
./LinuxChatScript32 local

or 
chmod +x ./LinuxChatScript64
./LinuxChatScript64 local

Documents

Take a look under the DOCUMENTATION folder and you'll see a comprehensive collection. It's not easy to navigate at first, but you'll quickly learn where the relevant information is.

Create a new bot

The first think you'll want to do is create a new bot - your bot.

The document "Bot Harry - basic bot.pdf" covers this in some detail.
Essentially you have to make a copy of HARRY and change all the Harry text references to your own. Mine's Sioni so I'll stick to that below. 

So...
In the folder RAWDATA copy the HARRY folder and rename it. Use this as the base for your new bot.

Change the name at the bottom of introductions.top
Change Harry in simplecontrol.top to Sioni

In main folder, copy filesHarry.txt and rename filesSion.txt
Edit this file and change the path for Harry's files to Sioni's files.

I've reused childhood.top by removing the content and stating again. This is fine for Sioni as it is all about his childhood. If you wanted a file with a different name simply rename it.

Common Rules

The complete ChatScript language is quite extensive and complicated. However you can build a complete chatbot with just a subset of simple commands.

# This is a comment. We're going to have a topic about my dog Sarah.
# If the user is asking about dog or Sarah this topic will 
# be active, or if there's no input anything with a t: may be 
# spoken by Sioni.

topic: ~about_dog ( dog Sarah )

# Sion will speak this once as a conversation starter.
t: We have a dog called Sarah.
# if the answer includes 'do you' Sioni will say the following text.
    a: ( do you ) Yes. What do you want to know about her?

# Again, random question from Sioni
t: Do you know what Sarah likes to eat?
# If you type anything with 'what' or anything that's the concept 'no' then Sioni will tell you she likes rabbits.
    a: ( [ what ~no ] ) She likes to catch and eat rabbits the most but also quite likes squirrel.
# If you guessed rabbits.
    a: ( rabbit ) Yes! She likes rabbit!
# If no match, capture input and write it back in another sentence.
    a: ( _* ) Oh I don't think she likes that... '_0 really?

Simple Build and Test cycle

You'll be editing code and wanting to test it quite frequently. It's important to remember that when you exit ChatScript and login again, ChatScript will continue the session where you left off. Just to be clear, you don't really login to ChatScript, rather you tell it who you are and it uses that to maintain a session state. But when you're writing and testing code, starting where you left off can be very confusing. It's better to have a clean slate. Occasionally your code will cause a catastrophic error which messes up the ChatScript state so badly that you have to rebuild the knowledge base. This is addressed below.

Note: I'm using local mode, hence the 'local' for the startup argument. This simply allows you to interact with ChatScript at the command line. You can run it as a server and use a web front end but for the purposes of developing the scripts themselves, the command line interaction is much more effective. I'll talk about other front-ends in some later post - honest.

Typical Steps are:

Edit some code the run the server, provide a user name (anything) and then type:
:build sioni

Test your new rules, debug and fix bugs and try again... etc.


to reset the rules and start the conversation afresh do this when you're logged on:
:reset user

I do this pretty much every time I want to retest by code.

Complete Clean Build

In the event of a bad crash, rebuild the whole system as follows:
Delete the contents of the following two directories: USERS and TOPIC

Then start the ChatScript again and run:
:build: 0
:build sioni

This builds the ChatScript base again and adds your bot. You have a fresh new bot with all the old history cleaned out!

Unit Tests

If you're a developer you'll be familiar with the notion of unit test. ChatScript supports this through a special comment type before a pattern. The comment is used to test the pattern that follows.  For example:

#! Does Sarah like rabbits?
?: ( Sarah like rabbits ) Yes she does.

It's worth adding these unit tests as you go.


Next time: I'll show you a much more functional Sioni and let you know what I've learnt along the way about constructing a chatbot using ChatScript.





4 comments:

  1. Hi there -- I'm excited to install ChatScript but for some reason the method you provide (and the method everyone else provides) to get it to compile on a Mac does not (do not) work. Could you please send me the binary for the most recent version? Thank you so much!

    ReplyDelete
    Replies
    1. Hi Julia - see Todd's comment below for the latest Mac build.
      Browse to:
      https://github.com/bwilcox-1234/ChatScript
      Open a console window and type:
      cd BINARIES && ./MacChatScript local
      Then change directory to ChatScript and type:
      cd BINARIES && ./MacChatScript local

      And you're up and running!


      Delete
  2. Thanks!

    FYI: For mac users I now maintain an Xcode project that is part of the chatscript source and compile a binary every release and commit it into BINARIES/MacChatScript in GitHub. You should always be able to grab a compiled version there. It unfortunately lags the official releases by one version though. If you want the absolute latest it's best to clone the git repo locally.

    ReplyDelete
    Replies
    1. Thanks Todd! Just tried it - works perfectly :)

      Delete