Thursday, 24 March 2016

ChatBot 4 - Sioni on the Web

Leat House
After a long break from ChatScript I now have enough to make Sioni vaguely interesting - to me at least.

Here he is: sioni.online

Status Update

I started out trying to find a way to build a conversational agent that would capture the life and times of someone like my grandmother. That way people could sit down and have a chat with a distant relative and find out about their lives in a much more engaging way than reading some facts about where they were born and seeing some photographs about where they lived.

How are things going so far? Here's a sanity check:

  • Is it engaging enough?
    Yes, it's not bad. It could be much more engaging if I put more time into the script. However, ChatScript is a great tool for bots, there's no doubt.
  • Is it possible to simply feed a bunch of stories and facts into a repository and have the bot use that for its memory of events for conversions?
    No, that's not what ChatScript does in it's current form. Although the recent extension of ChatScript to call out to other sources of truth using REST APIs is worth pursuing. Commercial bots obviously do this to some extent based on knowledge bases.
    Next blog post maybe. 
  • Is the experience rich enough?
    It's ok but it would be useful if the bot was able to use URL's in the script to display additional content or play audio files.  Another blog post.

What did I learn?

Events not Facts

I started with a mind-map of Sioni's world. Being as ChatScript is driven by topics, structuring the mind-map by topics is a good idea. I started with topics for friends, family and places etc. My initial approach was to have a whole bunch of facts as rules. This probably reflects my Prolog programming thought process. However, this approach doesn't result in particularly entertaining conversations! It is much better (as the ChatScript docs suggest) to have your bot drive the conversation. So I ended up with topics for specific life events driven by the bot. If you're building a bot based on your life events (as Sioni is) this is a much more natural way to remember your past and structure your bot. When we catch up with family and friends and talk about the past, we almost always talk about particular events - the memorable ones are often the most emotional too: funny or sad or significant in other ways. Once I figured out that was my best approach, the flood gates opened and the memories of particular events came in thick and fast: The day the onion seller came to the village, that time when I rode my bike into Annie May, the times we went to the big swing past the sand-banks and threw coins into the wishing pond, my first day at school, picking pees in the garden for dinner etc. etc.
If you have a chat to Sioni, you'll see these events come through along with some of my fact-based style weaved in. I haven't gone back and cleaned up the scripts - this is an experiment in bot creation and not a fully complete and consistent bot.

AWS Server

sioni.online is hosted in Amazon (AWS). If you're a bit technical, setting up the Linux server in AWS is straight forward. In fact, it's a lot easier than setting it up on the Mac. Look at "ChatScript Amazon Server.pdf" in the documentation directory.
I used the BETTER PHP files and modified them to call Sioni.

Topics and Behaviour

Because ChatScript remembers where you left off, its behaviour can be confusing when you're trying to develop more script. You need to rebuild and reset to get a clear idea of what it's doing. Initially, I was confused about why a certain topic was being executed and why. In simple terms, ChatScript stays in its current topic as long as there are things left to say and what you've typed matches a rule. If you type something that can't be matched, it will find a typic which will match and then find a rule within that topic. It's pretty obvious really but the behaviour when you're developing can be confusing.

Sublime Text Editor

There are now themes for ChatScript. Syntax highlighting makes writing ChatScript much easier!

ChatScript files

While ChatScript ships with example bots, creating a new bot by copying an existing one can lead to some confusion. I ended up copying quibble.topintroductions.topchildhood.top, keywordless.top, simplecontrol.top and the entire quibble Eliza file set. Update introductions for your bot, add a bunch of things to keywordless.top that makes sense for your bot, and update the simplecontrol.top to make sure it has the right topics are called in the control loop. Most of the text for Sioni is obviously in childhood.top.






Sunday, 24 January 2016

Science and the Grand Confusion




I have many issues with the way science is taught and portrayed in the media. 

I've been meaning to write an informed, pseudo-academic post on this topic for a long time, filled with examples and references, all framed in an epistemological context with enough philosophical jargon to scare the most enthusiastic reader.

But, it's never going to happen and I need to get my thoughts down on paper in whatever form comes to mind!



So here it is...

Firstly, there is a confusion between what science does and what science is that starts at school.

