code-education-rag / src /fulltext.py
FabIndy's picture
Fix FULLTEXT: make chunks_path robust to str or Path
716bc05
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
fulltext.py — Mode FULLTEXT (texte exact depuis JSONL, sans LLM)
Robuste :
- accepte chunks_path en str ou Path
- normalise l'article_id
"""
import json
from pathlib import Path
from typing import Optional, Union
def normalize_article_id(raw: str) -> str:
return (raw or "").strip().upper().replace(" ", "").replace(".", "-")
def load_article_text(article_id: str, chunks_path: Union[str, Path]) -> Optional[str]:
"""
Recherche l'article_id dans chunks_path (JSONL) et renvoie le texte exact.
chunks_path : peut être un Path OU une string (cas Hugging Face via config.py).
"""
chunks_path = Path(chunks_path) # <-- FIX: rend le code compatible str/Path
if not chunks_path.exists():
raise FileNotFoundError(f"Fichier chunks introuvable : {chunks_path}")
article_id = normalize_article_id(article_id)
with chunks_path.open("r", encoding="utf-8") as f:
for line in f:
if not line.strip():
continue
obj = json.loads(line)
aid = normalize_article_id(obj.get("article_id", ""))
if aid == article_id:
return (obj.get("text") or "").strip()
return None