Started work on matching
authorNeil Smith <neil.git@njae.me.uk>
Tue, 16 Feb 2016 11:32:09 +0000 (11:32 +0000)
committerNeil Smith <neil.git@njae.me.uk>
Tue, 16 Feb 2016 11:32:09 +0000 (11:32 +0000)
eliza.ipynb [new file with mode: 0644]

diff --git a/eliza.ipynb b/eliza.ipynb
new file mode 100644 (file)
index 0000000..fe7c199
--- /dev/null
@@ -0,0 +1,288 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "import yaml"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[{'pattern': '?X hello ?Y',\n",
+       "  'responses': ['how do you do.  please state your problem']},\n",
+       " {'pattern': '?X computer ?Y',\n",
+       "  'responses': ['do computers worry you',\n",
+       "   'what do you think about machines',\n",
+       "   'why do you mention computers',\n",
+       "   'what do you think machines have to do with your problem']},\n",
+       " {'pattern': '?X name ?Y', 'responses': ['i am not interested in names']},\n",
+       " {'pattern': '?X sorry ?Y',\n",
+       "  'responses': [\"please don't apologize\",\n",
+       "   'apologies are not necessary',\n",
+       "   'what feelings do you have when you apologize']},\n",
+       " {'pattern': '?X i remember ?Y',\n",
+       "  'responses': ['do you often think of ?Y',\n",
+       "   'does thinking of ?Y bring anything else to mind',\n",
+       "   'what else do you remember?',\n",
+       "   'why do you recall ?Y right now',\n",
+       "   'what in this present situation reminds you of ?Y',\n",
+       "   'what is the connection between me and ?Y']},\n",
+       " {'pattern': '?X do you remember ?Y',\n",
+       "  'responses': ['did you think i would forget ?Y',\n",
+       "   'why do you think i should recall ?Y',\n",
+       "   'what about ?Y',\n",
+       "   'you mentioned ?Y']},\n",
+       " {'pattern': '?X if ?Y',\n",
+       "  'responses': ['do you really think it is likely that ?Y',\n",
+       "   'do you wish that ?Y',\n",
+       "   'what do you think about ?Y',\n",
+       "   'really -- if ?Y']},\n",
+       " {'pattern': '?X i dreamt ?Y',\n",
+       "  'responses': ['really -- ?Y',\n",
+       "   'have you ever fantasized ?Y while you were awake',\n",
+       "   'have you dreamt ?Y before']},\n",
+       " {'pattern': '?X i dreamed ?Y',\n",
+       "  'responses': ['really -- ?Y',\n",
+       "   'have you ever fantasized ?Y while you were awake',\n",
+       "   'have you dreamed ?Y before']},\n",
+       " {'pattern': '?X dream ?Y',\n",
+       "  'responses': ['what does this dream suggest to you',\n",
+       "   'do you dream often',\n",
+       "   'what persons appear in your dreams',\n",
+       "   \"don't you believe that dream has to do with your problem\"]},\n",
+       " {'pattern': '?X my mother ?Y',\n",
+       "  'responses': ['who else is in your family ?Y',\n",
+       "   'tell me more about your family']},\n",
+       " {'pattern': '?X my father ?Y',\n",
+       "  'responses': ['your father',\n",
+       "   'does he influence you strongly',\n",
+       "   'what else comes to mind when you think of your father']},\n",
+       " {'pattern': '?X i want ?Y',\n",
+       "  'responses': ['what would it mean if you got ?Y',\n",
+       "   'why do you want ?Y',\n",
+       "   'suppose you got ?Y soon']},\n",
+       " {'pattern': '?X i am glad ?Y',\n",
+       "  'responses': ['how have i helped you to be ?Y',\n",
+       "   'what makes you happy just now',\n",
+       "   'can you explain why you are suddenly ?Y']},\n",
+       " {'pattern': '?X i am happy ?Y',\n",
+       "  'responses': ['how have i helped you to be ?Y',\n",
+       "   'what makes you glad just now',\n",
+       "   'can you explain why you are suddenly ?Y']},\n",
+       " {'pattern': '?X i am sad ?Y',\n",
+       "  'responses': ['i am sorry to hear you are depressed',\n",
+       "   \"i'm sure it's not pleasant to be sad\"]},\n",
+       " {'pattern': '?X i am unhappy ?Y',\n",
+       "  'responses': ['i am sorry to hear you are depressed',\n",
+       "   \"i'm sure it's not pleasant to be unhappy\"]},\n",
+       " {'pattern': '?X are like ?Y',\n",
+       "  'responses': ['what resemblence do you see between ?X and ?Y']},\n",
+       " {'pattern': '?X is like ?Y',\n",
+       "  'responses': ['in what way is it that ?X is like ?Y',\n",
+       "   'what resemblence do you see',\n",
+       "   'could there really be some connection',\n",
+       "   'how']},\n",
+       " {'pattern': '?X alike ?Y',\n",
+       "  'responses': ['in what way', 'what similarities are there']},\n",
+       " {'pattern': '?X same ?Y', 'responses': ['what other connections do you see']},\n",
+       " {'pattern': '?X i was ?Y',\n",
+       "  'responses': ['were you really',\n",
+       "   'perhaps i already knew you were ?Y',\n",
+       "   'why do you tell me you were ?Y now']},\n",
+       " {'pattern': '?X was i ?Y',\n",
+       "  'responses': ['what if you were ?Y',\n",
+       "   'do you think you were ?Y',\n",
+       "   'what would it mean if you were ?Y']},\n",
+       " {'pattern': '?X i am ?Y',\n",
+       "  'responses': ['in what way are you ?Y', 'do you want to be ?Y']},\n",
+       " {'pattern': '?X am i ?Y',\n",
+       "  'responses': ['do you believe you are ?Y',\n",
+       "   'would you want to be ?Y',\n",
+       "   'you wish i would tell you you are ?Y',\n",
+       "   'what would it mean if you were ?Y']},\n",
+       " {'pattern': '?X am ?Y',\n",
+       "  'responses': ['why do you say \"am\"', \"i don't understand that\"]},\n",
+       " {'pattern': '?X are you ?Y',\n",
+       "  'responses': ['why are you interested in whether i am ?Y or not',\n",
+       "   \"would you prefer it if i weren't ?Y\",\n",
+       "   'perhaps i am ?Y in your fantasies']},\n",
+       " {'pattern': '?X you are ?Y', 'responses': ['what makes you think i am ?Y']},\n",
+       " {'pattern': '?X because ?Y',\n",
+       "  'responses': ['is that the real reason',\n",
+       "   'what other reason might there be',\n",
+       "   'does that reason seem to explain anything else']},\n",
+       " {'pattern': '?X were you ?Y',\n",
+       "  'responses': ['perhaps i was ?Y',\n",
+       "   'what do you think',\n",
+       "   'what if i had been ?Y']},\n",
+       " {'pattern': \"?X i can't ?Y\",\n",
+       "  'responses': ['maybe you could ?Y now', 'what if you could ?Y']},\n",
+       " {'pattern': '?X i feel ?Y', 'responses': ['do you often feel ?Y']},\n",
+       " {'pattern': '?X i felt ?Y', 'responses': ['what other feelings do you have']},\n",
+       " {'pattern': '?X i ?Y you ?Z',\n",
+       "  'responses': ['perhaps in your fantasies we ?Y each other']},\n",
+       " {'pattern': \"?X why don't you ?Y\",\n",
+       "  'responses': ['should you ?Y yourself',\n",
+       "   \"do you believe i don't ?Y\",\n",
+       "   'perhaps i will ?Y in good time']},\n",
+       " {'pattern': '?X yes ?Y',\n",
+       "  'responses': ['you seem quite positive', 'you are sure', 'i understand']},\n",
+       " {'pattern': '?X no ?Y',\n",
+       "  'responses': ['why not',\n",
+       "   'you are being a bit negative',\n",
+       "   'are you saying \"no\" just to be negative']},\n",
+       " {'pattern': '?X someone ?Y', 'responses': ['can you be more specific']},\n",
+       " {'pattern': '?X everyone ?Y',\n",
+       "  'responses': ['surely not everyone',\n",
+       "   'can you think of anyone in particular',\n",
+       "   'who for example',\n",
+       "   'you are thinking of a special person']},\n",
+       " {'pattern': '?X always ?Y',\n",
+       "  'responses': ['can you think of a specific example',\n",
+       "   'when',\n",
+       "   'what incident are you thinking of',\n",
+       "   'really -- always']},\n",
+       " {'pattern': '?X what ?Y',\n",
+       "  'responses': ['why do you ask',\n",
+       "   'does that question interest you',\n",
+       "   'what is it you really want to know',\n",
+       "   'what do you think',\n",
+       "   'what comes to your mind when you ask that']},\n",
+       " {'pattern': '?X perhaps ?Y', 'responses': ['you do not seem quite certain']},\n",
+       " {'pattern': '?X are ?Y',\n",
+       "  'responses': ['do you think they might not be ?Y', 'possibly they are ?Y']},\n",
+       " {'pattern': '?X',\n",
+       "  'responses': ['very interesting',\n",
+       "   'i am not sure i understand you fully',\n",
+       "   'what does that suggest to you',\n",
+       "   'please continue',\n",
+       "   'go on',\n",
+       "   'do you feel strongly about discussing such things']}]"
+      ]
+     },
+     "execution_count": 2,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "with open('rules.yaml') as f:\n",
+    "    rules = yaml.load(f)\n",
+    "rules"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "> tom dick harry\n"
+     ]
+    },
+    {
+     "data": {
+      "text/plain": [
+       "['tom', 'dick', 'harry']"
+      ]
+     },
+     "execution_count": 4,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "i = input(\"> \")\n",
+    "i.split()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [],
+   "source": [
+    "def match(text, rule, bindings=None):\n",
+    "    if text == [] and rule == []:\n",
+    "        return bindings\n",
+    "    elif text != [] and rule != []:\n",
+    "        if bindings == None:\n",
+    "            bindings = {}\n",
+    "        remaining_text, remaining_rule, new_bindings = match_first(text, rule, bindings)\n",
+    "        return match(remaining_text, remaining_rule, new_bindings)\n",
+    "    else:\n",
+    "        return None"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 7,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "def is_var(word):\n",
+    "    return word[0] == '?'"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "def match_first(text, rule, bindings):\n",
+    "    if not is_var(rule[0]):\n",
+    "        if text[0] == rule[0]:\n",
+    "            return text[1:], rule[1:], bindings\n",
+    "    else:\n",
+    "        # Unfinished\n",
+    "        pass"
+   ]
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Python 3",
+   "language": "python",
+   "name": "python3"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 3
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython3",
+   "version": "3.4.3+"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}