File size: 1,467 Bytes
8e3a425
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""core/transfer_engine.py — Moteur de transfert analogique"""
import time, hashlib
from typing import Dict, List


class TransferEngine:
    def __init__(self):
        self.experiences = []

    def add_experience(self, problem: str, solution: str, score: float = 0.8):
        eid = hashlib.sha256(problem.encode()).hexdigest()[:16]
        # Éviter les doublons
        existing_ids = {e["id"] for e in self.experiences}
        if eid not in existing_ids:
            self.experiences.append({
                "id": eid, "problem": problem, "solution": solution,
                "score": score, "ts": time.time()
            })

    def find_analogy(self, problem: str, top_k: int = 3) -> List[Dict]:
        if not self.experiences:
            return []
        words = set(problem.lower().split())
        scored = []
        for exp in self.experiences:
            exp_words = set(exp["problem"].lower().split())
            overlap = len(words & exp_words)
            if overlap > 0:
                sim = overlap / max(len(words), 1)
                scored.append({
                    "problem": exp["problem"],
                    "solution": exp["solution"],
                    "similarity": round(sim, 3),
                    "score": exp["score"]
                })
        scored.sort(key=lambda x: x["similarity"], reverse=True)
        return scored[:top_k]

    def stats(self) -> Dict:
        return {"experiences": len(self.experiences)}