diff --git a/Assignments/Assignment_1/Propositional Logic and Data .ipynb b/Assignments/Assignment_1/Propositional Logic and Data .ipynb index 58cf233..9c0e9f6 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": 4, "metadata": {}, "outputs": [], "source": [ @@ -171,7 +207,20 @@ " 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", + " 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 +234,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 5, + "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 +280,60 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "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 +347,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 7, + "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 +390,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "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 +416,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 9, + "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 +472,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -370,10 +492,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "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 +531,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "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": 13, "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 +562,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 14, + "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 +603,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "def as_ntriple(triple):\n", - " ## first write a function that transforms the triples ...\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", + " else:\n", + " return f\"<{a}> <{b}> <{c}> .\"\n", " pass" ] }, @@ -473,9 +627,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 16, + "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,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ - "def tripleEntailedBy():\n", - " ## Your code here\n", + "def tripleEntailedBy(entailedTriples):\n", + " \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", + "\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" ] }, @@ -524,15 +697,29 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('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" + ] + } + ], "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(triples)" ] }, { @@ -565,12 +752,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "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 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", + " 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" ] }, @@ -583,15 +786,35 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 20, + "metadata": {}, + "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)" ] }, { @@ -623,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_1/__pycache__/logic.cpython-310.pyc b/Assignments/Assignment_1/__pycache__/logic.cpython-310.pyc new file mode 100644 index 0000000..50391d6 Binary files /dev/null and b/Assignments/Assignment_1/__pycache__/logic.cpython-310.pyc differ 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 0000000..f00881c Binary files /dev/null and b/Assignments/Assignment_1/__pycache__/logic.cpython-312.pyc differ 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,