Kids are taught science as a series of simple experiments about the world but rapidly realise that the science that matters is unbelievably complicated and largely inaccessible. If a teacher told you that playing chopsticks on the piano would give you a good grounding in the comprehension of symphonic masterpieces would you believe them? Would you bother?

Not only do people not understand much of what science has discovered, but they often don't have any confidence in scientists' proclamations about how things are. "We live in a multiverse where spooky action at a distance is true". Yep, really.

The scientific community are not doing a great job of convincing people that they know what they're talking about.  Unfortunately their approach is often arrogant and condescending. Strangely many scientists don't understand scientific topics that are only slightly to the left of their area of expertise. You can't expect people to believe in science by insisting they either a) be smart and do science or b) accept you are dumb but trust that someone else is smart enough to know what they're talking about. It doesn't work and it's not helping science.

I have a view of how science should be understood (and taught) that helps me through the quagmire of gibberish. And, fortunately for me, it's not such an odd view - as you'll see below....

I admit that this post takes a very simplistic view of the topic - but I'd rather take an approach that's accessible rather than fail to get my point across. The topic can be refined later.

Firstly, a definition:

Science is about building and testing models of the world around us. Models must fit what we know, and predict things we don't. Models that fit the best and predict the most top the leaderboard until a better model comes along.
Scientists are in the business of building models that compete in an open arena. 

That's it. No more, no less.

On face value, the concept is simple. It omits some of the loftier and more esoteric views of what it means to do science, but I believe the advantages of taking such a simple view are significant and could help humanity as a whole - a simple and worthwhile goal :)

But, is it reasonable to describe science this way?
Here's some views on the subject:
  • The Grand Design: Stephen Hawking and Leonard Mlodinow take the modelling idea to the point of suggesting that it is meaningless to talk about true reality - the only meaningful thing is to talk about the usefulness of the model. In many ways this isn't a new idea.
  • Nancy Nersessian highlights the central position of model building in scientific pursuit. Nancy makes the point that this is how science really gets done which goes against the traditional view that science is: dream up hypothesis, test etc. More on Nancy's work here.
  • David Hestenes' paper on reforming mathematical notation and the role of models in scientific knowledge acquisition provides another good example - he says: "Modeling Theory is about the structure and acquisition of scientific knowledge. Its central tenet is that scientific knowledge is created, first, by constructing and validating models to represent structure in real objects and processes, and second, by organizing models into theories structured by scientific laws. In other words, Modeling Theory is a particular brand of scientific epistemology that posits models as basic units of scientific knowledge and modeling (the process of creating and validating models) as the basic means of knowledge acquisition."
  • Models-Based Science Teaching: Understanding and Using Mental Models - Steven Gilbert.
Many have taken the view that models are central to the way in which we do science. In some cases they insist that science is nothing other than the pursuit of ever better models with an insistence that the models are our reality - talking about reality beyond the model is meaningless.

So this idea is not new - which is a relief.

What are the implications of this view?

Science and Religion

I'll talk about this topic first as it's probably the most common cause of confusion.
There is a constant dialog between "the religious" and "the scientific" community that resembles a conversation between a chicken and a duck.
The internet is awash with science vs religion discussions that get very heated very quickly.
What if we take the view that science is about building models? What part has God to play in such a scheme?
Firstly, if scientists are in the business of building predictive models, introducing God into the equation doesn't help. It doesn't introduce any predictive power into the model. It's not necessarily right or wrong, it's simply not of any use to a scientific model builder.
With this approach, you could believe in a God and do science. You would just have to accept the fact that God has so far been of no benefit to science. If you were to take the scientific model as reality, then God appears to play no objective role in our physical reality. However, we're talking about models here and not reality - more on that later.
So... you don't need to be an atheist to believe in science. You just have to accept that introducing God into a model doesn't improve the model's ability to predict objective outcomes.
Taking this approach helps dissect most of the religion vs science babble on the internet but comes at a cost for scientists. It means that scientists have to admit that while in a strong position, when it comes to predicting outcomes in the physical world, what they have is only a collection of models. Nothing more and nothing less.

Science and Facts

