File size: 13,004 Bytes
f4380aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
synthesis.py — Mode SYNTHÈSE extractive (déterministe, sans reformulation)

But :
- Produire une synthèse lisible ET juridiquement sûre (zéro hallucination)
- On ne "rédige" rien : on sélectionne des extraits exacts, non tronqués
- On affiche les extraits dans l'ordre du texte

Stratégie :
1) Découper l'article en "unités juridiques" (items I/II/1°/a)/-, sinon clauses ;, sinon phrases)
2) Scorer les unités sur des marqueurs normatifs (obligation, interdiction, conditions, exceptions, structure)
3) Sélectionner 3-5 unités max avec diversité + couverture
4) Budget global : on supprime des unités si nécessaire (jamais de troncature)

API publique :
- extractive_summary(article_id, article_text, cfg=None) -> str
"""

from __future__ import annotations

from dataclasses import dataclass
from typing import List, Tuple, Dict, Set
import re


# ==================== CONFIG ====================

@dataclass(frozen=True)
class SynthesisConfig:
    # Option A : 5 puces max, aucun tronquage
    max_bullets: int = 5

    # Budget global (caractères) : on retire des unités si on dépasse
    max_total_chars: int = 1500

    # Filtres
    min_unit_len: int = 35
    max_unit_len_soft: int = 900  # si une unité est trop longue, on essaie de la re-split (sans tronquer)

    # Diversité : seuil de similarité (0 = jamais similaire, 1 = identique)
    max_jaccard_similarity: float = 0.55

    # Couverture : essayer de prendre au moins 1 unité de chaque catégorie si possible
    enforce_coverage: bool = True


# ==================== TEXT NORMALIZATION ====================

_SPACE_RE = re.compile(r"\s+")

def _norm(s: str) -> str:
    return _SPACE_RE.sub(" ", s.strip())


# ==================== DETECT STRUCTURE / ITEMS ====================

_ITEM_RE = re.compile(
    r"""^(
        (?:[IVX]{1,6}\.) |           # I. II. III. IV. ...
        (?:\d+°) |                   # 1° 2° 3° ...
        (?:[a-z]\)) |                # a) b) c) ...
        (?:-\s)                      # - ...
    )""",
    re.VERBOSE
)

def _is_structured_item(line: str) -> bool:
    return bool(_ITEM_RE.match(line))


# ==================== SPLITTING INTO LEGAL UNITS ====================

_SENTENCE_SPLIT_RE = re.compile(r"(?<=[.!?])\s+")
# Split on semicolons, keeping semantic clauses. We'll keep ";" in the left clause for readability.
_SEMI_SPLIT_RE = re.compile(r"\s*;\s*")

def _merge_wrapped_lines(lines: List[str]) -> List[str]:
    """
    Heuristique simple :
    - si une ligne n'est pas un item structuré et semble être la continuation de la précédente,
      on la concatène.
    """
    merged: List[str] = []
    for ln in lines:
        ln = _norm(ln)
        if not ln:
            continue
        if not merged:
            merged.append(ln)
            continue

        prev = merged[-1]

        # Nouveau bloc si item structuré ou si ça ressemble à un titre
        if _is_structured_item(ln):
            merged.append(ln)
            continue

        # Si la ligne précédente se termine par ":" ou "—" ou si la nouvelle commence par une minuscule,
        # on considère souvent que c'est une continuation.
        if prev.endswith((":","—","-")) or (ln and ln[0].islower()):
            merged[-1] = _norm(prev + " " + ln)
        else:
            # Sinon, on garde séparé
            merged.append(ln)

    return merged


def _split_long_unit(unit: str, cfg: SynthesisConfig) -> List[str]:
    """
    Si l'unité est très longue, on essaie de la découper SANS tronquer :
    - priorité au ';' (souvent structure juridique)
    - sinon, phrases complètes
    """
    unit = _norm(unit)
    if len(unit) <= cfg.max_unit_len_soft:
        return [unit]

    # 1) Essai sur ';'
    parts = [p.strip() for p in _SEMI_SPLIT_RE.split(unit) if p.strip()]
    if len(parts) >= 2:
        out: List[str] = []
        for i, p in enumerate(parts):
            # On remet le ';' entre clauses sauf la dernière
            if i < len(parts) - 1 and not p.endswith(";"):
                p = p + ";"
            out.append(_norm(p))
        # Filtre : on ne veut pas de mini-clause vide
        out2 = [p for p in out if len(p) >= cfg.min_unit_len]
        if out2:
            return out2

    # 2) Fallback phrases
    sents = [s.strip() for s in _SENTENCE_SPLIT_RE.split(unit) if s.strip()]
    if len(sents) >= 2:
        sents2 = [ _norm(s) for s in sents if len(s) >= cfg.min_unit_len ]
        if sents2:
            return sents2

    # 3) Dernier recours : on garde l'unité entière (même longue). On préfèrera en éliminer au budget.
    return [unit]


def _split_into_units(article_text: str, cfg: SynthesisConfig) -> List[str]:
    if not article_text:
        return []

    raw_lines = [ln for ln in article_text.splitlines()]
    # enlève les lignes vides
    raw_lines = [ln for ln in raw_lines if _norm(ln)]

    lines = _merge_wrapped_lines(raw_lines)

    units: List[str] = []

    for ln in lines:
        ln = _norm(ln)
        if not ln:
            continue

        # Si c'est un item structuré, on le garde en tant qu'unité,
        # mais on peut le re-split s'il est gigantesque.
        if _is_structured_item(ln):
            units.extend(_split_long_unit(ln, cfg))
            continue

        # Sinon, on essaye d'abord de couper par ';' si c'est long
        if len(ln) > cfg.max_unit_len_soft:
            units.extend(_split_long_unit(ln, cfg))
        else:
            units.append(ln)

    # Filtrage final
    cleaned = []
    for u in units:
        u = _norm(u)
        if len(u) < cfg.min_unit_len:
            continue
        cleaned.append(u)

    return cleaned


# ==================== SCORING (NORMATIVE / LEGAL) ====================

# Patterns "durs" (évite le bruit : on NE met PAS "est/sont")
_PATTERNS: Dict[str, List[re.Pattern]] = {
    "obligation": [
        re.compile(r"\bdoit\b", re.I),
        re.compile(r"\bdoivent\b", re.I),
        re.compile(r"\best\s+tenu\b", re.I),
        re.compile(r"\bsont\s+tenus\b", re.I),
        re.compile(r"\best\s+tenu\s+de\b", re.I),
        re.compile(r"\bobligatoire\b", re.I),
        re.compile(r"\bobligation\b", re.I),
    ],
    "prohibition": [
        re.compile(r"\binterdit\b", re.I),
        re.compile(r"\best\s+interdit\b", re.I),
        re.compile(r"\bil\s+est\s+interdit\b", re.I),
        re.compile(r"\bne\s+peut\b", re.I),
        re.compile(r"\bne\s+peuvent\b", re.I),
    ],
    "condition": [
        re.compile(r"\bsi\b", re.I),
        re.compile(r"\blorsque\b", re.I),
        re.compile(r"\bà\s+condition\b", re.I),
        re.compile(r"\ba\s+condition\b", re.I),
        re.compile(r"\ben\s+cas\b", re.I),
        re.compile(r"\bdans\s+le\s+cas\b", re.I),
    ],
    "exception": [
        re.compile(r"\bsauf\b", re.I),
        re.compile(r"\btoutefois\b", re.I),
        re.compile(r"\bsous\s+réserve\b", re.I),
        re.compile(r"\bnonobstant\b", re.I),
    ],
    "permission": [
        re.compile(r"\bpeut\b", re.I),
        re.compile(r"\bpeuvent\b", re.I),
    ],
    "structure": [
        re.compile(r"^(?:[IVX]{1,6}\.|(?:\d+°)|(?:[a-z]\))|(?:-\s))", re.I),
    ]
}

_REF_RE = re.compile(r"\b(décret|arrêté|loi|code)\b", re.I)

def _score_unit(u: str) -> Tuple[int, Set[str]]:
    """
    Retourne (score, tags)
    tags = catégories rencontrées (obligation, prohibition, condition, exception, permission, structure)
    """
    score = 0
    tags: Set[str] = set()

    for tag, pats in _PATTERNS.items():
        for p in pats:
            if p.search(u):
                tags.add(tag)
                # pondération simple et justifiable
                if tag in ("obligation", "prohibition"):
                    score += 4
                elif tag in ("condition", "exception"):
                    score += 3
                elif tag == "structure":
                    score += 2
                elif tag == "permission":
                    score += 1
                break

    if _REF_RE.search(u):
        score += 1

    # pénalité légère si c'est extrêmement long (sans tronquer, juste pour favoriser concision)
    if len(u) > 700:
        score -= 1

    return score, tags


# ==================== DIVERSITY (ANTI-REDUNDANCY) ====================

_WORD_RE = re.compile(r"[a-zA-ZÀ-ÖØ-öø-ÿ0-9]+")

_STOPWORDS = {
    # très minimaliste : juste assez pour éviter le bruit
    "le","la","les","un","une","des","du","de","d","à","au","aux","et","ou",
    "en","dans","sur","pour","par","avec","sans","ce","cet","cette","ces",
    "qui","que","quoi","dont","où","il","elle","ils","elles","on",
    "est","sont","été","être","a","ont","avait","avaient","sera","seront",
    "ne","pas","plus","moins","se","sa","son","ses","leur","leurs",
}

def _keywords(u: str) -> Set[str]:
    toks = [t.lower() for t in _WORD_RE.findall(u)]
    return {t for t in toks if len(t) >= 3 and t not in _STOPWORDS}

def _jaccard(a: Set[str], b: Set[str]) -> float:
    if not a and not b:
        return 0.0
    inter = len(a & b)
    union = len(a | b)
    return inter / union if union else 0.0


# ==================== SELECTION ====================

def _pick_with_coverage(
    scored_units: List[Tuple[int, int, str, Set[str], Set[str]]],
    cfg: SynthesisConfig
) -> List[Tuple[int, str]]:
    """
    scored_units elements: (score, idx, unit_text, tags, keywords)
    returns list of (idx, unit_text) selected
    """

    # candidates sorted by score desc
    candidates = sorted(scored_units, key=lambda x: x[0], reverse=True)

    selected: List[Tuple[int, str, Set[str]]] = []  # (idx, text, keywords)

    def can_add(kw: Set[str]) -> bool:
        for (_, _, kw2) in selected:
            if _jaccard(kw, kw2) >= cfg.max_jaccard_similarity:
                return False
        return True

    def add_candidate(cand: Tuple[int, int, str, Set[str], Set[str]]) -> bool:
        score, idx, text, tags, kw = cand
        if score <= 0:
            return False
        if not can_add(kw):
            return False
        selected.append((idx, text, kw))
        return True

    # 1) Coverage pass (si activé)
    if cfg.enforce_coverage:
        # catégories prioritaires
        targets = [
            ("obligation",),
            ("prohibition",),
            ("exception", "condition"),
            ("structure",),
        ]
        for group in targets:
            if len(selected) >= cfg.max_bullets:
                break
            for cand in candidates:
                score, idx, text, tags, kw = cand
                if score <= 0:
                    continue
                if any(t in tags for t in group):
                    if add_candidate(cand):
                        break

    # 2) Fill remaining by best score
    for cand in candidates:
        if len(selected) >= cfg.max_bullets:
            break
        add_candidate(cand)

    # sort by original order (idx)
    selected_sorted = sorted(((idx, text) for (idx, text, _) in selected), key=lambda x: x[0])
    return selected_sorted


def _apply_budget(selected: List[Tuple[int, str]], cfg: SynthesisConfig) -> List[str]:
    """
    Applique le budget max_total_chars en supprimant des unités (jamais tronquer).
    On supprime en priorité les dernières (car on affiche dans l'ordre).
    """
    texts = [t for (_, t) in selected]
    # +2 per bullet for "- " and newline approx (rough, OK)
    def total_len(ts: List[str]) -> int:
        return sum(len(s) for s in ts) + 3 * len(ts)

    while texts and total_len(texts) > cfg.max_total_chars:
        texts.pop()  # remove last
    return texts


# ==================== PUBLIC API ====================

def extractive_summary(article_id: str, article_text: str, cfg: SynthesisConfig | None = None) -> str:
    cfg = cfg or SynthesisConfig()

    units = _split_into_units(article_text, cfg)

    if not units:
        return f"Synthèse impossible : texte vide ou non exploitable.\n\nArticles cités : {article_id}"

    scored_units: List[Tuple[int, int, str, Set[str], Set[str]]] = []
    for idx, u in enumerate(units):
        score, tags = _score_unit(u)
        kw = _keywords(u)
        scored_units.append((score, idx, u, tags, kw))

    selected = _pick_with_coverage(scored_units, cfg)

    # si rien (ex: aucun score > 0), fallback : premières unités (mais toujours non tronquées + ordre)
    if not selected:
        selected = [(i, units[i]) for i in range(min(cfg.max_bullets, len(units)))]

    texts = _apply_budget(selected, cfg)
    if not texts:
        # Si le budget est trop strict et qu'une seule unité est énorme, on affiche quand même la 1ère
        # (option produit : mieux vaut 1 extrait entier que rien)
        texts = [selected[0][1]]

    out_lines = ["Synthèse (extraits du texte, sans reformulation) :"]
    for t in texts:
        out_lines.append(f"- {t}")

    return "\n".join(out_lines) + f"\n\nArticles cités : {article_id}"