forked from interviewstreet/hackerrank-orchestrate-june26
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalizer.py
More file actions
154 lines (135 loc) · 4.63 KB
/
Copy pathnormalizer.py
File metadata and controls
154 lines (135 loc) · 4.63 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
"""Deterministic categorical normalizer.
Maps common free-form descriptions to the official enums BEFORE fuzzy-matching,
so 'back bumper' -> 'rear_bumper' is deterministic rather than reliant on
difflib proximity. This is the first stage of post-processing; the
postprocessor then enforces object-bound parts and consistency invariants.
Specs: problem_statement.md allowed values + Sprint 5 normalization rules.
"""
import difflib
from config import (
VALID_CLAIM_STATUS, VALID_ISSUE_TYPES, VALID_SEVERITY, VALID_RISK_FLAGS,
PARTS_MAP,
)
# Explicit alias -> canonical maps. Keys are matched case-insensitively after
# lowercasing + collapsing spaces to underscores.
NORMALIZER_ALIASES = {
"object_part": {
# car
"back bumper": "rear_bumper",
"rear bumper": "rear_bumper",
"front bumper": "front_bumper",
"rear side panel": "quarter_panel",
"side panel": "quarter_panel",
"rear windshield": "windshield",
"back glass": "windshield",
"back light": "taillight",
"tail light": "taillight",
"head light": "headlight",
"mirror": "side_mirror",
"side mirror": "side_mirror",
"trunk": "body",
"bonnet": "hood",
"wing mirror": "side_mirror",
# laptop
"laptop screen": "screen",
"display": "screen",
"touchpad": "trackpad",
"clamshell": "lid",
"edge": "corner",
"usb port": "port",
"charging port": "port",
"bottom": "base",
# package
"box corner": "package_corner",
"box side": "package_side",
"flap": "seal",
"sticker": "label",
"inside": "contents",
"product": "item",
},
"issue_type": {
"screen crack": "crack",
"cracked screen": "crack",
"hairline crack": "crack",
"water spill": "water_damage",
"liquid damage": "water_damage",
"spill": "water_damage",
"shattered glass": "glass_shatter",
"shattered": "glass_shatter",
"smashed": "broken_part",
"snapped": "broken_part",
"ripped": "torn_packaging",
"torn": "torn_packaging",
"crumpled": "crushed_packaging",
"dented": "dent",
"gouge": "scratch",
"scuff": "scratch",
"discoloration": "stain",
"mark": "stain",
},
"severity": {
"minor": "low",
"cosmetic": "low",
"small": "low",
"slight": "low",
"moderate": "medium",
"medium damage": "medium",
"severe": "high",
"major": "high",
"heavy": "high",
"extensive": "high",
"large": "high",
"bad": "high",
},
}
def _key(s):
return str(s).strip().lower()
def _canonical_or_fuzzy(value, allowed, fallback, aliases=None):
"""alias -> exact -> difflib closest -> fallback."""
k = _key(value)
if not k:
return fallback
if aliases and k in aliases:
return aliases[k]
# allow either spaces or underscores in the input
k_und = k.replace(" ", "_")
if k_und in allowed:
return k_und
if k in allowed:
return k
matches = difflib.get_close_matches(k_und, allowed, n=1, cutoff=0.6)
if not matches:
matches = difflib.get_close_matches(k, allowed, n=1, cutoff=0.6)
return matches[0] if matches else fallback
def normalize_object_part(value, claim_object):
allowed = PARTS_MAP.get(claim_object, PARTS_MAP["car"])
out = _canonical_or_fuzzy(value, allowed, "unknown", NORMALIZER_ALIASES["object_part"])
if out not in allowed:
return "unknown"
return out
def normalize_issue_type(value):
return _canonical_or_fuzzy(value, VALID_ISSUE_TYPES, "unknown",
NORMALIZER_ALIASES["issue_type"])
def normalize_severity(value):
return _canonical_or_fuzzy(value, VALID_SEVERITY, "unknown",
NORMALIZER_ALIASES["severity"])
def normalize_claim_status(value):
return _canonical_or_fuzzy(value, VALID_CLAIM_STATUS,
"not_enough_information")
def normalize_risk_flags(value):
if not value:
return "none"
tokens = [t.strip() for t in str(value).split(";") if t.strip()]
out = []
seen = set()
for t in tokens:
tl = t.lower()
if tl == "none":
continue
# alias-free; just validate against allowed
m = difflib.get_close_matches(tl, VALID_RISK_FLAGS, n=1, cutoff=0.8)
canonical = m[0] if m else None
if canonical and canonical not in seen:
seen.add(canonical)
out.append(canonical)
return ";".join(out) if out else "none"