File size: 3,817 Bytes
f9beba5 c125f43 f9beba5 c125f43 | 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 | import json
from functools import lru_cache
from pathlib import Path
CONFIDENCE_RANK = {
"high": 3,
"medium": 2,
"low": 1,
}
FALLBACK_OPEL_WMI = {
"W0L",
"WOL",
"XUF",
"XWF",
"VSX",
}
MODEL_CODE_OCR_REPAIRS = {
"P": ["A", "9"],
"9": ["A", "P"],
"4": ["A"],
"A": ["4"],
"8": ["B"],
"B": ["8"],
"5": ["S"],
"S": ["5"],
"0": ["O"],
"O": ["0"],
}
def normalize_vin(value):
return "".join(char for char in str(value).upper() if char.isalnum())
def opel_code_paths():
here = Path(__file__).resolve().parent
return [
here / "shared" / "vin" / "opel_vin_codes.json",
here.parent / "shared" / "vin" / "opel_vin_codes.json",
]
@lru_cache(maxsize=1)
def load_opel_codes():
for path in opel_code_paths():
if path.exists():
with path.open("r", encoding="utf-8") as handle:
payload = json.load(handle)
return {
"wmi": {
str(item.get("code", "")).upper()
for item in payload.get("wmi", [])
if item.get("code")
},
"model_codes": {
str(item.get("code", "")).upper(): item
for item in payload.get("modelCodes", [])
if item.get("code")
},
}
return {
"wmi": set(FALLBACK_OPEL_WMI),
"model_codes": {},
}
def opel_windows(vin):
vin = normalize_vin(vin)
return {
"wmi": vin[:3],
"model_code": vin[3:9] if len(vin) >= 9 else "",
}
def is_opel_wmi(wmi):
value = str(wmi).upper()
codes = load_opel_codes()
return value in codes["wmi"] or value in FALLBACK_OPEL_WMI
def public_match(rule, windows):
model_name = rule.get("modelName", "")
return {
"ok": True,
"source": "opel_vin_codes",
"confidence": "high",
"wmi": windows["wmi"],
"model_code": windows["model_code"],
"brand": "Opel",
"model": model_name,
"display_name": f"Opel {model_name}".strip(),
"family_hint": rule.get("familyHint", ""),
}
def detect_opel_model_code(vin):
windows = opel_windows(vin)
if not is_opel_wmi(windows["wmi"]):
return {
"ok": False,
"source": "opel_vin_codes",
"reason": "vin_not_opel_like",
**windows,
}
rule = load_opel_codes()["model_codes"].get(windows["model_code"])
if not rule:
return {
"ok": False,
"source": "opel_vin_codes",
"reason": "no_model_code_match",
**windows,
}
return public_match(rule, windows)
def generate_opel_model_code_variants(vin, base_reasons, max_variants=8):
windows = opel_windows(vin)
if len(normalize_vin(vin)) != 17 or not is_opel_wmi(windows["wmi"]):
return []
codes = load_opel_codes()["model_codes"]
if windows["model_code"] in codes:
return []
variants = []
model_code = windows["model_code"]
for offset, char in enumerate(model_code):
for replacement in MODEL_CODE_OCR_REPAIRS.get(char, []):
repaired_code = model_code[:offset] + replacement + model_code[offset + 1:]
if repaired_code not in codes:
continue
repaired_vin = vin[:3] + repaired_code + vin[9:]
variants.append((
repaired_vin,
list(base_reasons) + [
f"opel_model_repair:{model_code}->{repaired_code}",
f"ocr_repair:model_pos{offset + 4}:{char}->{replacement}",
],
))
if len(variants) >= max_variants:
return variants
return variants
|