File size: 3,445 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
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
import json
from pathlib import Path

import pytest

from legex import published


def _write_gold(tmp_path: Path, cc: str, records: list[dict]) -> Path:
    path = tmp_path / cc / f"goldenset_{cc}.jsonl"
    path.parent.mkdir(parents=True)
    path.write_text("\n".join(json.dumps(r) for r in records) + "\n", encoding="utf-8")
    return path


def _record(case_id: str, annotator_id: str, **labels) -> dict:
    rec = {"case_id": case_id, "link": None, "full_text": "text"}
    rec.update({f: None for f in published.SCHEMA_FIELDS})
    rec.update(labels)
    rec.update({"comment": None, "original_input": "{}", "annotator_id": annotator_id})
    return rec


@pytest.fixture
def gold_dir(tmp_path: Path) -> Path:
    _write_gold(tmp_path, "ch", [
        _record("1A_1/2024", "aaaa000000", legal_subject_judgement="Rental",
                plaintiff_loosing_share=0.5, plaintiffs_all_count=1),
        _record("1A_2/2024", "aaaa000000", legal_subject_judgement="Contract",
                court_cost_awarded_nominal=500),
        # reannotations, appended after the primary rows
        _record("1A_1/2024", "bbbb111111", legal_subject_judgement="Rental_Agreement",
                plaintiff_loosing_share=0),
        # empty reannotation row: dropped by the annotator-label loader
        _record("1A_2/2024", "bbbb111111"),
    ])
    return tmp_path


def test_label_fields_are_the_11_evaluated_fields() -> None:
    assert len(published.LABEL_FIELDS) == 11
    assert "Currency_dispute_value_nominal" not in published.LABEL_FIELDS
    assert published.LABEL_FIELDS[0] == "legal_subject_judgement"


def test_load_gold_labels_takes_first_row_per_case(gold_dir: Path) -> None:
    columns, gold = published.load_gold_labels(gold_dir, "ch")
    assert columns == list(published.LABEL_FIELDS)
    assert set(gold) == {"1A_1/2024", "1A_2/2024"}
    # primary row wins over the appended reannotation; values are normalised strings
    assert gold["1A_1/2024"]["legal_subject_judgement"] == "Rental"
    assert gold["1A_1/2024"]["plaintiff_loosing_share"] == "0.5"
    assert gold["1A_1/2024"]["plaintiffs_all_count"] == "1"
    assert gold["1A_2/2024"]["court_cost_awarded_nominal"] == "500"
    assert gold["1A_2/2024"]["trial_start_date"] == ""


def test_load_annotator_labels_roles_and_filtering(gold_dir: Path) -> None:
    labels = published.load_annotator_labels(gold_dir, ["ch"])
    keys = set(labels)
    # first row per case = primary; reannotation keyed by its hash; the
    # all-empty reannotation row is dropped
    assert (published.PRIMARY, "ch", "1a_1_2024") in keys
    assert (published.PRIMARY, "ch", "1a_2_2024") in keys
    assert ("bbbb111111", "ch", "1a_1_2024") in keys
    assert ("bbbb111111", "ch", "1a_2_2024") not in keys
    assert labels[("bbbb111111", "ch", "1a_1_2024")]["plaintiff_loosing_share"] == "0"


def test_inference_file_mapping(tmp_path: Path) -> None:
    assert published.inference_file(tmp_path, "ch", "harvey-2").name == "inference_harvey_2.jsonl"
    assert published.inference_file(tmp_path, "ch", "gemini/gemini-3.1-flash-lite").name == \
        "inference_gemini.jsonl"
    assert published.inference_file(tmp_path, "ch", "legora-1").name == "inference_legora_1.jsonl"
    with pytest.raises(KeyError):
        published.inference_file(tmp_path, "ch", "claude-fable-5")


def test_countries_with_gold(gold_dir: Path) -> None:
    assert published.countries_with_gold(gold_dir) == ["ch"]