There Is More To Life Than Tests
So far we’ve focused entirely on running our chatbot through automated tests. But eventually we’ll want some way for actual users to talk to DELPHI too. And since I just recently finished separating the chat code from the testing code I figure now is a great time to also introduce some user focused code.
Getting DELPHI to talk to a human is pretty easy. The generateResponse function already knows how to… well… generate responses to input. All that’s left is figuring out how to feed it human input instead of test input. Perl let’s us do this in under ten lines (which I put in a file name “chat.pl”):
#! /usr/bin/perl -w use strict; require 'DELPHI.pm'; while(<>){ chomp; print "DELPHI: ", DELPHI::generateResponse($_), "\n"; }
You Promised No Tricky Perl!
Oh, I did promise that. So I guess the only honorable thing to do is to write a new version of “chat.pl” that doesn’t use quite so many shortcuts.
#! /usr/bin/perl -w use strict; require 'DELPHI.pm'; while( my $userInput = <STDIN> ){ chomp($userInput); #Remove trailing newline character with chomp my $response = DELPHI::generateResponse($userInput); print "DELPHI: ", $response, "\n"; }
There, that’s better. Everything is much easier to understand now. We have a simple while loop that grabs lines of input from standard input, letting the user type questions for DELPHI. And then since DELPHI doesn’t like newlines we use the handy Perl function chomp to remove them from the input. Now that we have a user input string with no nasty newline at the end we pass it DELPHI::generateResponse and finally print out DELPHI’s reply for the user to read.
This Is A Horrible User Interface
If you tried to run “chat.pl” as is you probably noticed that it’s not very user friendly. When you first start the program it just sits on the command line and hopes that the user will eventually figure out he’s supposed to type something. And the only way to break out of the loop is to send the end of file character (ctrl-z or ctrl-d depending on your OS). We can do better than this!
#! /usr/bin/perl -w use strict; require 'DELPHI.pm'; print "DELPHI: The oracle greets you. What questions are on your mind?\n"; print '>'; while(my $userInput = <STDIN>){ chomp($userInput); #Remove trailing newline character with chomp if(lc($userInput) eq 'goodbye' || lc($userInput) eq 'quit'){ last; #Break out of while loop } my $response = DELPHI::generateResponse($userInput); print "DELPHI: ", $response, "\n"; print '>'; } print "DELPHI: Come back anytime\n";
Mostly cosmetic changes here. We start the program with some quick text that lets the user know they are dealing with a question answering bot and we end with a friendly farewell. We also put a helpful ‘>’ at the start of every input line so they user knows when they’re supposed to be typing.
I’ve also added the ability to exit the loop by typing “goodbye” or “quit”. To help with this I use the function lc, which returns an all lowercase version of whatever the user types. This way we don’t have to worry about users that capitalize “Goodbye” or people who try to “EXIT” with their caps lock key on. We then break the loop by using the last command which immediately exits the loop.
DELPHI’s First Words
Now DELPHI is currently only passing 6 out of 11 test cases so it’s really not ready for genuine human conversation. But we can still fire up our chat program and at see what happens.
DELPHI: The oracle greets you. What questions are on your mind?
>Is this working?
DELPHI: Fate indicates that this is working
>That’s nice.
DELPHI: I don’t want to talk about that. Please ask me a question
>Goodbye
DELPHI: Come back anytime
Not a wonderful conversation, but it does prove that DELPHI works just as well interactively as it does during tests. Now we can get back to writing more rules and responses in hope of getting DELPHI to perform better the next time we decide to talk to it directly.
Is DELPHI Too Strict?
If you’ve been playing with “chat.pl” you might have noticed that DELPHI is currently really stuck up about how you do or don’t capitalize words. For example:
DELPHI: The oracle greets you. What questions are on your mind?
>is this working?
DELPHI: I’m sorry, could you try rewording that?
>Is this working?
DELPHI: Fate indicates that this is working
I don’t plan to do anything about this problem right now. I just wanted to point it out to everyone so that you won’t think I overlooked this fairly big usability flaw. I’ll (probably) eventually fix this be rewriting DELPHI to care less about case, but for now I’m just going to go back to writing rules.