From b7e79018b765aec3ea6bf5f9fc685222aefadc16 Mon Sep 17 00:00:00 2001 From: keimperademaker Date: Tue, 3 Sep 2024 16:56:01 +0200 Subject: [PATCH 1/4] Started work on assignment 1 --- .../Propositional Logic and Data .ipynb | 290 +++++++++++++++--- .../__pycache__/logic.cpython-310.pyc | Bin 0 -> 16720 bytes 2 files changed, 240 insertions(+), 50 deletions(-) create mode 100644 Assignments/Assignment_1/__pycache__/logic.cpython-310.pyc diff --git a/Assignments/Assignment_1/Propositional Logic and Data .ipynb b/Assignments/Assignment_1/Propositional Logic and Data .ipynb index 58cf233..5d979ea 100644 --- a/Assignments/Assignment_1/Propositional Logic and Data .ipynb +++ b/Assignments/Assignment_1/Propositional Logic and Data .ipynb @@ -12,9 +12,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "- YOUR NAME: \n", + "- YOUR NAME: Keimpe Rademaker\n", "\n", - "- YOUR VUNetID: \n", + "- YOUR VUNetID: kra225\n", "\n", "*(If you do not provide your NAME and VUNetID we will not accept your submission.)*" ] @@ -108,7 +108,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -118,9 +118,18 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "/Users/keimperademaker/Documents/GitHub/knowledge-data-vu/Assignments/Assignment_1/PLknowledgebase.txt\n", + "['(~R & P) >> Q\\n', '~(((P | ~Q) >> R) >> (P & R)) \\n', '~(P >> Q) | (R >> P) \\n', '(P >> Q) | (Q >> ~P)']\n" + ] + } + ], "source": [ "fileDir = os.path.dirname(os.path.realpath('__file__'))\n", "filename = os.path.join(fileDir, 'PLknowledgebase.txt')\n", @@ -133,9 +142,36 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "(¬R ^ P) => Q\n", + "False\n", + "\n", + "¬R ^ P\n", + "\n", + "¬(((P v ¬Q) => R) => (P ^ R))\n", + "False\n", + "\n", + "((P v ¬Q) => R) => (P ^ R)\n", + "\n", + "¬(P => Q) v (R => P)\n", + "False\n", + "\n", + "¬(P => Q)\n", + "\n", + "(P => Q) v (Q => ¬P)\n", + "True\n", + "\n", + "P => Q\n" + ] + } + ], "source": [ "P, Q, R = vars('P', 'Q', 'R')\n", "\n", @@ -161,7 +197,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ @@ -172,6 +208,20 @@ " string = \"OR(\" + prefix(child1) + \",\" + prefix(child2) + \")\";\n", " return string\n", " ## Fill in the remaining cases (hint: not every formula has the same number of children) \n", + " elif type(s) is And:\n", + " child1 = s.children[0]\n", + " child2 = s.children[1]\n", + " string = \"AND(\" + prefix(child1) + \",\" + prefix(child2) + \")\";\n", + " return string\n", + " elif type(s) is Not:\n", + " child = s.children[0]\n", + " string = \"NEG(\" + prefix(child) + \")\";\n", + " return string\n", + " elif type(s) is Implies:\n", + " child1 = s.children[0]\n", + " child2 = s.children[1]\n", + " string = \"IMP(\" + prefix(child1) + \",\" + prefix(child2) + \")\";\n", + " return string\n", " else:\n", " return str(s)" ] @@ -185,9 +235,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(¬R ^ P) => Q == IMP(AND(NEG(R),P),Q)\n", + "¬(((P v ¬Q) => R) => (P ^ R)) == NEG(IMP(IMP(OR(P,NEG(Q)),R),AND(P,R)))\n", + "¬(P => Q) v (R => P) == OR(NEG(IMP(P,Q)),IMP(R,P))\n", + "(P => Q) v (Q => ¬P) == OR(IMP(P,Q),IMP(Q,NEG(P)))\n" + ] + } + ], "source": [ "for line in content:\n", " formula = eval(line.strip())\n", @@ -220,29 +281,60 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "def evaluate(s, p, q, r):\n", + "\n", + " ## Evaluating for disjunction\n", " if type(s) is Or:\n", " child1 = s.children[0]\n", " child2 = s.children[1]\n", " if (evaluate(child1,p,q,r) is True) or (evaluate(child2,p,q,r) is True):\n", " return True\n", - " ## Fill in the remaining cases\n", + " else:\n", + " return False\n", + " \n", + " ## Evaluating for conjunction\n", + " elif type(s) is And:\n", + " child1 = s.children[0]\n", + " child2 = s.children[1]\n", + " if (evaluate(child1,p,q,r) is True) and (evaluate(child2,p,q,r) is True):\n", + " return True\n", + " else:\n", + " return False\n", + " \n", + " ## Evaluating for negation \n", + " elif type(s) is Not:\n", + " child = s.children[0]\n", + " if evaluate(child,p,q,r) is False:\n", + " return True\n", + " else:\n", + " return False\n", " \n", + " ## Evaluating for implication\n", " elif type(s) is Implies:\n", " child1 = s.children[0]\n", " child2 = s.children[1]\n", " if (evaluate(child1,p,q,r) is False) and (evaluate(child2,p,q,r) is True):\n", " return True \n", - " ## Fill in the remaining cases\n", - " ## Fill in the remaining cases\n", + " elif (evaluate(child1,p,q,r) is True) and (evaluate(child2,p,q,r) is True):\n", + " return True\n", + " elif (evaluate(child1,p,q,r) is False) and (evaluate(child2,p,q,r) is False):\n", + " return True\n", + " else:\n", + " return False\n", + "\n", + " ## Evaluating for variables\n", " elif type(s) is Variable: \n", " if str(s) == 'P':\n", " return p\n", - " ## Fill in the base cases: \n", + " elif str(s) == 'Q':\n", + " return q\n", + " elif str(s) == 'R':\n", + " return r\n", + " \n", " else:\n", " print(\"Something went wrong\") " ] @@ -256,9 +348,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(~R & P) >> Q True\n", + "~(((P | ~Q) >> R) >> (P & R)) False\n", + "~(P >> Q) | (R >> P) True\n", + "(P >> Q) | (Q >> ~P) True\n" + ] + } + ], "source": [ "for l in content:\n", " formula = l.strip()\n", @@ -288,12 +391,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "def tautology(s):\n", - " ## Your code here\n", + "\n", + " ## Evaluating for all possible truth values of P, Q, R\n", + " for p in [True, False]:\n", + " for q in [True, False]:\n", + " for r in [True, False]:\n", + " if evaluate(s,p,q,r) is False:\n", + " return False\n", + " return True\n", + "\n", " pass" ] }, @@ -306,9 +417,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Formula Tautology\n", + "(~R & P) >> Q False\n", + "~(((P | ~Q) >> R) >> (P & R)) False\n", + "~(P >> Q) | (R >> P) False\n", + "(P >> Q) | (Q >> ~P) True\n" + ] + } + ], "source": [ "print(\"Formula\" + 23*\" \" + \"Tautology\")\n", "for l in content:\n", @@ -350,7 +473,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 52, "metadata": {}, "outputs": [], "source": [ @@ -370,10 +493,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 56, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('s', 'p', 'o'), ('s', 'p1', 'o2'), ('s2', 'p2', 'o')]\n", + "[('Netherlands', 'name', '\"The Netherlands\"'), ('Netherlands', 'has_capital', 'Amsterdam'), ('Amsterdam', 'type', 'Capital'), ('Amsterdam', 'has_population', '\"921.402\"')]\n", + "[('Netherlands', 'name', '\"The Netherlands\"'), ('Netherlands', 'has_capital', 'Amsterdam'), ('Amsterdam', 'type', 'Capital'), ('Amsterdam', 'has_population', '\"921.402\"'), ('Capital', 'type', 'City'), ('Netherlands', 'neighbours', 'Belgium'), ('Netherlands', 'type', 'Country')]\n" + ] + } + ], + "source": [ + "## Printing the content of the file\n", + "for g in content:\n", + " print(g)" + ] }, { "cell_type": "markdown", @@ -395,23 +532,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 79, "metadata": {}, "outputs": [], "source": [ "def as_ntriple(triple):\n", - " ## first write a function that transforms an arbitrary triple ...\n", - " pass" + " ## Converting the triple to N-Triple format\n", + " a, b, c = triple\n", + " return f\"<{a}> <{b}> <{c}> .\"" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 80, "metadata": {}, "outputs": [], "source": [ "def as_ntriples(graph):\n", " ## ... and then loops through all triples in the graph (hint: you can reuse the function above)\n", + " return ([as_ntriple(triple) for triple in graph])\n", " pass" ] }, @@ -424,9 +563,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 81, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['

