Coverage for src/data_api/utils/search.py: 100%
9 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-13 19:28 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-13 19:28 +0000
1"""Shared fuzzy ranking for nested published records."""
3from thefuzz import fuzz
6def fuzzy_matches(items: list[dict], query: str, field: str, threshold: int) -> list[dict]:
7 scored = []
8 for item in items:
9 score = fuzz.partial_ratio(query, item.get(field) or "")
10 if score >= threshold:
11 scored.append((score, item))
12 scored.sort(key=lambda pair: pair[0], reverse=True)
13 return [item for _, item in scored]