Spaces:
Sleeping
Sleeping
| """ | |
| query_builder.py β turn the 4-toggle selection into a natural-language query. | |
| Mirrors the logic in src/10_query_construction.py but as a pure function | |
| (no interactive prompts). `priority` is multi-select; the schema enforces a | |
| list, but we tolerate string/None defensively. | |
| """ | |
| from __future__ import annotations | |
| from .schemas import ToggleSelection | |
| _CUISINE_NO_PREF = {"No preference", "no preference", "Any", "any", None, ""} | |
| _PRIORITY_NONE = {"None", "none", None, ""} | |
| def _priority_list(p) -> list[str]: | |
| if p is None: | |
| return [] | |
| if isinstance(p, list): | |
| return p | |
| return [p] | |
| def build_query(t: ToggleSelection, free_text: str | None = None) -> str: | |
| """ | |
| Compose a query string from toggles + optional free text. | |
| Order matches 10_query_construction.py: cuisine β vibe β occasion β priority, | |
| with free_text prepended when provided. | |
| """ | |
| parts: list[str] = [] | |
| if t is None: | |
| return (free_text or "").strip() | |
| if t.cuisine and t.cuisine not in _CUISINE_NO_PREF: | |
| parts.append(t.cuisine) | |
| if t.vibe: | |
| parts.append(t.vibe) | |
| if t.occasion: | |
| parts.append(t.occasion) | |
| priorities = [p for p in _priority_list(t.priority) if p not in _PRIORITY_NONE] | |
| if priorities: | |
| parts.append(" ".join(priorities)) | |
| built = " ".join(parts).strip() | |
| if free_text and free_text.strip(): | |
| return f"{free_text.strip()} {built}".strip() if built else free_text.strip() | |
| return built | |