Skip to content

Commit 3442454

Browse files
committed
feat: add additional context options
1 parent e4e1a25 commit 3442454

3 files changed

Lines changed: 83 additions & 23 deletions

File tree

‎src/osw/core.py‎

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,8 @@ class JsonLdMode(str, Enum):
13121312
frame = "frame"
13131313

13141314
class ExportJsonLdParams(OswBaseModel):
1315+
context_loader_config: Optional[WtSite.JsonLdContextLoaderParams] = None
1316+
"""The configuration for the JSON-LD context loader."""
13151317
entities: Union[OswBaseModel, List[OswBaseModel]]
13161318
"""The entities to convert to JSON-LD. Can be a single entity or a list of
13171319
entities."""
@@ -1320,8 +1322,9 @@ class ExportJsonLdParams(OswBaseModel):
13201322
mode: Optional[OSW.JsonLdMode] = "expand"
13211323
"""The JSON-LD processing mode to apply if resolve_context is True."""
13221324
context: Optional[Union[str, list, Dict[str, Any]]] = None
1323-
"""The JSON-LD context to use for compacted mode.
1324-
If not set, the existing context is used"""
1325+
"""The JSON-LD context to apply. Replaces any existing context."""
1326+
additional_context: Optional[Union[str, list, Dict[str, Any]]] = None
1327+
"""The JSON-LD context to apply on top of the existing context."""
13251328
frame: Optional[Dict[str, Any]] = None
13261329
"""The JSON-LD frame to use for framed mode. If not set, the existing context is used"""
13271330
build_rdf_graph: Optional[bool] = False
@@ -1348,7 +1351,9 @@ def export_jsonld(self, params: ExportJsonLdParams) -> ExportJsonLdResult:
13481351
"""Exports the given entity/entities as JSON-LD."""
13491352

13501353
if params.resolve_context:
1351-
jsonld.set_document_loader(self.site.get_jsonld_context_loader())
1354+
jsonld.set_document_loader(
1355+
self.site.get_jsonld_context_loader(params.context_loader_config)
1356+
)
13521357

13531358
documents = []
13541359
graph_document = {"@graph": []}
@@ -1363,17 +1368,29 @@ def export_jsonld(self, params: ExportJsonLdParams) -> ExportJsonLdResult:
13631368
data = json.loads(e.json(exclude_none=True, indent=4, ensure_ascii=False))
13641369

13651370
data["@context"] = []
1366-
for t in e.type:
1367-
data["@context"].append("/wiki/" + t)
1368-
if params.context is not None:
1369-
data["@context"].append(params.context)
1371+
if params.context is None:
1372+
for t in e.type:
1373+
data["@context"].append("/wiki/" + t)
1374+
if params.context is not None:
1375+
data["@context"].append(params.context)
1376+
else:
1377+
data["@context"] = {
1378+
**self.site.get_jsonld_context_prefixes(),
1379+
**params.context,
1380+
}
1381+
if params.additional_context is not None:
1382+
if data["@context"] is None:
1383+
data["@context"] = []
1384+
elif not isinstance(data["@context"], list):
1385+
data["@context"] = [data["@context"]]
1386+
data["@context"].append(params.additional_context)
1387+
13701388
data["@id"] = get_full_title(e)
1371-
# print(data)
1389+
13721390
if params.resolve_context:
13731391
graph_document["@graph"].append(jsonld.expand(data))
13741392
if params.mode == "expand":
13751393
data = jsonld.expand(data)
1376-
print(data)
13771394
if isinstance(data, list):
13781395
data = data[0]
13791396
elif params.mode == "flatten":
@@ -1397,7 +1414,7 @@ def export_jsonld(self, params: ExportJsonLdParams) -> ExportJsonLdResult:
13971414

13981415
if params.build_rdf_graph:
13991416
graph.parse(data=json.dumps(data), format="json-ld")
1400-
print(data)
1417+
14011418
documents.append(data)
14021419

14031420
result = OSW.ExportJsonLdResult(

‎src/osw/wtsite.py‎

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,24 @@ def get_prefix_dict(self):
960960
)
961961
return prefix_dict
962962

