Download tests/test_inference_jsonl.py from legexbenchmark/code: direct link, hf CLI and curl.
- Browser
- Download file 2.26 kB
-
https://huggingface.co/datasets/legexbenchmark/code/resolve/main/tests/test_inference_jsonl.py
- Command line
-
hf download hf://datasets/legexbenchmark/code/tests/test_inference_jsonl.py
-
curl -L -o test_inference_jsonl.py https://huggingface.co/datasets/legexbenchmark/code/resolve/main/tests/test_inference_jsonl.py
2.26 kB
| """Inference JSONL I/O: faithful round-trip and CSV-scoring equivalence.""" | |
| import csv | |
| import json | |
| from legex.evaluation import normalise | |
| from legex.utils import inference_record, read_inference_jsonl, write_inference_jsonl | |
| COLUMNS = ["case_id", "link", "legal_subject_judgement", "dispute_value_nominal", | |
| "trial_end_date", "plaintiffs_all_count", "model", "error"] | |
| ROWS = [ | |
| {"case_id": "C-1", "link": "", "legal_subject_judgement": "Civil_Law", | |
| "dispute_value_nominal": "nonpecuniary", "trial_end_date": "2023-05-04", | |
| "plaintiffs_all_count": "2", "model": "harvey", "error": ""}, | |
| # refusal prose + unicode + empty cells stay verbatim / become null | |
| {"case_id": "C-2", "link": "", "legal_subject_judgement": "", | |
| "dispute_value_nominal": "None (the amount is not stated) N/A", | |
| "trial_end_date": "", "plaintiffs_all_count": "", "model": "harvey", | |
| "error": "श्रेणी"}, | |
| ] | |
| def test_empty_becomes_null_nonempty_stays_string(tmp_path): | |
| rec = inference_record(ROWS[1], COLUMNS) | |
| assert rec["link"] is None and rec["trial_end_date"] is None # empty -> null | |
| assert rec["dispute_value_nominal"] == "None (the amount is not stated) N/A" # verbatim | |
| assert rec["error"] == "श्रेणी" | |
| def test_roundtripnormalises_identically_to_csv(tmp_path): | |
| """Reading JSONL and reading the equivalent CSV must yield identical scored cells.""" | |
| jsonl = tmp_path / "inf.jsonl" | |
| write_inference_jsonl(jsonl, ROWS, COLUMNS) | |
| csv_path = tmp_path / "inf.csv" | |
| with csv_path.open("w", encoding="utf-8", newline="") as f: | |
| w = csv.DictWriter(f, fieldnames=COLUMNS) | |
| w.writeheader() | |
| w.writerows(ROWS) | |
| def scored(rows): | |
| return [{k: normalise(v) for k, v in r.items()} for r in rows] | |
| with csv_path.open(encoding="utf-8", newline="") as f: | |
| csv_rows = list(csv.DictReader(f)) | |
| assert scored(read_inference_jsonl(jsonl)) == scored(csv_rows) | |
| def test_jsonl_is_one_object_per_line(tmp_path): | |
| p = tmp_path / "inf.jsonl" | |
| write_inference_jsonl(p, ROWS, COLUMNS) | |
| lines = p.read_text(encoding="utf-8").splitlines() | |
| assert len(lines) == len(ROWS) | |
| assert all(json.loads(line) for line in lines) # each line is valid JSON | |