in src/graph_notebook/network/sparql/SPARQLNetwork.py [0:0]
def extract_prefix(self, uri: str) -> str:
"""
extracts the prefix and stores the namespace of a given uri to shorten the text
used for displaying to the user
:param uri: the full uri value. such as http://kelvinlawrence.net/air-routes/resource/24
:return: the prefix of the uri, such as 'resource'
"""
if not (uri.startswith('http://') or uri.startswith('https://')):
return None
hash_index = uri.find('#')
last_slash_index = uri.rfind('/', 0, hash_index)
if hash_index != -1: # for example: http://www.w3.org/1999/02/22-rdf-syntax-ns#type
namespace = uri[:hash_index + 1]
prefix = uri[last_slash_index + 1:hash_index]
else:
second_last_slash_index = uri.rindex('/', 0, last_slash_index)
namespace = uri[:last_slash_index + 1]
prefix = uri[second_last_slash_index + 1:last_slash_index]
if namespace in self.namespace_to_prefix:
return self.namespace_to_prefix[namespace]
else:
if prefix in self.prefix_to_namespace:
# this prefix is already reserved, we need to generate a new one.
# look at the previous section and attempt to append it to the prefix
num = 2
while True:
generated_prefix = f'{prefix}-{num}'
if generated_prefix not in self.prefix_to_namespace:
prefix = generated_prefix
break
else:
num += 1
self.namespace_to_prefix[namespace] = prefix
self.prefix_to_namespace[prefix] = namespace
return prefix