added normalize org uri helper

This commit is contained in:
2026-11-17 21:16:10 +01:00
parent 8322fea036
commit 22997f6a16
@@ -39,6 +39,21 @@ def strip_openalex_prefix(institution_id: str) -> str:
return institution_id.rsplit("/", 1)[-1]
def normalize_org_uri(uri: str | None) -> str | None:
"""Normalize an organization URL so hierarchy links compare reliably.
Trims whitespace, drops a trailing slash and the common ``/index`` suffix,
so ``https://www.example.org/x/``, ``https://www.example.org/x/index`` and
``https://www.example.org/x`` all map to the same value.
"""
if not uri:
return None
normalized = uri.strip().rstrip("/")
if normalized.endswith("/index"):
normalized = normalized[: -len("/index")].rstrip("/")
return normalized or None
def full_institution_url(institution_id: str) -> str:
"""Normalize any institution id form to the full OpenAlex URL form."""
return f"https://openalex.org/{strip_openalex_prefix(institution_id)}"