Coverage for presidio_analyzer / predefined_recognizers / country_specific / us / aba_routing_recognizer.py: 100%
19 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 AbaRoutingRecognizer(PatternRecognizer):
7 """
8 Recognize American Banking Association (ABA) routing number.
10 Also known as routing transit number (RTN) and used to identify financial
11 institutions and process transactions.
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 :param replacement_pairs: List of tuples with potential replacement values
18 for different strings to be used during pattern matching.
19 This can allow a greater variety in input, for example by removing dashes or spaces.
20 """
22 PATTERNS = [
23 Pattern(
24 "ABA routing number (weak)",
25 r"\b[0123678]\d{8}\b",
26 0.05,
27 ),
28 Pattern(
29 "ABA routing number",
30 r"\b[0123678]\d{3}-\d{4}-\d\b",
31 0.3,
32 ),
33 ]
35 CONTEXT = [
36 "aba",
37 "routing",
38 "abarouting",
39 "association",
40 "bankrouting",
41 ]
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 = "ABA_ROUTING_NUMBER",
49 replacement_pairs: Optional[List[Tuple[str, str]]] = None,
50 name: Optional[str] = None,
51 ):
52 self.replacement_pairs = replacement_pairs or [("-", "")]
53 patterns = patterns if patterns else self.PATTERNS
54 context = context if context else self.CONTEXT
55 super().__init__(
56 supported_entity=supported_entity,
57 patterns=patterns,
58 context=context,
59 supported_language=supported_language,
60 name=name,
61 )
63 def validate_result(self, pattern_text: str) -> bool: # noqa: D102
64 sanitized_value = EntityRecognizer.sanitize_value(
65 pattern_text, self.replacement_pairs
66 )
67 return self.__checksum(sanitized_value)
69 @staticmethod
70 def __checksum(sanitized_value: str) -> bool:
71 s = 0
72 for idx, m in enumerate([3, 7, 1, 3, 7, 1, 3, 7, 1]):
73 s += int(sanitized_value[idx]) * m
74 return s % 10 == 0