Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a function to auto-generate a JSON-LD context from a Closed/DefinedNamespace #1705

Closed
nicholascar opened this issue Feb 5, 2022 · 2 comments · Fixed by #1706
Closed
Assignees
Labels
enhancement New feature or request serialization Related to serialization.

Comments

@nicholascar
Copy link
Member

It would be entirely possible to generate a JSON-LD context object from an RDFLib DefinedNamespace or ClosedNamespace.

An example of an existing context and DefinedNamespace is available for GeoSPARQL:

@nicholascar nicholascar added enhancement New feature or request serialization Related to serialization. labels Feb 5, 2022
@nicholascar nicholascar self-assigned this Feb 5, 2022
@niklasl
Copy link
Member

niklasl commented Feb 5, 2022

This ought to do it:

import json
from rdflib.term import URIRef
from rdflib.namespace import SKOS


def make_context(pfx, ns):
    terms = {pfx: str(ns)}
    for key, term in ns.__annotations__.items():
        if term == URIRef:
            terms[key] = f'{pfx}:{key}'

    return {'@context': terms}


print(json.dumps(make_context('skos', SKOS), indent=2))

Remember though that for the basic case of using one ontology for nice, compact JSON-LD, all you need is @vocab. Like:

{
  "@context": {
    "@vocab": "http://www.w3.org/2004/02/skos/core#"
  }
}

Of course, if you're mixing ontologies, you need something more involved. OTOH in those cases it makes sense to take care in crafting what you actually need (and knowing why you need it). A simple run of the above yields a useful starter for that of course.

Sometimes you have some custom JSON that's isomorphic enough to RDF that defining a context is enough to convert it (called the "zero edits" case; alas in practise a lot of data "in the wild" is too idiomatic in some way to make if fully feasible). In these cases it's not uncommon to need aliases used for either mapping diffferent short-names to terms (e.g. "part": "dc:hasPart"), and/or coerce stings to typed values, vocabulary terms or regular links and so on. (Or any of the more complex mapping techniques like indexed objects, nests and scoped contexts.)

@niklasl
Copy link
Member

niklasl commented Feb 5, 2022

Aside: If you need a more involved tool, check out https://github.com/libris/definitions/blob/31ddca9fef60c82c6bb725aa7403f175b918dfd4/lxltools/contextmaker.py. It takes an RDF ontology and uses its RDFS and OWL mappings (sub-terms and equivalent terms) along with an ordered list of prefixes, and generates a mixed context of those. That was my first take of tackling the "Babel's Tower of Ontologies" problem, which I've subsequently turned into https://github.com/niklasl/ldtvm. To be continued.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request serialization Related to serialization.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants