File size: 1,248 Bytes
456aba5
 
 
 
 
 
716bc05
 
 
456aba5
 
 
 
716bc05
456aba5
 
 
716bc05
456aba5
 
716bc05
456aba5
 
716bc05
 
456aba5
716bc05
 
456aba5
 
 
 
 
 
 
 
 
 
 
 
 
716bc05
456aba5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/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