MORSATIPIK / core /transfer_engine.py
RDS777's picture
Upload 27 files
6eab95b verified
Raw
History Blame Contribute Delete
1.47 kB
"""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)}