963-
def replace_jsonld_context_mapping(self, context: Union[str, list, dict]):
963+
def get_jsonld_context_prefixes(self):
964+
"""Returns a dictionary with the custom prefixes used in osl for jsonld context"""
965+
context = {}
966+
for prefix, iri in self.get_prefix_dict().items():
967+
context[prefix] = {
968+
"@id": iri,
969+
"@prefix": True,
970+
}
971+
return context
972+
973+
class JsonLdContextLoaderParams(OswBaseModel):
974+
prefer_external_vocal: Optional[bool] = True
975+
"""Whether to prefer external vocabularies (e.g. skos, schema, etc.)
976+
over the local properties (Namespace 'Property:')"""
977+
978+
def _replace_jsonld_context_mapping(
979+
self, context: Union[str, list, dict], config: JsonLdContextLoaderParams
980+
):
964981
"""
965982
interate over a jsonld context
966983
set <prefix> to the value of <prefix>* if not set yet
@@ -971,7 +988,7 @@ def replace_jsonld_context_mapping(self, context: Union[str, list, dict]):
971988
if isinstance(context, str):
972989
return context
973990
if isinstance(context, list):
974-
return [self.replace_jsonld_context_mapping(e) for e in context]
991+
return [self._replace_jsonld_context_mapping(e, config) for e in context]
975992
if isinstance(context, dict):
976993
context_iter = context.copy()
977994
for key in context_iter:
@@ -984,28 +1001,48 @@ def replace_jsonld_context_mapping(self, context: Union[str, list, dict]):
9841001
if base_key not in context:
9851002
context[base_key] = value
9861003
# print(f"apply {key} to {base_key}")
1004+
if config.prefer_external_vocal is False:
1005+
base_mapping = context[base_key]
1006+
if isinstance(base_mapping, dict):
1007+
base_mapping = base_mapping["@id"]
1008+
mapping = value
1009+
if mapping.startswith(
1010+
"Property:"
1011+
) and not base_mapping.startswith("Property:"):
1012+
context[base_key] = value
1013+
# print(f"apply {key} to {base_key}")
9871014
elif isinstance(value, list):
9881015
context[key] = [
989-
self.replace_jsonld_context_mapping(e) for e in value
1016+
self._replace_jsonld_context_mapping(e, config) for e in value
9901017
]
9911018
elif isinstance(value, dict):
9921019
if "@id" in value:
9931020
base_key = key.split("*")[0]
9941021
if base_key not in context:
9951022
context[base_key] = value
996-
print(f"apply {key} to {base_key}")
1023+
# print(f"apply {key} to {base_key}")
1024+
if config.prefer_external_vocal is False:
1025+
base_mapping = context[base_key]
1026+
if isinstance(base_mapping, dict):
1027+
base_mapping = base_mapping["@id"]
1028+
mapping = value["@id"]
1029+
if mapping.startswith(
1030+
"Property:"
1031+
) and not base_mapping.startswith("Property:"):
1032+
context[base_key] = value
1033+
# print(f"apply {key} to {base_key}")
9971034
elif "@context" in value:
998-
context[key] = self.replace_jsonld_context_mapping(
999-
value["@context"]
1035+
context[key] = self._replace_jsonld_context_mapping(
1036+
value["@context"], config
10001037
)
10011038
return context
10021039

1003-
def get_jsonld_context_loader(self, *args, **kwargs):
1040+
def get_jsonld_context_loader(self, params: JsonLdContextLoaderParams = None):
1041+
if params is None:
1042+
params = self.JsonLdContextLoaderParams()
1043+
10041044
"""to overwrite the default jsonld document loader to load
10051045
relative context from the osl"""
1006-
requests_loader = pyld.documentloader.requests.requests_document_loader(
1007-
*args, **kwargs
1008-
)
10091046

10101047
def loader(url, options=None):
10111048
if options is None:
@@ -1020,8 +1057,8 @@ def loader(url, options=None):
10201057
schema = page.get_slot_content("jsonschema")
10211058
if isinstance(schema, str):
10221059
schema = json.loads(schema)
1023-
schema["@context"] = self.replace_jsonld_context_mapping(
1024-
schema["@context"]
1060+
schema["@context"] = self._replace_jsonld_context_mapping(
1061+
schema["@context"], params
10251062
)
10261063

10271064
doc = {
@@ -1034,6 +1071,9 @@ def loader(url, options=None):
10341071
return doc
10351072

10361073
else:
1074+
requests_loader = (
1075+
pyld.documentloader.requests.requests_document_loader()
1076+
)
10371077
return requests_loader(url, options)
10381078

10391079
return loader

‎tests/integration/test_rdf_generation.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ def test_local_rdf_graph_with_remote_context(wiki_domain, wiki_username, wiki_pa
2929

3030
res = osw_obj.export_jsonld(
3131
params=OSW.ExportJsonLdParams(
32-
entities=[i1, i2], mode=OSW.JsonLdMode.expand, build_rdf_graph=True
32+
entities=[i1, i2],
33+
mode=OSW.JsonLdMode.expand,
34+
build_rdf_graph=True,
35+
additional_context="https://json-ld.org/contexts/remote-context.jsonld",
3336
)
3437
)
3538

0 commit comments

Comments
 (0)