File size: 2,258 Bytes
2e511b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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