forked from interviewstreet/hackerrank-orchestrate-june26
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostprocessor.py
More file actions
164 lines (137 loc) · 5.84 KB
/
Copy pathpostprocessor.py
File metadata and controls
164 lines (137 loc) · 5.84 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""Deterministic postprocessor.
The sole place that guarantees output conforms to specs/data_contract.md and
the (data-grounded) consistency invariants in specs/behavior_spec.md. The
model is never trusted to emit contract-conforming values; everything it
returns passes through here first.
Invariants enforced (data-grounded; see behavior_spec.md scenario 7 refined):
* claim_status=supported => valid_image=True AND evidence_standard_met=True
AND supporting_image_ids has >=1 valid id
* evidence_standard_met=False => claim_status != supported
* valid_image=False => claim_status != supported
(NOTE: valid_image=False does NOT force evidence_standard_met=False.)
"""
import difflib
from typing import Any, Dict, List
from config import (
VALID_CLAIM_STATUS, VALID_ISSUE_TYPES, VALID_SEVERITY, VALID_RISK_FLAGS,
PARTS_MAP,
)
from image_utils import parse_image_paths, image_id_from_path
# Fallback sentinels for unfixable values.
_FALLBACK = {
"claim_status": "not_enough_information",
"issue_type": "unknown",
"object_part": "unknown",
"severity": "unknown",
}
def _norm(value: Any) -> str:
if value is None:
return ""
return str(value).strip().lower()
def _fuzzy_fix(value: Any, allowed: List[str], fallback: str) -> str:
"""Lowercase+strip, then exact or difflib closest-match against allowed.
Cutoff 0.6: if nothing is close enough, return ``fallback``.
"""
v = _norm(value)
if v in allowed:
return v
if v:
matches = difflib.get_close_matches(v, allowed, n=1, cutoff=0.6)
if matches:
return matches[0]
return fallback
def _coerce_bool(value: Any, default: bool) -> bool:
if isinstance(value, bool):
return value
n = _norm(value)
if n in ("true", "1", "yes"):
return True
if n in ("false", "0", "no"):
return False
return default
def _fill_str(value: Any, sentinel: str) -> str:
if value is None:
return sentinel
s = str(value).strip()
return s if s else sentinel
def _clean_risk_flags(raw: Any, history_risk: bool) -> str:
"""Split, normalize, validate, inject history flag, rejoin; 'none' if empty."""
if not raw:
tokens: List[str] = []
else:
tokens = [_norm(t) for t in str(raw).split(";")]
valid = [t for t in tokens if t in VALID_RISK_FLAGS and t != "none"]
# de-duplicate, preserve order
seen = set()
uniq = []
for t in valid:
if t not in seen:
seen.add(t)
uniq.append(t)
if history_risk and "user_history_risk" not in seen:
uniq.append("user_history_risk")
return ";".join(uniq) if uniq else "none"
def _clean_supporting_ids(raw: Any, submitted_ids: List[str]) -> str:
"""Keep only IDs actually present in the row's image_paths; 'none' if empty."""
submitted = set(submitted_ids)
if not raw:
return "none"
tokens = [t.strip() for t in str(raw).split(";") if t and t.strip() != "none"]
kept = [t for t in tokens if t in submitted]
# de-duplicate, preserve order
seen = set()
uniq = []
for t in kept:
if t not in seen:
seen.add(t)
uniq.append(t)
return ";".join(uniq) if uniq else "none"
def postprocess(analysis: Dict[str, Any], claim_object: str,
image_paths: str, history_risk: bool) -> Dict[str, Any]:
"""Validate and fix a model analysis dict into a contract-conforming dict.
Args:
analysis: raw model output (keys matching ClaimAnalysis fields).
claim_object: 'car' | 'laptop' | 'package'.
image_paths: the row's raw image_paths cell (for ID filtering).
history_risk: rule-based history risk signal (drives flag injection).
"""
a = analysis or {}
# Parse the submitted image IDs once.
submitted_ids = [image_id_from_path(p) for p in parse_image_paths(image_paths)]
# --- Rule 1 & 2: enum + object-bound part (alias-first normalization) ---
from normalizer import (
normalize_issue_type, normalize_severity, normalize_claim_status,
normalize_object_part, normalize_risk_flags as _norm_risk,
)
issue_type = normalize_issue_type(a.get("issue_type"))
severity = normalize_severity(a.get("severity"))
claim_status = normalize_claim_status(a.get("claim_status"))
object_part = normalize_object_part(a.get("object_part"), claim_object)
# --- Rule 3 & 4: risk flags + history injection ---
risk_flags = _clean_risk_flags(a.get("risk_flags"), history_risk)
# --- Rule 5/6: booleans + supporting IDs ---
valid_image = _coerce_bool(a.get("valid_image"), True)
evidence_standard_met = _coerce_bool(a.get("evidence_standard_met"), True)
supporting_image_ids = _clean_supporting_ids(a.get("supporting_image_ids"), submitted_ids)
has_supporting = supporting_image_ids != "none"
# --- Rule 7: no nulls ---
evidence_standard_met_reason = _fill_str(a.get("evidence_standard_met_reason"),
"Evidence standard not evaluated.")
claim_status_justification = _fill_str(a.get("claim_status_justification"),
"No justification provided.")
# --- Invariants (data-grounded): delegate to the validator's force_fix,
# the single source of truth for consistency rules. ---
from validator import force_fix
result = {
"evidence_standard_met": evidence_standard_met,
"evidence_standard_met_reason": evidence_standard_met_reason,
"risk_flags": risk_flags,
"issue_type": issue_type,
"object_part": object_part,
"claim_status": claim_status,
"claim_status_justification": claim_status_justification,
"supporting_image_ids": supporting_image_ids,
"valid_image": valid_image,
"severity": severity,
}
return force_fix(result)