Let a role that names no town join one group, never bridge two, and place dupes above places

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Opus 5.5 committed 2026-09-29 22:07:05 +02:00
1 parent d060bee5b7
commit 1e22537ae5
3 files changed
+36 -8

No files matched your search

+25 -7
View File
@@ -11,9 +11,10 @@ Two rows are one posting when they have the same non-empty `company_key`, the
same `title_key`, the same `country` (or either has none), and the sets of
towns their places name share a member, where a row with no town matches
every town. Host does not matter. The same title in Berlin and in Munich
stays two roles, so a place filter still finds each. The join is transitive:
if A matches B and B matches C, all three are one group, although A and C
alone would not match (a row with no town bridges two towns for this reason).
stays two roles, so a place filter still finds each. The join is transitive
among rows that name a town. A row that names none joins a group only when
exactly one group of its company and title would take it, so a bare
"Germany" never merges Berlin and Munich into one role.
Each group keeps one copy (`keep_order`, smallest wins) and the rest point at
it. Pure by design: no store, no SQL, no network, so the rule is tested on
@@ -144,9 +145,10 @@ def assign(rows: Iterable[Copy]) -> dict[str, str | None]:
for the kept copy itself and for a row with no copies.
Rows are partitioned by `(company_key, title_key)`, a row with no
`company_key` standing alone. Within a partition `same_posting` joins
rows transitively (union-find), and each component points every row but
its `keep_order` minimum at that minimum."""
`company_key` standing alone. Within a partition `same_posting` joins the
rows that name a town transitively (union-find); a row naming none joins
the one group it matches, or stays out when it matches two. Each
component points every row but its `keep_order` minimum at that minimum."""
result: dict[str, str | None] = {}
parts: dict[tuple[str, str], list[Copy]] = {}
for row in rows:
@@ -164,10 +166,26 @@ def assign(rows: Iterable[Copy]) -> dict[str, str | None]:
i = parent[i]
return i
placed = [bool(towns(row.place, row.country)) for row in group]
for i, a in enumerate(group):
for j in range(i + 1, len(group)):
if find(i) != find(j) and same_posting(a, group[j]):
if (placed[i] and placed[j] and find(i) != find(j)
and same_posting(a, group[j])):
parent[find(j)] = find(i)
# A row without a town matches every town, so it would bridge two
# groups it matches; it joins only when there is one to join. Rows
# without a town that fit no placed group group among themselves.
loose = [i for i in range(len(group)) if not placed[i]]
for i in loose:
roots = {find(j) for j in range(len(group))
if placed[j] and same_posting(group[i], group[j])}
if len(roots) == 1:
parent[i] = roots.pop()
for n, i in enumerate(loose):
for j in loose[n + 1:]:
if (parent[i] == i and parent[j] == j
and same_posting(group[i], group[j])):
parent[j] = i
comps: dict[int, list[Copy]] = {}
for i, row in enumerate(group):
comps.setdefault(find(i), []).append(row)