X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=eliza.ipynb;fp=eliza.ipynb;h=312bc8ad0dae6642df539c57d48d43a3f9fd0683;hb=d8d232077ea07852f6e627a78c1139d90c37b97a;hp=7a6da3ca54e4b4f1c11590d05acedee803385588;hpb=3e0b9e4fe16e573e9343495665d68ef836810d67;p=eliza.git diff --git a/eliza.ipynb b/eliza.ipynb index 7a6da3c..312bc8a 100644 --- a/eliza.ipynb +++ b/eliza.ipynb @@ -2,23 +2,20 @@ "cells": [ { "cell_type": "code", - "execution_count": 100, - "metadata": { - "collapsed": true - }, + "execution_count": 51, + "metadata": {}, "outputs": [], "source": [ - "import yaml\n", + "# import yaml\n", + "import json\n", "import collections\n", "import random" ] }, { "cell_type": "code", - "execution_count": 84, - "metadata": { - "collapsed": false - }, + "execution_count": 52, + "metadata": {}, "outputs": [], "source": [ "Match = collections.namedtuple('Match', 'text, rule, bindings')" @@ -26,10 +23,25 @@ }, { "cell_type": "code", - "execution_count": 169, - "metadata": { - "collapsed": false - }, + "execution_count": 53, + "metadata": {}, + "outputs": [], + "source": [ + "# def read_rules_y(rules_file):\n", + "# with open(rules_file) as f:\n", + "# rules = [{'pattern': r['pattern'].split(),\n", + "# 'responses': [t.split() for t in r['responses']]}\n", + "# for r in yaml.safe_load(f)]\n", + "# return rules\n", + "\n", + "# all_rules_y = read_rules_y('rules.yaml')\n", + "# all_rules_y" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, "outputs": [ { "data": { @@ -173,7 +185,7 @@ " [\"i'm\", 'sure', \"it's\", 'not', 'pleasant', 'to', 'be', 'unhappy']]},\n", " {'pattern': ['?X', 'are', 'like', '?Y'],\n", " 'responses': [['what',\n", - " 'resemblence',\n", + " 'resemblance',\n", " 'do',\n", " 'you',\n", " 'see',\n", @@ -310,7 +322,7 @@ " 'things']]}]" ] }, - "execution_count": 169, + "execution_count": 54, "metadata": {}, "output_type": "execute_result" } @@ -320,34 +332,32 @@ " with open(rules_file) as f:\n", " rules = [{'pattern': r['pattern'].split(),\n", " 'responses': [t.split() for t in r['responses']]}\n", - " for r in yaml.load(f)]\n", + " for r in json.load(f)]\n", " return rules\n", "\n", - "all_rules = read_rules('rules.yaml')\n", + "all_rules = read_rules('rules.json')\n", "all_rules" ] }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, + "execution_count": 55, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "> tom dick harry\n" + "> hello there\n" ] }, { "data": { "text/plain": [ - "['tom', 'dick', 'harry']" + "['hello', 'there']" ] }, - "execution_count": 4, + "execution_count": 55, "metadata": {}, "output_type": "execute_result" } @@ -359,10 +369,8 @@ }, { "cell_type": "code", - "execution_count": 102, - "metadata": { - "collapsed": true - }, + "execution_count": 56, + "metadata": {}, "outputs": [], "source": [ "def match(text, rule):\n", @@ -371,10 +379,8 @@ }, { "cell_type": "code", - "execution_count": 240, - "metadata": { - "collapsed": true - }, + "execution_count": 57, + "metadata": {}, "outputs": [], "source": [ "def all_matches(matches):\n", @@ -393,10 +399,8 @@ }, { "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": true - }, + "execution_count": 58, + "metadata": {}, "outputs": [], "source": [ "def is_var(word):\n", @@ -405,10 +409,8 @@ }, { "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": true - }, + "execution_count": 59, + "metadata": {}, "outputs": [], "source": [ "def successful_match(match):\n", @@ -417,10 +419,8 @@ }, { "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": true - }, + "execution_count": 60, + "metadata": {}, "outputs": [], "source": [ "def splits(item):\n", @@ -429,10 +429,8 @@ }, { "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": false - }, + "execution_count": 61, + "metadata": {}, "outputs": [ { "data": { @@ -445,7 +443,7 @@ " ('harry', '')]" ] }, - "execution_count": 25, + "execution_count": 61, "metadata": {}, "output_type": "execute_result" } @@ -456,10 +454,8 @@ }, { "cell_type": "code", - "execution_count": 236, - "metadata": { - "collapsed": true - }, + "execution_count": 62, + "metadata": {}, "outputs": [], "source": [ "def match_item(text, rule, bindings):\n", @@ -487,10 +483,8 @@ }, { "cell_type": "code", - "execution_count": 213, - "metadata": { - "collapsed": false - }, + "execution_count": 63, + "metadata": {}, "outputs": [ { "data": { @@ -498,7 +492,7 @@ "[{'?X': ['fred'], '?Y': ['jane']}]" ] }, - "execution_count": 213, + "execution_count": 63, "metadata": {}, "output_type": "execute_result" } @@ -509,10 +503,8 @@ }, { "cell_type": "code", - "execution_count": 214, - "metadata": { - "collapsed": false - }, + "execution_count": 64, + "metadata": {}, "outputs": [ { "data": { @@ -520,7 +512,7 @@ "[{'?X': [], '?Y': ['jane']}]" ] }, - "execution_count": 214, + "execution_count": 64, "metadata": {}, "output_type": "execute_result" } @@ -531,10 +523,8 @@ }, { "cell_type": "code", - "execution_count": 215, - "metadata": { - "collapsed": false - }, + "execution_count": 65, + "metadata": {}, "outputs": [ { "data": { @@ -542,7 +532,7 @@ "[{'?X': ['fred'], '?Y': []}]" ] }, - "execution_count": 215, + "execution_count": 65, "metadata": {}, "output_type": "execute_result" } @@ -553,10 +543,8 @@ }, { "cell_type": "code", - "execution_count": 216, - "metadata": { - "collapsed": false - }, + "execution_count": 66, + "metadata": {}, "outputs": [ { "data": { @@ -564,7 +552,7 @@ "[{'?X': [], '?Y': []}]" ] }, - "execution_count": 216, + "execution_count": 66, "metadata": {}, "output_type": "execute_result" } @@ -575,10 +563,8 @@ }, { "cell_type": "code", - "execution_count": 217, - "metadata": { - "collapsed": false - }, + "execution_count": 67, + "metadata": {}, "outputs": [ { "data": { @@ -586,7 +572,7 @@ "[]" ] }, - "execution_count": 217, + "execution_count": 67, "metadata": {}, "output_type": "execute_result" } @@ -597,10 +583,8 @@ }, { "cell_type": "code", - "execution_count": 218, - "metadata": { - "collapsed": false - }, + "execution_count": 68, + "metadata": {}, "outputs": [ { "data": { @@ -608,7 +592,7 @@ "[{'?X': ['fred']}]" ] }, - "execution_count": 218, + "execution_count": 68, "metadata": {}, "output_type": "execute_result" } @@ -619,10 +603,8 @@ }, { "cell_type": "code", - "execution_count": 219, - "metadata": { - "collapsed": false - }, + "execution_count": 69, + "metadata": {}, "outputs": [ { "data": { @@ -630,7 +612,7 @@ "[]" ] }, - "execution_count": 219, + "execution_count": 69, "metadata": {}, "output_type": "execute_result" } @@ -641,10 +623,8 @@ }, { "cell_type": "code", - "execution_count": 220, - "metadata": { - "collapsed": false - }, + "execution_count": 70, + "metadata": {}, "outputs": [ { "data": { @@ -652,7 +632,7 @@ "[{'?X': ['yes'], '?Y': ['rain']}]" ] }, - "execution_count": 220, + "execution_count": 70, "metadata": {}, "output_type": "execute_result" } @@ -663,10 +643,8 @@ }, { "cell_type": "code", - "execution_count": 221, - "metadata": { - "collapsed": false - }, + "execution_count": 71, + "metadata": {}, "outputs": [ { "data": { @@ -674,7 +652,7 @@ "[{'?X': ['no'], '?Y': ['rain']}]" ] }, - "execution_count": 221, + "execution_count": 71, "metadata": {}, "output_type": "execute_result" } @@ -685,10 +663,8 @@ }, { "cell_type": "code", - "execution_count": 222, - "metadata": { - "collapsed": false - }, + "execution_count": 72, + "metadata": {}, "outputs": [ { "data": { @@ -696,7 +672,7 @@ "[]" ] }, - "execution_count": 222, + "execution_count": 72, "metadata": {}, "output_type": "execute_result" } @@ -707,10 +683,8 @@ }, { "cell_type": "code", - "execution_count": 223, - "metadata": { - "collapsed": false - }, + "execution_count": 73, + "metadata": {}, "outputs": [ { "data": { @@ -718,7 +692,7 @@ "[{'?X': [], '?Y': ['rain']}]" ] }, - "execution_count": 223, + "execution_count": 73, "metadata": {}, "output_type": "execute_result" } @@ -729,9 +703,8 @@ }, { "cell_type": "code", - "execution_count": 241, + "execution_count": 74, "metadata": { - "collapsed": false, "scrolled": true }, "outputs": [ @@ -742,7 +715,7 @@ " {'?X': ['hello'], '?Y': ['there']}]" ] }, - "execution_count": 241, + "execution_count": 74, "metadata": {}, "output_type": "execute_result" } @@ -753,9 +726,8 @@ }, { "cell_type": "code", - "execution_count": 242, + "execution_count": 75, "metadata": { - "collapsed": false, "scrolled": true }, "outputs": [ @@ -765,7 +737,7 @@ "[{'?X': ['hello']}]" ] }, - "execution_count": 242, + "execution_count": 75, "metadata": {}, "output_type": "execute_result" } @@ -776,10 +748,8 @@ }, { "cell_type": "code", - "execution_count": 244, - "metadata": { - "collapsed": false - }, + "execution_count": 76, + "metadata": {}, "outputs": [ { "data": { @@ -790,7 +760,7 @@ " {'?X': ['hello', 'hello', 'hello'], '?Y': []}]" ] }, - "execution_count": 244, + "execution_count": 76, "metadata": {}, "output_type": "execute_result" } @@ -801,10 +771,8 @@ }, { "cell_type": "code", - "execution_count": 245, - "metadata": { - "collapsed": false - }, + "execution_count": 77, + "metadata": {}, "outputs": [ { "data": { @@ -813,7 +781,7 @@ " {'?X': ['hello'], '?Y': ['hello']}]" ] }, - "execution_count": 245, + "execution_count": 77, "metadata": {}, "output_type": "execute_result" } @@ -824,10 +792,8 @@ }, { "cell_type": "code", - "execution_count": 196, - "metadata": { - "collapsed": true - }, + "execution_count": 78, + "metadata": {}, "outputs": [], "source": [ "def candidate_rules(rules, comment):\n", @@ -838,9 +804,8 @@ }, { "cell_type": "code", - "execution_count": 197, + "execution_count": 79, "metadata": { - "collapsed": false, "scrolled": true }, "outputs": [ @@ -888,7 +853,7 @@ " {'?X': ['i', 'remember', 'rain']})]" ] }, - "execution_count": 197, + "execution_count": 79, "metadata": {}, "output_type": "execute_result" } @@ -899,10 +864,8 @@ }, { "cell_type": "code", - "execution_count": 198, - "metadata": { - "collapsed": true - }, + "execution_count": 80, + "metadata": {}, "outputs": [], "source": [ "def fill(response, bindings):\n", @@ -920,10 +883,8 @@ }, { "cell_type": "code", - "execution_count": 202, - "metadata": { - "collapsed": false - }, + "execution_count": 81, + "metadata": {}, "outputs": [ { "data": { @@ -931,7 +892,7 @@ "'do you often think of rain'" ] }, - "execution_count": 202, + "execution_count": 81, "metadata": {}, "output_type": "execute_result" } @@ -944,10 +905,8 @@ }, { "cell_type": "code", - "execution_count": 165, - "metadata": { - "collapsed": true - }, + "execution_count": 82, + "metadata": {}, "outputs": [], "source": [ "pronoun_swaps = {\n", @@ -973,10 +932,8 @@ }, { "cell_type": "code", - "execution_count": 166, - "metadata": { - "collapsed": false - }, + "execution_count": 83, + "metadata": {}, "outputs": [ { "data": { @@ -984,7 +941,7 @@ "{'?X': ['your', 'father'], '?Y': ['you', 'are', 'your', 'brother', 'keeper']}" ] }, - "execution_count": 166, + "execution_count": 83, "metadata": {}, "output_type": "execute_result" } @@ -995,10 +952,8 @@ }, { "cell_type": "code", - "execution_count": 101, - "metadata": { - "collapsed": true - }, + "execution_count": 84, + "metadata": {}, "outputs": [], "source": [ "def respond(rule, bindings):\n", @@ -1007,18 +962,16 @@ }, { "cell_type": "code", - "execution_count": 231, - "metadata": { - "collapsed": false - }, + "execution_count": 85, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "['why', 'do', 'you', 'recall', 'rain', 'right', 'now']" + "['what', 'is', 'the', 'connection', 'between', 'me', 'and', 'rain']" ] }, - "execution_count": 231, + "execution_count": 85, "metadata": {}, "output_type": "execute_result" } @@ -1030,10 +983,8 @@ }, { "cell_type": "code", - "execution_count": 167, - "metadata": { - "collapsed": false - }, + "execution_count": 86, + "metadata": {}, "outputs": [], "source": [ "def eliza_loop():\n", @@ -1049,9 +1000,8 @@ }, { "cell_type": "code", - "execution_count": 168, + "execution_count": 87, "metadata": { - "collapsed": false, "scrolled": true }, "outputs": [ @@ -1060,27 +1010,15 @@ "output_type": "stream", "text": [ "Hello. I'm Eliza. What seems to be the problem?\n", - "> this is weird\n", - "i am not sure i understand you fully\n", - "> i love my father\n", - "your father\n", - "> yes, my father\n", - "what else comes to mind when you think of your father\n", - "> i remember my father\n", - "does thinking of your father bring anything else to mind\n", - "> i remember i am a saint\n", - "does thinking of you are a saint bring anything else to mind\n", - "> saints are always happy\n", - "really -- always\n", - "> yes, always\n", - "can you think of a specific example\n", - "> saint george was happy when he killed the dragon\n", - "go on\n", - "> i don't think you understand me\n", - "perhaps in your fantasies we don't think each other\n", - "> i aardvark you understand me\n", - "perhaps in your fantasies we aardvark each other\n", - "> exit\n" + "> hello\n", + "how do you do. please state your problem\n", + "> i am sad about life\n", + "i'm sure it's not pleasant to be sad\n", + "> it isn't\n", + "very interesting\n", + "> are you happy\n", + "perhaps i am happy in your fantasies\n", + "> quit\n" ] } ], @@ -1091,9 +1029,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [] } @@ -1114,9 +1050,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.2+" + "version": "3.7.4" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 }