Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -699,65 +699,65 @@ def reinforce_entities_with_keywords(entities, query, food_map, master_symptom_m
|
|
| 699 |
|
| 700 |
def find_best_matches_hybrid(entities, data):
|
| 701 |
"""
|
| 702 |
-
|
| 703 |
-
|
| 704 |
"""
|
| 705 |
if not entities or not data: return []
|
| 706 |
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
|
|
|
|
|
|
|
|
|
| 710 |
|
| 711 |
RARE_CONDITIONS = [
|
| 712 |
"Porfiria Aguda Intermitente (PAI).", "Enfermedad de Refsum del Adulto.",
|
| 713 |
-
"Ataxia por Gluten.", "Encefalopat铆a por Gluten.", "Enfermedad de Wilson."
|
|
|
|
| 714 |
]
|
| 715 |
|
| 716 |
results = []
|
| 717 |
for entry in data:
|
| 718 |
-
# Puntuaci贸n de Alimento con Bonus de Confianza
|
| 719 |
score_details = {'food': 0, 'symptoms': 0, 'bonus': 0}
|
| 720 |
-
db_food_text = entry.get("compuesto_alimento", "").lower()
|
| 721 |
-
db_food_keywords = set(re.findall(r'\b\w+\b', re.sub(r'\(.*?\)', '', db_food_text)))
|
| 722 |
|
| 723 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 724 |
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
|
|
|
|
|
|
| 729 |
score_details['bonus'] = 30
|
| 730 |
-
else:
|
| 731 |
-
# Si no hay ninguna coincidencia de palabras clave de alimento, saltar esta entrada
|
| 732 |
-
continue
|
| 733 |
-
|
| 734 |
-
# Puntuaci贸n de S铆ntomas
|
| 735 |
-
db_symptoms_keys = set(s.lower() for s in entry.get("sintomas_clave", []))
|
| 736 |
-
symptom_score = 0
|
| 737 |
-
matched_symptoms = []
|
| 738 |
-
for user_symptom in user_symptoms:
|
| 739 |
-
if user_symptom in db_symptoms_keys:
|
| 740 |
-
symptom_score += 30
|
| 741 |
-
matched_symptoms.append(user_symptom)
|
| 742 |
-
score_details['symptoms'] = symptom_score
|
| 743 |
|
| 744 |
-
#
|
| 745 |
-
|
|
|
|
| 746 |
|
| 747 |
-
|
| 748 |
-
if
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 752 |
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
# A帽adir a resultados si tiene una puntuaci贸n m铆nima
|
| 756 |
-
if score_details['total'] > 10:
|
| 757 |
results.append({
|
| 758 |
'entry': entry,
|
| 759 |
'score': score_details,
|
| 760 |
-
'matched_symptoms':
|
| 761 |
})
|
| 762 |
|
| 763 |
if not results: return []
|
|
|
|
| 699 |
|
| 700 |
def find_best_matches_hybrid(entities, data):
|
| 701 |
"""
|
| 702 |
+
Motor de b煤squeda sem谩ntico y ponderado.
|
| 703 |
+
Descompone los s铆ntomas en palabras clave y punt煤a por intersecci贸n.
|
| 704 |
"""
|
| 705 |
if not entities or not data: return []
|
| 706 |
|
| 707 |
+
# 1. PROCESAR INPUT DEL USUARIO
|
| 708 |
+
user_symptoms_text = " ".join(entities.get("sintomas", []))
|
| 709 |
+
user_symptom_keywords = set(re.findall(r'\b\w+\b', sanitize_text(user_symptoms_text)))
|
| 710 |
+
|
| 711 |
+
user_foods_text = " ".join(entities.get("alimentos", []))
|
| 712 |
+
user_food_keywords = set(re.findall(r'\b\w+\b', sanitize_text(user_foods_text)))
|
| 713 |
|
| 714 |
RARE_CONDITIONS = [
|
| 715 |
"Porfiria Aguda Intermitente (PAI).", "Enfermedad de Refsum del Adulto.",
|
| 716 |
+
"Ataxia por Gluten.", "Encefalopat铆a por Gluten.", "Enfermedad de Wilson.",
|
| 717 |
+
"Deficiencia de Ornitina Transcarbamilasa." # A帽adir OTC a las raras
|
| 718 |
]
|
| 719 |
|
| 720 |
results = []
|
| 721 |
for entry in data:
|
|
|
|
| 722 |
score_details = {'food': 0, 'symptoms': 0, 'bonus': 0}
|
|
|
|
|
|
|
| 723 |
|
| 724 |
+
# 2. PROCESAR INPUT DE LA BASE DE DATOS
|
| 725 |
+
db_food_text = entry.get("compuesto_alimento", "")
|
| 726 |
+
db_food_keywords = set(re.findall(r'\b\w+\b', sanitize_text(re.sub(r'\(.*?\)', '', db_food_text))))
|
| 727 |
+
|
| 728 |
+
db_symptoms_text = " ".join(entry.get("sintomas_clave", []))
|
| 729 |
+
db_symptom_keywords = set(re.findall(r'\b\w+\b', sanitize_text(db_symptoms_text)))
|
| 730 |
+
|
| 731 |
+
# 3. COMPARACI脫N INTELIGENTE Y PUNTUACI脫N
|
| 732 |
|
| 733 |
+
# Puntuaci贸n de Alimento
|
| 734 |
+
food_intersection = user_food_keywords.intersection(db_food_keywords)
|
| 735 |
+
if food_intersection:
|
| 736 |
+
score_details['food'] = 20
|
| 737 |
+
# Bonus por alta confianza
|
| 738 |
+
if len(food_intersection) / len(user_food_keywords.union(db_food_keywords)) > 0.3:
|
| 739 |
score_details['bonus'] = 30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 740 |
|
| 741 |
+
# Puntuaci贸n de S铆ntomas por intersecci贸n
|
| 742 |
+
symptom_intersection = user_symptom_keywords.intersection(db_symptom_keywords)
|
| 743 |
+
score_details['symptoms'] = len(symptom_intersection) * 30
|
| 744 |
|
| 745 |
+
# Solo considerar resultados que tengan alguna coincidencia de alimento Y s铆ntoma
|
| 746 |
+
if score_details['food'] > 0 and score_details['symptoms'] > 0:
|
| 747 |
+
base_score = score_details['food'] + score_details['symptoms'] + score_details['bonus']
|
| 748 |
+
|
| 749 |
+
condition_name = entry.get("condicion_asociada", "")
|
| 750 |
+
if condition_name in RARE_CONDITIONS:
|
| 751 |
+
final_score = base_score * 0.4
|
| 752 |
+
else:
|
| 753 |
+
final_score = base_score * 1.0
|
| 754 |
|
| 755 |
+
score_details['total'] = int(final_score)
|
| 756 |
+
|
|
|
|
|
|
|
| 757 |
results.append({
|
| 758 |
'entry': entry,
|
| 759 |
'score': score_details,
|
| 760 |
+
'matched_symptoms': list(symptom_intersection) # Usar la intersecci贸n como s铆ntomas coincidentes
|
| 761 |
})
|
| 762 |
|
| 763 |
if not results: return []
|