File size: 10,043 Bytes
89d9642
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
"""The recommendation engine: healthier, similar, and safe for this person.

Three stages, in order:

  1. Candidate generation — products of the same *kind*. Constrained to a
     shared category tag, which is what stops a soda from returning a candy
     bar. Falls back to ingredient overlap for the ~49% of US products that
     carry no category at all.

  2. Hard filters — allergens, diet, and health-condition ceilings, applied in
     SQL before the LIMIT so the pool is filtered rather than truncated.

  3. Ranking — a weighted composite of health gain, macro similarity and
     ingredient overlap, with every component returned so the UI can explain
     each result rather than assert it.
"""

from __future__ import annotations

from dataclasses import dataclass, field

import numpy as np
import pandas as pd

from nutriweb.data import catalog
from nutriweb.profile.model import UserProfile
from nutriweb.reco import filters, similarity

# Ranking weights. They sum to 1.0 and live here so they can be justified and
# tuned in one place rather than being scattered through the query.
W_HEALTH_GAIN = 0.45
W_MACRO = 0.30
W_INGREDIENT = 0.20
W_POPULARITY = 0.05

# A category tag must cover at least this many scored products to be a usable
# candidate pool; otherwise we widen to a more general tag.
MIN_POOL = 12

# Ceiling on how many candidates we pull into Python for ranking. Candidates
# are ordered by popularity so the cap keeps the well-known products.
MAX_CANDIDATES = 1500


@dataclass
class Recommendation:
    product: dict
    health_gain: float
    macro_similarity: float
    ingredient_similarity: float
    score: float
    shared_macros: list[str] = field(default_factory=list)
    warnings: list[str] = field(default_factory=list)

    @property
    def why(self) -> str:
        """One line explaining this specific swap."""
        parts = [f"+{self.health_gain:.0f} health points"]
        if self.shared_macros:
            parts.append("similar " + ", ".join(self.shared_macros[:3]))
        if self.ingredient_similarity >= 0.3:
            parts.append(f"{self.ingredient_similarity * 100:.0f}% shared ingredients")
        return " · ".join(parts)


from nutriweb.util import tag_set as _tag_set  # noqa: E402


def choose_category(product: dict) -> str | None:
    """Pick the most specific category tag with a large enough candidate pool.

    `categories_tags` is not reliably ordered general-to-specific, so we rank
    the product's tags by how many products sit under each and take the
    smallest that still clears MIN_POOL. Smallest pool == most specific.
    """
    tags = [
        t for t in _tag_set(product.get("categories_tags"))
        if t not in ("en:null", "en:undefined")
    ]
    if not tags:
        return None

    sizes = catalog.category_sizes(tags)
    usable = [(n, t) for t, n in sizes.items() if n >= MIN_POOL]
    if usable:
        return min(usable)[1]  # smallest qualifying pool
    # Nothing clears the bar; use the broadest tag we have rather than nothing.
    return max(((n, t) for t, n in sizes.items()), default=(0, None))[1]


def _candidates_by_category(
    category: str, code: str, min_health: float, where: str, params: list
) -> pd.DataFrame:
    return catalog.connect().execute(
        f"""
        SELECT {catalog.PRODUCT_COLUMNS}
        FROM catalog
        WHERE list_contains(categories_tags, ?)
          AND code <> ?
          AND health_score IS NOT NULL
          AND health_score > ?
          AND {where}
        ORDER BY COALESCE(unique_scans_n, 0) DESC
        LIMIT {MAX_CANDIDATES}
        """,
        [category, code, min_health, *params],
    ).fetchdf()


def _candidates_by_ingredients(
    ingredient_tags: list[str], code: str, min_health: float, where: str, params: list
) -> pd.DataFrame:
    """Fallback for products with no category: nearest by ingredient overlap."""
    if not ingredient_tags:
        return pd.DataFrame()
    return catalog.connect().execute(
        f"""
        SELECT {catalog.PRODUCT_COLUMNS},
               len(list_intersect(ingredients_tags, ?::VARCHAR[])) AS overlap
        FROM catalog
        WHERE list_has_any(COALESCE(ingredients_tags, []::VARCHAR[]), ?::VARCHAR[])
          AND code <> ?
          AND health_score IS NOT NULL
          AND health_score > ?
          AND {where}
        ORDER BY overlap DESC, COALESCE(unique_scans_n, 0) DESC
        LIMIT {MAX_CANDIDATES}
        """,
        [ingredient_tags, ingredient_tags, code, min_health, *params],
    ).fetchdf()


