VIN_OCR / opel_codes.py
Dirner's picture
Upload 2 files
c125f43 verified
Raw History Blame Contribute Delete
3.82 kB
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