There is a whole body of philosophy dedicated to this topic which I won't cover here. But essentially, facts are simply things that we agree on (for now) that are true about things. There are also facts that are labels of convention: "that's a horse, my cat is black etc."
Facts are parts of our modelling frameworks that we hold true. Facts are often updated through model building. Come up with a better model that breaks some assumptions (facts) but predicts so much more and you have some new facts. There are no facts in the traditional sense.
Scientists have a habit of proclaiming things are facts when they know that the concept is quite fluid. They are simply terrified to admit that facts do change over time and they do not know all the answers. This is terrifying because, as soon as this ground is given up, there is a fear that the lunatic fringe will proclaim that "science has admitted it knows nothing", and so their opinion is as valid as any other! Scientists simply need to stick to the theme of explaining that while they may be proven wrong about something in the future, there's a good chance that the model they are using today is a pretty good bet compared to less scientific models. They should be prepared to simply state that they'd be happy to go head-to-head with alternative models and see who wins.

Rules of the Game

Which brings us to the rules of the game. If you want to play this game you have to agree on the rules. The rules are simple, as stated above, the better the predictive power of the model, the more points you get. Elegance and universality can be used to decide between models with similar predictive powers. If you don't want to stick to those rules then you can't participate in the science arena.
The rules have been selected through natural selection. They are the set of principles that when applied to the array of possible models have resulted in a working set of models that have helped humans survive in a difficult landscape. Our success as a species is clearly in part due to science which is a result of natural selection at many levels!
Obviously Science isn't the only thing that's helped us survive.

We all do so Science

Our brains do science all the time. We build and adapt models in our heads that allow us to catch balls, drive cars, understand language etc. How these things are modelled in our brains is up for debate, but whether or not we have a Bayesian machine or simply a large neural network the modelling is clearly going on. I'd go as far as to say that the reason science has emerged as a discipline is because it's simply a way for humans to collectively do what they do in their own heads all the time. Science wasn't invented, we've just taken it out of our heads and put it on the table - so to speak.

What about Reality

What about it? We only have models. Our entire experience of reality is through models. What we perceive as reality is simply the activation of models in our head. Whether or not there is an objective reality beyond our models is not something that science can answer. The limits of our models are not necessarily the limits of reality. We're moving away from science now an into the land of philosophy and religion.

Questions for another time:
  • What constitutes a good modelling framework? Mathematics? Computers? bits of stick and string? Why?
  • Is it always possible for models to overlap cleanly with other models? what makes models commensurate?
  • What can and can't be modelled?

Education

I'd like to see this view taken and used through all science education. I would prefer to see this view (philosophy of science) taught in schools instead of having kids conduct experiments. It would be much better for people to understand how science works than to do a few lame science experiments that teach kids very little. Very few people go on to do hard science. Ironically, teaching science by doing science may not be the best way for people to understand what science is, why it works and why you should trust it. Unlike cooking, geography or english, very little of what you teach kids in science has any use in day to day life. The same could be true for maths - more on that later.

There's hope: making models is accessible - "doing science" is less so.

Crowdsourcing approaches to scientific problems are now being framed as modelling problems: data sets are provided for analysis with the best solution winning a prize.
Defining science as the creation and selection of models may also help provide a more objective way to assess scientific progress. Who knows... what is the science of models themselves? Meta Science? or simply: Mathematics?

For my part, treating science as model building helps me to enter a new field of exploration armed only with a fertile imagination and the insistence that I check the validity of my models. It doesn't really matter that I'm not an expert before I start. I can still create models, review established literature and have fun [repeat]. 
The alternative is: Review established literature for many years, become bored, demoralised and despondent; fail to have an original idea and give up. 

Understanding the world should be fun - engage your creativity first and rigour second.

My hope is that this approach will not only help the status of science in society but also help many others enter science by simply unleashing their curiosity to build models.

Enough for now - more later.





Friday, 8 August 2014

The League of Clandestine Guitarists

The options for clandestine guitarists today are endless. I'm talking about that large group of people who pick up the guitar once a year, people who strummed away in their bedrooms as kids and still cling on to a distant memory of how much fun it was. They are the people who refuse to play anything in public because a) they can't remember a single tune and b) are transfixed with fear if asked.

I understand - I'm one of them!

And then one day I passed a guitar shop, stared longingly through the window for a while and decided to revisit guitar-land.

If you're thinking "mmmm that could be me, but how embarrassing...", then I'd encourage you to put your embarrassment to one side and think again - the options are accessible, easy, fun and relatively cheap. This blog post (and some to follow) record my trail of guitar devastation back into guitar-land as a clandestine guitarist.

