Lead a role in several cities with the one nearest the viewing agent, in the row, the reader and the posting link
Places are ordered in tiers off the agent's gates (the any group, then the require scope, then the rest), and within a tier by the home the any group gives, since the gazetteer holds no coordinates. GET /api/roles and /api/roles/{uid} carry place_texts, lead_place and lead_url for the roles served; the gates and the dupes rule are unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ac4cdb8a48
commit
fb24c8e4c9
8 files changed
+277
-29
No files matched your search
+105
-6
@@ -52,13 +52,13 @@ from collections.abc import Callable, Iterable, Sequence
|
||||
from dataclasses import dataclass
|
||||
from functools import lru_cache
|
||||
|
||||
from app.models import (_CITIES, _COUNTRY_NAMES, _EUROPE, _REGIONS, _SUBDIVISIONS, _place_fold,
|
||||
country_of, remote_of)
|
||||
from app.models import (_CITIES, _COUNTRY_NAMES, _EUROPE, _REGIONS, _SUBDIVISIONS, Gate,
|
||||
_place_fold, country_of, remote_of)
|
||||
|
||||
__all__ = ["ANYWHERE", "ANYWHERE_WORDS", "KINDS", "REGIONS", "SQL_FUNCTION", "Gazetteer", "Place",
|
||||
"Region", "Seed", "Suggestion", "Token", "fold", "known", "label", "match_sql",
|
||||
"matches", "parse", "placeless", "register", "remote_part", "seed", "suggest",
|
||||
"towns"]
|
||||
__all__ = ["ANYWHERE", "ANYWHERE_WORDS", "KINDS", "REGIONS", "SQL_FUNCTION", "Gazetteer",
|
||||
"Nearness", "Place", "Region", "Seed", "Suggestion", "Token", "fold", "known", "label",
|
||||
"match_sql", "matches", "nearest_first", "parse", "placeless", "register",
|
||||
"remote_part", "seed", "suggest", "towns"]
|
||||
|
||||
|
||||
def fold(text: str) -> str:
|
||||
@@ -1255,3 +1255,102 @@ class Gazetteer:
|
||||
else:
|
||||
found = {p for form in _expand(tok.value) for p in self.aliases.get(form, ())}
|
||||
return frozenset(found) or None
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ nearness
|
||||
|
||||
class Nearness:
|
||||
"""How near each place of a role is to one agent, read off its gates: a
|
||||
role posted in several cities is shown, and linked, by the nearest.
|
||||
|
||||
Three tiers. A place comes first when it meets the agent's `any` group:
|
||||
it lies under one of the group's `country` or `place` values, or it is a
|
||||
remote part whose area lies under one of them, is open to the agent
|
||||
through a `remote_from` value as the remote chips match, or meets the
|
||||
agent's scope (its `require` gates on country) under a plain `remote`
|
||||
any gate, as `remote:any` does. A place in the scope comes second, the rest third, a remote part with no
|
||||
stated area ("Remote") among them. The gazetteer holds no coordinates,
|
||||
so within a tier the home stands in for distance: the first value of
|
||||
the first `any` gate on country or place (Denmark for "DK, SE"). A
|
||||
place in one of the group's own towns inside the home comes first
|
||||
(Copenhagen and Odense), then one elsewhere in the home (Billund,
|
||||
"Denmark"), then the rest, each in the host's order. The gates
|
||||
themselves never read this: they read every place."""
|
||||
|
||||
def __init__(self, gaz: Gazetteer, gates: Iterable[Gate]) -> None:
|
||||
self.gaz = gaz
|
||||
near: set[int] = set()
|
||||
towns: set[int] = set()
|
||||
home: frozenset[int] | None = None
|
||||
self._words: list[str] = []
|
||||
self._remote_any = False
|
||||
self._remote_from: set[int] = set()
|
||||
scopes: list[frozenset[int]] = []
|
||||
for gate in gates:
|
||||
values = [str(v).strip() for v in gate.values if str(v).strip()]
|
||||
if gate.field == "country" and gate.mode == "require":
|
||||
scopes.append(frozenset(p for v in values for p in gaz.lookup(f"country:{v}") or ()))
|
||||
if gate.mode != "any":
|
||||
continue
|
||||
if gate.field == "remote":
|
||||
self._remote_any |= any(fold(v) == "remote" for v in values)
|
||||
elif gate.field == "remote_from":
|
||||
for value in values:
|
||||
tok = parse(f"remote:{value}")
|
||||
if tok.value == "any":
|
||||
self._remote_any = True
|
||||
elif named := gaz.lookup(str(tok)):
|
||||
self._remote_from |= named if tok.value == "anywhere" else gaz.overlap(named)
|
||||
elif gate.field in ("country", "place"):
|
||||
for value in values:
|
||||
token = f"country:{value}" if gate.field == "country" else value
|
||||
named = gaz.lookup(token)
|
||||
if named is None:
|
||||
self._words.append(token)
|
||||
continue
|
||||
near |= named
|
||||
if gate.field == "place":
|
||||
towns |= named
|
||||
if home is None:
|
||||
home = named
|
||||
self._near = gaz.under(near)
|
||||
self._home = gaz.under(home or ())
|
||||
self._home_towns = gaz.under(p for p in towns if p in self._home)
|
||||
self._scope = [gaz.under(s) for s in scopes]
|
||||
self._scope_areas = [gaz.overlap(s) for s in scopes]
|
||||
|
||||
def _in_scope(self, ids: Sequence[int]) -> bool:
|
||||
return bool(self._scope) and any(all(p in s for s in self._scope) for p in ids)
|
||||
|
||||
def _area_in_scope(self, area: Sequence[int]) -> bool:
|
||||
return all(any(p in s for p in area) for s in self._scope_areas)
|
||||
|
||||
def key(self, text: str, ids: Iterable[int | None]) -> tuple[int, int]:
|
||||
"""`(tier, 0 in a home town, 1 in the home, else 2)` of one place
|
||||
text and the places it resolved to, lower nearer."""
|
||||
found = [p for p in ids if p is not None and p in self.gaz.places]
|
||||
area = self.gaz.remote_places(text)
|
||||
named = any(p in self._near for p in (*found, *area)) or (
|
||||
not found and any(matches(fold(text), "", w) for w in self._words))
|
||||
remote = bool(area) and (any(p in self._remote_from for p in area)
|
||||
or (self._remote_any and self._area_in_scope(area)))
|
||||
if named or remote:
|
||||
tier = 1
|
||||
elif self._in_scope(found) or (area and self._scope and self._area_in_scope(area)):
|
||||
tier = 2
|
||||
else:
|
||||
tier = 3
|
||||
both = (*found, *area)
|
||||
return tier, (0 if any(p in self._home_towns for p in both)
|
||||
else 1 if any(p in self._home for p in both) else 2)
|
||||
|
||||
|
||||
def nearest_first(near: Nearness | None,
|
||||
entries: Sequence[tuple[str, Sequence[int | None]]]) -> list[int]:
|
||||
"""The indices of a role's places `(text, place ids)` nearest first
|
||||
(`Nearness.key`), the host's order within a key and throughout without
|
||||
an agent."""
|
||||
order = list(range(len(entries)))
|
||||
if near is None:
|
||||
return order
|
||||
return sorted(order, key=lambda i: near.key(*entries[i]))
|
||||
Reference in new issue
Block a user