Coverage for presidio_analyzer / predefined_recognizers / country_specific / india / in_aadhaar_recognizer.py: 100%
35 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 InAadhaarRecognizer(PatternRecognizer):
7 """
8 Recognizes Indian UIDAI Person Identification Number ("AADHAAR").
10 Reference: https://en.wikipedia.org/wiki/Aadhaar
11 A 12 digit unique number that is issued to each individual by Government of India
12 :param patterns: List of patterns to be used by this recognizer
13 :param context: List of context words to increase confidence in detection
14 :param supported_language: Language this recognizer supports
15 :param supported_entity: The entity this recognizer can detect
16 :param replacement_pairs: List of tuples with potential replacement values
17 for different strings to be used during pattern matching.
18 This can allow a greater variety in input, for example by removing dashes or spaces.
19 """
21 PATTERNS = [
22 Pattern(
23 "AADHAAR (Very Weak)",
24 r"\b[0-9]{12}\b",
25 0.01,
26 ),
27 Pattern("AADHAR (Very Weak)", r"\b[0-9]{4}[- :][0-9]{4}[- :][0-9]{4}\b", 0.01),
28 ]
30 CONTEXT = [
31 "aadhaar",
32 "uidai",
33 ]
35 utils = None
37 def __init__(
38 self,
39 patterns: Optional[List[Pattern]] = None,
40 context: Optional[List[str]] = None,
41 supported_language: str = "en",
42 supported_entity: str = "IN_AADHAAR",
43 replacement_pairs: Optional[List[Tuple[str, str]]] = None,
44 name: Optional[str] = None,
45 ) -> None:
46 self.replacement_pairs = (
47 replacement_pairs
48 if replacement_pairs
49 else [("-", ""), (" ", ""), (":", "")]
50 )
51 patterns = patterns if patterns else self.PATTERNS
52 context = context if context else self.CONTEXT
53 super().__init__(
54 supported_entity=supported_entity,
55 patterns=patterns,
56 context=context,
57 supported_language=supported_language,
58 name=name,
59 )
61 def validate_result(self, pattern_text: str) -> bool:
62 """Determine absolute value based on calculation."""
63 sanitized_value = EntityRecognizer.sanitize_value(
64 pattern_text, self.replacement_pairs
65 )
66 return self.__check_aadhaar(sanitized_value)
68 def __check_aadhaar(self, sanitized_value: str) -> bool:
69 is_valid_aadhaar: bool = False
70 if (
71 len(sanitized_value) == 12
72 and sanitized_value.isnumeric() is True
73 and int(sanitized_value[0]) >= 2
74 and self._is_verhoeff_number(int(sanitized_value)) is True
75 and self._is_palindrome(sanitized_value) is False
76 ):
77 is_valid_aadhaar = True
78 return is_valid_aadhaar
80 @staticmethod
81 def _is_palindrome(text: str, case_insensitive: bool = False) -> bool:
82 """
83 Validate if input text is a true palindrome.
85 :param text: input text string to check for palindrome
86 :param case_insensitive: optional flag to check palindrome with no case
87 :return: True / False
88 """
89 palindrome_text = text
90 if case_insensitive:
91 palindrome_text = palindrome_text.replace(" ", "").lower()
92 return palindrome_text == palindrome_text[::-1]
94 @staticmethod
95 def _is_verhoeff_number(input_number: int) -> bool:
96 """
97 Check if the input number is a true verhoeff number.
99 :param input_number:
100 :return:
101 """
102 __d__ = [
103 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
104 [1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
105 [2, 3, 4, 0, 1, 7, 8, 9, 5, 6],
106 [3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
107 [4, 0, 1, 2, 3, 9, 5, 6, 7, 8],
108 [5, 9, 8, 7, 6, 0, 4, 3, 2, 1],
109 [6, 5, 9, 8, 7, 1, 0, 4, 3, 2],
110 [7, 6, 5, 9, 8, 2, 1, 0, 4, 3],
111 [8, 7, 6, 5, 9, 3, 2, 1, 0, 4],
112 [9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
113 ]
114 __p__ = [
115 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
116 [1, 5, 7, 6, 2, 8, 3, 0, 9, 4],
117 [5, 8, 0, 3, 7, 9, 6, 1, 4, 2],
118 [8, 9, 1, 6, 0, 4, 3, 5, 2, 7],
119 [9, 4, 5, 3, 1, 2, 6, 8, 7, 0],
120 [4, 2, 8, 6, 5, 7, 3, 9, 0, 1],
121 [2, 7, 9, 3, 8, 0, 6, 4, 1, 5],
122 [7, 0, 4, 6, 9, 1, 3, 2, 5, 8],
123 ]
124 __inv__ = [0, 4, 3, 2, 1, 5, 6, 7, 8, 9]
126 c = 0
127 inverted_number = list(map(int, reversed(str(input_number))))
128 for i in range(len(inverted_number)):
129 c = __d__[c][__p__[i % 8][inverted_number[i]]]
130 return __inv__[c] == 0