diff --git a/nlp.ipynb b/nlp.ipynb index fba613ef7..f95d8283c 100644 --- a/nlp.ipynb +++ b/nlp.ipynb @@ -14,15 +14,15 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "import nlp\n", "from nlp import Page, HITS\n", "from nlp import Lexicon, Rules, Grammar, ProbLexicon, ProbRules, ProbGrammar\n", - "from nlp import CYK_parse, Chart" + "from nlp import CYK_parse, Chart\n", + "\n", + "from notebook import psource" ] }, { @@ -188,40 +188,16 @@ "\n", "#### Non-Probabilistic\n", "\n", - "Execute the cells below to view the implemenations:" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "%psource Lexicon" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "%psource Rules" + "Execute the cell below to view the implemenations:" ] }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": true - }, + "execution_count": null, + "metadata": {}, "outputs": [], "source": [ - "%psource Grammar" + "psource(Lexicon, Rules, Grammar)" ] }, { @@ -240,36 +216,37 @@ "name": "stdout", "output_type": "stream", "text": [ - "Lexicon {'Verb': ['is', 'say', 'are'], 'RelPro': ['that', 'who', 'which'], 'Conjuction': ['and', 'or', 'but'], 'Digit': ['1', '2', '0'], 'Noun': ['robot', 'sheep', 'fence'], 'Pronoun': ['me', 'you', 'he'], 'Preposition': ['to', 'in', 'at'], 'Name': ['john', 'mary', 'peter'], 'Article': ['the', 'a', 'an'], 'Adjective': ['good', 'new', 'sad'], 'Adverb': ['here', 'lightly', 'now']}\n", + "Lexicon {'Adverb': ['here', 'lightly', 'now'], 'Verb': ['is', 'say', 'are'], 'Digit': ['1', '2', '0'], 'RelPro': ['that', 'who', 'which'], 'Conjuction': ['and', 'or', 'but'], 'Name': ['john', 'mary', 'peter'], 'Pronoun': ['me', 'you', 'he'], 'Article': ['the', 'a', 'an'], 'Noun': ['robot', 'sheep', 'fence'], 'Adjective': ['good', 'new', 'sad'], 'Preposition': ['to', 'in', 'at']}\n", "\n", - "Rules: {'RelClause': [['RelPro', 'VP']], 'S': [['NP', 'VP'], ['S', 'Conjuction', 'S']], 'PP': [['Preposition', 'NP']], 'VP': [['Verb'], ['VP', 'NP'], ['VP', 'Adjective'], ['VP', 'PP'], ['VP', 'Adverb']], 'NP': [['Pronoun'], ['Name'], ['Noun'], ['Article', 'Noun'], ['Article', 'Adjs', 'Noun'], ['Digit'], ['NP', 'PP'], ['NP', 'RelClause']], 'Adjs': [['Adjective'], ['Adjective', 'Adjs']]}\n" + "Rules: {'RelClause': [['RelPro', 'VP']], 'Adjs': [['Adjective'], ['Adjective', 'Adjs']], 'NP': [['Pronoun'], ['Name'], ['Noun'], ['Article', 'Noun'], ['Article', 'Adjs', 'Noun'], ['Digit'], ['NP', 'PP'], ['NP', 'RelClause']], 'S': [['NP', 'VP'], ['S', 'Conjuction', 'S']], 'VP': [['Verb'], ['VP', 'NP'], ['VP', 'Adjective'], ['VP', 'PP'], ['VP', 'Adverb']], 'PP': [['Preposition', 'NP']]}\n" ] } ], "source": [ "lexicon = Lexicon(\n", - " Verb=\"is | say | are\",\n", - " Noun=\"robot | sheep | fence\",\n", - " Adjective=\"good | new | sad\",\n", - " Adverb=\"here | lightly | now\",\n", - " Pronoun=\"me | you | he\",\n", - " RelPro=\"that | who | which\",\n", - " Name=\"john | mary | peter\",\n", - " Article=\"the | a | an\",\n", - " Preposition=\"to | in | at\",\n", - " Conjuction=\"and | or | but\",\n", - " Digit=\"1 | 2 | 0\"\n", + " Verb = \"is | say | are\",\n", + " Noun = \"robot | sheep | fence\",\n", + " Adjective = \"good | new | sad\",\n", + " Adverb = \"here | lightly | now\",\n", + " Pronoun = \"me | you | he\",\n", + " RelPro = \"that | who | which\",\n", + " Name = \"john | mary | peter\",\n", + " Article = \"the | a | an\",\n", + " Preposition = \"to | in | at\",\n", + " Conjuction = \"and | or | but\",\n", + " Digit = \"1 | 2 | 0\"\n", ")\n", "\n", "print(\"Lexicon\", lexicon)\n", "\n", "rules = Rules(\n", - " S=\"NP VP | S Conjuction S\",\n", - " NP=\"Pronoun | Name | Noun | Article Noun | Article Adjs Noun | Digit | NP PP | NP RelClause\",\n", - " VP=\"Verb | VP NP | VP Adjective | VP PP | VP Adverb\",\n", - " Adjs=\"Adjective | Adjective Adjs\",\n", - " PP=\"Preposition NP\",\n", - " RelClause=\"RelPro VP\"\n", + " S = \"NP VP | S Conjuction S\",\n", + " NP = \"Pronoun | Name | Noun | Article Noun \\\n", + " | Article Adjs Noun | Digit | NP PP | NP RelClause\",\n", + " VP = \"Verb | VP NP | VP Adjective | VP PP | VP Adverb\",\n", + " Adjs = \"Adjective | Adjective Adjs\",\n", + " PP = \"Preposition NP\",\n", + " RelClause = \"RelPro VP\"\n", ")\n", "\n", "print(\"\\nRules:\", rules)" @@ -316,36 +293,36 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ - "E_Chomsky = Grammar('E_Prob_Chomsky', # A Grammar in Chomsky Normal Form\n", + "E_Chomsky = Grammar(\"E_Prob_Chomsky\", # A Grammar in Chomsky Normal Form\n", " Rules(\n", - " S='NP VP',\n", - " NP='Article Noun | Adjective Noun',\n", - " VP='Verb NP | Verb Adjective',\n", + " S = \"NP VP\",\n", + " NP = \"Article Noun | Adjective Noun\",\n", + " VP = \"Verb NP | Verb Adjective\",\n", " ),\n", " Lexicon(\n", - " Article='the | a | an',\n", - " Noun='robot | sheep | fence',\n", - " Adjective='good | new | sad',\n", - " Verb='is | say | are'\n", + " Article = \"the | a | an\",\n", + " Noun = \"robot | sheep | fence\",\n", + " Adjective = \"good | new | sad\",\n", + " Verb = \"is | say | are\"\n", " ))" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[('NP', 'Article', 'Noun'), ('NP', 'Adjective', 'Noun'), ('VP', 'Verb', 'NP'), ('VP', 'Verb', 'Adjective'), ('S', 'NP', 'VP')]\n" + "[('S', 'NP', 'VP'), ('VP', 'Verb', 'NP'), ('VP', 'Verb', 'Adjective'), ('NP', 'Article', 'Noun'), ('NP', 'Adjective', 'Noun')]\n" ] } ], @@ -362,16 +339,16 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'the fence are or 1 say in john that is here lightly to peter lightly sad good at you good here me good at john in an fence to fence at robot lightly and a robot who is here sad sheep in fence in fence at he sad here lightly to 0 say and fence is good in a sad sheep in a fence but he say here'" + "'sheep that say here mary are the sheep at 2'" ] }, - "execution_count": 7, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -393,35 +370,11 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "%psource ProbLexicon" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "%psource ProbRules" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": true - }, + "execution_count": null, + "metadata": {}, "outputs": [], "source": [ - "%psource ProbGrammar" + "psource(ProbLexicon, ProbRules, ProbGrammar)" ] }, { @@ -433,44 +386,44 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Lexicon {'Verb': [('is', 0.5), ('say', 0.3), ('are', 0.2)], 'Adjective': [('good', 0.5), ('new', 0.2), ('sad', 0.3)], 'Preposition': [('to', 0.4), ('in', 0.3), ('at', 0.3)], 'Pronoun': [('me', 0.3), ('you', 0.4), ('he', 0.3)], 'Conjuction': [('and', 0.5), ('or', 0.2), ('but', 0.3)], 'Adverb': [('here', 0.6), ('lightly', 0.1), ('now', 0.3)], 'Article': [('the', 0.5), ('a', 0.25), ('an', 0.25)], 'Digit': [('0', 0.35), ('1', 0.35), ('2', 0.3)], 'RelPro': [('that', 0.5), ('who', 0.3), ('which', 0.2)], 'Noun': [('robot', 0.4), ('sheep', 0.4), ('fence', 0.2)], 'Name': [('john', 0.4), ('mary', 0.4), ('peter', 0.2)]}\n", + "Lexicon {'Noun': [('robot', 0.4), ('sheep', 0.4), ('fence', 0.2)], 'Name': [('john', 0.4), ('mary', 0.4), ('peter', 0.2)], 'Adverb': [('here', 0.6), ('lightly', 0.1), ('now', 0.3)], 'Digit': [('0', 0.35), ('1', 0.35), ('2', 0.3)], 'Adjective': [('good', 0.5), ('new', 0.2), ('sad', 0.3)], 'Pronoun': [('me', 0.3), ('you', 0.4), ('he', 0.3)], 'Article': [('the', 0.5), ('a', 0.25), ('an', 0.25)], 'Preposition': [('to', 0.4), ('in', 0.3), ('at', 0.3)], 'Verb': [('is', 0.5), ('say', 0.3), ('are', 0.2)], 'Conjuction': [('and', 0.5), ('or', 0.2), ('but', 0.3)], 'RelPro': [('that', 0.5), ('who', 0.3), ('which', 0.2)]}\n", "\n", - "Rules: {'RelClause': [(['RelPro', 'VP'], 1.0)], 'Adjs': [(['Adjective'], 0.5), (['Adjective', 'Adjs'], 0.5)], 'PP': [(['Preposition', 'NP'], 1.0)], 'NP': [(['Pronoun'], 0.2), (['Name'], 0.05), (['Noun'], 0.2), (['Article', 'Noun'], 0.15), (['Article', 'Adjs', 'Noun'], 0.1), (['Digit'], 0.05), (['NP', 'PP'], 0.15), (['NP', 'RelClause'], 0.1)], 'S': [(['NP', 'VP'], 0.6), (['S', 'Conjuction', 'S'], 0.4)], 'VP': [(['Verb'], 0.3), (['VP', 'NP'], 0.2), (['VP', 'Adjective'], 0.25), (['VP', 'PP'], 0.15), (['VP', 'Adverb'], 0.1)]}\n" + "Rules: {'S': [(['NP', 'VP'], 0.6), (['S', 'Conjuction', 'S'], 0.4)], 'RelClause': [(['RelPro', 'VP'], 1.0)], 'VP': [(['Verb'], 0.3), (['VP', 'NP'], 0.2), (['VP', 'Adjective'], 0.25), (['VP', 'PP'], 0.15), (['VP', 'Adverb'], 0.1)], 'Adjs': [(['Adjective'], 0.5), (['Adjective', 'Adjs'], 0.5)], 'PP': [(['Preposition', 'NP'], 1.0)], 'NP': [(['Pronoun'], 0.2), (['Name'], 0.05), (['Noun'], 0.2), (['Article', 'Noun'], 0.15), (['Article', 'Adjs', 'Noun'], 0.1), (['Digit'], 0.05), (['NP', 'PP'], 0.15), (['NP', 'RelClause'], 0.1)]}\n" ] } ], "source": [ "lexicon = ProbLexicon(\n", - " Verb=\"is [0.5] | say [0.3] | are [0.2]\",\n", - " Noun=\"robot [0.4] | sheep [0.4] | fence [0.2]\",\n", - " Adjective=\"good [0.5] | new [0.2] | sad [0.3]\",\n", - " Adverb=\"here [0.6] | lightly [0.1] | now [0.3]\",\n", - " Pronoun=\"me [0.3] | you [0.4] | he [0.3]\",\n", - " RelPro=\"that [0.5] | who [0.3] | which [0.2]\",\n", - " Name=\"john [0.4] | mary [0.4] | peter [0.2]\",\n", - " Article=\"the [0.5] | a [0.25] | an [0.25]\",\n", - " Preposition=\"to [0.4] | in [0.3] | at [0.3]\",\n", - " Conjuction=\"and [0.5] | or [0.2] | but [0.3]\",\n", - " Digit=\"0 [0.35] | 1 [0.35] | 2 [0.3]\"\n", + " Verb = \"is [0.5] | say [0.3] | are [0.2]\",\n", + " Noun = \"robot [0.4] | sheep [0.4] | fence [0.2]\",\n", + " Adjective = \"good [0.5] | new [0.2] | sad [0.3]\",\n", + " Adverb = \"here [0.6] | lightly [0.1] | now [0.3]\",\n", + " Pronoun = \"me [0.3] | you [0.4] | he [0.3]\",\n", + " RelPro = \"that [0.5] | who [0.3] | which [0.2]\",\n", + " Name = \"john [0.4] | mary [0.4] | peter [0.2]\",\n", + " Article = \"the [0.5] | a [0.25] | an [0.25]\",\n", + " Preposition = \"to [0.4] | in [0.3] | at [0.3]\",\n", + " Conjuction = \"and [0.5] | or [0.2] | but [0.3]\",\n", + " Digit = \"0 [0.35] | 1 [0.35] | 2 [0.3]\"\n", ")\n", "\n", "print(\"Lexicon\", lexicon)\n", "\n", "rules = ProbRules(\n", - " S=\"NP VP [0.6] | S Conjuction S [0.4]\",\n", - " NP=\"Pronoun [0.2] | Name [0.05] | Noun [0.2] | Article Noun [0.15] \\\n", + " S = \"NP VP [0.6] | S Conjuction S [0.4]\",\n", + " NP = \"Pronoun [0.2] | Name [0.05] | Noun [0.2] | Article Noun [0.15] \\\n", " | Article Adjs Noun [0.1] | Digit [0.05] | NP PP [0.15] | NP RelClause [0.1]\",\n", - " VP=\"Verb [0.3] | VP NP [0.2] | VP Adjective [0.25] | VP PP [0.15] | VP Adverb [0.1]\",\n", - " Adjs=\"Adjective [0.5] | Adjective Adjs [0.5]\",\n", - " PP=\"Preposition NP [1]\",\n", - " RelClause=\"RelPro VP [1]\"\n", + " VP = \"Verb [0.3] | VP NP [0.2] | VP Adjective [0.25] | VP PP [0.15] | VP Adverb [0.1]\",\n", + " Adjs = \"Adjective [0.5] | Adjective Adjs [0.5]\",\n", + " PP = \"Preposition NP [1]\",\n", + " RelClause = \"RelPro VP [1]\"\n", ")\n", "\n", "print(\"\\nRules:\", rules)" @@ -485,7 +438,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -515,36 +468,34 @@ }, { "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": true - }, + "execution_count": 9, + "metadata": {}, "outputs": [], "source": [ - "E_Prob_Chomsky = ProbGrammar('E_Prob_Chomsky', # A Probabilistic Grammar in CNF\n", + "E_Prob_Chomsky = ProbGrammar(\"E_Prob_Chomsky\", # A Probabilistic Grammar in CNF\n", " ProbRules(\n", - " S='NP VP [1]',\n", - " NP='Article Noun [0.6] | Adjective Noun [0.4]',\n", - " VP='Verb NP [0.5] | Verb Adjective [0.5]',\n", + " S = \"NP VP [1]\",\n", + " NP = \"Article Noun [0.6] | Adjective Noun [0.4]\",\n", + " VP = \"Verb NP [0.5] | Verb Adjective [0.5]\",\n", " ),\n", " ProbLexicon(\n", - " Article='the [0.5] | a [0.25] | an [0.25]',\n", - " Noun='robot [0.4] | sheep [0.4] | fence [0.2]',\n", - " Adjective='good [0.5] | new [0.2] | sad [0.3]',\n", - " Verb='is [0.5] | say [0.3] | are [0.2]'\n", + " Article = \"the [0.5] | a [0.25] | an [0.25]\",\n", + " Noun = \"robot [0.4] | sheep [0.4] | fence [0.2]\",\n", + " Adjective = \"good [0.5] | new [0.2] | sad [0.3]\",\n", + " Verb = \"is [0.5] | say [0.3] | are [0.2]\"\n", " ))" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[('NP', 'Article', 'Noun', 0.6), ('NP', 'Adjective', 'Noun', 0.4), ('VP', 'Verb', 'NP', 0.5), ('VP', 'Verb', 'Adjective', 0.5), ('S', 'NP', 'VP', 1.0)]\n" + "[('S', 'NP', 'VP', 1.0), ('VP', 'Verb', 'NP', 0.5), ('VP', 'Verb', 'Adjective', 0.5), ('NP', 'Article', 'Noun', 0.6), ('NP', 'Adjective', 'Noun', 0.4)]\n" ] } ], @@ -561,15 +512,15 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "a sheep say at the sad sad robot the good new sheep but john at fence are to me who is to robot the good new fence to robot who is mary in robot to 1 to an sad sad sad robot in fence lightly now at 1 at a new robot here good at john an robot in a fence in john the sheep here 2 to sheep good and you is but sheep is sad a good robot or the fence is robot good lightly at a good robot at 2 now good new or 1 say but he say or peter are in you who is lightly and fence say to john to an robot and sheep say and me is good or a robot is and sheep that say good he new 2 which are sad to an good fence that say 1 good good new lightly are good at he sad here but an sheep who say say sad now lightly sad an sad sad sheep or mary are but a fence at he in 1 say and 2 are\n", - "5.453065905143236e-226\n" + "an good sad sheep to 1 is\n", + "3.54375e-08\n" ] } ], @@ -620,13 +571,11 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, + "execution_count": null, + "metadata": {}, "outputs": [], "source": [ - "%psource HITS" + "psource(HITS)" ] }, { @@ -663,7 +612,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 12, "metadata": { "collapsed": true }, @@ -673,12 +622,12 @@ " X from his mom and a Y from his dad.\"\"\"\n", "testHTML2 = \"a mom and a dad\"\n", "\n", - "pA = Page(\"A\", [\"B\", \"C\", \"E\"], [\"D\"])\n", - "pB = Page(\"B\", [\"E\"], [\"A\", \"C\", \"D\"])\n", - "pC = Page(\"C\", [\"B\", \"E\"], [\"A\", \"D\"])\n", - "pD = Page(\"D\", [\"A\", \"B\", \"C\", \"E\"], [])\n", - "pE = Page(\"E\", [], [\"A\", \"B\", \"C\", \"D\", \"F\"])\n", - "pF = Page(\"F\", [\"E\"], [])\n", + "pA = Page('A', ['B', 'C', 'E'], ['D'])\n", + "pB = Page('B', ['E'], ['A', 'C', 'D'])\n", + "pC = Page('C', ['B', 'E'], ['A', 'D'])\n", + "pD = Page('D', ['A', 'B', 'C', 'E'], [])\n", + "pE = Page('E', [], ['A', 'B', 'C', 'D', 'F'])\n", + "pF = Page('F', ['E'], [])\n", "\n", "nlp.pageDict = {pA.address: pA, pB.address: pB, pC.address: pC,\n", " pD.address: pD, pE.address: pE, pF.address: pF}\n", @@ -699,14 +648,14 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "HITS('mammals')\n", - "page_list = [\"A\", \"B\", \"C\", \"D\", \"E\", \"F\"]\n", + "page_list = ['A', 'B', 'C', 'D', 'E', 'F']\n", "auth_list = [pA.authority, pB.authority, pC.authority, pD.authority, pE.authority, pF.authority]\n", "hub_list = [pA.hub, pB.hub, pC.hub, pD.hub, pE.hub, pF.hub]" ] @@ -720,7 +669,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -728,7 +677,7 @@ "output_type": "stream", "text": [ "A: total=0.7696163397038682, auth=0.5583254178509696, hub=0.2112909218528986\n", - "B: total=0.7795962360479534, auth=0.23657856688600404, hub=0.5430176691619494\n", + "B: total=0.7795962360479536, auth=0.23657856688600404, hub=0.5430176691619495\n", "C: total=0.8204496913590655, auth=0.4211098490570872, hub=0.3993398423019784\n", "D: total=0.6316647735856309, auth=0.6316647735856309, hub=0.0\n", "E: total=0.7078245882072104, auth=0.0, hub=0.7078245882072104\n", @@ -797,13 +746,11 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, + "execution_count": null, + "metadata": {}, "outputs": [], "source": [ - "%psource CYK_parse" + "psource(CYK_parse)" ] }, { @@ -824,23 +771,23 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 15, "metadata": { "collapsed": true }, "outputs": [], "source": [ - "E_Prob_Chomsky = ProbGrammar('E_Prob_Chomsky', # A Probabilistic Grammar in CNF\n", + "E_Prob_Chomsky = ProbGrammar(\"E_Prob_Chomsky\", # A Probabilistic Grammar in CNF\n", " ProbRules(\n", - " S='NP VP [1]',\n", - " NP='Article Noun [0.6] | Adjective Noun [0.4]',\n", - " VP='Verb NP [0.5] | Verb Adjective [0.5]',\n", + " S = \"NP VP [1]\",\n", + " NP = \"Article Noun [0.6] | Adjective Noun [0.4]\",\n", + " VP = \"Verb NP [0.5] | Verb Adjective [0.5]\",\n", " ),\n", " ProbLexicon(\n", - " Article='the [0.5] | a [0.25] | an [0.25]',\n", - " Noun='robot [0.4] | sheep [0.4] | fence [0.2]',\n", - " Adjective='good [0.5] | new [0.2] | sad [0.3]',\n", - " Verb='is [0.5] | say [0.3] | are [0.2]'\n", + " Article = \"the [0.5] | a [0.25] | an [0.25]\",\n", + " Noun = \"robot [0.4] | sheep [0.4] | fence [0.2]\",\n", + " Adjective = \"good [0.5] | new [0.2] | sad [0.3]\",\n", + " Verb = \"is [0.5] | say [0.3] | are [0.2]\"\n", " ))" ] }, @@ -853,14 +800,14 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "defaultdict(, {('Noun', 3, 1): 0.0, ('VP', 0, 3): 0.0, ('Article', 1, 1): 0.0, ('Adjective', 2, 1): 0.0, ('NP', 2, 2): 0.0, ('Adjective', 1, 3): 0.0, ('S', 0, 4): 0.015, ('NP', 1, 3): 0.0, ('VP', 1, 3): 0.0, ('VP', 3, 1): 0.0, ('Verb', 1, 1): 0.0, ('Adjective', 2, 2): 0.0, ('NP', 1, 1): 0.0, ('NP', 2, 1): 0.0, ('NP', 1, 2): 0.0, ('Adjective', 0, 3): 0.0, ('Noun', 2, 1): 0.0, ('Verb', 2, 1): 0.5, ('S', 2, 2): 0.0, ('Adjective', 0, 2): 0.0, ('Noun', 2, 2): 0.0, ('Adjective', 0, 1): 0.0, ('Adjective', 3, 1): 0.5, ('Article', 0, 3): 0.0, ('Article', 0, 1): 0.5, ('VP', 0, 2): 0.0, ('Article', 0, 2): 0.0, ('Noun', 1, 1): 0.4, ('VP', 1, 2): 0.0, ('VP', 0, 4): 0.0, ('Article', 1, 2): 0.0, ('S', 1, 3): 0.0, ('NP', 0, 1): 0.0, ('Verb', 0, 3): 0.0, ('Noun', 1, 3): 0.0, ('VP', 2, 2): 0.125, ('S', 1, 2): 0.0, ('NP', 0, 2): 0.12, ('Verb', 0, 2): 0.0, ('Noun', 1, 2): 0.0, ('VP', 2, 1): 0.0, ('NP', 0, 3): 0.0, ('Verb', 0, 1): 0.0, ('S', 0, 2): 0.0, ('VP', 1, 1): 0.0, ('NP', 0, 4): 0.0, ('Article', 2, 1): 0.0, ('NP', 3, 1): 0.0, ('Adjective', 1, 1): 0.0, ('S', 0, 3): 0.0, ('Adjective', 1, 2): 0.0, ('Verb', 1, 2): 0.0})\n" + "defaultdict(, {('Adjective', 1, 1): 0.0, ('NP', 0, 3): 0.0, ('Verb', 1, 1): 0.0, ('NP', 0, 2): 0.12, ('S', 1, 2): 0.0, ('Article', 2, 1): 0.0, ('NP', 3, 1): 0.0, ('S', 1, 3): 0.0, ('Adjective', 1, 3): 0.0, ('VP', 0, 4): 0.0, ('Article', 0, 3): 0.0, ('Adjective', 1, 2): 0.0, ('Verb', 1, 2): 0.0, ('Adjective', 0, 2): 0.0, ('Article', 0, 1): 0.5, ('VP', 1, 1): 0.0, ('Verb', 0, 2): 0.0, ('Adjective', 0, 3): 0.0, ('VP', 1, 2): 0.0, ('Verb', 0, 3): 0.0, ('NP', 2, 2): 0.0, ('S', 2, 2): 0.0, ('NP', 1, 3): 0.0, ('VP', 1, 3): 0.0, ('Adjective', 3, 1): 0.5, ('Adjective', 0, 1): 0.0, ('NP', 1, 2): 0.0, ('Verb', 0, 1): 0.0, ('S', 0, 3): 0.0, ('NP', 1, 1): 0.0, ('NP', 2, 1): 0.0, ('S', 0, 2): 0.0, ('Noun', 1, 2): 0.0, ('S', 0, 4): 0.015, ('Noun', 1, 3): 0.0, ('Noun', 3, 1): 0.0, ('Noun', 2, 2): 0.0, ('NP', 0, 4): 0.0, ('VP', 2, 2): 0.125, ('Noun', 2, 1): 0.0, ('Noun', 1, 1): 0.4, ('VP', 0, 3): 0.0, ('Article', 1, 2): 0.0, ('Article', 1, 1): 0.0, ('VP', 2, 1): 0.0, ('Adjective', 2, 1): 0.0, ('Verb', 2, 1): 0.5, ('Adjective', 2, 2): 0.0, ('VP', 3, 1): 0.0, ('NP', 0, 1): 0.0, ('VP', 0, 2): 0.0, ('Article', 0, 2): 0.0})\n" ] } ], @@ -881,14 +828,14 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "{('NP', 0, 2): 0.12, ('Adjective', 3, 1): 0.5, ('S', 0, 4): 0.015, ('Verb', 2, 1): 0.5, ('Article', 0, 1): 0.5, ('VP', 2, 2): 0.125, ('Noun', 1, 1): 0.4}\n" + "{('Noun', 1, 1): 0.4, ('VP', 2, 2): 0.125, ('Adjective', 3, 1): 0.5, ('S', 0, 4): 0.015, ('Article', 0, 1): 0.5, ('NP', 0, 2): 0.12, ('Verb', 2, 1): 0.5}\n" ] } ], @@ -960,6 +907,22 @@ "* `extender`: Extends edges given an edge (called `E`). If `E`'s non-terminal is the same as the expected transformation of another edge (let's call it `A`), add to the chart a new edge with the non-terminal of `A` and the transformations of `A` minus the non-terminal that matched with `E`'s non-terminal. For example, if an edge `E` has 'Article' as its non-terminal and is expecting no transformation, we need to see what edges it can extend. Let's examine the edge `N`. This expects a transformation of 'Noun Verb'. 'Noun' does not match with 'Article', so we move on. Another edge, `A`, expects a transformation of 'Article Noun' and has a non-terminal of 'NP'. We have a match! A new edge will be added with 'NP' as its non-terminal (the non-terminal of `A`) and 'Noun' as the expected transformation (the rest of the expected transformation of `A`)." ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can view the source code by running the cell below:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "psource(Chart)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -973,7 +936,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 18, "metadata": { "collapsed": true }, @@ -991,7 +954,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 19, "metadata": {}, "outputs": [ { @@ -1015,111 +978,9 @@ }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Chart: added [0, 0, 'S_', [], ['S']]\n", - "Chart: added [0, 0, 'S', [], ['NP', 'VP']]\n", - "Chart: added [0, 0, 'NP', [], ['Pronoun']]\n", - "Chart: added [0, 0, 'NP', [], ['Name']]\n", - "Chart: added [0, 0, 'NP', [], ['Noun']]\n", - "Chart: added [0, 0, 'NP', [], ['Article', 'Noun']]\n", - "Chart: added [0, 0, 'NP', [], ['Digit', 'Digit']]\n", - "Chart: added [0, 0, 'NP', [], ['NP', 'PP']]\n", - "Chart: added [0, 0, 'NP', [], ['NP', 'RelClause']]\n", - "Chart: added [0, 0, 'S', [], ['S', 'Conjunction', 'S']]\n", - "Chart: added [0, 1, 'NP', [('Article', 'the')], ['Noun']]\n", - "Chart: added [0, 2, 'NP', [('Article', 'the'), ('Noun', 'stench')], []]\n", - "Chart: added [0, 2, 'S', [[0, 2, 'NP', [('Article', 'the'), ('Noun', 'stench')], []]], ['VP']]\n", - "Chart: added [2, 2, 'VP', [], ['Verb']]\n", - "Chart: added [2, 2, 'VP', [], ['VP', 'NP']]\n", - "Chart: added [2, 2, 'VP', [], ['VP', 'Adjective']]\n", - "Chart: added [2, 2, 'VP', [], ['VP', 'PP']]\n", - "Chart: added [2, 2, 'VP', [], ['VP', 'Adverb']]\n", - "Chart: added [0, 2, 'NP', [[0, 2, 'NP', [('Article', 'the'), ('Noun', 'stench')], []]], ['PP']]\n", - "Chart: added [2, 2, 'PP', [], ['Preposition', 'NP']]\n", - "Chart: added [0, 2, 'NP', [[0, 2, 'NP', [('Article', 'the'), ('Noun', 'stench')], []]], ['RelClause']]\n", - "Chart: added [2, 2, 'RelClause', [], ['That', 'VP']]\n", - "Chart: added [2, 3, 'VP', [('Verb', 'is')], []]\n", - "Chart: added [0, 3, 'S', [[0, 2, 'NP', [('Article', 'the'), ('Noun', 'stench')], []], [2, 3, 'VP', [('Verb', 'is')], []]], []]\n", - "Chart: added [0, 3, 'S_', [[0, 3, 'S', [[0, 2, 'NP', [('Article', 'the'), ('Noun', 'stench')], []], [2, 3, 'VP', [('Verb', 'is')], []]], []]], []]\n", - "Chart: added [0, 3, 'S', [[0, 3, 'S', [[0, 2, 'NP', [('Article', 'the'), ('Noun', 'stench')], []], [2, 3, 'VP', [('Verb', 'is')], []]], []]], ['Conjunction', 'S']]\n", - "Chart: added [2, 3, 'VP', [[2, 3, 'VP', [('Verb', 'is')], []]], ['NP']]\n", - "Chart: added [3, 3, 'NP', [], ['Pronoun']]\n", - "Chart: added [3, 3, 'NP', [], ['Name']]\n", - "Chart: added [3, 3, 'NP', [], ['Noun']]\n", - "Chart: added [3, 3, 'NP', [], ['Article', 'Noun']]\n", - "Chart: added [3, 3, 'NP', [], ['Digit', 'Digit']]\n", - "Chart: added [3, 3, 'NP', [], ['NP', 'PP']]\n", - "Chart: added [3, 3, 'NP', [], ['NP', 'RelClause']]\n", - "Chart: added [2, 3, 'VP', [[2, 3, 'VP', [('Verb', 'is')], []]], ['Adjective']]\n", - "Chart: added [2, 3, 'VP', [[2, 3, 'VP', [('Verb', 'is')], []]], ['PP']]\n", - "Chart: added [3, 3, 'PP', [], ['Preposition', 'NP']]\n", - "Chart: added [2, 3, 'VP', [[2, 3, 'VP', [('Verb', 'is')], []]], ['Adverb']]\n", - "Chart: added [3, 4, 'PP', [('Preposition', 'in')], ['NP']]\n", - "Chart: added [4, 4, 'NP', [], ['Pronoun']]\n", - "Chart: added [4, 4, 'NP', [], ['Name']]\n", - "Chart: added [4, 4, 'NP', [], ['Noun']]\n", - "Chart: added [4, 4, 'NP', [], ['Article', 'Noun']]\n", - "Chart: added [4, 4, 'NP', [], ['Digit', 'Digit']]\n", - "Chart: added [4, 4, 'NP', [], ['NP', 'PP']]\n", - "Chart: added [4, 4, 'NP', [], ['NP', 'RelClause']]\n", - "Chart: added [4, 5, 'NP', [('Digit', '2')], ['Digit']]\n", - "Chart: added [4, 6, 'NP', [('Digit', '2'), ('Digit', '2')], []]\n", - "Chart: added [3, 6, 'PP', [('Preposition', 'in'), [4, 6, 'NP', [('Digit', '2'), ('Digit', '2')], []]], []]\n", - "Chart: added [2, 6, 'VP', [[2, 3, 'VP', [('Verb', 'is')], []], [3, 6, 'PP', [('Preposition', 'in'), [4, 6, 'NP', [('Digit', '2'), ('Digit', '2')], []]], []]], []]\n", - "Chart: added [0, 6, 'S', [[0, 2, 'NP', [('Article', 'the'), ('Noun', 'stench')], []], [2, 6, 'VP', [[2, 3, 'VP', [('Verb', 'is')], []], [3, 6, 'PP', [('Preposition', 'in'), [4, 6, 'NP', [('Digit', '2'), ('Digit', '2')], []]], []]], []]], []]\n", - "Chart: added [0, 6, 'S_', [[0, 6, 'S', [[0, 2, 'NP', [('Article', 'the'), ('Noun', 'stench')], []], [2, 6, 'VP', [[2, 3, 'VP', [('Verb', 'is')], []], [3, 6, 'PP', [('Preposition', 'in'), [4, 6, 'NP', [('Digit', '2'), ('Digit', '2')], []]], []]], []]], []]], []]\n", - "Chart: added [0, 6, 'S', [[0, 6, 'S', [[0, 2, 'NP', [('Article', 'the'), ('Noun', 'stench')], []], [2, 6, 'VP', [[2, 3, 'VP', [('Verb', 'is')], []], [3, 6, 'PP', [('Preposition', 'in'), [4, 6, 'NP', [('Digit', '2'), ('Digit', '2')], []]], []]], []]], []]], ['Conjunction', 'S']]\n", - "Chart: added [2, 6, 'VP', [[2, 6, 'VP', [[2, 3, 'VP', [('Verb', 'is')], []], [3, 6, 'PP', [('Preposition', 'in'), [4, 6, 'NP', [('Digit', '2'), ('Digit', '2')], []]], []]], []]], ['NP']]\n", - "Chart: added [6, 6, 'NP', [], ['Pronoun']]\n", - "Chart: added [6, 6, 'NP', [], ['Name']]\n", - "Chart: added [6, 6, 'NP', [], ['Noun']]\n", - "Chart: added [6, 6, 'NP', [], ['Article', 'Noun']]\n", - "Chart: added [6, 6, 'NP', [], ['Digit', 'Digit']]\n", - "Chart: added [6, 6, 'NP', [], ['NP', 'PP']]\n", - "Chart: added [6, 6, 'NP', [], ['NP', 'RelClause']]\n", - "Chart: added [2, 6, 'VP', [[2, 6, 'VP', [[2, 3, 'VP', [('Verb', 'is')], []], [3, 6, 'PP', [('Preposition', 'in'), [4, 6, 'NP', [('Digit', '2'), ('Digit', '2')], []]], []]], []]], ['Adjective']]\n", - "Chart: added [2, 6, 'VP', [[2, 6, 'VP', [[2, 3, 'VP', [('Verb', 'is')], []], [3, 6, 'PP', [('Preposition', 'in'), [4, 6, 'NP', [('Digit', '2'), ('Digit', '2')], []]], []]], []]], ['PP']]\n", - "Chart: added [6, 6, 'PP', [], ['Preposition', 'NP']]\n", - "Chart: added [2, 6, 'VP', [[2, 6, 'VP', [[2, 3, 'VP', [('Verb', 'is')], []], [3, 6, 'PP', [('Preposition', 'in'), [4, 6, 'NP', [('Digit', '2'), ('Digit', '2')], []]], []]], []]], ['Adverb']]\n", - "Chart: added [4, 6, 'NP', [[4, 6, 'NP', [('Digit', '2'), ('Digit', '2')], []]], ['PP']]\n", - "Chart: added [4, 6, 'NP', [[4, 6, 'NP', [('Digit', '2'), ('Digit', '2')], []]], ['RelClause']]\n", - "Chart: added [6, 6, 'RelClause', [], ['That', 'VP']]\n" - ] - }, - { - "data": { - "text/plain": [ - "[[0,\n", - " 6,\n", - " 'S',\n", - " [[0, 2, 'NP', [('Article', 'the'), ('Noun', 'stench')], []],\n", - " [2,\n", - " 6,\n", - " 'VP',\n", - " [[2, 3, 'VP', [('Verb', 'is')], []],\n", - " [3,\n", - " 6,\n", - " 'PP',\n", - " [('Preposition', 'in'),\n", - " [4, 6, 'NP', [('Digit', '2'), ('Digit', '2')], []]],\n", - " []]],\n", - " []]],\n", - " []]]" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "chart_trace = Chart(nlp.E0, trace=True)\n", "chart_trace.parses('the stench is in 2 2')" @@ -1134,7 +995,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 20, "metadata": {}, "outputs": [ { diff --git a/notebook.py b/notebook.py index bfc34651f..2df7b7721 100644 --- a/notebook.py +++ b/notebook.py @@ -1,26 +1,37 @@ -from IPython.display import HTML, display from utils import argmax, argmin from games import TicTacToe, alphabeta_player, random_player, Fig52Extended, infinity from logic import parse_definite_clause, standardize_variables, unify, subst from learning import DataSet -from mpl_toolkits.mplot3d import Axes3D +from IPython.display import HTML, Markdown, display +from collections import Counter + import matplotlib.pyplot as plt +import numpy as np import os, struct import array -import numpy as np -from collections import Counter +#______________________________________________________________________________ + + +def psource(*functions): + """Print the source code for the given function(s).""" + import inspect + + print('\n\n'.join(inspect.getsource(fn) for fn in functions)) + # ______________________________________________________________________________ def show_iris(i=0, j=1, k=2): - '''Plots the iris dataset in a 3D plot. + """Plots the iris dataset in a 3D plot. The three axes are given by i, j and k, - which correspond to three of the four iris features.''' + which correspond to three of the four iris features.""" + from mpl_toolkits.mplot3d import Axes3D + plt.rcParams.update(plt.rcParamsDefault) - + fig = plt.figure() ax = fig.add_subplot(111, projection='3d') @@ -158,11 +169,9 @@ class Canvas: """Inherit from this class to manage the HTML canvas element in jupyter notebooks. To create an object of this class any_name_xyz = Canvas("any_name_xyz") The first argument given must be the name of the object being created. - IPython must be able to refernce the variable name that is being passed. - """ + IPython must be able to refernce the variable name that is being passed.""" def __init__(self, varname, width=800, height=600, cid=None): - """""" self.name = varname self.cid = cid or varname self.width = width @@ -172,14 +181,14 @@ def __init__(self, varname, width=800, height=600, cid=None): display_html(self.html) def mouse_click(self, x, y): - "Override this method to handle mouse click at position (x, y)" + """Override this method to handle mouse click at position (x, y)""" raise NotImplementedError def mouse_move(self, x, y): raise NotImplementedError def execute(self, exec_str): - "Stores the command to be exectued to a list which is used later during update()" + """Stores the command to be exectued to a list which is used later during update()""" if not isinstance(exec_str, str): print("Invalid execution argument:", exec_str) self.alert("Recieved invalid execution command format") @@ -187,23 +196,23 @@ def execute(self, exec_str): self.exec_list.append(prefix + exec_str + ';') def fill(self, r, g, b): - "Changes the fill color to a color in rgb format" + """Changes the fill color to a color in rgb format""" self.execute("fill({0}, {1}, {2})".format(r, g, b)) def stroke(self, r, g, b): - "Changes the colors of line/strokes to rgb" + """Changes the colors of line/strokes to rgb""" self.execute("stroke({0}, {1}, {2})".format(r, g, b)) def strokeWidth(self, w): - "Changes the width of lines/strokes to 'w' pixels" + """Changes the width of lines/strokes to 'w' pixels""" self.execute("strokeWidth({0})".format(w)) def rect(self, x, y, w, h): - "Draw a rectangle with 'w' width, 'h' height and (x, y) as the top-left corner" + """Draw a rectangle with 'w' width, 'h' height and (x, y) as the top-left corner""" self.execute("rect({0}, {1}, {2}, {3})".format(x, y, w, h)) def rect_n(self, xn, yn, wn, hn): - "Similar to rect(), but the dimensions are normalized to fall between 0 and 1" + """Similar to rect(), but the dimensions are normalized to fall between 0 and 1""" x = round(xn * self.width) y = round(yn * self.height) w = round(wn * self.width) @@ -211,11 +220,11 @@ def rect_n(self, xn, yn, wn, hn): self.rect(x, y, w, h) def line(self, x1, y1, x2, y2): - "Draw a line from (x1, y1) to (x2, y2)" + """Draw a line from (x1, y1) to (x2, y2)""" self.execute("line({0}, {1}, {2}, {3})".format(x1, y1, x2, y2)) def line_n(self, x1n, y1n, x2n, y2n): - "Similar to line(), but the dimensions are normalized to fall between 0 and 1" + """Similar to line(), but the dimensions are normalized to fall between 0 and 1""" x1 = round(x1n * self.width) y1 = round(y1n * self.height) x2 = round(x2n * self.width) @@ -223,46 +232,45 @@ def line_n(self, x1n, y1n, x2n, y2n): self.line(x1, y1, x2, y2) def arc(self, x, y, r, start, stop): - "Draw an arc with (x, y) as centre, 'r' as radius from angles 'start' to 'stop'" + """Draw an arc with (x, y) as centre, 'r' as radius from angles 'start' to 'stop'""" self.execute("arc({0}, {1}, {2}, {3}, {4})".format(x, y, r, start, stop)) def arc_n(self, xn, yn, rn, start, stop): """Similar to arc(), but the dimensions are normalized to fall between 0 and 1 The normalizing factor for radius is selected between width and height by - seeing which is smaller - """ + seeing which is smaller.""" x = round(xn * self.width) y = round(yn * self.height) r = round(rn * min(self.width, self.height)) self.arc(x, y, r, start, stop) def clear(self): - "Clear the HTML canvas" + """Clear the HTML canvas""" self.execute("clear()") def font(self, font): - "Changes the font of text" + """Changes the font of text""" self.execute('font("{0}")'.format(font)) def text(self, txt, x, y, fill=True): - "Display a text at (x, y)" + """Display a text at (x, y)""" if fill: self.execute('fill_text("{0}", {1}, {2})'.format(txt, x, y)) else: self.execute('stroke_text("{0}", {1}, {2})'.format(txt, x, y)) def text_n(self, txt, xn, yn, fill=True): - "Similar to text(), but with normalized coordinates" + """Similar to text(), but with normalized coordinates""" x = round(xn * self.width) y = round(yn * self.height) self.text(txt, x, y, fill) def alert(self, message): - "Immediately display an alert" + """Immediately display an alert""" display_html(''.format(message)) def update(self): - "Execute the JS code to execute the commands queued by execute()" + """Execute the JS code to execute the commands queued by execute()""" exec_code = "" self.exec_list = [] display_html(exec_code) @@ -276,8 +284,7 @@ def display_html(html_string): class Canvas_TicTacToe(Canvas): - """Play a 3x3 TicTacToe game on HTML canvas - """ + """Play a 3x3 TicTacToe game on HTML canvas""" def __init__(self, varname, player_1='human', player_2='random', width=300, height=350, cid=None): valid_players = ('human', 'random', 'alphabeta') @@ -377,8 +384,7 @@ def draw_o(self, position): class Canvas_minimax(Canvas): - """Minimax for Fig52Extended on HTML canvas - """ + """Minimax for Fig52Extended on HTML canvas""" def __init__(self, varname, util_list, width=800, height=600, cid=None): Canvas.__init__(self, varname, width, height, cid) self.utils = {node:util for node, util in zip(range(13, 40), util_list)} @@ -501,8 +507,7 @@ def draw_graph(self): class Canvas_alphabeta(Canvas): - """Alpha-beta pruning for Fig52Extended on HTML canvas - """ + """Alpha-beta pruning for Fig52Extended on HTML canvas""" def __init__(self, varname, util_list, width=800, height=600, cid=None): Canvas.__init__(self, varname, width, height, cid) self.utils = {node:util for node, util in zip(range(13, 40), util_list)} @@ -671,8 +676,7 @@ def draw_graph(self): class Canvas_fol_bc_ask(Canvas): - """fol_bc_ask() on HTML canvas - """ + """fol_bc_ask() on HTML canvas""" def __init__(self, varname, kb, query, width=800, height=600, cid=None): Canvas.__init__(self, varname, width, height, cid) self.kb = kb