#!/usr/bin/env python3 """Search-quality scorecard over the built site: the search_ref.py audit queries (contract v2 reference search, online + offline merged with the online-required filter, as the app does) scored against search_audit_compare.EXPECT (what a US user most likely wants first). python3 -I FoodIndex/scripts/search_eval.py [--mode contract|app] [--top 5] [--misses] [--cap N] [--queries q1 ...] Prints hit@1 / hit@3 / hit@5, and with --misses the top results of every query whose expected item is not first plus the lists of queries missed at 1, 3 and 5. Also reports per-query shard download sizes (raw and gzip). Stdlib only; reads only the built site and bundle. """ import argparse import os import re import sys HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) import search_ref as sr # noqa: E402 from search_audit_compare import EXPECT # noqa: E402 def main(): ap = argparse.ArgumentParser() ap.add_argument("--site", default=sr.DEFAULT_SITE) ap.add_argument("--bundle", default=sr.DEFAULT_BUNDLE) ap.add_argument("--top", type=int, default=5) ap.add_argument("--misses", action="store_true") ap.add_argument("--cap", type=int) ap.add_argument("--queries", nargs="*") ap.add_argument("--mode", choices=("contract", "app"), default="contract") a = ap.parse_args() site = sr.Site(a.site) if a.cap: site.cap = min(site.cap, a.cap) bundle = sr.Bundle(a.bundle) qs = a.queries or [q for g in sr.AUDIT.values() for q in g] h1 = h3 = h5 = n = 0 miss = {1: [], 3: [], 5: []} raw, gz = [], [] for q in qs: res = sr.run_query(site, bundle, q, a.mode) raw.append(res.get("shard_bytes", 0)) gz.append(res.get("shard_gzip", 0)) pat = EXPECT.get(q) top = res["top"] if pat is None: if a.queries: print(f"{q!r}: " + " / ".join(f"{h['name'][:40]}|{h['brand'][:15]}·{h['src']}·{h['kcal']:g}" for h in top[:a.top])) continue rx = re.compile(pat, re.I) ok = [bool(rx.search(f"{h['name']} | {h['brand']}")) for h in top[:5]] n += 1 h1 += ok[:1] == [True] h3 += any(ok[:3]) h5 += any(ok[:5]) for k in (1, 3, 5): if not any(ok[:k]): miss[k].append(q) if a.queries or (a.misses and not ok[:1] == [True]): mark = "OK " if ok[:1] == [True] else ("~3 " if any(ok[:3]) else "MISS") print(f"{mark} {q!r}: " + " / ".join(f"{h['name'][:40]}|{h['brand'][:15]}·{h['src']}·{h['kcal']:g}·{h['final']}" for h in top[:a.top])) raw.sort() gz.sort() med = lambda v: v[len(v) // 2] if v else 0 # noqa: E731 print(f"hit@1 {h1}/{n} hit@3 {h3}/{n} hit@5 {h5}/{n} | shards per query: median {med(raw)/1024:.0f} KB raw " f"/ {med(gz)/1024:.0f} KB gzip, max {max(raw)/1024:.0f} KB raw / {max(gz)/1024:.0f} KB gzip, " f">500 KB raw: {sum(1 for x in raw if x > 500 * 1024)}") if a.misses: for k in (1, 3, 5): print(f"miss@{k} ({len(miss[k])}): " + ", ".join(repr(q) for q in miss[k])) if __name__ == "__main__": main()