added name normalization functions for orgminer names matching
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user