Note: I'm not a musician as will become clear shortly. I'm a tinkerer and fiddler, I've learnt possibly two pieces of music in my entire life but love playing with guitars, keyboards and computers. So I can pretty much guarantee that you'll not be intimidated by my playing but hopefully inspired by the possibilities!


Before I start, here's an example I "created" and published on the web within a couple of days of starting my exploration:



Forget the guitar playing for a moment (yes I know), what's important is that it's just me, a guitar and an iPhone having a huge amount of fun without annoying people. The "tune" is made up as I go along - it's unlikely that I could ever play it again. This is in the spirit of the clandestine guitarist and should be considered a virtue.

What you need to do...

Guitar




You'll need a guitar. No surprise there. I have an accoustic which I rarely use - partly because other people can hear me and partly because the action is to too high. I decided to go electric. With headphones this provides a way to make as many mistakes as I like without my family throwing things at me. It also provides a much easier action and can be recorded easily. Not to mention the large selection of interesting effects now easily available.
I chose a Fender Squier Strat as they're very cheap considering what you get, freely available in most music shops and... I always wanted a Fender Strat. It was up there on my Armchair Bucket List but I really couldn't justify the cost of a "proper Strat", so I opted for Fender's lower end range of guitars and have been very happy with the result. As an clandestine guitarist, you really can't go to far wrong with one of these.


Amp






You'll need something to plug it in to. As I've mentioned, I picked an electric so I could play quietly not loudly. 
I bought an iRig with my guitar and downloaded AmpliTube Fender for my iPhone. Plug it in and off you go! Amazing.






Band

Next you'll need something to play along with. As a clandestine guitarist you'll need a backing track not real musicians. Remember, you can't be seen playing your guitar in public.
Fortunately there are thousands of free backing tracks to choose from on the web. Simply search for "Jam backing tracks" or "Jam tracks" or "guitar backing tracks" and you'll see a whole range of sites. Many of these sites also provide TAB notation for the lead and rhythm.
The track I've used above is: http://www.jamtracks.ru/mpjam2/Blues/Texas_Blues_In_A.mp3
YouTube is also a great place to find backing tracks and tutorials,

Record

AmpliTube allows you to load backing tracks, change their speed for practising and when you're ready - you can record it as well. That's how the Oxtail Soup track (above) was recorded: A guitar, and iPhone and an iRig adaptor and red wine.

Publish

I've used SoundCloud, it's very easy to get going and free to upload quite a bit of content. You can make it publicly visible or you could keep it private if you like. Don't be worried about making it public - no one really cares about what you play and the chances of anyone stumbling across it are close to zero anyway.

Of course you don't have to publish. I wanted to have something to aim for so I could say "done!". But also, as I've mentioned, I tend to make all my "music" up on the spot. Sure I figure out what the scale is in the key that's being played, but apart from that, all the things I've ever played have long faded from memory. Some of the tunes I quite liked and wish I'd recorded them. Publishing on the web makes it real and reminds me of what some of the tunes were like - that's if I ever feel like playing them again.

No one in their right mind would take this stuff seriously - so there's no reason for you to.

So go buy a guitar, download some software and have fun!



Tuesday, 5 August 2014

Music for Knob Twiddlers


Generative Music is fun, cheap and freely available. If you're a techie and interested in music you'd be mad not to play with this stuff. I'm not so I did.

As a kid, the Moog synthesiser was an object of desire - a cross between a church organ and a 1950s telephone exchange. What could be better?

I liked the sound, but if I'm honest, it was the technology that interested me the most.
So years later, my curiosity in electronics and music has resurfaced. Sadly my musical ability is still lacking and the keyboard is as elusive as ever.



Solution: Take a Generative Music (MIDI) app and a software synthesiser that accepts MIDI and away you go. In the video below I've used Nodal to create music in MIDI format and pump the resulting MIDI stream into a software synthesiser: MULAB.




Nodal & MU LABS


Nodal is a very simple, graph based music generator. Each note is represented by a node and the vertices represent the time between notes. Nodes may be connected to multiple nodes and you decide if the exit points are sequential, parallel, or random. You can build a number of separate graphs which output on separate MIDI channels. So you'll need a soft-synth that can handle multiple MIDI input channels. That excludes apps like Garage Band for example.

