code / legex /pdf_export /jsonl.py
anonymous
[code] Reproduction bundle.
2e511b5
Raw
History Blame Contribute Delete
2.16 kB
"""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