From 62b190afeaa6cf9336cb97ab363b30143fd7beb9 Mon Sep 17 00:00:00 2001 From: genja Date: Fri, 18 Sep 2026 19:24:47 +0200 Subject: [PATCH] added name normalization functions for orgminer names matching --- service/src/main/inse/utils.py | 59 ++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/service/src/main/inse/utils.py b/service/src/main/inse/utils.py index 5eee5a23..0249de63 100644 --- a/service/src/main/inse/utils.py +++ b/service/src/main/inse/utils.py @@ -30,3 +30,62 @@ def extract_raw_topic_id(topic_id: str) -> str: if match: return match.group().split("/")[-1] raise ValueError("Invalid topic ID format") + +# Name normalization utilities for exact-match comparison, used in orgminer logic to +# match person names against staff lists in organization documents. +import unicodedata + +_SAFE_TITLE_WORDS = { + "prof", "dr", "pd", "apl", "jun", "em", + "ing", "rer", "nat", "med", "phil", "habil", + "mr", "mrs", "ms", + "msc", "bsc", "mba", "phd", "llm", "dipl", + "dring", "junprof", "diplinf", "diplkfm", +} +_AMBIGUOUS_SHORT_DEGREES = {"ma", "ba"} + + +def _is_title_token(token: str) -> bool: + normalized = re.sub(r"[.\-]", "", token).lower() + if normalized in _SAFE_TITLE_WORDS: + return True + if normalized in _AMBIGUOUS_SHORT_DEGREES and ("." in token or "-" in token): + return True + return False + + +def strip_titles(name: str) -> str: + """ + Drop leading title/degree tokens and trailing degree-suffix tokens, + e.g. 'Prof. Dr.-Ing. Hans Muller' -> 'Hans Muller'. + Commas are treated as whitespace so comma-separated suffixes are + caught by the same trailing-token strip. + """ + tokens = name.replace(",", " ").split() + start = 0 + while start < len(tokens) and _is_title_token(tokens[start]): + start += 1 + end = len(tokens) + while end > start and _is_title_token(tokens[end - 1]): + end -= 1 + return " ".join(tokens[start:end]) + + +def normalize_name(name: str, fold_diacritics: bool = False) -> str: + """ + Normalize a name for exact-match comparison. + + Steps: strip titles -> collapse whitespace -> lowercase. + Optionally also strips diacritics (accent-insensitive fallback tier). + """ + if not name: + return "" + result = strip_titles(name) + result = result.replace("-", " ") + result = re.sub(r"\s+", " ", result).strip() + result = result.lower() + if fold_diacritics: + result = "".join( + c for c in unicodedata.normalize("NFKD", result) if not unicodedata.combining(c) + ) + return result