@@ -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
0 commit comments