VIN_OCR / vag_codes.py
Dirner's picture
Upload 2 files
9390f30 verified
Raw History Blame Contribute Delete
4.65 kB
import json
from functools import lru_cache
from pathlib import Path
VAG_WMI_PREFIXES = {
"XW8",
"XW7",
"WVW",
"WVG",
"WV1",
"WV2",
"3VW",
"1VW",
"9BW",
"LSV",
"LFV",
"MEX",
"TMB",
"TMK",
"TRU",
"WAU",
"WA1",
"VSS",
"WP0",
"WP1",
}
CONFIDENCE_RANK = {
"very_high": 4,
"high": 3,
"medium": 2,
"low": 1,
}
VIN_YEAR_CODES_2010 = {
"A": 2010,
"B": 2011,
"C": 2012,
"D": 2013,
"E": 2014,
"F": 2015,
"G": 2016,
"H": 2017,
"J": 2018,
"K": 2019,
"L": 2020,
"M": 2021,
"N": 2022,
"P": 2023,
"R": 2024,
"S": 2025,
"T": 2026,
"V": 2027,
"W": 2028,
"X": 2029,
"Y": 2030,
}
def normalize_vin(value):
return "".join(char for char in str(value).upper() if char.isalnum())
def vag_code_paths():
here = Path(__file__).resolve().parent
return [
here / "shared" / "vin" / "vag_sales_codes.json",
here.parent / "shared" / "vin" / "vag_sales_codes.json",
]
@lru_cache(maxsize=1)
def load_vag_sales_rules():
for path in vag_code_paths():
if path.exists():
with path.open("r", encoding="utf-8") as handle:
payload = json.load(handle)
return payload.get("rules", [])
return []
def vin_year(vin):
vin = normalize_vin(vin)
if len(vin) != 17:
return None
return VIN_YEAR_CODES_2010.get(vin[9])
def vag_windows(vin):
vin = normalize_vin(vin)
if len(vin) < 9:
return {
"wmi": vin[:3],
"vds": "",
"sales_code": "",
"model_code": "",
"model_year": None,
}
return {
"wmi": vin[:3],
"vds": vin[3:9],
"sales_code": vin[3:9],
"model_code": vin[6:8],
"model_year": vin_year(vin),
}
def is_vag_wmi(wmi):
return any(str(wmi).upper().startswith(prefix) for prefix in VAG_WMI_PREFIXES)
def rule_matches(rule, windows):
match = rule.get("match", {})
rule_wmi = match.get("wmi")
if rule_wmi and windows["wmi"] not in set(rule_wmi):
return False
sales_code = match.get("sales_code")
if sales_code and windows["sales_code"] != str(sales_code).upper():
return False
model_code = match.get("model_code")
if model_code and windows["model_code"] != str(model_code).upper():
return False
year = windows.get("model_year")
year_from = match.get("model_year_from")
year_to = match.get("model_year_to")
if year is not None and year_from is not None and year < int(year_from):
return False
if year is not None and year_to is not None and year > int(year_to):
return False
return True
def rule_specificity(rule):
match = rule.get("match", {})
score = 0
if match.get("sales_code"):
score += 20
if match.get("model_code"):
score += 8
if match.get("model_year_from") is not None or match.get("model_year_to") is not None:
score += 5
if match.get("wmi"):
score += 3
return score
def public_match(rule, windows):
vehicle = rule.get("vehicle", {})
return {
"ok": True,
"source": "vag_sales_codes",
"rule_id": rule.get("id", ""),
"confidence": rule.get("confidence", "low"),
"wmi": windows["wmi"],
"vds": windows["vds"],
"sales_code": windows["sales_code"],
"model_code": windows["model_code"],
"model_year": windows["model_year"],
"brand": vehicle.get("brand", ""),
"model": vehicle.get("model", ""),
"display_name": vehicle.get("display_name", ""),
"catalog_name": vehicle.get("catalog_name", ""),
}
def detect_vag_sales_code(vin):
windows = vag_windows(vin)
if not is_vag_wmi(windows["wmi"]):
return {
"ok": False,
"source": "vag_sales_codes",
"reason": "vin_not_vag_like",
**windows,
}
matches = [
rule for rule in load_vag_sales_rules()
if rule_matches(rule, windows)
]
matches.sort(
key=lambda rule: (
CONFIDENCE_RANK.get(rule.get("confidence", "low"), 0),
rule_specificity(rule),
),
reverse=True,
)
if not matches:
return {
"ok": False,
"source": "vag_sales_codes",
"reason": "no_rule_match",
**windows,
}
best = public_match(matches[0], windows)
best["candidates"] = [
public_match(rule, windows) for rule in matches[:5]
]
return best