"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')"
},
{
"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": {
" [\"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",
" 'things']]}]"
]
},
- "execution_count": 169,
+ "execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
" 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"
}
},
{
"cell_type": "code",
- "execution_count": 102,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 56,
+ "metadata": {},
"outputs": [],
"source": [
"def match(text, rule):\n",
},
{
"cell_type": "code",
- "execution_count": 240,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 57,
+ "metadata": {},
"outputs": [],
"source": [
"def all_matches(matches):\n",
},
{
"cell_type": "code",
- "execution_count": 22,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 58,
+ "metadata": {},
"outputs": [],
"source": [
"def is_var(word):\n",
},
{
"cell_type": "code",
- "execution_count": 23,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 59,
+ "metadata": {},
"outputs": [],
"source": [
"def successful_match(match):\n",
},
{
"cell_type": "code",
- "execution_count": 24,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 60,
+ "metadata": {},
"outputs": [],
"source": [
"def splits(item):\n",
},
{
"cell_type": "code",
- "execution_count": 25,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 61,
+ "metadata": {},
"outputs": [
{
"data": {
" ('harry', '')]"
]
},
- "execution_count": 25,
+ "execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 236,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 62,
+ "metadata": {},
"outputs": [],
"source": [
"def match_item(text, rule, bindings):\n",
},
{
"cell_type": "code",
- "execution_count": 213,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 63,
+ "metadata": {},
"outputs": [
{
"data": {
"[{'?X': ['fred'], '?Y': ['jane']}]"
]
},
- "execution_count": 213,
+ "execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 214,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 64,
+ "metadata": {},
"outputs": [
{
"data": {
"[{'?X': [], '?Y': ['jane']}]"
]
},
- "execution_count": 214,
+ "execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 215,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 65,
+ "metadata": {},
"outputs": [
{
"data": {
"[{'?X': ['fred'], '?Y': []}]"
]
},
- "execution_count": 215,
+ "execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 216,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 66,
+ "metadata": {},
"outputs": [
{
"data": {
"[{'?X': [], '?Y': []}]"
]
},
- "execution_count": 216,
+ "execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 217,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 67,
+ "metadata": {},
"outputs": [
{
"data": {
"[]"
]
},
- "execution_count": 217,
+ "execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 218,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 68,
+ "metadata": {},
"outputs": [
{
"data": {
"[{'?X': ['fred']}]"
]
},
- "execution_count": 218,
+ "execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 219,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 69,
+ "metadata": {},
"outputs": [
{
"data": {
"[]"
]
},
- "execution_count": 219,
+ "execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 220,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 70,
+ "metadata": {},
"outputs": [
{
"data": {
"[{'?X': ['yes'], '?Y': ['rain']}]"
]
},
- "execution_count": 220,
+ "execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 221,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 71,
+ "metadata": {},
"outputs": [
{
"data": {
"[{'?X': ['no'], '?Y': ['rain']}]"
]
},
- "execution_count": 221,
+ "execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 222,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 72,
+ "metadata": {},
"outputs": [
{
"data": {
"[]"
]
},
- "execution_count": 222,
+ "execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 223,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 73,
+ "metadata": {},
"outputs": [
{
"data": {
"[{'?X': [], '?Y': ['rain']}]"
]
},
- "execution_count": 223,
+ "execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 241,
+ "execution_count": 74,
"metadata": {
- "collapsed": false,
"scrolled": true
},
"outputs": [
" {'?X': ['hello'], '?Y': ['there']}]"
]
},
- "execution_count": 241,
+ "execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 242,
+ "execution_count": 75,
"metadata": {
- "collapsed": false,
"scrolled": true
},
"outputs": [
"[{'?X': ['hello']}]"
]
},
- "execution_count": 242,
+ "execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 244,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 76,
+ "metadata": {},
"outputs": [
{
"data": {
" {'?X': ['hello', 'hello', 'hello'], '?Y': []}]"
]
},
- "execution_count": 244,
+ "execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 245,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 77,
+ "metadata": {},
"outputs": [
{
"data": {
" {'?X': ['hello'], '?Y': ['hello']}]"
]
},
- "execution_count": 245,
+ "execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 196,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 78,
+ "metadata": {},
"outputs": [],
"source": [
"def candidate_rules(rules, comment):\n",
},
{
"cell_type": "code",
- "execution_count": 197,
+ "execution_count": 79,
"metadata": {
- "collapsed": false,
"scrolled": true
},
"outputs": [
" {'?X': ['i', 'remember', 'rain']})]"
]
},
- "execution_count": 197,
+ "execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 198,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 80,
+ "metadata": {},
"outputs": [],
"source": [
"def fill(response, bindings):\n",
},
{
"cell_type": "code",
- "execution_count": 202,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 81,
+ "metadata": {},
"outputs": [
{
"data": {
"'do you often think of rain'"
]
},
- "execution_count": 202,
+ "execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 165,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 82,
+ "metadata": {},
"outputs": [],
"source": [
"pronoun_swaps = {\n",
},
{
"cell_type": "code",
- "execution_count": 166,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 83,
+ "metadata": {},
"outputs": [
{
"data": {
"{'?X': ['your', 'father'], '?Y': ['you', 'are', 'your', 'brother', 'keeper']}"
]
},
- "execution_count": 166,
+ "execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 101,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 84,
+ "metadata": {},
"outputs": [],
"source": [
"def respond(rule, bindings):\n",
},
{
"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"
}
},
{
"cell_type": "code",
- "execution_count": 167,
- "metadata": {
- "collapsed": false
- },
+ "execution_count": 86,
+ "metadata": {},
"outputs": [],
"source": [
"def eliza_loop():\n",
},
{
"cell_type": "code",
- "execution_count": 168,
+ "execution_count": 87,
"metadata": {
- "collapsed": false,
"scrolled": true
},
"outputs": [
"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"
]
}
],
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "collapsed": true
- },
+ "metadata": {},
"outputs": [],
"source": []
}
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.5.2+"
+ "version": "3.7.4"
}
},
"nbformat": 4,
- "nbformat_minor": 0
+ "nbformat_minor": 1
}
--- /dev/null
+[
+ {
+ "pattern": "?X hello ?Y",
+ "responses": [
+ "how do you do. please state your problem"
+ ]
+ },
+ {
+ "pattern": "?X computer ?Y",
+ "responses": [
+ "do computers worry you",
+ "what do you think about machines",
+ "why do you mention computers",
+ "what do you think machines have to do with your problem"
+ ]
+ },
+ {
+ "pattern": "?X name ?Y",
+ "responses": [
+ "i am not interested in names"
+ ]
+ },
+ {
+ "pattern": "?X sorry ?Y",
+ "responses": [
+ "please don't apologize",
+ "apologies are not necessary",
+ "what feelings do you have when you apologize"
+ ]
+ },
+ {
+ "pattern": "?X i remember ?Y",
+ "responses": [
+ "do you often think of ?Y",
+ "does thinking of ?Y bring anything else to mind",
+ "what else do you remember?",
+ "why do you recall ?Y right now",
+ "what in this present situation reminds you of ?Y",
+ "what is the connection between me and ?Y"
+ ]
+ },
+ {
+ "pattern": "?X do you remember ?Y",
+ "responses": [
+ "did you think i would forget ?Y",
+ "why do you think i should recall ?Y",
+ "what about ?Y",
+ "you mentioned ?Y"
+ ]
+ },
+ {
+ "pattern": "?X if ?Y",
+ "responses": [
+ "do you really think it is likely that ?Y",
+ "do you wish that ?Y",
+ "what do you think about ?Y",
+ "really -- if ?Y"
+ ]
+ },
+ {
+ "pattern": "?X i dreamt ?Y",
+ "responses": [
+ "really -- ?Y",
+ "have you ever fantasized ?Y while you were awake",
+ "have you dreamt ?Y before"
+ ]
+ },
+ {
+ "pattern": "?X i dreamed ?Y",
+ "responses": [
+ "really -- ?Y",
+ "have you ever fantasized ?Y while you were awake",
+ "have you dreamed ?Y before"
+ ]
+ },
+ {
+ "pattern": "?X dream ?Y",
+ "responses": [
+ "what does this dream suggest to you",
+ "do you dream often",
+ "what persons appear in your dreams",
+ "don't you believe that dream has to do with your problem"
+ ]
+ },
+ {
+ "pattern": "?X my mother ?Y",
+ "responses": [
+ "who else is in your family ?Y",
+ "tell me more about your family"
+ ]
+ },
+ {
+ "pattern": "?X my father ?Y",
+ "responses": [
+ "your father",
+ "does he influence you strongly",
+ "what else comes to mind when you think of your father"
+ ]
+ },
+ {
+ "pattern": "?X i want ?Y",
+ "responses": [
+ "what would it mean if you got ?Y",
+ "why do you want ?Y",
+ "suppose you got ?Y soon"
+ ]
+ },
+ {
+ "pattern": "?X i am glad ?Y",
+ "responses": [
+ "how have i helped you to be ?Y",
+ "what makes you happy just now",
+ "can you explain why you are suddenly ?Y"
+ ]
+ },
+ {
+ "pattern": "?X i am happy ?Y",
+ "responses": [
+ "how have i helped you to be ?Y",
+ "what makes you glad just now",
+ "can you explain why you are suddenly ?Y"
+ ]
+ },
+ {
+ "pattern": "?X i am sad ?Y",
+ "responses": [
+ "i am sorry to hear you are depressed",
+ "i'm sure it's not pleasant to be sad"
+ ]
+ },
+ {
+ "pattern": "?X i am unhappy ?Y",
+ "responses": [
+ "i am sorry to hear you are depressed",
+ "i'm sure it's not pleasant to be unhappy"
+ ]
+ },
+ {
+ "pattern": "?X are like ?Y",
+ "responses": [
+ "what resemblance do you see between ?X and ?Y"
+ ]
+ },
+ {
+ "pattern": "?X is like ?Y",
+ "responses": [
+ "in what way is it that ?X is like ?Y",
+ "what resemblence do you see",
+ "could there really be some connection",
+ "how"
+ ]
+ },
+ {
+ "pattern": "?X alike ?Y",
+ "responses": [
+ "in what way",
+ "what similarities are there"
+ ]
+ },
+ {
+ "pattern": "?X same ?Y",
+ "responses": [
+ "what other connections do you see"
+ ]
+ },
+ {
+ "pattern": "?X i was ?Y",
+ "responses": [
+ "were you really",
+ "perhaps i already knew you were ?Y",
+ "why do you tell me you were ?Y now"
+ ]
+ },
+ {
+ "pattern": "?X was i ?Y",
+ "responses": [
+ "what if you were ?Y",
+ "do you think you were ?Y",
+ "what would it mean if you were ?Y"
+ ]
+ },
+ {
+ "pattern": "?X i am ?Y",
+ "responses": [
+ "in what way are you ?Y",
+ "do you want to be ?Y"
+ ]
+ },
+ {
+ "pattern": "?X am i ?Y",
+ "responses": [
+ "do you believe you are ?Y",
+ "would you want to be ?Y",
+ "you wish i would tell you you are ?Y",
+ "what would it mean if you were ?Y"
+ ]
+ },
+ {
+ "pattern": "?X am ?Y",
+ "responses": [
+ "why do you say \"am\"",
+ "i don't understand that"
+ ]
+ },
+ {
+ "pattern": "?X are you ?Y",
+ "responses": [
+ "why are you interested in whether i am ?Y or not",
+ "would you prefer it if i weren't ?Y",
+ "perhaps i am ?Y in your fantasies"
+ ]
+ },
+ {
+ "pattern": "?X you are ?Y",
+ "responses": [
+ "what makes you think i am ?Y"
+ ]
+ },
+ {
+ "pattern": "?X because ?Y",
+ "responses": [
+ "is that the real reason",
+ "what other reason might there be",
+ "does that reason seem to explain anything else"
+ ]
+ },
+ {
+ "pattern": "?X were you ?Y",
+ "responses": [
+ "perhaps i was ?Y",
+ "what do you think",
+ "what if i had been ?Y"
+ ]
+ },
+ {
+ "pattern": "?X i can't ?Y",
+ "responses": [
+ "maybe you could ?Y now",
+ "what if you could ?Y"
+ ]
+ },
+ {
+ "pattern": "?X i feel ?Y",
+ "responses": [
+ "do you often feel ?Y"
+ ]
+ },
+ {
+ "pattern": "?X i felt ?Y",
+ "responses": [
+ "what other feelings do you have"
+ ]
+ },
+ {
+ "pattern": "?X i ?Y you ?Z",
+ "responses": [
+ "perhaps in your fantasies we ?Y each other"
+ ]
+ },
+ {
+ "pattern": "?X why don't you ?Y",
+ "responses": [
+ "should you ?Y yourself",
+ "do you believe i don't ?Y",
+ "perhaps i will ?Y in good time"
+ ]
+ },
+ {
+ "pattern": "?X yes ?Y",
+ "responses": [
+ "you seem quite positive",
+ "you are sure",
+ "i understand"
+ ]
+ },
+ {
+ "pattern": "?X no ?Y",
+ "responses": [
+ "why not",
+ "you are being a bit negative",
+ "are you saying \"no\" just to be negative"
+ ]
+ },
+ {
+ "pattern": "?X someone ?Y",
+ "responses": [
+ "can you be more specific"
+ ]
+ },
+ {
+ "pattern": "?X everyone ?Y",
+ "responses": [
+ "surely not everyone",
+ "can you think of anyone in particular",
+ "who for example",
+ "you are thinking of a special person"
+ ]
+ },
+ {
+ "pattern": "?X always ?Y",
+ "responses": [
+ "can you think of a specific example",
+ "when",
+ "what incident are you thinking of",
+ "really -- always"
+ ]
+ },
+ {
+ "pattern": "?X what ?Y",
+ "responses": [
+ "why do you ask",
+ "does that question interest you",
+ "what is it you really want to know",
+ "what do you think",
+ "what comes to your mind when you ask that"
+ ]
+ },
+ {
+ "pattern": "?X perhaps ?Y",
+ "responses": [
+ "you do not seem quite certain"
+ ]
+ },
+ {
+ "pattern": "?X are ?Y",
+ "responses": [
+ "do you think they might not be ?Y",
+ "possibly they are ?Y"
+ ]
+ },
+ {
+ "pattern": "?X",
+ "responses": [
+ "very interesting",
+ "i am not sure i understand you fully",
+ "what does that suggest to you",
+ "please continue",
+ "go on",
+ "do you feel strongly about discussing such things"
+ ]
+ }
+]
\ No newline at end of file