Sand Box#

from gd05 import GenDifS_Map
mm = "Schnitzel_DE"
o = GenDifS_Map(f"../mm/{mm}.mm", verbose = 1, update_mindmap=True)
cwd into input_dir: /home/dsci/a/l/LA_2022_ws/mm
reading /home/dsci/a/l/LA_2022_ws/mm > Schnitzel_DE.mm (20 nodes)
Parsing XML: #1 start nodes
ontology node IDs: {'ID_1341768789': 'ONTOLOGY'}
RDF: 280 1180
warning: no glossary file found.
updated mindmap with backup to /home/dsci/a/l/LA_2022_ws/mm/Schnitzel_DE.mm_backup_2022-11-20T13-49-55
my_query = """
SELECT DISTINCT ?s ?p ?o
WHERE {
    ?s ?p ?o .
    ?s rdf:type :Kalbschnitzel 
}"""

qres = o.rdflib.query(my_query)
for row in qres:
    print(row.s, row.p, row.o)
http://mm2ttl.net/namespace/ex#Kalbschnitzel_ID_1827400828 http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://mm2ttl.net/namespace/default#Kalbschnitzel
http://mm2ttl.net/namespace/ex#Kalbschnitzel_ID_1953543047 http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://mm2ttl.net/namespace/default#Kalbschnitzel
import pandas as pd
pd.DataFrame.from_dict(o.node_dict, orient='index').sort_index().T
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
parseType object collection object collection object collection object collection collection object collection object collection collection object collection object collection object collection
TEXT topConcept NaN Schnitzel BY hat_Herkunft SOME Tier Schweineschnitzel SOME Schwein Kalbschnitzel SOME Kalb BY wendet_sich_an SOME Zielgruppe Kinderschnitzel SOME Kind Seniorenschnitzel SOME Senior BY serviert_an SOME Sauce Jägerschnitzel SOME Pfifferlinge Zigeunerschnitzel SOME Paprika Rahmschnitzel SOME Sahne
parent_object_ID 0 0 0 2 2 4 2 6 2 2 9 2 11 2 2 14 2 16 2 18
parent_collection_ID 1.0 1.0 NaN 1.0 NaN 3.0 NaN 3.0 1.0 NaN 8.0 NaN 8.0 1.0 NaN 13.0 NaN 13.0 NaN 13.0
tag NaN BY NaN BY NaN SOME NaN SOME BY NaN SOME NaN SOME BY NaN SOME NaN SOME NaN SOME
BY NaN topProperty NaN hat_Herkunft NaN NaN NaN NaN wendet_sich_an NaN NaN NaN NaN serviert_an NaN NaN NaN NaN NaN NaN
SOME NaN topConcept NaN Tier NaN Schwein NaN Kalb Zielgruppe NaN Kind NaN Senior Sauce NaN Pfifferlinge NaN Paprika NaN Sahne
ID NaN NaN 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0 19.0
mm_ID NaN NaN ID_703681491 ID_1909572786 ID_1892873496 ID_775016421 ID_1827400828 ID_1953543047 ID_369046285 ID_1270104247 ID_1031562158 ID_305097846 ID_1820306917 ID_1011508176 ID_130548072 ID_1609648857 ID_260209826 ID_1773526489 ID_1782430077 ID_827143455
RESOURCE NaN NaN Schnitzel hat_Herkunft%20SOME%20Tier Schweineschnitzel Schwein Kalbschnitzel Kalb wendet_sich_an%20SOME%20Zielgruppe Kinderschnitzel Kind Seniorenschnitzel Senior serviert_an%20SOME%20Sauce J%C3%A4gerschnitzel Pfifferlinge Zigeunerschnitzel Paprika Rahmschnitzel Sahne
LINK NaN NaN
object_ID NaN NaN 2.0 NaN 4.0 NaN 6.0 NaN NaN 9.0 NaN 11.0 NaN NaN 14.0 NaN 16.0 NaN 18.0 NaN
collection_ID NaN NaN 1.0 3.0 3.0 5.0 3.0 7.0 8.0 8.0 10.0 8.0 12.0 13.0 13.0 15.0 13.0 17.0 13.0 19.0
md NaN NaN * **Schnitzel** * BY hat_Herkunft SOME *Tier* * **Schweineschnitzel** * SOME *Schwein* * **Kalbschnitzel** * SOME *Kalb* * BY wendet_sich_an SOME *Zielgruppe* * **Kinderschnitzel** * SOME *Kind* * **Seniorenschnitzel** * SOME *Senior* * BY serviert_an SOME *Sauce* * **Jägerschnitzel** * SOME *Pfifferlinge* * **Zigeunerschnitzel** * SOME *Paprika* * **Rahmschnitzel** * SOME *Sahne*
BY_LINK NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
o.result_dict
{'start': ['# Start']}
o.parser_markdown()
  • Schnitzel

    • BY hat_Herkunft SOME Tier

      • Schweineschnitzel

        • SOME Schwein

      • Kalbschnitzel

        • SOME Kalb

    • BY wendet_sich_an SOME Zielgruppe

      • Kinderschnitzel

        • SOME Kind

      • Seniorenschnitzel

        • SOME Senior

    • BY serviert_an SOME Sauce

      • Jägerschnitzel

        • SOME Pfifferlinge

      • Zigeunerschnitzel

        • SOME Paprika

      • Rahmschnitzel

        • SOME Sahne

