{ "metadata": { "name": "", "signature": "sha256:1cfb8c57177729100e868ac0e07f7768f9ded1cceacb17f7e8d69a42ef9a2dde" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "A basic Python implementation of Eliza.\n", "\n", "Based on [this source code][1].\n", "\n", "[1]:https://www.daniweb.com/software-development/python/code/380743/eliza-aka-therapist-facelift\n", "\n", "Uses a random number generator and the standard Python regular expression code." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import random\n", "import re" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "This chatbot gets all of its content by mirroring phrases that the user has just contributed. In order to do this meaningfully, the program has to change the perspective of what it says, swapping `I` and `you` for example. Chatbot programs often use very simple strategies to do something that is approximately correct here. We'll see a much better way later, using off-the-shelf natural langauge processing tools.\n", "\n", "Here we have a Python dictionary `reflection_of` mapping some words to replacements. The `translate` function takes a matched pattern, segments it into words using a regular expression, then substitutes reflections wherever they are found." ] }, { "cell_type": "code", "collapsed": false, "input": [ "reflection_of = {\n", " \"am\" : \"are\",\n", " \"was\" : \"were\",\n", " \"i\" : \"you\",\n", " \"i'd\" : \"you would\",\n", " \"i've\" : \"you have\",\n", " \"i'll\" : \"you will\",\n", " \"i'm\" : \"you are\",\n", " \"my\" : \"your\",\n", " \"are\" : \"am\",\n", " \"you're\": \"I am\",\n", " \"you've\": \"I have\",\n", " \"you'll\": \"I will\",\n", " \"your\" : \"my\",\n", " \"yours\" : \"mine\",\n", " \"you\" : \"me\",\n", " \"me\" : \"you\" }\n", "\n", "def translate(this):\n", " return ' '.join(reflection_of[word] if word in reflection_of else word\n", " for word in re.findall(r\"[\\w']+\",this.lower())) \n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The bulk of Eliza is just a set of rules. Each rule consists of \n", "- A regular expression that should be matched against what the user says. If there is a match, that means that this rule can apply to generate possible responses for the therapist.\n", "- A list of possible responses. The therapist picks one of these responses at random. The responses can have expressions of the form `%n` inside them. These will be replaced with the translation of group number `n` from the regular expression match.\n", "This version of Eliza has 39 different rules. You can imagine that this table could get pretty big and it would potentially lead to more complex and interesting behavior." ] }, { "cell_type": "code", "collapsed": false, "input": [ "rules = [(re.compile(x[0]), x[1]) for x in [\n", " ['How are you?',\n", " [ \"I'm fine, thank you.\"]],\n", " [\"I need (.*)\",\n", " [ \"Why do you need %1?\",\n", " \"Would it really help you to get %1?\",\n", " \"Are you sure you need %1?\"]],\n", " [\"Why don't you (.*)\",\n", " [ \"Do you really think I don't %1?\",\n", " \"Perhaps eventually I will %1.\",\n", " \"Do you really want me to %1?\"]],\n", " [\"Why can't I (.*)\",\n", " [ \"Do you think you should be able to %1?\",\n", " \"If you could %1, what would you do?\",\n", " \"I don't know -- why can't you %1?\",\n", " \"Have you really tried?\"]],\n", " [\"I can't (.*)\",\n", " [ \"How do you know you can't %1?\",\n", " \"Perhaps you could %1 if you tried.\",\n", " \"What would it take for you to %1?\"]],\n", " [\"I am (.*)\",\n", " [ \"Did you come to me because you are %1?\",\n", " \"How long have you been %1?\",\n", " \"How do you feel about being %1?\"]],\n", " [\"I'm (.*)\",\n", " [ \"How does being %1 make you feel?\",\n", " \"Do you enjoy being %1?\",\n", " \"Why do you tell me you're %1?\",\n", " \"Why do you think you're %1?\"]],\n", " [\"Are you (.*)\",\n", " [ \"Why does it matter whether I am %1?\",\n", " \"Would you prefer it if I were not %1?\",\n", " \"Perhaps you believe I am %1.\",\n", " \"I may be %1 -- what do you think?\"]],\n", " [\"What (.*)\",\n", " [ \"Why do you ask?\",\n", " \"How would an answer to that help you?\",\n", " \"What do you think?\"]],\n", " [\"How (.*)\",\n", " [ \"How do you suppose?\",\n", " \"Perhaps you can answer your own question.\",\n", " \"What is it you're really asking?\"]],\n", " [\"Because (.*)\",\n", " [ \"Is that the real reason?\",\n", " \"What other reasons come to mind?\",\n", " \"Does that reason apply to anything else?\",\n", " \"If %1, what else must be true?\"]],\n", " [\"(.*) sorry (.*)\",\n", " [ \"There are many times when no apology is needed.\",\n", " \"What feelings do you have when you apologize?\"]],\n", " [\"Hello(.*)\",\n", " [ \"Hello... I'm glad you could drop by today.\",\n", " \"Hi there... how are you today?\",\n", " \"Hello, how are you feeling today?\"]],\n", " [\"I think (.*)\",\n", " [ \"Do you doubt %1?\",\n", " \"Do you really think so?\",\n", " \"But you're not sure %1?\"]],\n", " [\"(.*) friend(.*)\",\n", " [ \"Tell me more about your friends.\",\n", " \"When you think of a friend, what comes to mind?\",\n", " \"Why don't you tell me about a childhood friend?\"]],\n", " [\"Yes\",\n", " [ \"You seem quite sure.\",\n", " \"OK, but can you elaborate a bit?\"]],\n", " [\"No\",\n", " [ \"Why not?\"]],\n", " [\"(.*) computer(.*)\",\n", " [ \"Are you really talking about me?\",\n", " \"Does it seem strange to talk to a computer?\",\n", " \"How do computers make you feel?\",\n", " \"Do you feel threatened by computers?\"]],\n", " [\"Is it (.*)\",\n", " [ \"Do you think it is %1?\",\n", " \"Perhaps it's %1 -- what do you think?\",\n", " \"If it were %1, what would you do?\",\n", " \"It could well be that %1.\"]],\n", " [\"It is (.*)\",\n", " [ \"You seem very certain.\",\n", " \"If I told you that it probably isn't %1, what would you feel?\"]],\n", " [\"Can you (.*)\",\n", " [ \"What makes you think I can't %1?\",\n", " \"If I could %1, then what?\",\n", " \"Why do you ask if I can %1?\"]],\n", " [\"Can I (.*)\",\n", " [ \"Perhaps you don't want to %1.\",\n", " \"Do you want to be able to %1?\",\n", " \"If you could %1, would you?\"]],\n", " [\"You are (.*)\",\n", " [ \"Why do you think I am %1?\",\n", " \"Does it please you to think that I'm %1?\",\n", " \"Perhaps you would like me to be %1.\",\n", " \"Perhaps you're really talking about yourself?\"]],\n", " [\"You're (.*)\",\n", " [ \"Why do you say I am %1?\",\n", " \"Why do you think I am %1?\",\n", " \"Are we talking about you, or me?\"]],\n", " [\"I don't (.*)\",\n", " [ \"Don't you really %1?\",\n", " \"Why don't you %1?\",\n", " \"Do you want to %1?\"]],\n", " [\"I feel (.*)\",\n", " [ \"Good, tell me more about these feelings.\",\n", " \"Do you often feel %1?\",\n", " \"When do you usually feel %1?\",\n", " \"When you feel %1, what do you do?\"]],\n", " [\"I have (.*)\",\n", " [ \"Why do you tell me that you've %1?\",\n", " \"Have you really %1?\",\n", " \"Now that you have %1, what will you do next?\"]],\n", " [\"I would (.*)\",\n", " [ \"Could you explain why you would %1?\",\n", " \"Why would you %1?\",\n", " \"Who else knows that you would %1?\"]],\n", " [\"Is there (.*)\",\n", " [ \"Do you think there is %1?\",\n", " \"It's likely that there is %1.\",\n", " \"Would you like there to be %1?\"]],\n", " [\"My (.*)\",\n", " [ \"I see, your %1.\",\n", " \"Why do you say that your %1?\",\n", " \"When your %1, how do you feel?\"]],\n", " [\"You (.*)\",\n", " [ \"We should be discussing you, not me.\",\n", " \"Why do you say that about me?\",\n", " \"Why do you care whether I %1?\"]],\n", " [\"Why (.*)\",\n", " [ \"Why don't you tell me the reason why %1?\",\n", " \"Why do you think %1?\" ]],\n", " [\"I want (.*)\",\n", " [ \"What would it mean to you if you got %1?\",\n", " \"Why do you want %1?\",\n", " \"What would you do if you got %1?\",\n", " \"If you got %1, then what would you do?\"]],\n", " [\"(.*) mother(.*)\",\n", " [ \"Tell me more about your mother.\",\n", " \"What was your relationship with your mother like?\",\n", " \"How do you feel about your mother?\",\n", " \"How does this relate to your feelings today?\",\n", " \"Good family relations are important.\"]],\n", " [\"(.*) father(.*)\",\n", " [ \"Tell me more about your father.\",\n", " \"How did your father make you feel?\",\n", " \"How do you feel about your father?\",\n", " \"Does your relationship with your father relate to your feelings today?\",\n", " \"Do you have trouble showing affection with your family?\"]],\n", " [\"(.*) child(.*)\",\n", " [ \"Did you have close friends as a child?\",\n", " \"What is your favorite childhood memory?\",\n", " \"Do you remember any dreams or nightmares from childhood?\",\n", " \"Did the other children sometimes tease you?\",\n", " \"How do you think your childhood experiences relate to your feelings today?\"]],\n", " [\"(.*)\\?\",\n", " [ \"Why do you ask that?\",\n", " \"Please consider whether you can answer your own question.\",\n", " \"Perhaps the answer lies within yourself?\",\n", " \"Why don't you tell me?\"]],\n", " [\"quit\",\n", " [ \"Thank you for talking with me.\",\n", " \"Good-bye.\",\n", " \"Thank you, that will be $150. Have a good day!\"]],\n", " [\"(.*)\",\n", " [ \"Please tell me more.\",\n", " \"Let's change focus a bit... Tell me about your family.\",\n", " \"Can you elaborate on that?\",\n", " \"Why do you say that %1?\",\n", " \"I see.\",\n", " \"Very interesting.\",\n", " \"%1.\",\n", " \"I see. And what does that tell you?\",\n", " \"How does that make you feel?\",\n", " \"How do you feel when you say that?\"]]\n", "]]\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python's regular expressions make it ridiculously easy to execute the rules. This `respond` function does it all. Given a `sentence` from the user, it looks through all the rules, taking the first one for which a match is found. Then it picks a response at random, replaces the `%n` terms as needed, and returns the result." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def respond(sentence):\n", " # find a match among keys, last one is quaranteed to match.\n", " for rule, value in rules:\n", " match = rule.search(sentence)\n", " if match is not None:\n", " # found a match ... stuff with corresponding value\n", " # chosen randomly from among the available options\n", " resp = random.choice(value)\n", " # we've got a response... stuff in reflected text where indicated\n", " while '%' in resp:\n", " pos = resp.find('%')\n", " num = int(resp[pos+1:pos+2])\n", " resp = resp.replace(resp[pos:pos+2], translate(match.group(num)))\n", " return resp\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want to make an interactive version of Eliza that runs in a shell from a python script, \n", "just append the following code to your file:\n", "```python\n", "if __name__ == '__main__':\n", " print(\"\"\"\n", "Therapist\n", "---------\n", "Talk to the program by typing in plain English, using normal upper-\n", "and lower-case letters and punctuation. Enter \"quit\" when done.'\"\"\")\n", " print('='*72)\n", " print(\"Hello. How are you feeling today?\")\n", " s = \"\"\n", " while s != \"quit\":\n", " s = input(\">\")\n", " while s and s[-1] in \"!.\":\n", " s = s[:-1]\n", " \n", " print(respond(s))\n", "```\n", "In the meantime, here are some illustatrations of the `respond` function so you can get a flavor for what Eliza does." ] }, { "cell_type": "code", "collapsed": false, "input": [ "respond(\"My mother hates me.\")" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "'When your mother hates you, how do you feel?'" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "respond(\"How are you?\")" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "\"I'm fine, thank you.\"" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "respond(\"I need a break.\")" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "'Why do you need a break?'" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, looking ahead, there are some bugs with the `translate` function. Sometimes it works, but sometimes not. The problem is that it has no way to recognize the grammatical role of the words in the user's text, so it can't tell whether `you` is the subject of a sentence or the object, or whether `you` or `they` is the subject of the verb `were`. These kinds of problems require some real AI to fix, but it's actually pretty easy to do!" ] }, { "cell_type": "code", "collapsed": false, "input": [ "translate(\"my mother hates me\")" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "'your mother hates you'" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "translate(\"i will do anything you ask.\")" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "'you will do anything me ask'" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "translate(\"the dogs were crazy\")" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "'the dogs were crazy'" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "translate(\"the dog was crazy\")" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "'the dog were crazy'" ] } ], "prompt_number": 11 } ], "metadata": {} } ] }