File size: 2,155 Bytes
2e511b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Write one PDF per case from a recovered ``full_text.jsonl``.

For jurisdictions whose judgment text was recovered into
``data/<cc>/full_text.jsonl`` (Taiwan/Hong Kong via rendering, Serbia via
scraping, India via the real PDF bundle) rather than living in the workbook's
``full_text`` column, this produces the original-language PDFs that the Harvey
hand-off needs. Font is chosen per country code (Traditional Chinese for
tw/hk, Devanagari for in, etc.).
"""

from __future__ import annotations

import json
from pathlib import Path

from legex.pdf_export.core import write_one_pdf
from legex.pdf_export.font_keys import FONT_BY_CC


def export_jsonl(
    jsonl_path: Path,
    out_dir: Path,
    *,
    cc: str,
    min_chars: int = 1,
    limit: int | None = None,
) -> int:
    """Write PDFs from ``jsonl_path`` into ``out_dir``. Returns count written."""
    font_key = FONT_BY_CC.get(cc, "default")
    out_dir.mkdir(parents=True, exist_ok=True)
    used_names: dict[str, int] = {}
    count = 0
    with jsonl_path.open(encoding="utf-8") as f:
        for line in f:
            line = line.strip()
            if not line:
                continue
            try:
                rec = json.loads(line)
            except json.JSONDecodeError:
                continue
            case_id = str(rec.get("case_id") or "").strip()
            body = str(rec.get("full_text") or "")
            if not case_id or len(body) < min_chars:
                continue
            base = case_id or f"row_{count}"
            used_names[base] = used_names.get(base, 0) + 1
            fname = f"{base}_{used_names[base]}.pdf" if used_names[base] > 1 else f"{base}.pdf"
            write_one_pdf(
                out_dir / fname,
                case_id,
                "",
                body,
                font_key,
                text_source="full_text.jsonl",
            )
            count += 1
            if count % 25 == 0:
                print(f"  {cc}: {count} …", flush=True)
            if limit is not None and count >= limit:
                break
    print(f"  {cc}: done, {count} PDFs -> {out_dir}", flush=True)
    return count