o.rdflib.serialize(destination=f"../ttl/{mm}.ttl")
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
File ~/miniconda3/lib/python3.9/shutil.py:825, in move(src, dst, copy_function)
    824 try:
--> 825     os.rename(src, real_dst)
    826 except OSError:

FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmpqpfdckub' -> '../ttl/Schnitzel_DE.ttl'

During handling of the above exception, another exception occurred:

FileNotFoundError                         Traceback (most recent call last)
Cell In [8], line 1
----> 1 o.rdflib.serialize(destination=f"../ttl/{mm}.ttl")

File ~/miniconda3/lib/python3.9/site-packages/rdflib/graph.py:1210, in Graph.serialize(self, destination, format, base, encoding, **args)
   1208 dest = url2pathname(path) if scheme == "file" else location
   1209 if hasattr(shutil, "move"):
-> 1210     shutil.move(name, dest)
   1211 else:
   1212     shutil.copy(name, dest)

File ~/miniconda3/lib/python3.9/shutil.py:845, in move(src, dst, copy_function)
    843         rmtree(src)
    844     else:
--> 845         copy_function(src, real_dst)
    846         os.unlink(src)
    847 return real_dst

File ~/miniconda3/lib/python3.9/shutil.py:444, in copy2(src, dst, follow_symlinks)
    442 if os.path.isdir(dst):
    443     dst = os.path.join(dst, os.path.basename(src))
--> 444 copyfile(src, dst, follow_symlinks=follow_symlinks)
    445 copystat(src, dst, follow_symlinks=follow_symlinks)
    446 return dst

File ~/miniconda3/lib/python3.9/shutil.py:266, in copyfile(src, dst, follow_symlinks)
    264 with open(src, 'rb') as fsrc:
    265     try:
--> 266         with open(dst, 'wb') as fdst:
    267             # macOS
    268             if _HAS_FCOPYFILE:
    269                 try:

FileNotFoundError: [Errno 2] No such file or directory: '../ttl/Schnitzel_DE.ttl'
import rdflib
from rdflib.extras.external_graph_libs import rdflib_to_networkx_multidigraph
#!pip install networkx
import networkx as nx
import matplotlib.pyplot as plt
url = 'https://www.w3.org/TeamSubmission/turtle/tests/test-30.ttl'

g = rdflib.Graph()
result = g.parse(url, format='turtle')

#G = rdflib_to_networkx_multidigraph(result)

G = rdflib_to_networkx_multidigraph(o.rdflib)


# Plot Networkx instance of RDF Graph
pos = nx.shell_layout(G, scale=1000)
edge_labels = nx.get_edge_attributes(G, 'r')
#nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
nx.draw(G, with_labels=True)
_images/h_sand-box_12_0.png
o.owlrl
<Graph identifier=N7e7daad5fdfe42af9b424caf40bbebc4 (<class 'rdflib.graph.Graph'>)>