.', ' .', ' .']\n", + "[' <\"The Netherlands\"> .', ' .', ' .', ' <\"921.402\"> .']\n", + "[' <\"The Netherlands\"> .', ' .', ' .', ' <\"921.402\"> .', ' .', ' .', ' .']\n" + ] + } + ], "source": [ "for graph in content:\n", " print(as_ntriples(graph))" @@ -455,12 +604,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 94, "metadata": {}, "outputs": [], "source": [ "def as_ntriple(triple):\n", - " ## first write a function that transforms the triples ...\n", + " ## Converting the triple to N-Triple format, removing angle brackets if the object is a string\n", + " a, b, c = triple\n", + " if isinstance(c, str) and ('\"' in c or \"'\" in c):\n", + " return f\"<{a}> <{b}> {c} .\"\n", + " else:\n", + " return f\"<{a}> <{b}> <{c}> .\"\n", " pass" ] }, @@ -473,9 +627,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 95, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['

.', ' .', ' .']\n", + "[' \"The Netherlands\" .', ' .', ' .', ' \"921.402\" .']\n", + "[' \"The Netherlands\" .', ' .', ' .', ' \"921.402\" .', ' .', ' .', ' .']\n" + ] + } + ], "source": [ "for graph in content:\n", " print(as_ntriples(graph))" @@ -506,12 +670,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 136, "metadata": {}, "outputs": [], "source": [ "def tripleEntailedBy():\n", - " ## Your code here\n", + " ## You need to open the file and check for all triples whether it is entailed by the knowledge graphs in *knowledgegraph.txt* (which is already opened). Hint: you can use *eval* to convert a raw string to a list.\n", + " fileDir = os.path.dirname(os.path.realpath('__file__'))\n", + " filename = os.path.join(fileDir, 'entailedTriples.txt')\n", + " with open(filename) as file:\n", + " entailedTriples = [eval(t) for t in file.readlines()]\n", + " print(entailedTriples)\n", + " ## Then, you need to check for each triple whether it is entailed by the knowledge graph\n", + " \n", + " for triple in entailedTriples:\n", + " for graph in content:\n", + " if triple in graph:\n", + " print(str(triple) + \" is entailed by the knowledge graph\")\n", + " break\n", + " else:\n", + " print(str(triple) + \" is not entailed by the knowledge graph\")\n", + "\n", " pass" ] }, @@ -524,15 +703,26 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 137, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('s2', 'p2', 'o'), ('Netherlands', 'name', '\"The Netherlands\"')]\n", + "('s2', 'p2', 'o') is entailed by the knowledge graph\n", + "('Netherlands', 'name', '\"The Netherlands\"') is entailed by the knowledge graph\n" + ] + } + ], "source": [ "filename = os.path.join(fileDir, 'entailedTriples.txt')\n", "with open(filename) as file:\n", " triples = [eval(t) for t in file.readlines()]\n", - "\n", - "# your code here" + " \n", + "# your code here\n", + "tripleEntailedBy()" ] }, { @@ -623,7 +813,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.3" + "version": "3.10.13" } }, "nbformat": 4, diff --git a/Assignments/Assignment_1/__pycache__/logic.cpython-310.pyc b/Assignments/Assignment_1/__pycache__/logic.cpython-310.pyc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U4%LPdbiE|0ENk@(av~ zkYpcPo(aP!&>V{TXOZzAN--_H zCkEh}s+BXBWW^;i4maD|^b-0nmI#}SxS%h%&>1ZQOOD2Q?;smXf7X6 z%O}Rh>56!1L1v}>1atI8<|ytwb6g~o#THpl&}%FTD`FxfXH?^~*| Date: Tue, 3 Sep 2024 17:26:23 +0200 Subject: [PATCH 2/4] Task 5 complete --- .../Propositional Logic and Data .ipynb | 80 +++++++++--------- .../__pycache__/logic.cpython-312.pyc | Bin 0 -> 24717 bytes 2 files changed, 38 insertions(+), 42 deletions(-) create mode 100644 Assignments/Assignment_1/__pycache__/logic.cpython-312.pyc diff --git a/Assignments/Assignment_1/Propositional Logic and Data .ipynb b/Assignments/Assignment_1/Propositional Logic and Data .ipynb index 5d979ea..c5b38f0 100644 --- a/Assignments/Assignment_1/Propositional Logic and Data .ipynb +++ b/Assignments/Assignment_1/Propositional Logic and Data .ipynb @@ -108,7 +108,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -118,14 +118,14 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "/Users/keimperademaker/Documents/GitHub/knowledge-data-vu/Assignments/Assignment_1/PLknowledgebase.txt\n", + "C:\\Users\\keimp\\Documents\\GitHub\\knowledge-data-vu\\Assignments\\Assignment_1\\PLknowledgebase.txt\n", "['(~R & P) >> Q\\n', '~(((P | ~Q) >> R) >> (P & R)) \\n', '~(P >> Q) | (R >> P) \\n', '(P >> Q) | (Q >> ~P)']\n" ] } @@ -142,7 +142,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -197,7 +197,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -235,7 +235,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -281,7 +281,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -348,7 +348,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -391,7 +391,7 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ @@ -417,7 +417,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -473,7 +473,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ @@ -493,7 +493,7 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -532,7 +532,7 @@ }, { "cell_type": "code", - "execution_count": 79, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ @@ -544,7 +544,7 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ @@ -563,7 +563,7 @@ }, { "cell_type": "code", - "execution_count": 81, + "execution_count": 18, "metadata": {}, "outputs": [ { @@ -604,7 +604,7 @@ }, { "cell_type": "code", - "execution_count": 94, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ @@ -627,7 +627,7 @@ }, { "cell_type": "code", - "execution_count": 95, + "execution_count": 20, "metadata": {}, "outputs": [ { @@ -670,26 +670,19 @@ }, { "cell_type": "code", - "execution_count": 136, + "execution_count": 49, "metadata": {}, "outputs": [], "source": [ - "def tripleEntailedBy():\n", - " ## You need to open the file and check for all triples whether it is entailed by the knowledge graphs in *knowledgegraph.txt* (which is already opened). Hint: you can use *eval* to convert a raw string to a list.\n", - " fileDir = os.path.dirname(os.path.realpath('__file__'))\n", - " filename = os.path.join(fileDir, 'entailedTriples.txt')\n", - " with open(filename) as file:\n", - " entailedTriples = [eval(t) for t in file.readlines()]\n", - " print(entailedTriples)\n", - " ## Then, you need to check for each triple whether it is entailed by the knowledge graph\n", - " \n", - " for triple in entailedTriples:\n", - " for graph in content:\n", - " if triple in graph:\n", - " print(str(triple) + \" is entailed by the knowledge graph\")\n", - " break\n", - " else:\n", - " print(str(triple) + \" is not entailed by the knowledge graph\")\n", + "def tripleEntailedBy(triples):\n", + " entailedTriples = triples\n", + " \n", + " for graph in content:\n", + " for entailedTriple in entailedTriples:\n", + " if entailedTriple in graph:\n", + " print(str(graph) + \" entails \" + str(entailedTriple))\n", + " else:\n", + " print(str(graph) + \" does NOT entail \" + str(entailedTriple))\n", "\n", " pass" ] @@ -703,16 +696,19 @@ }, { "cell_type": "code", - "execution_count": 137, + "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[('s2', 'p2', 'o'), ('Netherlands', 'name', '\"The Netherlands\"')]\n", - "('s2', 'p2', 'o') is entailed by the knowledge graph\n", - "('Netherlands', 'name', '\"The Netherlands\"') is entailed by the knowledge graph\n" + "[('s', 'p', 'o'), ('s', 'p1', 'o2'), ('s2', 'p2', 'o')] entails ('s2', 'p2', 'o')\n", + "[('s', 'p', 'o'), ('s', 'p1', 'o2'), ('s2', 'p2', 'o')] does NOT entail ('Netherlands', 'name', '\"The Netherlands\"')\n", + "[('Netherlands', 'name', '\"The Netherlands\"'), ('Netherlands', 'has_capital', 'Amsterdam'), ('Amsterdam', 'type', 'Capital'), ('Amsterdam', 'has_population', '\"921.402\"')] does NOT entail ('s2', 'p2', 'o')\n", + "[('Netherlands', 'name', '\"The Netherlands\"'), ('Netherlands', 'has_capital', 'Amsterdam'), ('Amsterdam', 'type', 'Capital'), ('Amsterdam', 'has_population', '\"921.402\"')] entails ('Netherlands', 'name', '\"The Netherlands\"')\n", + "[('Netherlands', 'name', '\"The Netherlands\"'), ('Netherlands', 'has_capital', 'Amsterdam'), ('Amsterdam', 'type', 'Capital'), ('Amsterdam', 'has_population', '\"921.402\"'), ('Capital', 'type', 'City'), ('Netherlands', 'neighbours', 'Belgium'), ('Netherlands', 'type', 'Country')] does NOT entail ('s2', 'p2', 'o')\n", + "[('Netherlands', 'name', '\"The Netherlands\"'), ('Netherlands', 'has_capital', 'Amsterdam'), ('Amsterdam', 'type', 'Capital'), ('Amsterdam', 'has_population', '\"921.402\"'), ('Capital', 'type', 'City'), ('Netherlands', 'neighbours', 'Belgium'), ('Netherlands', 'type', 'Country')] entails ('Netherlands', 'name', '\"The Netherlands\"')\n" ] } ], @@ -722,7 +718,7 @@ " triples = [eval(t) for t in file.readlines()]\n", " \n", "# your code here\n", - "tripleEntailedBy()" + "tripleEntailedBy(triples)" ] }, { @@ -755,7 +751,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, "outputs": [], "source": [ @@ -773,7 +769,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, "outputs": [], "source": [ @@ -813,7 +809,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.13" + "version": "3.12.3" } }, "nbformat": 4, diff --git a/Assignments/Assignment_1/__pycache__/logic.cpython-312.pyc b/Assignments/Assignment_1/__pycache__/logic.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f00881c4a0839dc3c5f38075aee8be04fce288f6 GIT binary patch literal 24717 zcmdsfeRLbwb?3|gATa>=0TTQYMGB-y$q=m%ilSx7GA-)MlI>6yNjZ)ziKaM^1ezbx z0F+IL@FI>Ak(L`#jUCZxdQ2y7O;t`qH||<)(qpBkdycp5o&o|TFsOFdXY12#`j<;; zPa^L*yZgIuJ}>}=vf^y_qjSiMH*em1bKkx9-Fx4C@6BHq6gW6sYa5csuh($V&pCAXxw{k`>S@*#K<<_b4ajz067ZR|KW@p*)snN1o`< zyJAsu^I5I~xlTi_o#ncaTVTi)S#BY6iwwCAmRpQmw;|Wba!Zg~YRGl5Tn}=~47mj? zw;Z_@hTKAyTZ!B%y$?mB%Y4;+pn`toAw_*zd<@_c7t)`Nal5z+e4l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1kpw^M`l1#BnCa=GA5mJ2eikZ(@3erX6t;apDaNd;NNN6) z*RWr&B15*)d-llN3GPNEGR|eQp${VOMQ&E^nXQ|ud-MJ`?w^)t>TfJtd%bc`Onf1B z@N3C~Qv9HlJUARbI2=1A$ATk?gJ~U(L6um(^b literal 0 HcmV?d00001 From 2107310f8193d44c9fb4d764dd5d1558214e966f Mon Sep 17 00:00:00 2001 From: keimperademaker Date: Tue, 3 Sep 2024 17:45:54 +0200 Subject: [PATCH 3/4] Task 6 complete --- .../Propositional Logic and Data .ipynb | 70 ++++++++++++++----- 1 file changed, 54 insertions(+), 16 deletions(-) diff --git a/Assignments/Assignment_1/Propositional Logic and Data .ipynb b/Assignments/Assignment_1/Propositional Logic and Data .ipynb index c5b38f0..d28439e 100644 --- a/Assignments/Assignment_1/Propositional Logic and Data .ipynb +++ b/Assignments/Assignment_1/Propositional Logic and Data .ipynb @@ -609,7 +609,8 @@ "outputs": [], "source": [ "def as_ntriple(triple):\n", - " ## Converting the triple to N-Triple format, removing angle brackets if the object is a string\n", + "\n", + " ## Converting the triple to N-Triple format, removing angle brackets if the object is a string, and if the object is enclosed in quotation marks\n", " a, b, c = triple\n", " if isinstance(c, str) and ('\"' in c or \"'\" in c):\n", " return f\"<{a}> <{b}> {c} .\"\n", @@ -670,20 +671,21 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 57, "metadata": {}, "outputs": [], "source": [ - "def tripleEntailedBy(triples):\n", - " entailedTriples = triples\n", + "def tripleEntailedBy(entailedTriples):\n", " \n", - " for graph in content:\n", + " ## Looping through all knowledge graphs in the content, as well as all entailed triples\n", + " for knowledgeGraph in content:\n", " for entailedTriple in entailedTriples:\n", - " if entailedTriple in graph:\n", - " print(str(graph) + \" entails \" + str(entailedTriple))\n", - " else:\n", - " print(str(graph) + \" does NOT entail \" + str(entailedTriple))\n", "\n", + " ## Checking if the entailed triple is in the knowledge graph\n", + " if entailedTriple in knowledgeGraph:\n", + " print(str(knowledgeGraph) + \" entails \" + str(entailedTriple))\n", + " else:\n", + " print(str(knowledgeGraph) + \" does NOT entail \" + str(entailedTriple))\n", " pass" ] }, @@ -696,7 +698,7 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 58, "metadata": {}, "outputs": [ { @@ -751,12 +753,28 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 55, "metadata": {}, "outputs": [], "source": [ - "## Your code here\n", - "def graphEntailedBy():\n", + "def graphEntailedBy(entailedGraphs):\n", + "\n", + " ## Converting the triples to N-Triples format, replacing instances of 'a' with 'type'\n", + " def convertTriple(triple):\n", + " return (triple[0], 'type' if triple[1] == 'a' else triple[1], triple[2])\n", + " \n", + " ## Looping through all knowledge graphs in the content, as well as all entailed graphs\n", + " for knowledgeGraph in content:\n", + " for entailedGraph in entailedGraphs:\n", + " \n", + " ## Creates transformedGraph by converting all triples in entailedGraph using the function above\n", + " transformedGraph = [convertTriple(triple) for triple in entailedGraph]\n", + "\n", + " ## Checks if all triples in transformedGraph are in the knowledge graph\n", + " if all(triple in knowledgeGraph for triple in transformedGraph):\n", + " print(str(knowledgeGraph) + \" entails \" + str(entailedGraph))\n", + " else:\n", + " print(str(knowledgeGraph) + \" does NOT entail \" + str(entailedGraph))\n", " pass" ] }, @@ -769,15 +787,35 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 56, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('s', 'p', 'o'), ('s', 'p1', 'o2'), ('s2', 'p2', 'o')] entails [('s2', 'p2', 'o'), ('s2', 'p2', 'o')]\n", + "[('s', 'p', 'o'), ('s', 'p1', 'o2'), ('s2', 'p2', 'o')] entails [('s', 'p', 'o'), ('s2', 'p2', 'o')]\n", + "[('s', 'p', 'o'), ('s', 'p1', 'o2'), ('s2', 'p2', 'o')] does NOT entail [('Netherlands', 'name', '\"The Netherlands\"'), ('Netherlands', 'has_capital', 'Amsterdam'), ('Capital', 'a', 'City'), ('Netherlands', 'neighbours', 'Belgium')]\n", + "[('s', 'p', 'o'), ('s', 'p1', 'o2'), ('s2', 'p2', 'o')] does NOT entail [('Netherlands', 'has_capital', 'Amsterdam'), ('Capital', 'a', 'Country'), ('Netherlands', 'neighbours', 'Belgium')]\n", + "[('Netherlands', 'name', '\"The Netherlands\"'), ('Netherlands', 'has_capital', 'Amsterdam'), ('Amsterdam', 'type', 'Capital'), ('Amsterdam', 'has_population', '\"921.402\"')] does NOT entail [('s2', 'p2', 'o'), ('s2', 'p2', 'o')]\n", + "[('Netherlands', 'name', '\"The Netherlands\"'), ('Netherlands', 'has_capital', 'Amsterdam'), ('Amsterdam', 'type', 'Capital'), ('Amsterdam', 'has_population', '\"921.402\"')] does NOT entail [('s', 'p', 'o'), ('s2', 'p2', 'o')]\n", + "[('Netherlands', 'name', '\"The Netherlands\"'), ('Netherlands', 'has_capital', 'Amsterdam'), ('Amsterdam', 'type', 'Capital'), ('Amsterdam', 'has_population', '\"921.402\"')] does NOT entail [('Netherlands', 'name', '\"The Netherlands\"'), ('Netherlands', 'has_capital', 'Amsterdam'), ('Capital', 'a', 'City'), ('Netherlands', 'neighbours', 'Belgium')]\n", + "[('Netherlands', 'name', '\"The Netherlands\"'), ('Netherlands', 'has_capital', 'Amsterdam'), ('Amsterdam', 'type', 'Capital'), ('Amsterdam', 'has_population', '\"921.402\"')] does NOT entail [('Netherlands', 'has_capital', 'Amsterdam'), ('Capital', 'a', 'Country'), ('Netherlands', 'neighbours', 'Belgium')]\n", + "[('Netherlands', 'name', '\"The Netherlands\"'), ('Netherlands', 'has_capital', 'Amsterdam'), ('Amsterdam', 'type', 'Capital'), ('Amsterdam', 'has_population', '\"921.402\"'), ('Capital', 'type', 'City'), ('Netherlands', 'neighbours', 'Belgium'), ('Netherlands', 'type', 'Country')] does NOT entail [('s2', 'p2', 'o'), ('s2', 'p2', 'o')]\n", + "[('Netherlands', 'name', '\"The Netherlands\"'), ('Netherlands', 'has_capital', 'Amsterdam'), ('Amsterdam', 'type', 'Capital'), ('Amsterdam', 'has_population', '\"921.402\"'), ('Capital', 'type', 'City'), ('Netherlands', 'neighbours', 'Belgium'), ('Netherlands', 'type', 'Country')] does NOT entail [('s', 'p', 'o'), ('s2', 'p2', 'o')]\n", + "[('Netherlands', 'name', '\"The Netherlands\"'), ('Netherlands', 'has_capital', 'Amsterdam'), ('Amsterdam', 'type', 'Capital'), ('Amsterdam', 'has_population', '\"921.402\"'), ('Capital', 'type', 'City'), ('Netherlands', 'neighbours', 'Belgium'), ('Netherlands', 'type', 'Country')] entails [('Netherlands', 'name', '\"The Netherlands\"'), ('Netherlands', 'has_capital', 'Amsterdam'), ('Capital', 'a', 'City'), ('Netherlands', 'neighbours', 'Belgium')]\n", + "[('Netherlands', 'name', '\"The Netherlands\"'), ('Netherlands', 'has_capital', 'Amsterdam'), ('Amsterdam', 'type', 'Capital'), ('Amsterdam', 'has_population', '\"921.402\"'), ('Capital', 'type', 'City'), ('Netherlands', 'neighbours', 'Belgium'), ('Netherlands', 'type', 'Country')] does NOT entail [('Netherlands', 'has_capital', 'Amsterdam'), ('Capital', 'a', 'Country'), ('Netherlands', 'neighbours', 'Belgium')]\n" + ] + } + ], "source": [ "filename = os.path.join(fileDir, 'entailedGraphs.txt')\n", "with open(filename) as file:\n", " graphs = [[t for t in eval(g)] for g in file.readlines()]\n", "\n", - "# your code here" + "# your code here\n", + "graphEntailedBy(graphs)" ] }, { From d72252d6ddb30b861a138b5efc35da713f818005 Mon Sep 17 00:00:00 2001 From: keimperademaker Date: Sun, 15 Sep 2024 14:46:43 +0200 Subject: [PATCH 4/4] tweaks --- .../Propositional Logic and Data .ipynb | 47 ++-- .../Assignment 2. Manipulating RDF data.ipynb | 257 +++++++++++++++--- 2 files changed, 247 insertions(+), 57 deletions(-) diff --git a/Assignments/Assignment_1/Propositional Logic and Data .ipynb b/Assignments/Assignment_1/Propositional Logic and Data .ipynb index d28439e..9c0e9f6 100644 --- a/Assignments/Assignment_1/Propositional Logic and Data .ipynb +++ b/Assignments/Assignment_1/Propositional Logic and Data .ipynb @@ -108,7 +108,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -118,14 +118,14 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "C:\\Users\\keimp\\Documents\\GitHub\\knowledge-data-vu\\Assignments\\Assignment_1\\PLknowledgebase.txt\n", + "/Users/keimperademaker/Documents/GitHub/knowledge-data-vu/Assignments/Assignment_1/PLknowledgebase.txt\n", "['(~R & P) >> Q\\n', '~(((P | ~Q) >> R) >> (P & R)) \\n', '~(P >> Q) | (R >> P) \\n', '(P >> Q) | (Q >> ~P)']\n" ] } @@ -142,7 +142,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -197,7 +197,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -207,7 +207,6 @@ " child2 = s.children[1]\n", " string = \"OR(\" + prefix(child1) + \",\" + prefix(child2) + \")\";\n", " return string\n", - " ## Fill in the remaining cases (hint: not every formula has the same number of children) \n", " elif type(s) is And:\n", " child1 = s.children[0]\n", " child2 = s.children[1]\n", @@ -235,7 +234,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -281,7 +280,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -348,7 +347,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -391,7 +390,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -417,7 +416,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -473,7 +472,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -493,7 +492,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -532,7 +531,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ @@ -544,7 +543,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ @@ -563,7 +562,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -604,7 +603,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -628,7 +627,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -671,7 +670,7 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ @@ -698,7 +697,7 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": 18, "metadata": {}, "outputs": [ { @@ -753,7 +752,7 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ @@ -767,7 +766,7 @@ " for knowledgeGraph in content:\n", " for entailedGraph in entailedGraphs:\n", " \n", - " ## Creates transformedGraph by converting all triples in entailedGraph using the function above\n", + " ## Creates transformedGraph by converting all triples in entailedGraph using the convertTriple() function above\n", " transformedGraph = [convertTriple(triple) for triple in entailedGraph]\n", "\n", " ## Checks if all triples in transformedGraph are in the knowledge graph\n", @@ -787,7 +786,7 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 20, "metadata": {}, "outputs": [ { @@ -847,7 +846,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.3" + "version": "3.10.13" } }, "nbformat": 4, diff --git a/Assignments/Assignment_2/Assignment 2. Manipulating RDF data.ipynb b/Assignments/Assignment_2/Assignment 2. Manipulating RDF data.ipynb index 61adf1d..e1a8c8f 100644 --- a/Assignments/Assignment_2/Assignment 2. Manipulating RDF data.ipynb +++ b/Assignments/Assignment_2/Assignment 2. Manipulating RDF data.ipynb @@ -12,9 +12,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "YOUR NAME: \n", + "YOUR NAME: Keimpe Rademaker\n", "\n", - "YOUR VUNetID: \n", + "YOUR VUNetID: kra225\n", "\n", "*(If you do not provide your name and VUNetID we will not accept your submission).*" ] @@ -81,9 +81,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 118, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: rdflib in /Users/keimperademaker/opt/anaconda3/envs/project-environment/lib/python3.10/site-packages (7.0.0)\n", + "Requirement already satisfied: isodate<0.7.0,>=0.6.0 in /Users/keimperademaker/opt/anaconda3/envs/project-environment/lib/python3.10/site-packages (from rdflib) (0.6.1)\n", + "Requirement already satisfied: pyparsing<4,>=2.1.0 in /Users/keimperademaker/opt/anaconda3/envs/project-environment/lib/python3.10/site-packages (from rdflib) (3.1.2)\n", + "Requirement already satisfied: six in /Users/keimperademaker/opt/anaconda3/envs/project-environment/lib/python3.10/site-packages (from isodate<0.7.0,>=0.6.0->rdflib) (1.16.0)\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], "source": [ "# Before starting with the tasks of this assignment, do not forget to install **rdflib** so we can start using it. \n", "%pip install rdflib" @@ -91,7 +103,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 119, "metadata": {}, "outputs": [], "source": [ @@ -126,9 +138,41 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 120, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "@prefix ex1: .\n", + "@prefix rdfs: .\n", + "\n", + "ex1:Germany a ex1:EuropeanCountry .\n", + "\n", + "ex1:Netherlands a ex1:Country ;\n", + " ex1:hasCapital ex1:Amsterdam ;\n", + " ex1:hasName \"The Netherlands\" ;\n", + " ex1:neighbours ex1:Belgium .\n", + "\n", + "ex1:hasCapital rdfs:range ex1:Capital ;\n", + " rdfs:subPropertyOf ex1:containsCity .\n", + "\n", + "ex1:Amsterdam a ex1:Capital .\n", + "\n", + "ex1:Belgium a ex1:Country .\n", + "\n", + "ex1:EuropeanCountry rdfs:subClassOf ex1:Country .\n", + "\n", + "ex1:containsCity rdfs:domain ex1:Country ;\n", + " rdfs:range ex1:City .\n", + "\n", + "ex1:Capital rdfs:subClassOf ex1:City .\n", + "\n", + "\n" + ] + } + ], "source": [ "load_graph('example-from-slides.ttl')\n", "serialize_graph()" @@ -143,11 +187,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 121, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "http://example.com/kad/hasName\n" + ] + } + ], "source": [ "for s,p,o in g:\n", " if type(o) is Literal:\n", @@ -178,17 +230,81 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 122, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "@prefix ex1: .\n", + "@prefix rdfs: .\n", + "\n", + "ex1:Germany a ex1:EuropeanCountry .\n", + "\n", + "ex1:Italy a ex1:EuropeanCountry ;\n", + " ex1:hasCapital ex1:Rome ;\n", + " ex1:hasName \"Italy\" ;\n", + " ex1:neighbours ex1:France .\n", + "\n", + "ex1:Netherlands a ex1:Country ;\n", + " ex1:hasCapital ex1:Amsterdam ;\n", + " ex1:hasName \"The Netherlands\" ;\n", + " ex1:neighbours ex1:Belgium .\n", + "\n", + "ex1:hasCapital rdfs:range ex1:Capital ;\n", + " rdfs:subPropertyOf ex1:containsCity .\n", + "\n", + "ex1:Amsterdam a ex1:Capital .\n", + "\n", + "ex1:Belgium a ex1:Country .\n", + "\n", + "ex1:Spain a ex1:EuropeanCountry ;\n", + " ex1:hasCapital ex1:Madrid ;\n", + " ex1:hasName \"Spain\" ;\n", + " ex1:neighbours ex1:France .\n", + "\n", + "ex1:containsCity rdfs:domain ex1:Country ;\n", + " rdfs:range ex1:City .\n", + "\n", + "ex1:Capital rdfs:subClassOf ex1:City .\n", + "\n", + "ex1:France a ex1:EuropeanCountry ;\n", + " ex1:hasCapital ex1:Paris ;\n", + " ex1:hasName \"France\" ;\n", + " ex1:neighbours ex1:Spain .\n", + "\n", + "ex1:EuropeanCountry rdfs:subClassOf ex1:Country .\n", + "\n", + "\n" + ] + } + ], "source": [ "ex = Namespace(\"http://example.com/kad/\")\n", "owl = Namespace(\"http://www.w3.org/2002/07/owl#\")\n", "rdf = Namespace(\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\")\n", "rdfs = Namespace(\"http://www.w3.org/2000/01/rdf-schema#\")\n", "\n", + "# Add France as a country with Paris as its capital and Spain as its neighbour. Save the name as a literal.\n", + "g.add((ex['France'], rdf.type, ex['EuropeanCountry']))\n", + "g.add((ex['France'], ex.hasName, Literal('France')))\n", + "g.add((ex['France'], ex.hasCapital, ex['Paris']))\n", + "g.add((ex['France'], ex.neighbours, ex['Spain']))\n", + "\n", + "## Add Spain as a country with Madrid as its capital and France as its neighbour.\n", + "g.add((ex['Spain'], rdf.type, ex['EuropeanCountry']))\n", + "g.add((ex['Spain'], ex.hasCapital, ex['Madrid']))\n", + "g.add((ex['Spain'], ex.hasName, Literal('Spain')))\n", + "g.add((ex['Spain'], ex.neighbours, ex['France']))\n", + "\n", + "## Add Italy as a country with Rome as its capital and France as its neighbour.\n", + "g.add((ex['Italy'], rdf.type, ex['EuropeanCountry']))\n", + "g.add((ex['Italy'], ex.hasCapital, ex['Rome']))\n", + "g.add((ex['Italy'], ex.hasName, Literal('Italy')))\n", + "g.add((ex['Italy'], ex.neighbours, ex['France']))\n", + "\n", "\n", - "# add triples here to the graph 'g' (do not forget the namespaces).\n", "\n", "serialize_graph()" ] @@ -218,12 +334,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 123, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "France\n", + "Italy\n", + "Spain\n", + "The Netherlands\n" + ] + } + ], "source": [ "for s,p,o in g:\n", - " # Your code here\n", + " if type(o) is Literal:\n", + " print(o) \n", " pass" ] }, @@ -251,13 +379,45 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 138, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7: http://www.w3.org/1999/02/22-rdf-syntax-ns#type\n", + "4: http://example.com/kad/hasName\n", + "4: http://example.com/kad/neighbours\n", + "4: http://example.com/kad/hasCapital\n", + "2: http://www.w3.org/2000/01/rdf-schema#range\n", + "2: http://www.w3.org/2000/01/rdf-schema#subClassOf\n", + "1: http://www.w3.org/2000/01/rdf-schema#subPropertyOf\n", + "1: http://www.w3.org/2000/01/rdf-schema#domain\n" + ] + } + ], "source": [ - "for s,p,o in g:\n", - " # Your code here\n", - " pass" + "def get_unique_predicates(graph):\n", + "\n", + " # Create a dictionary to store the counts of each predicate\n", + " predicate_counts = {}\n", + "\n", + " # Iterate through the triples in the graph\n", + " for s, p, o in g:\n", + " if p in predicate_counts:\n", + " predicate_counts[p] += 1\n", + " else:\n", + " predicate_counts[p] = 1\n", + "\n", + " # Sort predicates by frequency of occurrences\n", + " sorted_predicates = sorted(predicate_counts.items(), key=lambda item: item[1], reverse=True)\n", + "\n", + " # Print the sorted predicates\n", + " for predicate, count in sorted_predicates:\n", + " print(f\"{count}: {predicate}\")\n", + "\n", + "get_unique_predicates(g)" ] }, { @@ -295,9 +455,18 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 111, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: graphviz in /Users/keimperademaker/opt/anaconda3/envs/project-environment/lib/python3.10/site-packages (0.20.3)\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], "source": [ "# install and import the graphviz library\n", "%pip install graphviz\n", @@ -313,7 +482,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 112, "metadata": {}, "outputs": [], "source": [ @@ -331,7 +500,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 113, "metadata": {}, "outputs": [], "source": [ @@ -350,9 +519,31 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 114, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "strict digraph {\n", + "\tgraph [dpi=50]\n", + "}\n", + "\n" + ] + }, + { + "ename": "AttributeError", + "evalue": "'function' object has no attribute 'display_svg'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m/Users/keimperademaker/Documents/GitHub/knowledge-data-vu/Assignments/Assignment_2/Assignment 2. Manipulating RDF data.ipynb Cell 34\u001b[0m line \u001b[0;36m2\n\u001b[1;32m 1\u001b[0m \u001b[39mprint\u001b[39m(dot\u001b[39m.\u001b[39msource)\n\u001b[0;32m----> 2\u001b[0m display\u001b[39m.\u001b[39;49mdisplay_svg(dot) \u001b[39m# paste the source at www.webgraphviz.com if this does not produce anything \u001b[39;00m\n", + "\u001b[0;31mAttributeError\u001b[0m: 'function' object has no attribute 'display_svg'" + ] + } + ], "source": [ "print(dot.source)\n", "display.display_svg(dot) # paste the source at www.webgraphviz.com if this does not produce anything " @@ -604,7 +795,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.3" + "version": "3.10.13" } }, "nbformat": 4,