File size: 1,496 Bytes
f1ba785
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Conservative field-specific accept/retry/abstain policy."""
from .contracts import RankedCandidate, SemanticClass
from .reliability import AGREEMENT_CLASSES, CHECKSUM_CLASSES, MIN_RELIABILITY

BEAM_MARGIN_FLOOR = 0.05
RECOVERY_MARK = "nid_recovery"

# Character floors stay as a second screen after the reliability bound.
# They are not calibration.
THRESHOLDS: dict[SemanticClass, float] = {
    "nid": 97.0,
    "pan": 97.0,
    "registration_number": 94.0,
    "tax_id": 94.0,
    "account_number": 94.0,
    "date": 92.0,
    "amount": 92.0,
    "percentage": 90.0,
    "generic_numeric": 92.0,
}


def decide(

    candidate: RankedCandidate | None,

    semantic_class: SemanticClass,

    *,

    agreement: bool = False,

    reliability_lower_bound: float = 0.0,

) -> str:
    if candidate is None or not candidate.schema_valid or candidate.checksum_valid is False:
        return "abstain"
    if candidate.beam_margin < BEAM_MARGIN_FLOOR:
        return "retry"
    if RECOVERY_MARK in candidate.preprocessing_variant:
        return "retry"
    if reliability_lower_bound < MIN_RELIABILITY:
        return "retry"
    if candidate.min_character_confidence < THRESHOLDS[semantic_class]:
        return "retry"
    if semantic_class in CHECKSUM_CLASSES:
        return "accepted" if candidate.checksum_valid is True else "retry"
    if semantic_class in AGREEMENT_CLASSES:
        return "accepted" if agreement else "retry"
    return "retry"