The parser will read the mindmap file, parse the content, and store the result in the dictionary o.node_dict.

This dict is acessable from outside, e.g. like this:

Then raw turtle code will be generated. It’s stored in the list o.turtle_list. To conveniently inspect distinct entries, use the method o.show_turtle([ <list of IDs > ]), e.g.:

print(o.show_turtle([]))
# 1
# COLLECTION.a, context
#   None: None
#     None: None
#       None: None



# 1
# BY.a, owl
:topProperty
   rdf:type owl:ObjectProperty .


# 1
# BY.b, owl
:topConcept
   a owl:Class .


# 1
# BY.c, owl-test
ex:topConcept_None
   a :topConcept .



# 1
# BY.g, owl-classification
:BY_1_restriction
   a owl:Class ;
   rdfs:label "BY topProperty SOME topConcept" ;
   owl:equivalentClass [ a owl:Restriction ;
      owl:onProperty :topProperty ;
      owl:someValuesFrom :topConcept ] .



# 1
# BY.h, owl-classification
:BY_1_intersection
   a owl:Class ;
   rdfs:label "(BY topProperty SOME topConcept) INTERSECT None_None" ;
   rdfs:subClassOf :None ;
   owl:equivalentClass [ a owl:Class ;
      owl:intersectionOf (
         :BY_1_restriction
         :None ) ] .



# 2
# OBJECT_BY.a, owl
:Milch
   a owl:Class ; # <pwn:milk#1>
   rdfs:subClassOf :None .


# 2
# OBJECT_BY.b, owl-test
ex:Milch_ID_792590473
   a :Milch .



# 2
# OBJECT_BY.c, skos
cpt:Milch
   a skos:Concept ;
   skos:broaderTransitive cpt:None .


# 3
# COLLECTION.a, context
#   None: None
#     2: Milch
#       3: 



# 4
# LITERAL.a, owl
:Milch 
    rdfs:comment '''Eine \'\'\' Emulsion [Wikipedia -> Milch](https://de.wikipedia.org/wiki/Milch)''' .  # None


# 4
# LITERAL.c, skos
cpt:Milch 
    skos:definition '''Eine \'\'\' Emulsion [Wikipedia -> Milch](https://de.wikipedia.org/wiki/Milch)''' . # None 


# 5
# OBJECT_BY.a, owl
:Milch
   a owl:Class ; # 
   rdfs:subClassOf :None .


# 5
# OBJECT_BY.b, owl-test
ex:Milch_ID_250317734
   a :Milch .



# 5
# OBJECT_BY.c, skos
cpt:Milch
   a skos:Concept ;
   skos:broaderTransitive cpt:None .


# 6
# OBJECT_BY.a, owl
:Rapphengst
   a owl:Class ; # <http://wiki/Rapphengst>
   rdfs:subClassOf :None .


# 6
# OBJECT_BY.b, owl-test
ex:Rapphengst_ID_858326988
   a :Rapphengst .



# 6
# OBJECT_BY.c, skos
cpt:Rapphengst
   a skos:Concept ;
   skos:broaderTransitive cpt:None .


# 7
# COLLECTION.a, context
#   None: None
#     6: Rapphengst
#       7: 



# 7
# SUP.b, owl
:SUP_7
   a owl:Class ;
   rdfs:label "Rappe(8) AND Hengst(9)" ;
   rdfs:subClassOf :Rapphengst ;
   owl:equivalentClass [ a owl:Class ;
      owl:intersectionOf (
          :Rappe :Hengst ) ] .



# 7
# SUP.d, owl-test
ex:SUP_7_ID_1403649387
   a :Rappe .



# 7
# SUP.d, owl-test
ex:SUP_7_ID_1403649387
   a :Hengst .



# 7
# SUP.c, owl-test
ex:Rapphengst_ID_858326988
   a :Rapphengst .
ex:SUP_7_ID_1403649387
   ex:classifyLike ex:Rapphengst_ID_858326988 .



