Coverage for presidio_analyzer / predefined_recognizers / country_specific / australia / au_medicare_recognizer.py: 100%

19 statements  

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

1from typing import List, Optional, Tuple 

2 

3from presidio_analyzer import EntityRecognizer, Pattern, PatternRecognizer 

4 

5 

6class AuMedicareRecognizer(PatternRecognizer): 

7 """ 

8 Recognizes Australian Medicare number using regex, context words, and checksum. 

9 

10 Medicare number is a unique identifier issued by Australian Government 

11 that enables the cardholder to receive a rebates of medical expenses 

12 under Australia's Medicare system. 

13 It uses a modulus 10 checksum scheme to validate the number. 

14 Reference: https://en.wikipedia.org/wiki/Medicare_card_(Australia) 

15 

16 

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

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

19 :param supported_language: Language this recognizer supports 

20 :param supported_entity: The entity this recognizer can detect 

21 :param replacement_pairs: List of tuples with potential replacement values 

22 for different strings to be used during pattern matching. 

23 This can allow a greater variety in input, for example by removing dashes or spaces. 

24 """ 

25 

26 PATTERNS = [ 

27 Pattern( 

28 "Australian Medicare Number (Medium)", 

29 r"\b[2-6]\d{3}\s\d{5}\s\d\b", 

30 0.1, 

31 ), 

32 Pattern( 

33 "Australian Medicare Number (Low)", 

34 r"\b[2-6]\d{9}\b", 

35 0.01, 

36 ), 

37 ] 

38 

39 CONTEXT = [ 

40 "medicare", 

41 ] 

42 

43 def __init__( 

44 self, 

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

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

47 supported_language: str = "en", 

48 supported_entity: str = "AU_MEDICARE", 

49 replacement_pairs: Optional[List[Tuple[str, str]]] = None, 

50 name: Optional[str] = None, 

51 ): 

52 self.replacement_pairs = ( 

53 replacement_pairs if replacement_pairs else [("-", ""), (" ", "")] 

54 ) 

55 patterns = patterns if patterns else self.PATTERNS 

56 context = context if context else self.CONTEXT 

57 super().__init__( 

58 supported_entity=supported_entity, 

59 patterns=patterns, 

60 context=context, 

61 supported_language=supported_language, 

62 name=name, 

63 ) 

64 

65 def validate_result(self, pattern_text: str) -> bool: 

66 """ 

67 Validate the pattern logic e.g., by running checksum on a detected pattern. 

68 

69 :param pattern_text: the text to validated. 

70 Only the part in text that was detected by the regex engine 

71 :return: A bool indicating whether the validation was successful. 

72 """ 

73 # Pre-processing before validation checks 

74 text = EntityRecognizer.sanitize_value(pattern_text, self.replacement_pairs) 

75 medicare_list = [int(digit) for digit in text if not digit.isspace()] 

76 

77 # Set weights based on digit position 

78 weight = [1, 3, 7, 9, 1, 3, 7, 9] 

79 

80 # Perform checksums 

81 sum_product = 0 

82 for i in range(8): 

83 sum_product += medicare_list[i] * weight[i] 

84 remainder = sum_product % 10 

85 return remainder == medicare_list[8]