Coverage for presidio_analyzer / predefined_recognizers / country_specific / us / us_passport_recognizer.py: 100%

9 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-03-29 09:03 +0000

1from typing import List, Optional 

2 

3from presidio_analyzer import Pattern, PatternRecognizer 

4 

5 

6class UsPassportRecognizer(PatternRecognizer): 

7 """ 

8 Recognizes US Passport number using regex. 

9 

10 :param patterns: List of patterns to be used by this recognizer 

11 :param context: List of context words to increase confidence in detection 

12 :param supported_language: Language this recognizer supports 

13 :param supported_entity: The entity this recognizer can detect 

14 """ 

15 

16 # Weak pattern: all passport numbers are a weak match, e.g., 14019033 

17 PATTERNS = [ 

18 Pattern("Passport (very weak)", r"(\b[0-9]{9}\b)", 0.05), 

19 Pattern("Passport Next Generation (very weak)", r"(\b[A-Z][0-9]{8}\b)", 0.1), 

20 ] 

21 CONTEXT = ["us", "united", "states", "passport", "passport#", "travel", "document"] 

22 

23 def __init__( 

24 self, 

25 patterns: Optional[List[Pattern]] = None, 

26 context: Optional[List[str]] = None, 

27 supported_language: str = "en", 

28 supported_entity: str = "US_PASSPORT", 

29 name: Optional[str] = None, 

30 ): 

31 patterns = patterns if patterns else self.PATTERNS 

32 context = context if context else self.CONTEXT 

33 super().__init__( 

34 supported_entity=supported_entity, 

35 patterns=patterns, 

36 context=context, 

37 supported_language=supported_language, 

38 name=name, 

39 )