Skip to content

Instantly share code, notes, and snippets.

@white-gecko
Last active January 16, 2018 10:25
Show Gist options
  • Save white-gecko/338a0d16e0b24e86f1fedb5bb6d7912a to your computer and use it in GitHub Desktop.
Save white-gecko/338a0d16e0b24e86f1fedb5bb6d7912a to your computer and use it in GitHub Desktop.
"""Untill https://github.com/RDFLib/rdflib/pull/807 is merged and released."""
from rdflib import Graph, BNode, URIRef
from six.moves.urllib.parse import urljoin
"""requirements.txt:
rdflib>=4.2.2
"""
def bNodeSkolemize(self, authority=None, basepath=None):
""" Create a URIRef "skolem" representation of the BNode, in accordance
with http://www.w3.org/TR/rdf11-concepts/#section-skolemization
.. versionadded:: 4.0
"""
rdflib_skolem_genid = "/.well-known/genid/rdflib/"
if authority is None:
authority = "http://rdlib.net/"
if basepath is None:
basepath = rdflib_skolem_genid
skolem = "%s%s" % (basepath, str(self))
return URIRef(urljoin(authority, skolem))
def skolemize(self, new_graph=None, bnode=None, authority=None, basepath=None):
def do_skolemize(bnode, t):
(s, p, o) = t
if s == bnode:
s = bNodeSkolemize(s, authority=authority, basepath=basepath)
if o == bnode:
o = bNodeSkolemize(o, authority=authority, basepath=basepath)
return (s, p, o)
def do_skolemize2(t):
(s, p, o) = t
if isinstance(s, BNode):
s = bNodeSkolemize(s, authority=authority, basepath=basepath)
if isinstance(o, BNode):
o = bNodeSkolemize(o, authority=authority, basepath=basepath)
return (s, p, o)
retval = Graph() if new_graph is None else new_graph
if bnode is None:
self._process_skolem_tuples(retval, do_skolemize2)
elif isinstance(bnode, BNode):
self._process_skolem_tuples(
retval, lambda t: do_skolemize(bnode, t))
return retval
inputUri = "file.ttl"
graph = Graph()
graph.parse(inputUri, format="ttl")
skolemGraph = skolemize(graph, authority="http://ub.uni-leipzig.de", basepath="mobigames/")
skolemGraph.serialize(destination="skolemized.ttl", format='ttl')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment