Landtag lists: match existing persons by birth date, Landtag membership and name variants
- person_by_name rules out homonyms born on another day and prefers the one who sat in that Landtag; failing an exact name, accepts a unique variant among that Landtag's members (Rudi/Rudolf, Chiara-Sophia/Chiara, new double surnames) - normalize() maps ß to ss like SQL unaccent(); migration 0008 aligns stored aliases - clean_name drops image captions, parenthesised suffixes, LTP, other abbreviations and zero-width characters Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
faf9572b9f
commit
68be25a953
10 files changed
+114
-13
No files matched your search
@@ -162,7 +162,7 @@ class Loader:
|
||||
def person(self, rec: PersonRecord) -> int | None:
|
||||
person_id = self.resolver.person_by_ids(rec.ref.ids)
|
||||
if person_id is None and rec.match_by_name:
|
||||
person_id = self.resolver.person_by_name(rec.full_name)
|
||||
person_id = self.resolver.person_by_name(rec.full_name, rec.birth_date, rec.match_landtag)
|
||||
if person_id is None:
|
||||
if not self.ctx.creates_persons:
|
||||
self.ctx.stats["persons_not_created"] += 1
|
||||
|
||||
@@ -98,6 +98,9 @@ class PersonRecord:
|
||||
#: if no identifier matches, merge with an existing person of exactly this (unambiguous) name before
|
||||
#: creating a new one - for authoritative sources that share no id with others (e.g. EP)
|
||||
match_by_name: bool = False
|
||||
#: with match_by_name: state key ('noe', ...) of a Landtag list, to tell homonyms apart and accept name
|
||||
#: variants among that Landtag's members (Resolver.person_by_name)
|
||||
match_landtag: str | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
|
||||
@@ -28,6 +28,24 @@ OFFICE_FILLER = re.compile(r"\b(der republik osterreich|der bundesrepublik oster
|
||||
r"\(osterreich\)")
|
||||
|
||||
|
||||
def name_parts(name: str) -> tuple[str, str]:
|
||||
"""'Mag. Chiara-Sophia Glawogger' -> ('chiara', 'glawogger'): first given name and surname, normalised."""
|
||||
tokens = normalize(strip_titles(name)).replace(",", " ").split()
|
||||
if len(tokens) < 2:
|
||||
return "", ""
|
||||
return tokens[0].split("-")[0], tokens[-1]
|
||||
|
||||
|
||||
def variant_of(a: str, b: str) -> bool:
|
||||
"""Same person written differently: surnames equal or one is part of the other's double surname
|
||||
('Kirchmayr' / 'Kirchmayr-Felbermaier'), first names share their first three letters ('Rudi' / 'Rudolf')."""
|
||||
(ga, sa), (gb, sb) = name_parts(a), name_parts(b)
|
||||
if not (ga and sa and gb and sb):
|
||||
return False
|
||||
surname = sa == sb or sa in sb.split("-") or sb in sa.split("-")
|
||||
return surname and len(ga) >= 3 and len(gb) >= 3 and ga[:3] == gb[:3]
|
||||
|
||||
|
||||
def office_key(title: str) -> str:
|
||||
"""'Bundesministerin für Justiz' / 'Bundeskanzler der Republik Österreich' -> 'bundesminister fur justiz' /
|
||||
'bundeskanzler' (gender-neutral, without filler, punctuation-free) for comparing office titles."""
|
||||
@@ -109,18 +127,54 @@ class Resolver:
|
||||
self._person_cache[cache_key] = found
|
||||
return found
|
||||
|
||||
def person_by_name(self, name: str) -> int | None:
|
||||
def person_by_name(self, name: str, birth_date: date | None = None, landtag: str | None = None) -> int | None:
|
||||
"""Exact alias match. A known birth date rules out persons born on another day; `landtag` (state key such
|
||||
as 'noe') prefers, among homonyms, the one who has sat in that Landtag, and falls back to name variants
|
||||
among that Landtag's members (landtag_member_by_variant)."""
|
||||
norm = normalize(strip_titles(name))
|
||||
rows = self.conn.execute(
|
||||
"""SELECT DISTINCT a.entity_id AS id, ps.status
|
||||
FROM alias a LEFT JOIN person_status ps ON ps.person_id = a.entity_id
|
||||
"""SELECT DISTINCT a.entity_id AS id, ps.status, p.birth_date,
|
||||
%s::text IS NOT NULL AND EXISTS (
|
||||
SELECT 1 FROM role r JOIN claim c ON c.id = r.claim_id
|
||||
WHERE r.person_id = a.entity_id AND c.merged_into IS NULL AND r.level = 'state'
|
||||
AND (landtag_key(r.role) = %s OR c.created_by = 'landtag.' || %s)) AS in_landtag
|
||||
FROM alias a JOIN person p ON p.id = a.entity_id
|
||||
LEFT JOIN person_status ps ON ps.person_id = a.entity_id
|
||||
WHERE a.entity_type = 'person' AND a.alias_norm = %s""",
|
||||
(norm,),
|
||||
(landtag, landtag, landtag, norm),
|
||||
).fetchall()
|
||||
if birth_date:
|
||||
same = [r for r in rows if r["birth_date"] == birth_date]
|
||||
if len(same) == 1:
|
||||
return same[0]["id"]
|
||||
rows = [r for r in rows if r["birth_date"] is None]
|
||||
if len(rows) == 1:
|
||||
return rows[0]["id"]
|
||||
active = [r for r in rows if r["status"] == "active"]
|
||||
return active[0]["id"] if len(active) == 1 else None
|
||||
for preferred in ([r for r in rows if r["in_landtag"]], [r for r in rows if r["status"] == "active"]):
|
||||
if len(preferred) == 1:
|
||||
return preferred[0]["id"]
|
||||
if not rows and landtag:
|
||||
return self.landtag_member_by_variant(name, landtag, birth_date)
|
||||
return None
|
||||
|
||||
def landtag_member_by_variant(self, name: str, landtag: str, birth_date: date | None = None) -> int | None:
|
||||
"""A Landtag list writes 'Rudi Hemetsberger', 'Chiara-Sophia Glawogger' or 'Helena Kirchmayr-Felbermaier'
|
||||
where Wikidata has 'Rudolf Hemetsberger', 'Chiara Glawogger' and 'Helena Kirchmayr'. Among persons who
|
||||
have sat in that Landtag, accept the single one whose surname matches (or is one part of a double surname)
|
||||
and whose first name shares its first three letters, unless the birth dates differ."""
|
||||
given, surname = name_parts(name)
|
||||
if not given or not surname:
|
||||
return None
|
||||
rows = self.conn.execute(
|
||||
"""SELECT DISTINCT p.id, p.full_name, p.birth_date FROM person p
|
||||
JOIN role r ON r.person_id = p.id JOIN claim c ON c.id = r.claim_id
|
||||
WHERE c.merged_into IS NULL AND r.level = 'state' AND landtag_key(r.role) = %s""",
|
||||
(landtag,),
|
||||
).fetchall()
|
||||
hits = {row["id"] for row in rows
|
||||
if not (birth_date and row["birth_date"] and row["birth_date"] != birth_date)
|
||||
and variant_of(name, row["full_name"])}
|
||||
return hits.pop() if len(hits) == 1 else None
|
||||
|
||||
def _person_by_hints(self, ref: PersonRef) -> int | None:
|
||||
hints = ref.hints
|
||||
|
||||
Reference in new issue
Block a user