File size: 3,193 Bytes
6f5156a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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() and not force:
            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}")