Coverage for presidio_analyzer / predefined_recognizers / country_specific / korea / kr_driver_license_recognizer.py: 90%
21 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-03-29 09:03 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-03-29 09:03 +0000
1from typing import List, Optional, Tuple
3from presidio_analyzer import EntityRecognizer, Pattern, PatternRecognizer
6class KrDriverLicenseRecognizer(PatternRecognizer):
7 """
8 Recognize Korean Driver's License Number.
10 The Korean Driver's License Number consists of 12 digits,
11 typically formatted as AA-BB-CCCCCC-DD.
13 Format Breakdown:
14 - AA: Regional code
15 - BB: Year of issuance (last two digits of the year)
16 - CCCCCC: Serial number
17 - DD: Check digit (Verification number; not publicly disclosed)
19 :param patterns: List of patterns to be used by this recognizer
20 :param context: List of context words to increase confidence in detection
21 :param supported_language: Language this recognizer supports
22 :param supported_entity: The entity this recognizer can detect
23 :param replacement_pairs: List of tuples with potential replacement values
24 for different strings to be used during pattern matching.
25 This can allow a greater variety in input, for example by removing dashes.
26 """
28 PATTERNS = [
29 Pattern(
30 "Driver License (very weak)",
31 r"(?<!\d)(\d{2})[- ]?(\d{2})[- ]?(\d{6})[- ]?(\d{2})(?!\d)",
32 0.05,
33 )
34 ]
36 CONTEXT = [
37 "운전면허",
38 "운전면허번호",
39 "면허번호",
40 "Korean driver license",
41 "Korean driver's license",
42 ]
44 REGION_CODES = {
45 "11",
46 "12",
47 "13",
48 "14",
49 "15",
50 "16",
51 "17",
52 "18",
53 "19",
54 "20",
55 "21",
56 "22",
57 "23",
58 "24",
59 "25",
60 "26",
61 "28",
62 }
64 def __init__(
65 self,
66 patterns: Optional[List[Pattern]] = None,
67 context: Optional[List[str]] = None,
68 supported_language: str = "ko",
69 supported_entity: str = "KR_DRIVER_LICENSE",
70 replacement_pairs: Optional[List[Tuple[str, str]]] = None,
71 ):
72 self.replacement_pairs = (
73 replacement_pairs if replacement_pairs else [("-", ""), (" ", "")]
74 )
76 patterns = patterns if patterns else self.PATTERNS
77 context = context if context else self.CONTEXT
78 super().__init__(
79 supported_entity=supported_entity,
80 patterns=patterns,
81 context=context,
82 supported_language=supported_language,
83 )
85 def validate_result(self, pattern_text: str) -> bool:
86 """
87 Validate length, region code.
89 :param pattern_text: the text to validated.
90 Only the part in text that was detected by the regex engine
91 :return: A bool or None, indicating whether the validation was successful.
92 """
93 sanitized_value = EntityRecognizer.sanitize_value(
94 pattern_text, self.replacement_pairs
95 )
97 if len(sanitized_value) != 12:
98 return False
100 if not sanitized_value.isdigit():
101 return False
103 region_code = sanitized_value[:2]
104 if region_code not in self.REGION_CODES:
105 return False
107 return True