# 8
# OBJECT_SUP.a, owl
:Rappe
   a owl:Class . # 
:Rapphengst
    rdfs:subClassOf :Rappe .


# 8
# OBJECT_SUP.b, owl-test
ex:Rappe_ID_24101473
   a :Rappe .



# 8
# OBJECT_SUP.c, skos
cpt:Rappe
   a skos:Concept . # 
cpt:Rapphengst
    skos:broaderTransitive cpt:Rappe .


# 9
# OBJECT_SUP.a, owl
:Hengst
   a owl:Class . # 
:Rapphengst
    rdfs:subClassOf :Hengst .


# 9
# OBJECT_SUP.b, owl-test
ex:Hengst_ID_1221408874
   a :Hengst .



# 9
# OBJECT_SUP.c, skos
cpt:Hengst
   a skos:Concept . # 
cpt:Rapphengst
    skos:broaderTransitive cpt:Hengst .


# 10
# OBJECT_BY.a, owl
:Anna
   a owl:Class ; # 
   rdfs:subClassOf :None .


# 10
# OBJECT_BY.b, owl-test
ex:Anna_ID_1789270621
   a :Anna .



# 10
# OBJECT_BY.c, skos
cpt:Anna
   a skos:Concept ;
   skos:broaderTransitive cpt:None .


# 11
# COLLECTION.a, context
#   None: None
#     10: Anna
#       11: liebt



# 13
# OBJECT_BY.a, owl
:Russland
   a owl:Class ; # 
   rdfs:subClassOf :None .


# 13
# OBJECT_BY.b, owl-test
ex:Russland_ID_1042433107
   a :Russland .



# 13
# OBJECT_BY.c, skos
cpt:Russland
   a skos:Concept ;
   skos:broaderTransitive cpt:None .


# 14
# COLLECTION.a, context
#   None: None
#     13: Russland
#       14: 



# 14
# BT.a, skos
# BT or ISA



# 15
# BT/ISA_OBJECT.a, owl
:Putin
   a owl:Class . #  



# 15
# BT/ISA_OBJECT.c, owl-test
ex:Putin_ID_575441509
   a :Putin .



# 15
# BT/ISA_OBJECT.d, skos
cpt:Putin
   a skos:Concept .



# 15
# BT_OBJECT.f, skos
cpt:Putin 
   skos:broader cpt:Russland 
.


# 16
# COLLECTION.a, context
#   14: 
#     15: Putin
#       16: 

focus_map prints a subset of the mindmap focused on a set of nodes

from rdflib import Graph, URIRef, Literal
import owlrl
graph = Graph()
ttl = o.show_turtle([2])
print(ttl)
# 2
# OBJECT_BY.a, owl
:Milch
   a owl:Class ; # <pwn:milk#1>
   rdfs:subClassOf :None .


# 2
# OBJECT_BY.b, owl-test
ex:Milch_ID_792590473
   a :Milch .



# 2
# OBJECT_BY.c, skos
cpt:Milch
   a skos:Concept ;
   skos:broaderTransitive cpt:None .
namespaces = """
# namespaces to be aligned by the user 
@prefix ex: <http://mm2ttl.net/namespace/ex#> .
@prefix cpt: <http://mm2ttl.net/namespace/x#> .
@prefix : <http://mm2ttl.net/namespace/default#> .

# standard namespaces, c.f. https://www.w3.org/TR/owl2-quick-reference/
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .

ex:classifyLike a owl:ObjectProperty .
[ rdf:type owl:Ontology ; owl:imports <http://www.w3.org/2004/02/skos/core> ] .
"""
graph.parse(data = namespaces+ttl, format='ttl')
<Graph identifier=Ne6d86911e3d94a29bfff6c5579156317 (<class 'rdflib.graph.Graph'>)>
len(o.turtle_list)
43
from IPython.display import display, Markdown
display(Markdown("# Hallo <span style='color:red'>ROT</span> und <span style='color:blue'>BLAU</span>"))
txt = """text mit 3 '''!"""
"'''" in txt
True
import re