I’ve been playing around with Scheme for a little while. (Yeah, I guess I still don’t really have the urge to work with dotnet.) One of the things I did a while ago was ‘port’ the Common Lisp version of Eliza in Peter Norvig’s ‘Paradigms Of Artificial Intelligence Programming’. Eliza, in case you haven’t heard of it, was one of the earliest natural language processing programs. It tried to imitate a Rogerian psychotherapist using simple pattern matching. (I thought the original Eliza paper by Weizenbaum was exclusively behind a paywall but there’s a freely-available version of Weizenbaum’s Eliza paper here.)

This port is available on Github, of course (that great single point of failure in the distributed version control system) and it’s called ElizaNorvig.

It’s not a super-smart chatbot, it has to be said. Norvig’s focus was on the beautiful pattern-matching lisp allows, and he supplied a fairly small rule-set (about 40 rules) to demonstrate its use. It’s enough to show some simple conversations but it quickly shows its limitations:

You: Hello there

Eliza: How do you do. Please state your problem.
You: I'm tired
Eliza: Do you feel strongly about discussing such things?
You: Not really
Eliza: What does that suggest to you?
You: That I'm tired
Eliza: Please continue.
You: Please don't make me
Eliza: I am not sure I understand you fully.

If I were smarter or more persistent I’d have extended the rules to give what the Cool Kids these days are calling a better ‘conversational UI’. But I didn’t. I thought I’d keep that version a pretty pure port of Norvig’s code, and I’d put my tweaks in a separate repository. But again I didn’tAfter that tweet in May I made a few tweaks and then pretty much forgot about it.

I’d tried a bunch of different Scheme implementations on the way but found Chibi Scheme to be closest to what I wanted - a simple, pure R7RS scheme implementation that stuck to the standards instead of requiring special command-line switches to make it standards compliant. It’s pretty cool. I like it.

Then I noticed there was a web site that allowed you to run Chibi scheme in your browser. That’s pretty neat, given that Chibi is a C program...

Turns out you can build Chibi using emscripten to create an asm.js version that can run in browsers.

So of course my next step was to try running Eliza in a browser too.

And it worked.

There’s no documentation on running the asm.js version of Chibi, but I got a lot of the way there just looking at the existing online implementation. Then a few emails to the authors of Chibi Scheme and Chibi’s emscripten build got me answers to my remaining questions. (Thanks guys!)

The big hurdle that wasn’t shown in the existing online implementation was sharing data between the browser/Javascript and the scheme code. The easy approach is to create a global variable in Javascript, say ‘foo’:

window.foo = '';

This can then be accessed in scheme code with:

(define (foo)

  (string-eval-script "window.foo"))

And set using something like:

(define (set-foo! value)

  (eval-script! (string-append "window.foo = '" value "'")))

(You’ll need to be careful of quoting ‘value’ above if it contains quote marks.)

With those wrinkles sorted, getting my existing scheme code running was remarkable easy. All the SRFIs I needed were available and my code wasn’t doing anything tricky so I shouldn’t have been too surprised, but surprised I was. It worked!

And you can try the Eliza out live in your browser right now!

Tags: Scheme
Created by on Logo15659OpinionatedGeek Ltd.Logo15659