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

20 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 AuAcnRecognizer(PatternRecognizer): 

7 """ 

8 Recognizes Australian Company Number ("ACN"). 

9 

10 The Australian Company Number (ACN) is a nine digit number 

11 with the last digit being a check digit calculated using a 

12 modified modulus 10 calculation. 

13 This recognizer identifies ACN using regex, context words, and checksum. 

14 Reference: https://asic.gov.au/ 

15 

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

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

18 :param supported_language: Language this recognizer supports 

19 :param supported_entity: The entity this recognizer can detect 

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

21 for different strings to be used during pattern matching. 

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

23 """ 

24 

25 PATTERNS = [ 

26 Pattern( 

27 "ACN (Medium)", 

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

29 0.1, 

30 ), 

31 Pattern( 

32 "ACN (Low)", 

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

34 0.01, 

35 ), 

36 ] 

37 

38 CONTEXT = [ 

39 "australian company number", 

40 "acn", 

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_ACN", 

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 acn_list = [int(digit) for digit in text if not digit.isspace()] 

76 

77 # Set weights based on digit position 

78 weight = [8, 7, 6, 5, 4, 3, 2, 1] 

79 

80 # Perform checksums 

81 sum_product = 0 

82 for i in range(8): 

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

84 remainder = sum_product % 10 

85 complement = 10 - remainder 

86 return complement == acn_list[-1]