diff --git a/service/src/main/inse/orgminer/institutions_helper.py b/service/src/main/inse/orgminer/institutions_helper.py index d22872da..4b1050bd 100644 --- a/service/src/main/inse/orgminer/institutions_helper.py +++ b/service/src/main/inse/orgminer/institutions_helper.py @@ -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)}"