{ "cells": [ { "cell_type": "markdown", "id": "f53f9245", "metadata": {}, "source": [ "# Hello World" ] }, { "cell_type": "markdown", "id": "0d63ae3d", "metadata": {}, "source": [ "Install GenDifS: Until further notice, there is no installation via pip. Installation by hand is simple:\n", "\n", "* Download the module `gd06.py` into any directory, e.g. the directory `../py/`.\n", "* Include this directory in the Python import path." ] }, { "cell_type": "code", "execution_count": 13, "id": "baa35578", "metadata": {}, "outputs": [], "source": [ "import sys\n", "sys.path.append('../py/')\n", "from gd06 import GenDifS_Map" ] }, { "cell_type": "markdown", "id": "e11dd6f0", "metadata": {}, "source": [ "We recommend to create mindmaps in a separate directory, e.g. `../mm/`." ] }, { "cell_type": "code", "execution_count": 3, "id": "abf335ed", "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "GenDifS 0.61 (2023-05-20)\n", "cwd into input_dir: /home/dsci/a/l/LA_2023_ss/gendifs/mm\n", "reading /home/dsci/a/l/LA_2023_ss/gendifs/mm > hello_world.mm (9 nodes)\n" ] } ], "source": [ "mindmap = \"hello_world\" # filename without .mm extension\n", "o = GenDifS_Map(f\"../mm/{mindmap}.mm\", verbose = 1)" ] }, { "cell_type": "markdown", "id": "05783e43", "metadata": {}, "source": [ "If you plan to write back the current mindmap it might be wise to create a backup?" ] }, { "cell_type": "code", "execution_count": 4, "id": "c2171d46", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "wrote backup to ../mm/hello_world_backup_2023-05-20T08-21-29\n" ] } ], "source": [ "backup_mindmap = True\n", "\n", "import time\n", "if backup_mindmap:\n", " mindmap_backup_filename = f\"../mm/{mindmap}_backup_{time.strftime('%Y-%m-%dT%H-%M-%S')}\" \n", " o.mindmap.write(mindmap_backup_filename, pretty_print=True)\n", " print(f\"wrote backup to {mindmap_backup_filename}\")" ] }, { "cell_type": "markdown", "id": "08db3406", "metadata": {}, "source": [ "## Compile the mindmap into a ttl representation:" ] }, { "cell_type": "code", "execution_count": 5, "id": "52e00147", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "rdflib: 77 triples.\n" ] } ], "source": [ "o.compile()" ] }, { "cell_type": "code", "execution_count": 6, "id": "ddd7ac8b", "metadata": {}, "outputs": [], "source": [ "# The compiled code is put into a pandas data frame\n", "# o.code_df.head(6)" ] }, { "cell_type": "markdown", "id": "09207ea1", "metadata": {}, "source": [ "`o.compile()` generates all necessary class attributes, but does not perform inferencing. To get all instances which can be inferred use `o.owlrl()`:" ] }, { "cell_type": "code", "execution_count": 7, "id": "92b7d849", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "owlrl: 388 triples.\n" ] } ], "source": [ "o.owlrl()" ] }, { "cell_type": "markdown", "id": "88a836c1", "metadata": {}, "source": [ "## SPARQL\n", "\n", "Feel free to query the graph with SPARQL. Some useful default namespaces are contained in the attribute `o.sparql_namespaces`." ] }, { "cell_type": "code", "execution_count": 8, "id": "c44066a2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(rdflib.term.URIRef('http://example.net/namespace/cpt#milk'), rdflib.term.URIRef('http://example.net/namespace/cpt#topConcept'))\n", "(rdflib.term.URIRef('http://example.net/namespace/cpt#milk_of_a_cow'), rdflib.term.URIRef('http://example.net/namespace/cpt#milk'))\n", "(rdflib.term.URIRef('http://example.net/namespace/cpt#goat_milk'), rdflib.term.URIRef('http://example.net/namespace/cpt#milk'))\n" ] } ], "source": [ "Q = o.sparql_namespaces + \"\"\"\n", "SELECT ?sub ?super\n", "WHERE { ?sub skos:broader ?super }\n", "\"\"\"\n", "\n", "qres = o.rdflib_graph.query(Q)\n", "\n", "for row in qres:\n", " print(row)" ] }, { "cell_type": "markdown", "id": "49b0face", "metadata": {}, "source": [ "## Save the ontologies" ] }, { "cell_type": "markdown", "id": "d494a905", "metadata": {}, "source": [ "We recommend saving the generated taxonomy in Turtle format in a separate directory, e.g. `../ttl/`." ] }, { "cell_type": "markdown", "id": "a1b2616d", "metadata": {}, "source": [ "This is what the ttl code looks like, generated directly by GenDifS:" ] }, { "cell_type": "code", "execution_count": 9, "id": "b31071f0", "metadata": {}, "outputs": [], "source": [ "with open(f\"../ttl/{mindmap}_mm.ttl\", \"w\") as file:\n", " file.write(o.ttl_code)" ] }, { "cell_type": "markdown", "id": "1f3f76a6", "metadata": {}, "source": [ "Export of all triples through rdflib, *without* inferencing:" ] }, { "cell_type": "code", "execution_count": 10, "id": "5d8ee67e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "77" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(o.rdflib_graph.serialize(destination=f\"../ttl/{mindmap}_rdflib.ttl\"))" ] }, { "cell_type": "markdown", "id": "a4296ffa", "metadata": {}, "source": [ "Export of all triples through rdflib, *after* inferencing:" ] }, { "cell_type": "code", "execution_count": 11, "id": "a9c614d8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "388" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(o.owlrl_graph.serialize(destination=f\"../ttl/{mindmap}_owlrl.ttl\"))" ] }, { "cell_type": "markdown", "id": "20895478", "metadata": {}, "source": [ "## Save back the mindmap\n", "\n" ] }, { "cell_type": "code", "execution_count": 12, "id": "6f31abd4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "updated mindmap ../mm/hello_world.mm\n" ] } ], "source": [ "update_mindmap = True\n", "\n", "if update_mindmap:\n", " update_mindmap_filename = f\"../mm/{mindmap}.mm\"\n", " o.mindmap.write(update_mindmap_filename,pretty_print=True)\n", " print(f\"updated mindmap {update_mindmap_filename}\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }