Coverage for presidio_analyzer / predefined_recognizers / country_specific / uk / uk_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 UkPassportRecognizer(PatternRecognizer): 

7 """ 

8 Recognizes UK passport numbers using regex. 

9 

10 UK passports issued from 2015 onwards use a 2-letter prefix 

11 followed by 7 digits (e.g., AB1234567). 

12 

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

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

15 :param supported_language: Language this recognizer supports 

16 :param supported_entity: The entity this recognizer can detect 

17 """ 

18 

19 PATTERNS = [ 

20 Pattern( 

21 "UK Passport (weak)", 

22 r"\b[A-Z]{2}\d{7}\b", 

23 0.1, 

24 ), 

25 ] 

26 

27 CONTEXT = [ 

28 "passport", 

29 "passport number", 

30 "travel document", 

31 "uk passport", 

32 "british passport", 

33 "her majesty", 

34 "his majesty", 

35 "hm passport", 

36 "hmpo", 

37 ] 

38 

39 def __init__( 

40 self, 

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

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

43 supported_language: str = "en", 

44 supported_entity: str = "UK_PASSPORT", 

45 name: Optional[str] = None, 

46 ): 

47 patterns = patterns if patterns else self.PATTERNS 

48 context = context if context else self.CONTEXT 

49 super().__init__( 

50 supported_entity=supported_entity, 

51 patterns=patterns, 

52 context=context, 

53 supported_language=supported_language, 

54 name=name, 

55 )