from __future__ import annotations import re from ted.ted_history import has_pronoun_reference from ted.ted_support import guard_names def rewrite_history_context(question: str, condensed_history: str) -> str: if not condensed_history: return "" return condensed_history if has_pronoun_reference(question) else "" def rewrite_has_unsupported_fact(answer: str, retrieved_names: list[str]) -> bool: text = answer.lower() if re.search(r"\bdr\.?\s+[a-zà-ÿ-]+", text) or "docteur" in text or "vétérinaire" in text or "veterinaire" in text: return True return bool(guard_names(answer, retrieved_names)) if retrieved_names else False def select_nodes_named_in_answer(answer: str, nodes: list, source_limit: int = 3): if not nodes: return [], None focus_node = nodes[0] focus_count = 0 answer_lower = answer.lower() mentioned_nodes = [] for nd in nodes: name = str(nd.metadata.get("nom", "")).strip() if not name: continue pattern = rf"(? focus_count: focus_count = len(occurrences) focus_node = nd if mentioned_nodes: selected = [nd for _, nd in sorted(mentioned_nodes, key=lambda x: x[0])][:source_limit] else: selected = nodes[:source_limit] return selected, focus_node