Adzacam commited on
Commit
59ad42d
·
1 Parent(s): 5a1b47c

refactor: update student fuzzy matching logic to use find_best_match with entity objects

Browse files
Files changed (1) hide show
  1. app.py +6 -8
app.py CHANGED
@@ -703,7 +703,7 @@ def procesar_lote_financiero(payloads: List[FinancePayload], db: Session = Depen
703
  if current_user.role not in ["analista_datos_marketing", "admin"]:
704
  raise HTTPException(status_code=403, detail="Acceso denegado: Se requiere rol de Analista de Datos.")
705
  try:
706
- from similarity import get_top_matches
707
 
708
  for payload in payloads:
709
  # 1. Resolver Estudiante
@@ -718,15 +718,13 @@ def procesar_lote_financiero(payloads: List[FinancePayload], db: Session = Depen
718
  if not estudiante:
719
  estudiante = db.query(DimEstudiante).filter(DimEstudiante.nombre_completo.ilike(f"%{nombre_resuelto}%")).first()
720
 
721
- # Si no hay match exacto, usar fuzzy matching simulado
722
  if not estudiante:
723
- todos_estudiantes = db.query(DimEstudiante.id_estudiante, DimEstudiante.nombre_completo).all()
724
- nombres_db = {e.id_estudiante: e.nombre_completo for e in todos_estudiantes}
725
- matches = get_top_matches(nombre_resuelto, nombres_db, limit=1)
726
 
727
- if matches and matches[0]["score"] >= 85:
728
- est_id = matches[0]["id"]
729
- estudiante = db.query(DimEstudiante).filter(DimEstudiante.id_estudiante == est_id).first()
730
 
731
  # Si de plano no existe, lo creamos para que no falle la FK
732
  if not estudiante:
 
703
  if current_user.role not in ["analista_datos_marketing", "admin"]:
704
  raise HTTPException(status_code=403, detail="Acceso denegado: Se requiere rol de Analista de Datos.")
705
  try:
706
+ from similarity import find_best_match
707
 
708
  for payload in payloads:
709
  # 1. Resolver Estudiante
 
718
  if not estudiante:
719
  estudiante = db.query(DimEstudiante).filter(DimEstudiante.nombre_completo.ilike(f"%{nombre_resuelto}%")).first()
720
 
721
+ # Si no hay match exacto, usar fuzzy matching
722
  if not estudiante:
723
+ todos_estudiantes = db.query(DimEstudiante).all()
724
+ best_match, score = find_best_match(nombre_resuelto, todos_estudiantes, threshold=0.85)
 
725
 
726
+ if best_match:
727
+ estudiante = best_match
 
728
 
729
  # Si de plano no existe, lo creamos para que no falle la FK
730
  if not estudiante: