Download scripts/paper_tables.py from legexbenchmark/code: direct link, hf CLI and curl.
- Browser
- Download file 7.41 kB
-
https://huggingface.co/datasets/legexbenchmark/code/resolve/main/scripts/paper_tables.py
- Command line
-
hf download hf://datasets/legexbenchmark/code/scripts/paper_tables.py
-
curl -L -o paper_tables.py https://huggingface.co/datasets/legexbenchmark/code/resolve/main/scripts/paper_tables.py
7.41 kB
| #!/usr/bin/env python3 | |
| """Render the manuscript's by-jurisdiction and by-field results tables. | |
| Reads ``data/analysis/per_country_per_column.csv`` (from ``legex-analysis``) | |
| and emits ``tab:metrics-by-jurisdiction`` and ``tab:metrics-by-field`` in the | |
| manuscript's combined four-system layout (recall / false-fill per system, | |
| each with ±1 SE), restricted to the 19 release jurisdictions and the 10 | |
| structured fields (poster convention: the free-text | |
| ``legal_subject_judgement`` is excluded from scoring and reported separately). | |
| The gold-side denominators (n gold-filled / n gold-empty) are shown per row; | |
| they are shared by the two LLM pipelines, while Harvey and Legora cover fewer | |
| judgments (Harvey's ingest failed on some case packets; Legora returned empty | |
| tables for some jurisdictions, e.g. Armenia and Georgia) — where a system's n | |
| differs it is appended in parentheses, in system order. Rows a system did not | |
| cover at all render as ``---``. | |
| Usage: | |
| uv run python scripts/paper_tables.py > data/analysis/paper_tables.tex | |
| """ | |
| import csv | |
| import math | |
| import sys | |
| from pathlib import Path | |
| REPO_ROOT = Path(__file__).resolve().parents[1] | |
| sys.path.insert(0, str(REPO_ROOT)) | |
| from legex.analysis.countries import COUNTRY_NAMES, RELEASE_COUNTRIES # noqa: E402 | |
| from legex.analysis.quant_results import ( # noqa: E402 | |
| STRUCTURED_FIELDS, | |
| _BUCKETS, | |
| ) | |
| CSV_PATH = REPO_ROOT / "data/analysis/per_country_per_column.csv" | |
| SYSTEMS = ( | |
| ("gemini/gemini-3.1-flash-lite", "Gemini"), | |
| ("gpt-5.4-mini", "GPT-5.4-mini"), | |
| ("harvey", "Harvey"), | |
| ("legora-1", "Legora"), | |
| ) | |
| # System whose gold-side denominators are the reference for the n column; any | |
| # system whose coverage differs gets its own n appended in parentheses. | |
| BASELINE_SYSTEM = "gpt-5.4-mini" | |
| def _pct_se(k: int, n: int) -> str: | |
| if not n: | |
| return "---" | |
| p = k / n | |
| se = 100.0 * math.sqrt(p * (1.0 - p) / n) | |
| if se == 0.0: # boundary estimate (0% or 100%): a degenerate ±0.0% adds nothing | |
| return f"{p * 100:.1f}\\%" | |
| return f"{p * 100:.1f}\\% $\\pm$ {se:.1f}\\%" | |
| def _load() -> dict[tuple[str, str, str], dict[str, int]]: | |
| """(model, country, column) -> buckets, release countries + structured fields only.""" | |
| out: dict[tuple[str, str, str], dict[str, int]] = {} | |
| release, structured = set(RELEASE_COUNTRIES), set(STRUCTURED_FIELDS) | |
| with CSV_PATH.open(newline="") as f: | |
| for row in csv.DictReader(f): | |
| if row["country"] in release and row["column"] in structured: | |
| out[(row["model"], row["country"], row["column"])] = { | |
| k: int(row[k]) for k in _BUCKETS | |
| } | |
| return out | |
| def _sums(data, model: str, country: str | None = None, column: str | None = None): | |
| agg = {k: 0 for k in _BUCKETS} | |
| for (m, cc, col), c in data.items(): | |
| if m != model or (country and cc != country) or (column and col != column): | |
| continue | |
| for k in _BUCKETS: | |
| agg[k] += c[k] | |
| return agg | |
| def _row_cells(data, country=None, column=None) -> tuple[list[str], str]: | |
| """8 metric cells (recall/false-fill × 4 systems) + the n cell.""" | |
| cells: list[str] = [] | |
| n_filled: dict[str, int] = {} | |
| n_empty: dict[str, int] = {} | |
| for model, _ in SYSTEMS: | |
| c = _sums(data, model, country, column) | |
| filled = c["tp"] + c["mismatch"] + c["missed"] | |
| empty = c["hallucinated"] + c["tn"] | |
| n_filled[model], n_empty[model] = filled, empty | |
| cells.append(_pct_se(c["tp"], filled)) | |
| cells.append(_pct_se(c["hallucinated"], empty)) | |
| base = (n_filled[BASELINE_SYSTEM], n_empty[BASELINE_SYSTEM]) | |
| n_cell = f"{base[0]}/{base[1]}" | |
| for model, label in SYSTEMS: | |
| if model == BASELINE_SYSTEM: | |
| continue | |
| n = (n_filled[model], n_empty[model]) | |
| # Zero coverage is already communicated by the --- metric cells. | |
| if n != base and n != (0, 0): | |
| n_cell += f" ({label[0]}: {n[0]}/{n[1]})" | |
| return cells, n_cell | |
| def main() -> None: | |
| data = _load() | |
| print("% Auto-generated by scripts/paper_tables.py — do not edit by hand.") | |
| print("% 19 release jurisdictions, 10 structured fields (free-text excluded).") | |
| print() | |
| # --- by jurisdiction --- | |
| print(r"\begin{table*}[htbp]") | |
| print( | |
| r"\caption{Extraction metrics by jurisdiction over the ten structured" | |
| r" fields (recall on expert-filled cells and false-fill rate on" | |
| r" expert-empty cells, each $\pm$1\,SE). $n$ lists the goldenset-filled/" | |
| r"goldenset-empty denominators; where a system's coverage differs its" | |
| r" denominators follow in parentheses (G = Gemini, H = Harvey," | |
| r" L = Legora); \mbox{---} marks jurisdictions a system did" | |
| r" not cover.}" | |
| ) | |
| print(r"\label{tab:metrics-by-jurisdiction}") | |
| print(r"\centering\small") | |
| print(r"\resizebox{\textwidth}{!}{%") | |
| print(r"\begin{tabular}{@{}lrr@{\hskip 8pt}rr@{\hskip 8pt}rr@{\hskip 8pt}rr@{\hskip 8pt}r@{}}") | |
| print(r"\toprule") | |
| print( | |
| r"& \multicolumn{2}{c}{\textbf{Gemini}} & \multicolumn{2}{c}{\textbf{GPT-5.4-mini}}" | |
| r" & \multicolumn{2}{c}{\textbf{Harvey}} & \multicolumn{2}{c}{\textbf{Legora}} & \\" | |
| ) | |
| print(r"\cmidrule(lr){2-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7}\cmidrule(lr){8-9}") | |
| print( | |
| r"\textbf{Jurisdiction} & \textbf{Recall} & \textbf{False-fill}" | |
| r" & \textbf{Recall} & \textbf{False-fill}" | |
| r" & \textbf{Recall} & \textbf{False-fill}" | |
| r" & \textbf{Recall} & \textbf{False-fill} & \textbf{$n$ (filled/empty)} \\" | |
| ) | |
| print(r"\midrule") | |
| for cc in sorted(RELEASE_COUNTRIES, key=lambda c: COUNTRY_NAMES[c]): | |
| cells, n_cell = _row_cells(data, country=cc) | |
| print(f"{COUNTRY_NAMES[cc]} & " + " & ".join(cells) + f" & {n_cell} \\\\") | |
| print(r"\bottomrule") | |
| print(r"\end{tabular}%") | |
| print(r"}") | |
| print(r"\end{table*}") | |
| print() | |
| # --- by field --- | |
| print(r"\begin{table*}[htbp]") | |
| print( | |
| r"\caption{Extraction metrics by field over the 19 release" | |
| r" jurisdictions (recall and false-fill rate, each $\pm$1\,SE)." | |
| r" $n$ as in \cref{tab:metrics-by-jurisdiction}.}" | |
| ) | |
| print(r"\label{tab:metrics-by-field}") | |
| print(r"\centering\small") | |
| print(r"\resizebox{\textwidth}{!}{%") | |
| print(r"\begin{tabular}{@{}lrr@{\hskip 8pt}rr@{\hskip 8pt}rr@{\hskip 8pt}rr@{\hskip 8pt}r@{}}") | |
| print(r"\toprule") | |
| print( | |
| r"& \multicolumn{2}{c}{\textbf{Gemini}} & \multicolumn{2}{c}{\textbf{GPT-5.4-mini}}" | |
| r" & \multicolumn{2}{c}{\textbf{Harvey}} & \multicolumn{2}{c}{\textbf{Legora}} & \\" | |
| ) | |
| print(r"\cmidrule(lr){2-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7}\cmidrule(lr){8-9}") | |
| print( | |
| r"\textbf{Variable} & \textbf{Recall} & \textbf{False-fill}" | |
| r" & \textbf{Recall} & \textbf{False-fill}" | |
| r" & \textbf{Recall} & \textbf{False-fill}" | |
| r" & \textbf{Recall} & \textbf{False-fill} & \textbf{$n$ (filled/empty)} \\" | |
| ) | |
| print(r"\midrule") | |
| for col in sorted(STRUCTURED_FIELDS): | |
| cells, n_cell = _row_cells(data, column=col) | |
| name = r"\texttt{" + col.replace("_", r"\_") + "}" | |
| print(f"{name} & " + " & ".join(cells) + f" & {n_cell} \\\\") | |
| print(r"\bottomrule") | |
| print(r"\end{tabular}%") | |
| print(r"}") | |
| print(r"\end{table*}") | |
| if __name__ == "__main__": | |
| main() | |