Coverage for presidio_analyzer / predefined_recognizers / country_specific / australia / au_tfn_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 AuTfnRecognizer(PatternRecognizer): 

7 """ 

8 Recognizes Australian Tax File Numbers ("TFN"). 

9 

10 The tax file number (TFN) is a unique identifier 

11 issued by the Australian Taxation Office 

12 to each taxpaying entity — an individual, company, 

13 superannuation fund, partnership, or trust. 

14 The TFN consists of a nine digit number, usually 

15 presented in the format NNN NNN NNN. 

16 TFN includes a check digit for detecting erroneous 

17 number based on simple modulo 11. 

18 This recognizer uses regex, context words, 

19 and checksum to identify TFN. 

20 Reference: https://www.ato.gov.au/individuals/tax-file-number/ 

21 

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

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

24 :param supported_language: Language this recognizer supports 

25 :param supported_entity: The entity this recognizer can detect 

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

27 for different strings to be used during pattern matching. 

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

29 """ 

30 

31 PATTERNS = [ 

32 Pattern( 

33 "TFN (Medium)", 

34 r"\b\d{3}\s\d{3}\s\d{3}\b", 

35 0.1, 

36 ), 

37 Pattern( 

38 "TFN (Low)", 

39 r"\b\d{9}\b", 

40 0.01, 

41 ), 

42 ] 

43 

44 CONTEXT = [ 

45 "tax file number", 

46 "tfn", 

47 ] 

48 

49 def __init__( 

50 self, 

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

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

53 supported_language: str = "en", 

54 supported_entity: str = "AU_TFN", 

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

56 name: Optional[str] = None, 

57 ): 

58 self.replacement_pairs = ( 

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

60 ) 

61 patterns = patterns if patterns else self.PATTERNS 

62 context = context if context else self.CONTEXT 

63 super().__init__( 

64 supported_entity=supported_entity, 

65 patterns=patterns, 

66 context=context, 

67 supported_language=supported_language, 

68 name=name, 

69 ) 

70 

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

72 """ 

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

74 

75 :param pattern_text: the text to validated. 

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

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

78 """ 

79 # Pre-processing before validation checks 

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

81 tfn_list = [int(digit) for digit in text if not digit.isspace()] 

82 

83 # Set weights based on digit position 

84 weight = [1, 4, 3, 7, 5, 8, 6, 9, 10] 

85 

86 # Perform checksums 

87 sum_product = 0 

88 for i in range(9): 

89 sum_product += tfn_list[i] * weight[i] 

90 remainder = sum_product % 11 

91 return remainder == 0