Skip to content

Commit

Permalink
Update ttl_to_context.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jsimonclark committed Mar 28, 2024
1 parent ea3383b commit 3172d48
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions docs/scripts/ttl_to_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def generate_jsonld_context(ttl_file, predicate_uri, label_uri='http://www.w3.or
"""
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 = {}
Expand All @@ -44,21 +47,34 @@ def generate_jsonld_context(ttl_file, predicate_uri, label_uri='http://www.w3.or
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 = 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():
namespace_prefixes[prefix] = str(uri)
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 = {**sorted_namespace_prefixes, **sorted_object_properties, **sorted_other_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

Expand Down

0 comments on commit 3172d48

Please sign in to comment.