I've used MULAB as the synth as it can easily be configured to handle multiple MIDI input channels.

MULAB ships with a free soft-synth that supports a large number of synths and sound effects. It's easy to set up and use.
I've used sound flower to record the output and generate the video. Unfortunately I've used a relatively low resolution video - I'll change it next time.

Having rekindled my interest in music, I'm off on the next adventure:  Intermorphic's Noatikl. This is a much more complex environment than Nodal - more to follow.

If you've got a favourite Generative Music app then please let me know.



Saturday, 2 August 2014

Reverse Engineering the Fretboard


Ever wondered how you'd work out the spacing of the frets on a guitar neck? No?

Well, I did and it led me through the maze of (western) music scales and on to an awful truth... there is no single universal harmonious musical scale!

First the Maths

I have no idea what the formal derivation of this is, but wanted to think it through to understand a bit more about the basic building blocks of music. That and, how would I make a guitar if I was stranded on a desert island?

So... the things I know as a starting point:
  • I know that a string with a frequency A (say) will have a frequency A*2 when the string length is halved. That's basic physics.
  • I also know that there are 12 frets between any given note and its double (octave above in music speak). That's twelve frets with the thirteenth being twice the frequency of where we started. Twelve is a western music convention. People have tried other combinations.
But there are many ways of dividing up a length of string into 12 parts. They could be equal, they could be a few percent closer for each fret etc. They could increase and decrease in distance apart. But we also know that the ratio between a string length and the next note up must be the same regardless of the string's length. To understand why this must be so, imagine a string that plays frequency A again. Now assume you play the next note up A' with a string length L'. Now detune the string so that L' now plays A again. To play A' on the L' string you have to move up to L''. Now we know that the frequency is relative to the length (physics again) and so L/L' = L'/L'' etc. So it doesn't matter how long the string is, to get to the next note you have to move up by a fixed ratio (R say) to the length of the string. The next note up on any string is a fixed ratio of  the length of the string regardless of its length. The 'scale' is length invariant as you might expect.


So we have:





That is, to work out how long the string is for the next note up, multiply its length by the inverse of the 12th root of 2. Nice and simple :)

Test Results

If you plug this formula into a spread-sheet and take the first note to be A=440 Hz (it's the standard) then you get the following:

LCalc HzEven tempered scaleMusical Note
0440.0440A
1466.2466.2Bb
2493.9493.9B
3523.3523.3C
4554.4554.4C#
5587.3587.3D
6622.3622.3Eb
7659.3659.3E
8698.5698.5F
9740.0740F#
10784.0784G
11830.6830.6G#
12880.0880A
R0.9438743127
12 root of 1/2

As you can see, the calculated values match those I've found on the internet. So they must be right.

More than one scale?

It turns out there is more than one scale in western music. In fact, people have created quite a number over the years. I've reverse engineered only one, the even tempered scale.

Why would you have more than one? Let's start with the scale we've created above. It turns out that the ratios of the notes to each other cannot be expressed exactly as simple fractions. Why does this matter? Because simple multiples of frequencies played together sound good - they beat together to form harmonious combinations. You could say that's a fundamental aspect of what we think of as music. Conversely, frequencies that are not simple multiples of each other often sound unpleasant when played together. 

It is possible to create a series of notes, similar to the ones above, but based on simple multiples of frequencies only. That is, they do not follow a pre-defined formula but rather they are a hand picked selection of notes that sound good together. Take a look at the just intonation.
The problem with this approach is that music played with these notes cannot be transposed (moved up and down) easily without breaking the simple ratios. But the even tempered scale does allow you to do this - it has that property but is a compromise between the harmonious benefit of simple ratios and the ability to play the same sequence of notes anywhere on the keyboard and sound much the same. Obviously higher or lower in frequency that is. 

So... as much as I'd like to think of the musical scale as something mathematically pure that's straight out of nature, the truth is that what's used in practice is often a compromise between creating harmonious combinations of notes, the physical implications of scale invariance for stringed instruments and man's desire to make the scale even, so that music can be played much the same in any key.

For an comprehensive overview of the history behind the various scales:

And for very good short description of the differences between the scales and their implications: 






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.