Coverage for presidio_analyzer / predefined_recognizers / country_specific / australia / au_abn_recognizer.py: 100%
20 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-03-29 09:03 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-03-29 09:03 +0000
1from typing import List, Optional, Tuple
3from presidio_analyzer import EntityRecognizer, Pattern, PatternRecognizer
6class AuAbnRecognizer(PatternRecognizer):
7 """
8 Recognizes Australian Business Number ("ABN").
10 The Australian Business Number (ABN) is a unique 11
11 digit identifier issued to all entities registered in
12 the Australian Business Register (ABR).
13 The 11 digit ABN is structured as a 9 digit identifier
14 with two leading check digits.
15 The leading check digits are derived using a modulus 89 calculation.
16 This recognizer identifies ABN using regex, context words and checksum.
17 Reference: https://abr.business.gov.au/Help/AbnFormat
19 :param patterns: List of patterns to be used by this recognizer
20 :param context: List of context words to increase confidence in detection
21 :param supported_language: Language this recognizer supports
22 :param supported_entity: The entity this recognizer can detect
23 :param replacement_pairs: List of tuples with potential replacement values
24 for different strings to be used during pattern matching.
25 This can allow a greater variety in input, for example by removing dashes or spaces.
26 """
28 PATTERNS = [
29 Pattern(
30 "ABN (Medium)",
31 r"\b\d{2}\s\d{3}\s\d{3}\s\d{3}\b",
32 0.1,
33 ),
34 Pattern(
35 "ABN (Low)",
36 r"\b\d{11}\b",
37 0.01,
38 ),
39 ]
41 CONTEXT = [
42 "australian business number",
43 "abn",
44 ]
46 def __init__(
47 self,
48 patterns: Optional[List[Pattern]] = None,
49 context: Optional[List[str]] = None,
50 supported_language: str = "en",
51 supported_entity: str = "AU_ABN",
52 replacement_pairs: Optional[List[Tuple[str, str]]] = None,
53 name: Optional[str] = None,
54 ):
55 self.replacement_pairs = (
56 replacement_pairs if replacement_pairs else [("-", ""), (" ", "")]
57 )
58 patterns = patterns if patterns else self.PATTERNS
59 context = context if context else self.CONTEXT
60 super().__init__(
61 supported_entity=supported_entity,
62 patterns=patterns,
63 context=context,
64 supported_language=supported_language,
65 name=name,
66 )
68 def validate_result(self, pattern_text: str) -> bool:
69 """
70 Validate the pattern logic e.g., by running checksum on a detected pattern.
72 :param pattern_text: the text to validated.
73 Only the part in text that was detected by the regex engine
74 :return: A bool indicating whether the validation was successful.
75 """
76 # Pre-processing before validation checks
77 text = EntityRecognizer.sanitize_value(pattern_text, self.replacement_pairs)
78 abn_list = [int(digit) for digit in text if not digit.isspace()]
80 # Set weights based on digit position
81 weight = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
83 # Perform checksums
84 abn_list[0] = 9 if abn_list[0] == 0 else abn_list[0] - 1
85 sum_product = 0
86 for i in range(11):
87 sum_product += abn_list[i] * weight[i]
88 remainder = sum_product % 89
89 return remainder == 0