"""Étapes 2-3 : retrieval, score de compatibilité, écarts réels (jamais inventés).""" import numpy as np from .kb import norm, tok def _hit(keyword: str, text_norm: str, text_tokens: set) -> bool: k = norm(keyword) if k in text_norm: return True toks = [t[:5] for t in tok(k) if len(t) > 3] return bool(toks) and all(any(w.startswith(t) for w in text_tokens) for t in toks) def match(job: dict, atoms, retriever, top_n: int = 14): reqs = job["must_have"] + job["nice_to_have"] + job["responsibilities"] corpus = norm(" ".join(a["text"] for a in atoms)) ctoks = set(tok(corpus)) def cov(lst): found = [k for k in lst if _hit(k, corpus, ctoks)] return found, [k for k in lst if k not in found] must_f, must_m = cov(job["must_have"]) nice_f, nice_m = cov(job["nice_to_have"]) kw_f, kw_m = cov(job["ats_keywords"]) S, cos = retriever.matrix(reqs) if reqs else (np.zeros((1, len(atoms))), None) relevance = S.sum(axis=0) order = np.argsort(-relevance)[:top_n] top = [atoms[i] for i in order] if cos is not None: semantic = float(np.clip((cos.max(axis=1) - 0.75) / 0.2, 0, 1).mean()) else: semantic = float(S.max(axis=1).mean()) r = lambda f, m: len(f) / max(1, len(f) + len(m)) score = 100 * (0.45 * r(must_f, must_m) + 0.15 * r(nice_f, nice_m) + 0.15 * r(kw_f, kw_m) + 0.25 * semantic) return {"score": round(score), "must_found": must_f, "must_missing": must_m, "nice_found": nice_f, "nice_missing": nice_m, "kw_found": kw_f, "kw_missing": kw_m, "semantic": round(semantic, 2), "top_atoms": top}