def recommend(
    product: dict, profile: UserProfile, top_n: int = 8
) -> tuple[list[Recommendation], dict]:
    """Return ranked healthier alternatives plus a note on how they were found.

    The second element describes the search (which category was used, how many
    candidates survived filtering) so the UI can be honest when results are
    thin rather than silently showing three items.
    """
    source_health = product.get("health_score")
    if source_health is None or pd.isna(source_health):
        return [], {"reason": "unscored", "message": "This product has no health score, so we cannot compare it."}

    source_health = float(source_health)
    where, params = filters.sql_exclusions(profile)

    category = choose_category(product)
    if category:
        frame = _candidates_by_category(
            category, product["code"], source_health, where, params
        )
        basis = {"mode": "category", "category": category}
    else:
        frame = _candidates_by_ingredients(
            sorted(_tag_set(product.get("ingredients_tags"))),
            product["code"], source_health, where, params,
        )
        basis = {"mode": "ingredients", "category": None}

    basis["pool"] = len(frame)
    if frame.empty:
        basis.update(_diagnose_empty(product, source_health, category))
        return [], basis

    ranked = _rank(product, frame, source_health, profile, top_n)
    basis["returned"] = len(ranked)
    return ranked, basis


def _diagnose_empty(product: dict, source_health: float, category: str | None) -> dict:
    """Work out *why* there are no results, so the UI can say something true.

    An empty pool has three quite different causes, and telling a user their
    filters were too strict when the real answer is "this is already the best
    product in its category" is simply wrong.
    """
    if category is None:
        return {
            "reason": "no_candidates",
            "message": "We couldn't find comparable products to rank this against.",
        }

    # Is anything in this category healthier at all, ignoring the profile?
    unfiltered = catalog.connect().execute(
        """SELECT count(*) FROM catalog
           WHERE list_contains(categories_tags, ?) AND code <> ?
             AND health_score IS NOT NULL AND health_score > ?""",
        [category, product["code"], source_health],
    ).fetchone()[0]

    if unfiltered == 0:
        label = str(category).split(":", 1)[-1].replace("-", " ")
        return {
            "reason": "already_best",
            "message": (
                f"Nothing in {label} scores higher — this is already among the "
                f"healthiest options we have ({source_health:.0f}/100)."
            ),
        }

    return {
        "reason": "filtered_out",
        "filtered_out": unfiltered,
        "message": (
            f"{unfiltered} healthier option exists, but it did not pass your "
            f"allergen and diet filters."
            if unfiltered == 1
            else f"{unfiltered} healthier options exist, but none passed your "
                 f"allergen and diet filters."
        ),
    }


def _rank(
    product: dict,
    frame: pd.DataFrame,
    source_health: float,
    profile: UserProfile,
    top_n: int,
) -> list[Recommendation]:
    stats = catalog.macro_stats()

    source_macros = similarity.macro_vector(product, stats)
    candidate_macros = similarity.macro_matrix(frame, stats)
    macro_sim = similarity.macro_similarity(source_macros, candidate_macros)

    source_ingredients = _tag_set(product.get("ingredients_tags"))
    ingredient_sim = similarity.jaccard(
        source_ingredients, [_tag_set(t) for t in frame["ingredients_tags"]]
    )

    health = frame["health_score"].astype(float).to_numpy()
    gain = health - source_health
    # Normalise gain against the best available so weights stay comparable.
    gain_norm = gain / gain.max() if gain.max() > 0 else np.zeros_like(gain)

    scans = frame["unique_scans_n"].fillna(0).astype(float).to_numpy()
    popularity = np.log1p(scans)
    popularity = popularity / popularity.max() if popularity.max() > 0 else popularity

    total = (
        W_HEALTH_GAIN * gain_norm
        + W_MACRO * macro_sim
        + W_INGREDIENT * ingredient_sim
        + W_POPULARITY * popularity
    )

    order = np.argsort(-total)[: top_n * 3]
    results: list[Recommendation] = []
    seen_names: set[str] = set()

    for i in order:
        row = frame.iloc[int(i)].to_dict()
        # OFF holds many near-identical listings of the same item; collapsing on
        # name keeps the results a real choice rather than one product repeated.
        key = (str(row.get("product_name") or "").strip().lower(), str(row.get("brands") or "").lower())
        if key in seen_names:
            continue
        seen_names.add(key)

        verdict = filters.evaluate(row, profile)
        if not verdict.passed:  # belt and braces; SQL should have excluded these
            continue

        results.append(
            Recommendation(
                product=row,
                health_gain=float(gain[i]),
                macro_similarity=float(macro_sim[i]),
                ingredient_similarity=float(ingredient_sim[i]),
                score=float(total[i]),
                shared_macros=similarity.shared_macros(product, row),
                warnings=verdict.warnings,
            )
        )
        if len(results) >= top_n:
            break

    return results