ELIZA Chatbot Part 3

Now that we can match the patterns, we have to get the keywords from a user’s input and find it in the rulebook. Then use the keywords to answer the question using the patterns and responses from the rulebook. Let’s go!

The Rulebook

For the rulebook we will be using a python file derived from the original ELIZA program script. Below is a sample of what it looks like. You can download the entire file here.

rulebook = { "sorry": (0,
			[
				[[0],
					["please do not apologize"],
					["apologies are not necessary"],
					["what feelings do you have when you apologise"]]					
			]),
		"remember": (5,
			[
				[[0, "you", "remember", 0],
					["do you think often of", 4],
					["does thinking of", 4, "bring anything else to mind"],
					["what else do you remember"],
					["why do you remember", 4, "just now"],
					["what in the present situation reminds you of", 4],
					["what is the connection between me and", 4]],
				[[0, "do", "i", "remember", 0],
					["did you think I would forget", 5],
					["why do you think I should recall", 5, "now"],
					["what about", 5]]
			]),

To use the rulebook we will import the file using this code:

from ELIZArules import conversions, rulebook

Finding Keywords from Rulebook

We will use the findKeywords() function to return a list of keywords and the phrases they appear in. There’s no fancy sorting here, it just looks through the rulebook for matching keywords and putting the highest priority items first.

def findKeywords(text):
    for phrase in splitClauses(text):
        words = list(transform(splitWords(phrase)))
        maxPriority  = 0
        keywords = []
        for word in words:
            if word in rulebook:
                priority = rulebook[word][0]
                if priority > maxPriority:
                    maxPriority = priority
                    keywords.insert(0, word)
                else:
                    keywords.append(word)
    if len(keywords) > 0:
        return keywords, words
    return [], []

Answering the Question

With all the keywords found, all we need is to match the input to a response pattern. We will use the compose() function to put the sentences together. If no matches are found, then a randomized default answer will be provided.

defaultAnswers = [
    "I'm not sure I understand you fully.",
    "Please go on.",
    "What does that suggest to you?",
    "Do you feel strongly about discussing such things?",
    "That is quite interesting.",
    "Tell me more about that.", 
    "How does that make you feel?",
    "Why do you say that?"
]

def compose(template, fields):
    result = ''
    for t in template:
        if type(t) == int:
            result += ' ' + fields[t-1]
        else:
            result += ' ' + t
    return result.strip()

def answer(keywords, words):
    for keyword in keywords:
        for test in rulebook[keyword][1]:
            pattern = test[0]
            responses = test[1:]
            success, matches = matchPattern(pattern, words)
            if success:
                response = random.choice(responses)
                return compose(response, matches)
    return random.choice(defaultAnswers)

Putting it all together

To run program, we just have to call the functions we just created:

while True:
    text = input('?')
    if len(text) == 0:
        break

    keywords, words = findKeywords(text)
    print(answer(keywords, words))

That’s it, below is a sample conversation I’ve had with this chatbot. The entire code can be downloaded here.

?i feel sad, i want to play
why do you want to play
?it make me feel happy
of what does feeling happy remind you
?good times on the playground
Please go on.
?playing on the swings, jumping up and down
What does that suggest to you?
?i am a child at heart
do you believe it is normal to be a child at heart
?i think it should be
That is quite interesting.

Conclusion

As primitive as this 1966 chatbot is, it can in some instances create interesting conversations as an approximation of a therapist. Obviously, it can easily be fooled to giving out non-sensical responses, but it can be made better with a more complex rulebook, input scanning, and spell checks. What I learned from this exercise is that a chatbot does not necessarily need to understand language to function.

The main advantage of this kind of chat bot is that it is very predictable and will never hallucinate answers. If the chatbot is given enough information in its rulebook, it can be an expert in a particular field. For example, querying banking information where a user is expected to ask certain types of questions.

Overall, this has been a fun exercise and introduction to AI. I hope you learn something as well. Leave a comment below!

Leave a Reply

Your email address will not be published. Required fields are marked *