File size: 6,081 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""Refusal detection, value resolution, and the cleaning provenance format."""
import json

import pytest

from legex.evaluation.comparison import classify_cell, normalise, refusal_reason, resolve
from legex.evaluation.cleaning import format_comment, format_original_input

REFUSALS = [
    ("The document does not state a specific date. N/A", "trial_start_date"),
    ("None (the judgment does not contain a party designation block)", "plaintiffs_all_count"),
    ("Based on my review, I cannot determine the amount in dispute.", "dispute_value_nominal"),
    ("N/A", "trial_end_date"),
    ("not specified", "trial_start_date"),
]

KEEPERS = [
    ("nonpecuniary", "dispute_value_nominal"),
    ("no_allocation_possible", "plaintiff_no1_ISIC1_industry_category"),
    ("Employment_Law/Discrimination", "legal_subject_judgement"),
    ("2024-01-21", "trial_end_date"),
    ("25400000", "dispute_value_nominal"),
    ("1", "plaintiffs_all_count"),
    ("1.0", "plaintiff_loosing_share"),      # already a clean float → kept as-is
    ("0.86", "plaintiff_loosing_share"),
    ("l_financial_insurance", "defendant_no1_ISIC1_industry_category"),
]

# (raw, column, expected canonical) — a single value recoverable after stripping
# formatting noise / citations / backticks (never a digit scraped from prose).
RECOVERABLE = [
    ("20'000", "court_cost_awarded_nominal", "20000"),
    ("1 000 000", "dispute_value_nominal", "1000000"),
    ("2024/01/21", "trial_end_date", "2024-01-21"),
    ("1\n```", "plaintiffs_all_count", "1"),
    ("[1]\n\n2025-08-19", "trial_end_date", "2025-08-19"),
    ("1950\n[1] [2]", "court_cost_awarded_nominal", "1950"),
    # 2026-08 Harvey run: standalone English prose dates despite YYYY-MM-DD rule.
    ("March 19, 2026", "trial_end_date", "2026-03-19"),
    ("30 September 2020", "trial_end_date", "2020-09-30"),
    # 2026-08 Harvey run: ISIC codes with the sector letter capitalised.
    ("L_financial_insurance", "defendant_no1_ISIC1_industry_category", "l_financial_insurance"),
    ("No_allocation_possible", "plaintiff_no1_ISIC1_industry_category", "no_allocation_possible"),
    # Currency-decorated clean amounts.
    ("CHF 9'728'400.00", "dispute_value_nominal", "9728400"),
    ("30'000.--", "dispute_value_nominal", "30000"),
    ("6'500 fr.", "court_cost_awarded_nominal", "6500"),
    ("22.201,76 EUR", "dispute_value_nominal", "22201.76"),
]

# Prose (has letters) → never auto-recovered, even with a number present → review.
PROSE_TO_REVIEW = [
    ("1.0\n\nThe complaint was declared inadmissible.", "plaintiff_loosing_share"),
    ("The operative part imposes court costs of 800 Swiss Francs.[1]\n\n800",
     "court_cost_awarded_nominal"),
    ("Greer (No. 19-8709): 1.0\n\nGary (No. 20-444): 1.0", "plaintiff_loosing_share"),
    # A number with a parenthetical qualifier is still prose — a human decides.
    ("1 (total plaintiffs/claimants/appellants)", "plaintiffs_all_count"),
]

# Grouped integers without a decimal part: thousands- vs decimal-separator is
# ambiguous ('5.000 €' = 5000 EU or 5.0 US) → review, never auto-recovered.
AMBIGUOUS_AMOUNTS_TO_REVIEW = [
    ("5.000 €", "dispute_value_nominal"),
    ("538,183 euro", "dispute_value_nominal"),
]


@pytest.mark.parametrize("text,col", REFUSALS)
def test_refusals_go_to_review(text, col):
    status, _, reason = resolve(text, col)
    assert status == "review" and reason


@pytest.mark.parametrize("text,col", KEEPERS)
def test_valid_values_kept(text, col):
    status, value, _ = resolve(text, col)
    assert status == "valid" and value == text


@pytest.mark.parametrize("text,col,canon", RECOVERABLE)
def test_recoverable_values_canonicalised(text, col, canon):
    status, value, _ = resolve(text, col)
    assert status == "recovered" and value == canon


# Formatting-only recoveries the scorer already parses identically → must be score-neutral.
# (Noise-stripped dates like "[1]\n\n2025-08-19" are intentional corrections, not neutral.)
NEUTRAL_RECOVERABLE = [
    ("20'000", "court_cost_awarded_nominal", "20000"),
    ("1 000 000", "dispute_value_nominal", "1000000"),
    ("2024/01/21", "trial_end_date", "2024-01-21"),
    ("1\n```", "plaintiffs_all_count", "1"),
    ("1950\n[1] [2]", "court_cost_awarded_nominal", "1950"),
]


@pytest.mark.parametrize("text,col,canon", NEUTRAL_RECOVERABLE)
def test_recovery_is_score_neutral(text, col, canon):
    for gold in ("", canon, "999", "2024-01-21"):
        assert classify_cell(gold, normalise(text), col) == classify_cell(gold, canon, col)


@pytest.mark.parametrize("text,col", PROSE_TO_REVIEW)
def test_prose_with_number_goes_to_review_not_garbage(text, col):
    # Never scrape a number out of prose into the data — a human decides.
    status, _, _ = resolve(text, col)
    assert status == "review"


@pytest.mark.parametrize("text,col", AMBIGUOUS_AMOUNTS_TO_REVIEW)
def test_ambiguous_grouped_amounts_go_to_review(text, col):
    status, _, _ = resolve(text, col)
    assert status == "review"


def test_prose_without_value_or_marker_still_reviewed():
    status, _, reason = resolve(
        "Based on the decision, the respondent substantially prevailed on the merits.",
        "plaintiff_loosing_share",
    )
    assert status == "review" and reason == "no recoverable value"


def test_normalise_is_pure():
    assert normalise("The document does not state a date. N/A") != ""
    assert refusal_reason("nonpecuniary", "dispute_value_nominal") is None


def test_provenance_format():
    changes = {
        "trial_start_date": ("Not specified ", ""),
        "court_cost_awarded_nominal": ("20'000", "20000"),
    }
    assert format_comment(changes) == (
        "The trial_start_date was sanitized from 'Not specified ' to 'empty (removed)'. "
        "The court_cost_awarded_nominal was sanitized from '20'000' to '20000'."
    )
    assert json.loads(format_original_input(changes)) == {
        "trial_start_date": "Not specified ", "court_cost_awarded_nominal": "20'000",
    }
    assert format_comment({}) is None
    assert format_original_input({}) == "{}"