diff --git a/sphinx/requirements.txt b/sphinx/requirements.txt index 4039140..39ad93c 100644 --- a/sphinx/requirements.txt +++ b/sphinx/requirements.txt @@ -9,6 +9,7 @@ idna==3.6 imagesize==1.4.1 importlib-metadata==6.7.0 Jinja2==3.1.3 +json MarkupSafe==2.1.3 nbsphinx packaging==23.2 @@ -17,6 +18,7 @@ pydata-sphinx-theme==0.13.3 Pygments==2.17.2 pytz==2023.3.post1 requests==2.31.0 +rdflib snowballstemmer==2.2.0 soupsieve==2.4.1 Sphinx==5.3.0 diff --git a/sphinx/ttl_to_context.py b/sphinx/ttl_to_context.py new file mode 100644 index 0000000..c1597b6 --- /dev/null +++ b/sphinx/ttl_to_context.py @@ -0,0 +1,98 @@ +import rdflib +from rdflib.namespace import RDF, OWL, SKOS +import json +import os +from urllib.parse import urljoin +from urllib.request import pathname2url +import warnings + + +def generate_jsonld_context(ttl_file, predicate_uri, label_uri='http://www.w3.org/2000/01/rdf-schema#label'): + """ + Generate a JSON-LD context file from a Turtle file. + + Args: + - ttl_file: Path to the Turtle (.ttl) file. + - predicate_uri: The URI of the predicate to map to JSON-LD. + - label_uri: The URI for the label (default is rdfs:label). + + Returns: + - A Python dictionary representing the JSON-LD context. + """ + g = rdflib.Graph() + g.parse(ttl_file, format='ttl') + + CHAMEO = rdflib.Namespace("https://w3id.org/emmo/domain/chameo#") + g.bind('chameo', CHAMEO) + + context = {} + object_properties = {} + other_entries = {} + namespace_prefixes= {} + predicate = rdflib.URIRef(predicate_uri) + label = rdflib.URIRef(label_uri) + existing_keys = set() + + for s, p, o in g: + if (s, RDF.type, OWL.ObjectProperty) in g: + # If the subject is an OWL.ObjectProperty + label_value = g.value(s, SKOS.prefLabel) + if label_value: + object_properties[str(label_value)] = { + "@id": str(s), + "@type": "@id" + } + + + elif p == predicate: + # Normal context entry + # Use the label as key if it exists + #label_value = g.value(s, label) if g.value(s, label) else str(s) + label_value = str(s) + other_entries[str(o)] = str(label_value) + + + # Add namespace prefixes to the context + for prefix, uri in g.namespace_manager.namespaces(): + if len(prefix) >= 2: + namespace_prefixes[prefix] = str(uri) + + # Sort the entries alphabetically + sorted_object_properties = dict(sorted(object_properties.items())) + sorted_other_entries = dict(sorted(other_entries.items())) + sorted_namespace_prefixes = dict(sorted(namespace_prefixes.items())) + + # Merge the sorted entries + context = { + "@context": { + **sorted_namespace_prefixes, + **sorted_object_properties, + **sorted_other_entries + } + } + + print("Namespaces:") + for prefix, uri in g.namespace_manager.namespaces(): + print(f"{prefix}: {uri}") + + + return context + + +# Example usage +filename = 'electrochemistry-inferred.ttl' +parent_dir = os.path.dirname(os.path.abspath(__file__)) +file_path = os.path.join(parent_dir, '..', filename) + +# Convert the file path to a file URI +file_uri = urljoin('file:', pathname2url(file_path)) + +predicate_uri = 'http://www.w3.org/2004/02/skos/core#prefLabel' +context = generate_jsonld_context(file_uri, predicate_uri) + +context_file_path = os.path.join(os.path.dirname(file_path), 'context.json') + +# Save to JSON file +with open(context_file_path, 'w') as f: + json.dump(context, f, indent=4) +