11"""LicensePlateProvider."""
22
3+ import difflib
34import random
45from pathlib import Path
56
67from fakernaija .providers .state import StateProvider
78from fakernaija .utils import load_json , normalize_input
89
10+ DIGIT_COUNT = 3
11+ LETTER_COUNT = 2
12+
913
1014class LicensePlateProvider :
1115 """Provider class to generate Nigerian license plates."""
@@ -38,6 +42,9 @@ def generate_license_plate(self, state: str | None = None) -> str:
3842
3943 Returns:
4044 str: A randomly generated Nigerian license plate.
45+
46+ Raises:
47+ ValueError: If the state name is invalid.
4148 """
4249 state = normalize_input (state )
4350
@@ -46,7 +53,14 @@ def generate_license_plate(self, state: str | None = None) -> str:
4653 # Map lowercase state names to original case
4754 state_dict = {s .lower (): s for s in self .state_names }
4855 if state .lower () not in state_dict :
49- msg = f"Invalid state name: { state } . Valid states are: { ', ' .join (self .state_names )} "
56+ # Find close matches to the input state name
57+ suggestions = difflib .get_close_matches (
58+ state , self .state_names , n = 3 , cutoff = 0.6
59+ )
60+ if suggestions :
61+ msg = f"Invalid state name: { state } . Did you mean: { ', ' .join (suggestions )} ?"
62+ else :
63+ msg = f"Invalid state name: { state } . Valid states are: { ', ' .join (self .state_names )} "
5064 raise ValueError (msg )
5165 # Use the correctly cased state name to access LGA codes
5266 state_name = state_dict [state .lower ()]
@@ -56,6 +70,6 @@ def generate_license_plate(self, state: str | None = None) -> str:
5670 all_lgas = [code for codes in self .lga_codes .values () for code in codes ]
5771 lga_code = random .choice (all_lgas )
5872
59- digits = "" .join (random .choices ("0123456789" , k = 3 ))
60- letters = "" .join (random .choices ("ABCDEFGHIJKLMNOPQRSTUVWXYZ" , k = 2 ))
73+ digits = "" .join (random .choices ("0123456789" , k = DIGIT_COUNT ))
74+ letters = "" .join (random .choices ("ABCDEFGHIJKLMNOPQRSTUVWXYZ" , k = LETTER_COUNT ))
6175 return f"{ lga_code } -{ digits } { letters } "
0 commit comments