Search reads more than the name: summary, places, author and land unit, strongest matches first

Each row's search words come in four tiers (name and abbreviation, author, the full names of
the states, provinces and countries plus where it starts and ends and the land unit or body its
record names, and the summary). Typed words match at the start of words in any order, without
regard to case, accents or apostrophes, and the strongest matches come first. A row that matched
only beyond its name says where in one quiet line.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-19 14:04:13 -07:00
1 parent ffc342b590
commit 3ff2281129
6 files changed
+453 -15

No files matched your search

+8 -6
View File
@@ -7,7 +7,7 @@ attributes in olt/facets.py, under the same names the trail pages use.
import collections
import math
from . import browse, common, datafiles, facets, hike, sketch, words
from . import browse, common, datafiles, facets, hike, searchtext, sketch, words
from .markup import Markup, esc
# Length range choices for the filters, round in the unit they are written in.
@@ -151,7 +151,7 @@ def route_by_html(rec):
return Markup(text)
def entry(rec, *, level=3, extra="", routes_list=False, hidden=False, eager=False, lead=None, rank=None, more=(), by=None, here=None):
def entry(rec, *, level=3, extra="", routes_list=False, hidden=False, eager=False, lead=None, rank=None, more=(), by=None, here=None, search=True):
"""One trail in a list. Its data-* attributes drive the filters in app.js.
level: the heading level of the trail's name. extra: HTML placed last in the row.
@@ -167,6 +167,8 @@ def entry(rec, *, level=3, extra="", routes_list=False, hidden=False, eager=Fals
names only the others a route is credited to); "" for none.
here: 0 for a trail wholly in the place a place page is about, 1 for one that lies elsewhere too
(data-here, the order that page opens in; see results()).
search: write the row's words for the search box (data-search, olt/searchtext.py); results() leaves
them out of a list with no search box over it.
The Save star follows the name in the markup (so it is the next stop for the
keyboard) but is drawn under the logo, out of the row's flow (style.css
@@ -174,8 +176,6 @@ def entry(rec, *, level=3, extra="", routes_list=False, hidden=False, eager=Fals
"""
route = facets.kind(rec) == "route"
lo, hi, est = facets.time_days(rec)
search = " ".join([rec["name"], rec.get("common_name", ""), rec.get("short_name", ""), rec["id"],
facets.author(rec)]).lower()
abbr = rec.get("short_name", "")
abbr_cls = "entry__abbr" if common.shield_url(rec) else "entry__abbr vh"
abbr_html = f' <span class="{abbr_cls}">{esc(abbr)}</span>' if abbr else ""
@@ -188,8 +188,10 @@ def entry(rec, *, level=3, extra="", routes_list=False, hidden=False, eager=Fals
"climb": climb_m if climb_m is not None else "",
"days": hi, "time": facets.time_band(rec), "months": " ".join(map(str, facets.months(rec))),
"terrain": " ".join(facets.terrain(rec)), "kind": facets.kind(rec), "walking": facets.walking(rec),
"shape": facets.shape(rec), "gpx": facets.gpx(rec), "name": sort_name(rec), "search": search.strip(),
"shape": facets.shape(rec), "gpx": facets.gpx(rec), "name": sort_name(rec),
}
if search:
data["search"] = searchtext.attribute(rec)
attrs = "".join(f' data-{k}="{esc(v)}"' for k, v in data.items())
if common.line_differs(rec):
attrs += ' data-partial="yes"'
@@ -588,7 +590,7 @@ def results(recs=None, *, finder=False, groups=None, routes_list=False, default_
eager=not saved and seen + i < EAGER_ROWS,
lead=lead(r) if lead else None, more=more(r) if more else (),
rank=seen + i + 1 if ranked else None, by=by,
here=(0 if wholly(r) else 1) if wholly else None) for i, r in enumerate(items))
here=(0 if wholly(r) else 1) if wholly else None, search=finder) for i, r in enumerate(items))
seen += len(items)
if isinstance(heading, tuple):
heading, intro = heading
+81
View File
@@ -0,0 +1,81 @@
"""The words a search box looks in (data-search on each row of a list; app.js matches against them).
A row's words come in four tiers, strongest first, so that the list can put the best matches first:
0 name the name, its common name, its abbreviation and its id
1 author who made a route
2 places the full names of the states and provinces it lies in, the countries, the place it
starts and the place it ends, and the land unit or managing body its record names
3 summary the trail's summary
Each tier is folded (fold) and written once per word (the summary's words of fewer than three letters are
left out): a word that a stronger tier has is left out of the
weaker ones, since a search asks for the strongest tier a word is found in. The tiers are joined with "|".
app.js folds what is typed the same way (its fold(), checked against this one by web/tests/test_search.py)
and finds each typed word at the start of a word of the row.
"""
import re
import unicodedata
from . import common, facets, hike, words
TIERS = 4
SUMMARY = 3 # the summary tier
SUMMARY_MIN = 3 # it holds words of this many letters or more, and a typed word is looked for there only if it has as many
# A designation that only says who made the route, and that no agency holds it: the author is in
# the author tier already, and "no agency or association" would match every route.
_NO_BODY = re.compile(r"^(person-defined route|guidebook route)\b", re.I)
_APOSTROPHES = re.compile("[’‘'`´]")
_NOT_WORD = re.compile(r"[\W_]+")
def fold(text):
"""Lower-case, without accents, apostrophes dropped ("Road's End" -> "roads end"), every other run of
anything that is not a letter or a digit a single space."""
text = unicodedata.normalize("NFD", str(text or ""))
text = "".join(c for c in text if not unicodedata.combining(c)).lower()
return _NOT_WORD.sub(" ", _APOSTROPHES.sub("", text)).strip()
def place_names(rec):
"""The names of the places a record carries: states, provinces and countries in full, where it
starts and ends, and the land unit or body of its designation."""
names = [facets.place_name(k) for k in facets.place_keys(rec)]
names += [words.COUNTRIES.get(c, c) for c in common.countries(rec)]
h = hike.table(rec)
names += [h.get("start", ""), h.get("end", "")]
designation = rec.get("designation", "")
if designation and not _NO_BODY.match(designation):
names.append(designation)
return [n for n in names if isinstance(n, str) and n.strip()]
def tier_texts(rec):
"""The four texts of a record, unfolded."""
return [
" ".join([rec["name"], rec.get("common_name", ""), rec.get("short_name", ""), rec["id"]]),
facets.author(rec),
" ".join(place_names(rec)),
hike.table(rec).get("summary", ""),
]
def tiers(rec):
"""The four tiers as lists of words, each word in the strongest tier that has it."""
seen = set()
out = []
for i, text in enumerate(tier_texts(rec)):
mine = []
for word in fold(text).split():
if i == SUMMARY and len(word) < SUMMARY_MIN:
continue
if word not in seen:
seen.add(word)
mine.append(word)
out.append(mine)
return out
def attribute(rec):
"""The value of a row's data-search: the tiers' words, tiers joined with "|"."""
return "|".join(" ".join(t) for t in tiers(rec))