Coverage for presidio_analyzer / predefined_recognizers / country_specific / poland / pl_pesel_recognizer.py: 100%
15 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
3from presidio_analyzer import Pattern, PatternRecognizer
6class PlPeselRecognizer(PatternRecognizer):
7 """
8 Recognize PESEL number using regex and checksum.
10 For more information about PESEL: https://en.wikipedia.org/wiki/PESEL
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 """
18 PATTERNS = [
19 Pattern(
20 "PESEL",
21 r"[0-9]{2}([02468][1-9]|[13579][012])(0[1-9]|1[0-9]|2[0-9]|3[01])[0-9]{5}",
22 0.4,
23 ),
24 ]
26 CONTEXT = ["PESEL"]
28 def __init__(
29 self,
30 patterns: Optional[List[Pattern]] = None,
31 context: Optional[List[str]] = None,
32 supported_language: str = "pl",
33 supported_entity: str = "PL_PESEL",
34 name: Optional[str] = None,
35 ):
36 patterns = patterns if patterns else self.PATTERNS
37 context = context if context else self.CONTEXT
38 super().__init__(
39 supported_entity=supported_entity,
40 patterns=patterns,
41 context=context,
42 supported_language=supported_language,
43 name=name,
44 )
46 def validate_result(self, pattern_text: str) -> bool: # noqa: D102
47 digits = [int(digit) for digit in pattern_text]
48 weights = [1, 3, 7, 9, 1, 3, 7, 9, 1, 3]
50 checksum = sum(digit * weight for digit, weight in zip(digits[:10], weights))
51 checksum %= 10
53 return checksum == digits[10]