5 "execution_count": 100,
12 "import collections\n",
18 "execution_count": 84,
24 "Match = collections.namedtuple('Match', 'text, rule, bindings')"
29 "execution_count": 169,
37 "[{'pattern': ['?X', 'hello', '?Y'],\n",
38 " 'responses': [['how',\n",
46 " {'pattern': ['?X', 'computer', '?Y'],\n",
47 " 'responses': [['do', 'computers', 'worry', 'you'],\n",
48 " ['what', 'do', 'you', 'think', 'about', 'machines'],\n",
49 " ['why', 'do', 'you', 'mention', 'computers'],\n",
61 " {'pattern': ['?X', 'name', '?Y'],\n",
62 " 'responses': [['i', 'am', 'not', 'interested', 'in', 'names']]},\n",
63 " {'pattern': ['?X', 'sorry', '?Y'],\n",
64 " 'responses': [['please', \"don't\", 'apologize'],\n",
65 " ['apologies', 'are', 'not', 'necessary'],\n",
66 " ['what', 'feelings', 'do', 'you', 'have', 'when', 'you', 'apologize']]},\n",
67 " {'pattern': ['?X', 'i', 'remember', '?Y'],\n",
68 " 'responses': [['do', 'you', 'often', 'think', 'of', '?Y'],\n",
69 " ['does', 'thinking', 'of', '?Y', 'bring', 'anything', 'else', 'to', 'mind'],\n",
70 " ['what', 'else', 'do', 'you', 'remember?'],\n",
71 " ['why', 'do', 'you', 'recall', '?Y', 'right', 'now'],\n",
81 " ['what', 'is', 'the', 'connection', 'between', 'me', 'and', '?Y']]},\n",
82 " {'pattern': ['?X', 'do', 'you', 'remember', '?Y'],\n",
83 " 'responses': [['did', 'you', 'think', 'i', 'would', 'forget', '?Y'],\n",
84 " ['why', 'do', 'you', 'think', 'i', 'should', 'recall', '?Y'],\n",
85 " ['what', 'about', '?Y'],\n",
86 " ['you', 'mentioned', '?Y']]},\n",
87 " {'pattern': ['?X', 'if', '?Y'],\n",
88 " 'responses': [['do',\n",
97 " ['do', 'you', 'wish', 'that', '?Y'],\n",
98 " ['what', 'do', 'you', 'think', 'about', '?Y'],\n",
99 " ['really', '--', 'if', '?Y']]},\n",
100 " {'pattern': ['?X', 'i', 'dreamt', '?Y'],\n",
101 " 'responses': [['really', '--', '?Y'],\n",
111 " ['have', 'you', 'dreamt', '?Y', 'before']]},\n",
112 " {'pattern': ['?X', 'i', 'dreamed', '?Y'],\n",
113 " 'responses': [['really', '--', '?Y'],\n",
123 " ['have', 'you', 'dreamed', '?Y', 'before']]},\n",
124 " {'pattern': ['?X', 'dream', '?Y'],\n",
125 " 'responses': [['what', 'does', 'this', 'dream', 'suggest', 'to', 'you'],\n",
126 " ['do', 'you', 'dream', 'often'],\n",
127 " ['what', 'persons', 'appear', 'in', 'your', 'dreams'],\n",
139 " {'pattern': ['?X', 'my', 'mother', '?Y'],\n",
140 " 'responses': [['who', 'else', 'is', 'in', 'your', 'family', '?Y'],\n",
141 " ['tell', 'me', 'more', 'about', 'your', 'family']]},\n",
142 " {'pattern': ['?X', 'my', 'father', '?Y'],\n",
143 " 'responses': [['your', 'father'],\n",
144 " ['does', 'he', 'influence', 'you', 'strongly'],\n",
156 " {'pattern': ['?X', 'i', 'want', '?Y'],\n",
157 " 'responses': [['what', 'would', 'it', 'mean', 'if', 'you', 'got', '?Y'],\n",
158 " ['why', 'do', 'you', 'want', '?Y'],\n",
159 " ['suppose', 'you', 'got', '?Y', 'soon']]},\n",
160 " {'pattern': ['?X', 'i', 'am', 'glad', '?Y'],\n",
161 " 'responses': [['how', 'have', 'i', 'helped', 'you', 'to', 'be', '?Y'],\n",
162 " ['what', 'makes', 'you', 'happy', 'just', 'now'],\n",
163 " ['can', 'you', 'explain', 'why', 'you', 'are', 'suddenly', '?Y']]},\n",
164 " {'pattern': ['?X', 'i', 'am', 'happy', '?Y'],\n",
165 " 'responses': [['how', 'have', 'i', 'helped', 'you', 'to', 'be', '?Y'],\n",
166 " ['what', 'makes', 'you', 'glad', 'just', 'now'],\n",
167 " ['can', 'you', 'explain', 'why', 'you', 'are', 'suddenly', '?Y']]},\n",
168 " {'pattern': ['?X', 'i', 'am', 'sad', '?Y'],\n",
169 " 'responses': [['i', 'am', 'sorry', 'to', 'hear', 'you', 'are', 'depressed'],\n",
170 " [\"i'm\", 'sure', \"it's\", 'not', 'pleasant', 'to', 'be', 'sad']]},\n",
171 " {'pattern': ['?X', 'i', 'am', 'unhappy', '?Y'],\n",
172 " 'responses': [['i', 'am', 'sorry', 'to', 'hear', 'you', 'are', 'depressed'],\n",
173 " [\"i'm\", 'sure', \"it's\", 'not', 'pleasant', 'to', 'be', 'unhappy']]},\n",
174 " {'pattern': ['?X', 'are', 'like', '?Y'],\n",
175 " 'responses': [['what',\n",
184 " {'pattern': ['?X', 'is', 'like', '?Y'],\n",
185 " 'responses': [['in',\n",
195 " ['what', 'resemblence', 'do', 'you', 'see'],\n",
196 " ['could', 'there', 'really', 'be', 'some', 'connection'],\n",
198 " {'pattern': ['?X', 'alike', '?Y'],\n",
199 " 'responses': [['in', 'what', 'way'],\n",
200 " ['what', 'similarities', 'are', 'there']]},\n",
201 " {'pattern': ['?X', 'same', '?Y'],\n",
202 " 'responses': [['what', 'other', 'connections', 'do', 'you', 'see']]},\n",
203 " {'pattern': ['?X', 'i', 'was', '?Y'],\n",
204 " 'responses': [['were', 'you', 'really'],\n",
205 " ['perhaps', 'i', 'already', 'knew', 'you', 'were', '?Y'],\n",
206 " ['why', 'do', 'you', 'tell', 'me', 'you', 'were', '?Y', 'now']]},\n",
207 " {'pattern': ['?X', 'was', 'i', '?Y'],\n",
208 " 'responses': [['what', 'if', 'you', 'were', '?Y'],\n",
209 " ['do', 'you', 'think', 'you', 'were', '?Y'],\n",
210 " ['what', 'would', 'it', 'mean', 'if', 'you', 'were', '?Y']]},\n",
211 " {'pattern': ['?X', 'i', 'am', '?Y'],\n",
212 " 'responses': [['in', 'what', 'way', 'are', 'you', '?Y'],\n",
213 " ['do', 'you', 'want', 'to', 'be', '?Y']]},\n",
214 " {'pattern': ['?X', 'am', 'i', '?Y'],\n",
215 " 'responses': [['do', 'you', 'believe', 'you', 'are', '?Y'],\n",
216 " ['would', 'you', 'want', 'to', 'be', '?Y'],\n",
217 " ['you', 'wish', 'i', 'would', 'tell', 'you', 'you', 'are', '?Y'],\n",
218 " ['what', 'would', 'it', 'mean', 'if', 'you', 'were', '?Y']]},\n",
219 " {'pattern': ['?X', 'am', '?Y'],\n",
220 " 'responses': [['why', 'do', 'you', 'say', '\"am\"'],\n",
221 " ['i', \"don't\", 'understand', 'that']]},\n",
222 " {'pattern': ['?X', 'are', 'you', '?Y'],\n",
223 " 'responses': [['why',\n",
234 " ['would', 'you', 'prefer', 'it', 'if', 'i', \"weren't\", '?Y'],\n",
235 " ['perhaps', 'i', 'am', '?Y', 'in', 'your', 'fantasies']]},\n",
236 " {'pattern': ['?X', 'you', 'are', '?Y'],\n",
237 " 'responses': [['what', 'makes', 'you', 'think', 'i', 'am', '?Y']]},\n",
238 " {'pattern': ['?X', 'because', '?Y'],\n",
239 " 'responses': [['is', 'that', 'the', 'real', 'reason'],\n",
240 " ['what', 'other', 'reason', 'might', 'there', 'be'],\n",
241 " ['does', 'that', 'reason', 'seem', 'to', 'explain', 'anything', 'else']]},\n",
242 " {'pattern': ['?X', 'were', 'you', '?Y'],\n",
243 " 'responses': [['perhaps', 'i', 'was', '?Y'],\n",
244 " ['what', 'do', 'you', 'think'],\n",
245 " ['what', 'if', 'i', 'had', 'been', '?Y']]},\n",
246 " {'pattern': ['?X', 'i', \"can't\", '?Y'],\n",
247 " 'responses': [['maybe', 'you', 'could', '?Y', 'now'],\n",
248 " ['what', 'if', 'you', 'could', '?Y']]},\n",
249 " {'pattern': ['?X', 'i', 'feel', '?Y'],\n",
250 " 'responses': [['do', 'you', 'often', 'feel', '?Y']]},\n",
251 " {'pattern': ['?X', 'i', 'felt', '?Y'],\n",
252 " 'responses': [['what', 'other', 'feelings', 'do', 'you', 'have']]},\n",
253 " {'pattern': ['?X', 'i', '?Y', 'you', '?Z'],\n",
254 " 'responses': [['perhaps',\n",
262 " {'pattern': ['?X', 'why', \"don't\", 'you', '?Y'],\n",
263 " 'responses': [['should', 'you', '?Y', 'yourself'],\n",
264 " ['do', 'you', 'believe', 'i', \"don't\", '?Y'],\n",
265 " ['perhaps', 'i', 'will', '?Y', 'in', 'good', 'time']]},\n",
266 " {'pattern': ['?X', 'yes', '?Y'],\n",
267 " 'responses': [['you', 'seem', 'quite', 'positive'],\n",
268 " ['you', 'are', 'sure'],\n",
269 " ['i', 'understand']]},\n",
270 " {'pattern': ['?X', 'no', '?Y'],\n",
271 " 'responses': [['why', 'not'],\n",
272 " ['you', 'are', 'being', 'a', 'bit', 'negative'],\n",
273 " ['are', 'you', 'saying', '\"no\"', 'just', 'to', 'be', 'negative']]},\n",
274 " {'pattern': ['?X', 'someone', '?Y'],\n",
275 " 'responses': [['can', 'you', 'be', 'more', 'specific']]},\n",
276 " {'pattern': ['?X', 'everyone', '?Y'],\n",
277 " 'responses': [['surely', 'not', 'everyone'],\n",
278 " ['can', 'you', 'think', 'of', 'anyone', 'in', 'particular'],\n",
279 " ['who', 'for', 'example'],\n",
280 " ['you', 'are', 'thinking', 'of', 'a', 'special', 'person']]},\n",
281 " {'pattern': ['?X', 'always', '?Y'],\n",
282 " 'responses': [['can', 'you', 'think', 'of', 'a', 'specific', 'example'],\n",
284 " ['what', 'incident', 'are', 'you', 'thinking', 'of'],\n",
285 " ['really', '--', 'always']]},\n",
286 " {'pattern': ['?X', 'what', '?Y'],\n",
287 " 'responses': [['why', 'do', 'you', 'ask'],\n",
288 " ['does', 'that', 'question', 'interest', 'you'],\n",
289 " ['what', 'is', 'it', 'you', 'really', 'want', 'to', 'know'],\n",
290 " ['what', 'do', 'you', 'think'],\n",
291 " ['what', 'comes', 'to', 'your', 'mind', 'when', 'you', 'ask', 'that']]},\n",
292 " {'pattern': ['?X', 'perhaps', '?Y'],\n",
293 " 'responses': [['you', 'do', 'not', 'seem', 'quite', 'certain']]},\n",
294 " {'pattern': ['?X', 'are', '?Y'],\n",
295 " 'responses': [['do', 'you', 'think', 'they', 'might', 'not', 'be', '?Y'],\n",
296 " ['possibly', 'they', 'are', '?Y']]},\n",
297 " {'pattern': ['?X'],\n",
298 " 'responses': [['very', 'interesting'],\n",
299 " ['i', 'am', 'not', 'sure', 'i', 'understand', 'you', 'fully'],\n",
300 " ['what', 'does', 'that', 'suggest', 'to', 'you'],\n",
301 " ['please', 'continue'],\n",
313 "execution_count": 169,
315 "output_type": "execute_result"
319 "def read_rules(rules_file):\n",
320 " with open(rules_file) as f:\n",
321 " rules = [{'pattern': r['pattern'].split(),\n",
322 " 'responses': [t.split() for t in r['responses']]}\n",
323 " for r in yaml.load(f)]\n",
326 "all_rules = read_rules('rules.yaml')\n",
332 "execution_count": 4,
339 "output_type": "stream",
347 "['tom', 'dick', 'harry']"
350 "execution_count": 4,
352 "output_type": "execute_result"
356 "i = input(\"> \")\n",
362 "execution_count": 102,
368 "def match(text, rule):\n",
369 " return all_matches([Match(text, rule, {})])"
374 "execution_count": 240,
380 "def all_matches(matches):\n",
383 " # print(matches, successes)\n",
384 " current = matches[0]\n",
385 " new_matches = []\n",
386 " if successful_match(current):\n",
387 " successes += [current.bindings]\n",
388 " elif current.rule:\n",
389 " new_matches = match_item(current.text, current.rule, current.bindings)\n",
390 " matches = matches[1:] + new_matches\n",
396 "execution_count": 22,
402 "def is_var(word):\n",
403 " return word[0] == '?'"
408 "execution_count": 23,
414 "def successful_match(match):\n",
415 " return match.text == [] and match.rule == []"
420 "execution_count": 24,
426 "def splits(item):\n",
427 " return [(item[:i], item[i:]) for i in range(len(item)+1)]"
432 "execution_count": 25,
448 "execution_count": 25,
450 "output_type": "execute_result"
459 "execution_count": 236,
465 "def match_item(text, rule, bindings):\n",
468 " if r0 in bindings:\n",
469 " # already seen this variable\n",
470 " if text[:len(bindings[r0])] == bindings[r0]:\n",
471 " return [Match(text[(len(bindings[r0])):], rule[1:], bindings)]\n",
475 " # not seen this variable yet\n",
477 " for pre, suf in splits(text):\n",
478 " new_bindings = bindings.copy()\n",
479 " new_bindings[r0] = pre\n",
480 " matches += [Match(suf, rule[1:], new_bindings)]\n",
482 " elif text and text[0] == r0:\n",
483 " return [Match(text[1:], rule[1:], bindings)]\n",
490 "execution_count": 213,
498 "[{'?X': ['fred'], '?Y': ['jane']}]"
501 "execution_count": 213,
503 "output_type": "execute_result"
507 "match('fred hello jane'.split(), '?X hello ?Y'.split())"
512 "execution_count": 214,
520 "[{'?X': [], '?Y': ['jane']}]"
523 "execution_count": 214,
525 "output_type": "execute_result"
529 "match('hello jane'.split(), '?X hello ?Y'.split())"
534 "execution_count": 215,
542 "[{'?X': ['fred'], '?Y': []}]"
545 "execution_count": 215,
547 "output_type": "execute_result"
551 "match('fred hello'.split(), '?X hello ?Y'.split())"
556 "execution_count": 216,
564 "[{'?X': [], '?Y': []}]"
567 "execution_count": 216,
569 "output_type": "execute_result"
573 "match('hello'.split(), '?X hello ?Y'.split())"
578 "execution_count": 217,
589 "execution_count": 217,
591 "output_type": "execute_result"
595 "match('fred jane'.split(), '?X hello ?Y'.split())"
600 "execution_count": 218,
611 "execution_count": 218,
613 "output_type": "execute_result"
617 "match('fred hello'.split(), '?X hello'.split())"
622 "execution_count": 219,
633 "execution_count": 219,
635 "output_type": "execute_result"
639 "match('hello fred'.split(), '?X hello'.split())"
644 "execution_count": 220,
652 "[{'?X': ['yes'], '?Y': ['rain']}]"
655 "execution_count": 220,
657 "output_type": "execute_result"
661 "match('yes i remember rain'.split(), '?X i remember ?Y'.split())"
666 "execution_count": 221,
674 "[{'?X': ['no'], '?Y': ['rain']}]"
677 "execution_count": 221,
679 "output_type": "execute_result"
683 "match('no i remember rain'.split(), '?X i remember ?Y'.split())"
688 "execution_count": 222,
699 "execution_count": 222,
701 "output_type": "execute_result"
705 "match(\"no remember rain\".split(), '?X i remember ?Y'.split())"
710 "execution_count": 223,
718 "[{'?X': [], '?Y': ['rain']}]"
721 "execution_count": 223,
723 "output_type": "execute_result"
727 "match(\"i remember rain\".split(), '?X i remember ?Y'.split())"
732 "execution_count": 241,
741 "[{'?X': [], '?Y': ['hello', 'there', 'hello']},\n",
742 " {'?X': ['hello'], '?Y': ['there']}]"
745 "execution_count": 241,
747 "output_type": "execute_result"
751 "match(\"hello there hello\".split(), '?X ?Y ?X'.split())"
756 "execution_count": 242,
765 "[{'?X': ['hello']}]"
768 "execution_count": 242,
770 "output_type": "execute_result"
774 "match(\"hello there hello\".split(), '?X there ?X'.split())"
779 "execution_count": 244,
787 "[{'?X': [], '?Y': ['hello', 'hello', 'hello']},\n",
788 " {'?X': ['hello'], '?Y': ['hello', 'hello']},\n",
789 " {'?X': ['hello', 'hello'], '?Y': ['hello']},\n",
790 " {'?X': ['hello', 'hello', 'hello'], '?Y': []}]"
793 "execution_count": 244,
795 "output_type": "execute_result"
799 "match(\"hello hello hello\".split(), '?X ?Y'.split())"
804 "execution_count": 245,
812 "[{'?X': [], '?Y': ['hello', 'hello', 'hello']},\n",
813 " {'?X': ['hello'], '?Y': ['hello']}]"
816 "execution_count": 245,
818 "output_type": "execute_result"
822 "match(\"hello hello hello\".split(), '?X ?Y ?X'.split())"
827 "execution_count": 196,
833 "def candidate_rules(rules, comment):\n",
834 " return [(rule, bindings) \n",
835 " for rule in rules \n",
836 " for bindings in match(comment, rule['pattern'])]"
841 "execution_count": 197,
850 "[({'pattern': ['?X', 'i', 'remember', '?Y'],\n",
851 " 'responses': [['do', 'you', 'often', 'think', 'of', '?Y'],\n",
861 " ['what', 'else', 'do', 'you', 'remember?'],\n",
862 " ['why', 'do', 'you', 'recall', '?Y', 'right', 'now'],\n",
872 " ['what', 'is', 'the', 'connection', 'between', 'me', 'and', '?Y']]},\n",
873 " {'?X': [], '?Y': ['rain']}),\n",
874 " ({'pattern': ['?X'],\n",
875 " 'responses': [['very', 'interesting'],\n",
876 " ['i', 'am', 'not', 'sure', 'i', 'understand', 'you', 'fully'],\n",
877 " ['what', 'does', 'that', 'suggest', 'to', 'you'],\n",
878 " ['please', 'continue'],\n",
888 " {'?X': ['i', 'remember', 'rain']})]"
891 "execution_count": 197,
893 "output_type": "execute_result"
897 "candidate_rules(all_rules, \"i remember rain\".split())"
902 "execution_count": 198,
908 "def fill(response, bindings):\n",
909 " filled_response = []\n",
910 " for w in response:\n",
912 " if w in bindings:\n",
913 " filled_response += bindings[w]\n",
915 " filled_response += ['MISSING']\n",
917 " filled_response += [w]\n",
918 " return filled_response"
923 "execution_count": 202,
931 "'do you often think of rain'"
934 "execution_count": 202,
936 "output_type": "execute_result"
940 "bs = match('yes i remember rain'.split(), '?X i remember ?Y'.split())[0]\n",
941 "fr = fill(['do', 'you', 'often', 'think', 'of', '?Y'], bs)\n",
947 "execution_count": 165,
953 "pronoun_swaps = {\n",
957 " 'mine': 'yours',\n",
961 "def pronoun_person_swap(bindings):\n",
962 " def swapped(words):\n",
964 " for w in words:\n",
965 " if w in pronoun_swaps:\n",
966 " sw += [pronoun_swaps[w]]\n",
971 " return {var: swapped(bindings[var]) for var in bindings}"
976 "execution_count": 166,
984 "{'?X': ['your', 'father'], '?Y': ['you', 'are', 'your', 'brother', 'keeper']}"
987 "execution_count": 166,
989 "output_type": "execute_result"
993 "pronoun_person_swap({'?X': ['my', 'father'], '?Y': 'i am my brother keeper'.split()})"
998 "execution_count": 101,
1004 "def respond(rule, bindings):\n",
1005 " return fill(random.choice(rule['responses']), bindings)"
1009 "cell_type": "code",
1010 "execution_count": 231,
1018 "['why', 'do', 'you', 'recall', 'rain', 'right', 'now']"
1021 "execution_count": 231,
1023 "output_type": "execute_result"
1027 "r0, b0 = candidate_rules(all_rules, \"i remember rain\".split())[0]\n",
1032 "cell_type": "code",
1033 "execution_count": 167,
1039 "def eliza_loop():\n",
1040 " print(\"Hello. I'm Eliza. What seems to be the problem?\")\n",
1042 " c = input(\"> \")\n",
1043 " if c.strip() in 'quit halt exit stop'.split(): break\n",
1044 " comment = c.split()\n",
1045 " rule, bindings = candidate_rules(all_rules, comment)[0]\n",
1046 " swapped_bindings = pronoun_person_swap(bindings)\n",
1047 " print(' '.join(respond(rule, swapped_bindings)))"
1051 "cell_type": "code",
1052 "execution_count": 168,
1060 "output_type": "stream",
1062 "Hello. I'm Eliza. What seems to be the problem?\n",
1063 "> this is weird\n",
1064 "i am not sure i understand you fully\n",
1065 "> i love my father\n",
1067 "> yes, my father\n",
1068 "what else comes to mind when you think of your father\n",
1069 "> i remember my father\n",
1070 "does thinking of your father bring anything else to mind\n",
1071 "> i remember i am a saint\n",
1072 "does thinking of you are a saint bring anything else to mind\n",
1073 "> saints are always happy\n",
1074 "really -- always\n",
1076 "can you think of a specific example\n",
1077 "> saint george was happy when he killed the dragon\n",
1079 "> i don't think you understand me\n",
1080 "perhaps in your fantasies we don't think each other\n",
1081 "> i aardvark you understand me\n",
1082 "perhaps in your fantasies we aardvark each other\n",
1092 "cell_type": "code",
1093 "execution_count": null,
1103 "display_name": "Python 3",
1104 "language": "python",
1108 "codemirror_mode": {
1112 "file_extension": ".py",
1113 "mimetype": "text/x-python",
1115 "nbconvert_exporter": "python",
1116 "pygments_lexer": "ipython3",