11 "import collections\n",
17 "execution_count": 52,
21 "Match = collections.namedtuple('Match', 'text, rule, bindings')"
26 "execution_count": 53,
30 "# def read_rules_y(rules_file):\n",
31 "# with open(rules_file) as f:\n",
32 "# rules = [{'pattern': r['pattern'].split(),\n",
33 "# 'responses': [t.split() for t in r['responses']]}\n",
34 "# for r in yaml.safe_load(f)]\n",
37 "# all_rules_y = read_rules_y('rules.yaml')\n",
43 "execution_count": 54,
49 "[{'pattern': ['?X', 'hello', '?Y'],\n",
50 " 'responses': [['how',\n",
58 " {'pattern': ['?X', 'computer', '?Y'],\n",
59 " 'responses': [['do', 'computers', 'worry', 'you'],\n",
60 " ['what', 'do', 'you', 'think', 'about', 'machines'],\n",
61 " ['why', 'do', 'you', 'mention', 'computers'],\n",
73 " {'pattern': ['?X', 'name', '?Y'],\n",
74 " 'responses': [['i', 'am', 'not', 'interested', 'in', 'names']]},\n",
75 " {'pattern': ['?X', 'sorry', '?Y'],\n",
76 " 'responses': [['please', \"don't\", 'apologize'],\n",
77 " ['apologies', 'are', 'not', 'necessary'],\n",
78 " ['what', 'feelings', 'do', 'you', 'have', 'when', 'you', 'apologize']]},\n",
79 " {'pattern': ['?X', 'i', 'remember', '?Y'],\n",
80 " 'responses': [['do', 'you', 'often', 'think', 'of', '?Y'],\n",
81 " ['does', 'thinking', 'of', '?Y', 'bring', 'anything', 'else', 'to', 'mind'],\n",
82 " ['what', 'else', 'do', 'you', 'remember?'],\n",
83 " ['why', 'do', 'you', 'recall', '?Y', 'right', 'now'],\n",
93 " ['what', 'is', 'the', 'connection', 'between', 'me', 'and', '?Y']]},\n",
94 " {'pattern': ['?X', 'do', 'you', 'remember', '?Y'],\n",
95 " 'responses': [['did', 'you', 'think', 'i', 'would', 'forget', '?Y'],\n",
96 " ['why', 'do', 'you', 'think', 'i', 'should', 'recall', '?Y'],\n",
97 " ['what', 'about', '?Y'],\n",
98 " ['you', 'mentioned', '?Y']]},\n",
99 " {'pattern': ['?X', 'if', '?Y'],\n",
100 " 'responses': [['do',\n",
109 " ['do', 'you', 'wish', 'that', '?Y'],\n",
110 " ['what', 'do', 'you', 'think', 'about', '?Y'],\n",
111 " ['really', '--', 'if', '?Y']]},\n",
112 " {'pattern': ['?X', 'i', 'dreamt', '?Y'],\n",
113 " 'responses': [['really', '--', '?Y'],\n",
123 " ['have', 'you', 'dreamt', '?Y', 'before']]},\n",
124 " {'pattern': ['?X', 'i', 'dreamed', '?Y'],\n",
125 " 'responses': [['really', '--', '?Y'],\n",
135 " ['have', 'you', 'dreamed', '?Y', 'before']]},\n",
136 " {'pattern': ['?X', 'dream', '?Y'],\n",
137 " 'responses': [['what', 'does', 'this', 'dream', 'suggest', 'to', 'you'],\n",
138 " ['do', 'you', 'dream', 'often'],\n",
139 " ['what', 'persons', 'appear', 'in', 'your', 'dreams'],\n",
151 " {'pattern': ['?X', 'my', 'mother', '?Y'],\n",
152 " 'responses': [['who', 'else', 'is', 'in', 'your', 'family', '?Y'],\n",
153 " ['tell', 'me', 'more', 'about', 'your', 'family']]},\n",
154 " {'pattern': ['?X', 'my', 'father', '?Y'],\n",
155 " 'responses': [['your', 'father'],\n",
156 " ['does', 'he', 'influence', 'you', 'strongly'],\n",
168 " {'pattern': ['?X', 'i', 'want', '?Y'],\n",
169 " 'responses': [['what', 'would', 'it', 'mean', 'if', 'you', 'got', '?Y'],\n",
170 " ['why', 'do', 'you', 'want', '?Y'],\n",
171 " ['suppose', 'you', 'got', '?Y', 'soon']]},\n",
172 " {'pattern': ['?X', 'i', 'am', 'glad', '?Y'],\n",
173 " 'responses': [['how', 'have', 'i', 'helped', 'you', 'to', 'be', '?Y'],\n",
174 " ['what', 'makes', 'you', 'happy', 'just', 'now'],\n",
175 " ['can', 'you', 'explain', 'why', 'you', 'are', 'suddenly', '?Y']]},\n",
176 " {'pattern': ['?X', 'i', 'am', 'happy', '?Y'],\n",
177 " 'responses': [['how', 'have', 'i', 'helped', 'you', 'to', 'be', '?Y'],\n",
178 " ['what', 'makes', 'you', 'glad', 'just', 'now'],\n",
179 " ['can', 'you', 'explain', 'why', 'you', 'are', 'suddenly', '?Y']]},\n",
180 " {'pattern': ['?X', 'i', 'am', 'sad', '?Y'],\n",
181 " 'responses': [['i', 'am', 'sorry', 'to', 'hear', 'you', 'are', 'depressed'],\n",
182 " [\"i'm\", 'sure', \"it's\", 'not', 'pleasant', 'to', 'be', 'sad']]},\n",
183 " {'pattern': ['?X', 'i', 'am', 'unhappy', '?Y'],\n",
184 " 'responses': [['i', 'am', 'sorry', 'to', 'hear', 'you', 'are', 'depressed'],\n",
185 " [\"i'm\", 'sure', \"it's\", 'not', 'pleasant', 'to', 'be', 'unhappy']]},\n",
186 " {'pattern': ['?X', 'are', 'like', '?Y'],\n",
187 " 'responses': [['what',\n",
196 " {'pattern': ['?X', 'is', 'like', '?Y'],\n",
197 " 'responses': [['in',\n",
207 " ['what', 'resemblence', 'do', 'you', 'see'],\n",
208 " ['could', 'there', 'really', 'be', 'some', 'connection'],\n",
210 " {'pattern': ['?X', 'alike', '?Y'],\n",
211 " 'responses': [['in', 'what', 'way'],\n",
212 " ['what', 'similarities', 'are', 'there']]},\n",
213 " {'pattern': ['?X', 'same', '?Y'],\n",
214 " 'responses': [['what', 'other', 'connections', 'do', 'you', 'see']]},\n",
215 " {'pattern': ['?X', 'i', 'was', '?Y'],\n",
216 " 'responses': [['were', 'you', 'really'],\n",
217 " ['perhaps', 'i', 'already', 'knew', 'you', 'were', '?Y'],\n",
218 " ['why', 'do', 'you', 'tell', 'me', 'you', 'were', '?Y', 'now']]},\n",
219 " {'pattern': ['?X', 'was', 'i', '?Y'],\n",
220 " 'responses': [['what', 'if', 'you', 'were', '?Y'],\n",
221 " ['do', 'you', 'think', 'you', 'were', '?Y'],\n",
222 " ['what', 'would', 'it', 'mean', 'if', 'you', 'were', '?Y']]},\n",
223 " {'pattern': ['?X', 'i', 'am', '?Y'],\n",
224 " 'responses': [['in', 'what', 'way', 'are', 'you', '?Y'],\n",
225 " ['do', 'you', 'want', 'to', 'be', '?Y']]},\n",
226 " {'pattern': ['?X', 'am', 'i', '?Y'],\n",
227 " 'responses': [['do', 'you', 'believe', 'you', 'are', '?Y'],\n",
228 " ['would', 'you', 'want', 'to', 'be', '?Y'],\n",
229 " ['you', 'wish', 'i', 'would', 'tell', 'you', 'you', 'are', '?Y'],\n",
230 " ['what', 'would', 'it', 'mean', 'if', 'you', 'were', '?Y']]},\n",
231 " {'pattern': ['?X', 'am', '?Y'],\n",
232 " 'responses': [['why', 'do', 'you', 'say', '\"am\"'],\n",
233 " ['i', \"don't\", 'understand', 'that']]},\n",
234 " {'pattern': ['?X', 'are', 'you', '?Y'],\n",
235 " 'responses': [['why',\n",
246 " ['would', 'you', 'prefer', 'it', 'if', 'i', \"weren't\", '?Y'],\n",
247 " ['perhaps', 'i', 'am', '?Y', 'in', 'your', 'fantasies']]},\n",
248 " {'pattern': ['?X', 'you', 'are', '?Y'],\n",
249 " 'responses': [['what', 'makes', 'you', 'think', 'i', 'am', '?Y']]},\n",
250 " {'pattern': ['?X', 'because', '?Y'],\n",
251 " 'responses': [['is', 'that', 'the', 'real', 'reason'],\n",
252 " ['what', 'other', 'reason', 'might', 'there', 'be'],\n",
253 " ['does', 'that', 'reason', 'seem', 'to', 'explain', 'anything', 'else']]},\n",
254 " {'pattern': ['?X', 'were', 'you', '?Y'],\n",
255 " 'responses': [['perhaps', 'i', 'was', '?Y'],\n",
256 " ['what', 'do', 'you', 'think'],\n",
257 " ['what', 'if', 'i', 'had', 'been', '?Y']]},\n",
258 " {'pattern': ['?X', 'i', \"can't\", '?Y'],\n",
259 " 'responses': [['maybe', 'you', 'could', '?Y', 'now'],\n",
260 " ['what', 'if', 'you', 'could', '?Y']]},\n",
261 " {'pattern': ['?X', 'i', 'feel', '?Y'],\n",
262 " 'responses': [['do', 'you', 'often', 'feel', '?Y']]},\n",
263 " {'pattern': ['?X', 'i', 'felt', '?Y'],\n",
264 " 'responses': [['what', 'other', 'feelings', 'do', 'you', 'have']]},\n",
265 " {'pattern': ['?X', 'i', '?Y', 'you', '?Z'],\n",
266 " 'responses': [['perhaps',\n",
274 " {'pattern': ['?X', 'why', \"don't\", 'you', '?Y'],\n",
275 " 'responses': [['should', 'you', '?Y', 'yourself'],\n",
276 " ['do', 'you', 'believe', 'i', \"don't\", '?Y'],\n",
277 " ['perhaps', 'i', 'will', '?Y', 'in', 'good', 'time']]},\n",
278 " {'pattern': ['?X', 'yes', '?Y'],\n",
279 " 'responses': [['you', 'seem', 'quite', 'positive'],\n",
280 " ['you', 'are', 'sure'],\n",
281 " ['i', 'understand']]},\n",
282 " {'pattern': ['?X', 'no', '?Y'],\n",
283 " 'responses': [['why', 'not'],\n",
284 " ['you', 'are', 'being', 'a', 'bit', 'negative'],\n",
285 " ['are', 'you', 'saying', '\"no\"', 'just', 'to', 'be', 'negative']]},\n",
286 " {'pattern': ['?X', 'someone', '?Y'],\n",
287 " 'responses': [['can', 'you', 'be', 'more', 'specific']]},\n",
288 " {'pattern': ['?X', 'everyone', '?Y'],\n",
289 " 'responses': [['surely', 'not', 'everyone'],\n",
290 " ['can', 'you', 'think', 'of', 'anyone', 'in', 'particular'],\n",
291 " ['who', 'for', 'example'],\n",
292 " ['you', 'are', 'thinking', 'of', 'a', 'special', 'person']]},\n",
293 " {'pattern': ['?X', 'always', '?Y'],\n",
294 " 'responses': [['can', 'you', 'think', 'of', 'a', 'specific', 'example'],\n",
296 " ['what', 'incident', 'are', 'you', 'thinking', 'of'],\n",
297 " ['really', '--', 'always']]},\n",
298 " {'pattern': ['?X', 'what', '?Y'],\n",
299 " 'responses': [['why', 'do', 'you', 'ask'],\n",
300 " ['does', 'that', 'question', 'interest', 'you'],\n",
301 " ['what', 'is', 'it', 'you', 'really', 'want', 'to', 'know'],\n",
302 " ['what', 'do', 'you', 'think'],\n",
303 " ['what', 'comes', 'to', 'your', 'mind', 'when', 'you', 'ask', 'that']]},\n",
304 " {'pattern': ['?X', 'perhaps', '?Y'],\n",
305 " 'responses': [['you', 'do', 'not', 'seem', 'quite', 'certain']]},\n",
306 " {'pattern': ['?X', 'are', '?Y'],\n",
307 " 'responses': [['do', 'you', 'think', 'they', 'might', 'not', 'be', '?Y'],\n",
308 " ['possibly', 'they', 'are', '?Y']]},\n",
309 " {'pattern': ['?X'],\n",
310 " 'responses': [['very', 'interesting'],\n",
311 " ['i', 'am', 'not', 'sure', 'i', 'understand', 'you', 'fully'],\n",
312 " ['what', 'does', 'that', 'suggest', 'to', 'you'],\n",
313 " ['please', 'continue'],\n",
325 "execution_count": 54,
327 "output_type": "execute_result"
331 "def read_rules(rules_file):\n",
332 " with open(rules_file) as f:\n",
333 " rules = [{'pattern': r['pattern'].split(),\n",
334 " 'responses': [t.split() for t in r['responses']]}\n",
335 " for r in json.load(f)]\n",
338 "all_rules = read_rules('rules.json')\n",
344 "execution_count": 55,
349 "output_type": "stream",
360 "execution_count": 55,
362 "output_type": "execute_result"
366 "i = input(\"> \")\n",
372 "execution_count": 56,
376 "def match(text, rule):\n",
377 " return all_matches([Match(text, rule, {})])"
382 "execution_count": 57,
386 "def all_matches(matches):\n",
389 " # print(matches, successes)\n",
390 " current = matches[0]\n",
391 " new_matches = []\n",
392 " if successful_match(current):\n",
393 " successes += [current.bindings]\n",
394 " elif current.rule:\n",
395 " new_matches = match_item(current.text, current.rule, current.bindings)\n",
396 " matches = matches[1:] + new_matches\n",
402 "execution_count": 58,
406 "def is_var(word):\n",
407 " return word[0] == '?'"
412 "execution_count": 59,
416 "def successful_match(match):\n",
417 " return match.text == [] and match.rule == []"
422 "execution_count": 60,
426 "def splits(item):\n",
427 " return [(item[:i], item[i:]) for i in range(len(item)+1)]"
432 "execution_count": 61,
446 "execution_count": 61,
448 "output_type": "execute_result"
457 "execution_count": 62,
461 "def match_item(text, rule, bindings):\n",
464 " if r0 in bindings:\n",
465 " # already seen this variable\n",
466 " if text[:len(bindings[r0])] == bindings[r0]:\n",
467 " return [Match(text[(len(bindings[r0])):], rule[1:], bindings)]\n",
471 " # not seen this variable yet\n",
473 " for pre, suf in splits(text):\n",
474 " new_bindings = bindings.copy()\n",
475 " new_bindings[r0] = pre\n",
476 " matches += [Match(suf, rule[1:], new_bindings)]\n",
478 " elif text and text[0] == r0:\n",
479 " return [Match(text[1:], rule[1:], bindings)]\n",
486 "execution_count": 63,
492 "[{'?X': ['fred'], '?Y': ['jane']}]"
495 "execution_count": 63,
497 "output_type": "execute_result"
501 "match('fred hello jane'.split(), '?X hello ?Y'.split())"
506 "execution_count": 64,
512 "[{'?X': [], '?Y': ['jane']}]"
515 "execution_count": 64,
517 "output_type": "execute_result"
521 "match('hello jane'.split(), '?X hello ?Y'.split())"
526 "execution_count": 65,
532 "[{'?X': ['fred'], '?Y': []}]"
535 "execution_count": 65,
537 "output_type": "execute_result"
541 "match('fred hello'.split(), '?X hello ?Y'.split())"
546 "execution_count": 66,
552 "[{'?X': [], '?Y': []}]"
555 "execution_count": 66,
557 "output_type": "execute_result"
561 "match('hello'.split(), '?X hello ?Y'.split())"
566 "execution_count": 67,
575 "execution_count": 67,
577 "output_type": "execute_result"
581 "match('fred jane'.split(), '?X hello ?Y'.split())"
586 "execution_count": 68,
595 "execution_count": 68,
597 "output_type": "execute_result"
601 "match('fred hello'.split(), '?X hello'.split())"
606 "execution_count": 69,
615 "execution_count": 69,
617 "output_type": "execute_result"
621 "match('hello fred'.split(), '?X hello'.split())"
626 "execution_count": 70,
632 "[{'?X': ['yes'], '?Y': ['rain']}]"
635 "execution_count": 70,
637 "output_type": "execute_result"
641 "match('yes i remember rain'.split(), '?X i remember ?Y'.split())"
646 "execution_count": 71,
652 "[{'?X': ['no'], '?Y': ['rain']}]"
655 "execution_count": 71,
657 "output_type": "execute_result"
661 "match('no i remember rain'.split(), '?X i remember ?Y'.split())"
666 "execution_count": 72,
675 "execution_count": 72,
677 "output_type": "execute_result"
681 "match(\"no remember rain\".split(), '?X i remember ?Y'.split())"
686 "execution_count": 73,
692 "[{'?X': [], '?Y': ['rain']}]"
695 "execution_count": 73,
697 "output_type": "execute_result"
701 "match(\"i remember rain\".split(), '?X i remember ?Y'.split())"
706 "execution_count": 74,
714 "[{'?X': [], '?Y': ['hello', 'there', 'hello']},\n",
715 " {'?X': ['hello'], '?Y': ['there']}]"
718 "execution_count": 74,
720 "output_type": "execute_result"
724 "match(\"hello there hello\".split(), '?X ?Y ?X'.split())"
729 "execution_count": 75,
737 "[{'?X': ['hello']}]"
740 "execution_count": 75,
742 "output_type": "execute_result"
746 "match(\"hello there hello\".split(), '?X there ?X'.split())"
751 "execution_count": 76,
757 "[{'?X': [], '?Y': ['hello', 'hello', 'hello']},\n",
758 " {'?X': ['hello'], '?Y': ['hello', 'hello']},\n",
759 " {'?X': ['hello', 'hello'], '?Y': ['hello']},\n",
760 " {'?X': ['hello', 'hello', 'hello'], '?Y': []}]"
763 "execution_count": 76,
765 "output_type": "execute_result"
769 "match(\"hello hello hello\".split(), '?X ?Y'.split())"
774 "execution_count": 77,
780 "[{'?X': [], '?Y': ['hello', 'hello', 'hello']},\n",
781 " {'?X': ['hello'], '?Y': ['hello']}]"
784 "execution_count": 77,
786 "output_type": "execute_result"
790 "match(\"hello hello hello\".split(), '?X ?Y ?X'.split())"
795 "execution_count": 78,
799 "def candidate_rules(rules, comment):\n",
800 " return [(rule, bindings) \n",
801 " for rule in rules \n",
802 " for bindings in match(comment, rule['pattern'])]"
807 "execution_count": 79,
815 "[({'pattern': ['?X', 'i', 'remember', '?Y'],\n",
816 " 'responses': [['do', 'you', 'often', 'think', 'of', '?Y'],\n",
826 " ['what', 'else', 'do', 'you', 'remember?'],\n",
827 " ['why', 'do', 'you', 'recall', '?Y', 'right', 'now'],\n",
837 " ['what', 'is', 'the', 'connection', 'between', 'me', 'and', '?Y']]},\n",
838 " {'?X': [], '?Y': ['rain']}),\n",
839 " ({'pattern': ['?X'],\n",
840 " 'responses': [['very', 'interesting'],\n",
841 " ['i', 'am', 'not', 'sure', 'i', 'understand', 'you', 'fully'],\n",
842 " ['what', 'does', 'that', 'suggest', 'to', 'you'],\n",
843 " ['please', 'continue'],\n",
853 " {'?X': ['i', 'remember', 'rain']})]"
856 "execution_count": 79,
858 "output_type": "execute_result"
862 "candidate_rules(all_rules, \"i remember rain\".split())"
867 "execution_count": 80,
871 "def fill(response, bindings):\n",
872 " filled_response = []\n",
873 " for w in response:\n",
875 " if w in bindings:\n",
876 " filled_response += bindings[w]\n",
878 " filled_response += ['MISSING']\n",
880 " filled_response += [w]\n",
881 " return filled_response"
886 "execution_count": 81,
892 "'do you often think of rain'"
895 "execution_count": 81,
897 "output_type": "execute_result"
901 "bs = match('yes i remember rain'.split(), '?X i remember ?Y'.split())[0]\n",
902 "fr = fill(['do', 'you', 'often', 'think', 'of', '?Y'], bs)\n",
908 "execution_count": 82,
912 "pronoun_swaps = {\n",
916 " 'mine': 'yours',\n",
920 "def pronoun_person_swap(bindings):\n",
921 " def swapped(words):\n",
923 " for w in words:\n",
924 " if w in pronoun_swaps:\n",
925 " sw += [pronoun_swaps[w]]\n",
930 " return {var: swapped(bindings[var]) for var in bindings}"
935 "execution_count": 83,
941 "{'?X': ['your', 'father'], '?Y': ['you', 'are', 'your', 'brother', 'keeper']}"
944 "execution_count": 83,
946 "output_type": "execute_result"
950 "pronoun_person_swap({'?X': ['my', 'father'], '?Y': 'i am my brother keeper'.split()})"
955 "execution_count": 84,
959 "def respond(rule, bindings):\n",
960 " return fill(random.choice(rule['responses']), bindings)"
965 "execution_count": 85,
971 "['what', 'is', 'the', 'connection', 'between', 'me', 'and', 'rain']"
974 "execution_count": 85,
976 "output_type": "execute_result"
980 "r0, b0 = candidate_rules(all_rules, \"i remember rain\".split())[0]\n",
986 "execution_count": 86,
990 "def eliza_loop():\n",
991 " print(\"Hello. I'm Eliza. What seems to be the problem?\")\n",
993 " c = input(\"> \")\n",
994 " if c.strip() in 'quit halt exit stop'.split(): break\n",
995 " comment = c.split()\n",
996 " rule, bindings = candidate_rules(all_rules, comment)[0]\n",
997 " swapped_bindings = pronoun_person_swap(bindings)\n",
998 " print(' '.join(respond(rule, swapped_bindings)))"
1002 "cell_type": "code",
1003 "execution_count": 87,
1010 "output_type": "stream",
1012 "Hello. I'm Eliza. What seems to be the problem?\n",
1014 "how do you do. please state your problem\n",
1015 "> i am sad about life\n",
1016 "i'm sure it's not pleasant to be sad\n",
1018 "very interesting\n",
1019 "> are you happy\n",
1020 "perhaps i am happy in your fantasies\n",
1030 "cell_type": "code",
1031 "execution_count": null,
1039 "display_name": "Python 3",
1040 "language": "python",
1044 "codemirror_mode": {
1048 "file_extension": ".py",
1049 "mimetype": "text/x-python",
1051 "nbconvert_exporter": "python",
1052 "pygments_lexer": "ipython3",