Download legex/processing.py from legexbenchmark/code: direct link, hf CLI and curl.
- Browser
- Download file 3.3 kB
-
https://huggingface.co/datasets/legexbenchmark/code/resolve/main/legex/processing.py
- Command line
-
hf download hf://datasets/legexbenchmark/code/legex/processing.py
-
curl -L -o processing.py https://huggingface.co/datasets/legexbenchmark/code/resolve/main/legex/processing.py
3.3 kB
| """Pipeline steps for ``legex-run``: scrape sources, filter/sample, and build | |
| the per-country Goldenset workbooks.""" | |
| import logging | |
| import zipfile | |
| from pathlib import Path | |
| from legex.config import settings | |
| from legex.scrapers import SCRAPERS | |
| from legex.utils import ( | |
| filtered_path, | |
| goldenset_path, | |
| random_sample, | |
| raw_path, | |
| read_jsonl, | |
| sampled_path, | |
| write_goldenset_xlsx, | |
| write_jsonl, | |
| ) | |
| log = logging.getLogger(__name__) | |
| def scrape(force: bool = False) -> None: | |
| for cc, cls in SCRAPERS.items(): | |
| out = raw_path(cc) | |
| if out.exists() and not force: | |
| log.info(f"[{cc}] {out} exists, skipping") | |
| continue | |
| try: | |
| cases = cls().scrape(settings.date_start, settings.date_end) | |
| except Exception as e: | |
| log.warning(f"[{cc}] scrape failed ({e}), skipping") | |
| continue | |
| write_jsonl(cases, out) | |
| log.info(f"[{cc}] {len(cases)} → {out}") | |
| def filter_and_sample(force: bool = False) -> None: | |
| for cc, cls in SCRAPERS.items(): | |
| sampled = sampled_path(cc) | |
| if sampled.exists() and not force: | |
| log.info(f"[{cc}] {sampled} exists, skipping") | |
| continue | |
| raw = raw_path(cc) | |
| if not raw.exists(): | |
| log.warning(f"[{cc}] missing {raw}, skipping") | |
| continue | |
| cases = read_jsonl(raw) | |
| filtered = cls.civil_filter(cases) | |
| write_jsonl(filtered, filtered_path(cc)) | |
| log.info(f"[{cc}] filtered {len(cases)} → {len(filtered)}") | |
| sample = random_sample(filtered, settings.sample_n, settings.sample_seed) | |
| write_jsonl(sample, sampled) | |
| log.info(f"[{cc}] sampled {len(sample)} → {sampled}") | |
| def fill_goldenset() -> None: | |
| for cc, cls in SCRAPERS.items(): | |
| out = goldenset_path(cc) | |
| if out.exists(): | |
| log.info(f"[{cc}] {out} exists, skipping") | |
| continue | |
| sampled = sampled_path(cc) | |
| if not sampled.exists(): | |
| log.warning(f"[{cc}] missing {sampled}, skipping") | |
| continue | |
| cases = read_jsonl(sampled) | |
| enriched = cls.enrich(cases) | |
| if any(e.full_text != c.full_text for e, c in zip(enriched, cases)): | |
| write_jsonl(enriched, sampled) | |
| log.info(f"[{cc}] enriched {len(enriched)} cases → {sampled}") | |
| write_goldenset_xlsx(enriched, settings.template, out) | |
| log.info(f"[{cc}] → {out}") | |
| def dist(force: bool = False) -> None: | |
| settings.dist_dir.mkdir(parents=True, exist_ok=True) | |
| produced: list[Path] = [] | |
| for cc in SCRAPERS: | |
| xlsx = goldenset_path(cc) | |
| zip_path = settings.dist_dir / f"{cc}.zip" | |
| if zip_path.exists() and not force: | |
| log.info(f"[{cc}] {zip_path} exists, skipping") | |
| produced.append(zip_path) | |
| continue | |
| if not xlsx.exists(): | |
| log.warning(f"[{cc}] missing {xlsx}, skipping") | |
| continue | |
| with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf: | |
| zf.write(xlsx, arcname=xlsx.name) | |
| log.info(f"[{cc}] → {zip_path}") | |
| produced.append(zip_path) | |
| if produced: | |
| log.info(f"Upload to {settings.drive_folder_url}:") | |
| for p in produced: | |
| log.info(f" {p}") | |