-
Notifications
You must be signed in to change notification settings - Fork 559
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ABA routing number recognizer (#348)
* aba routing number with checksum digit * Update test_aba_routing_recognizer.py Co-authored-by: Omri Mendels <omri374@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
115 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
presidio-analyzer/presidio_analyzer/predefined_recognizers/aba_routing_recognizer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from presidio_analyzer import Pattern, PatternRecognizer | ||
|
||
|
||
class AbaRoutingRecognizer(PatternRecognizer): | ||
""" | ||
Recognizes American Banking Association (ABA) routing number. | ||
Also known as routing transit number (RTN) and used to identify financial | ||
institutions and process transactions. | ||
""" | ||
|
||
PATTERNS = [ | ||
Pattern("ABA routing number (weak)", r"\b[0123678]\d{8}\b", 0.05,), | ||
Pattern("ABA routing number", | ||
r"\b[0123678]\d{3}-\d{4}-\d\b", | ||
0.3,), | ||
] | ||
|
||
CONTEXT = [ | ||
"aba", | ||
"routing", | ||
"abarouting", | ||
"association", | ||
"bankrouting", | ||
] | ||
|
||
def __init__( | ||
self, | ||
patterns=None, | ||
context=None, | ||
supported_language="en", | ||
supported_entity="ABA_ROUTING_NUMBER", | ||
replacement_pairs=None, | ||
): | ||
self.replacement_pairs = replacement_pairs or [("-", "")] | ||
patterns = patterns if patterns else self.PATTERNS | ||
context = context if context else self.CONTEXT | ||
super().__init__( | ||
supported_entity=supported_entity, | ||
patterns=patterns, | ||
context=context, | ||
supported_language=supported_language, | ||
) | ||
|
||
def validate_result(self, pattern_text): | ||
sanitized_value = self.__sanitize_value(pattern_text, | ||
self.replacement_pairs) | ||
return self.__checksum(sanitized_value) | ||
|
||
@staticmethod | ||
def __checksum(sanitized_value): | ||
s = 0 | ||
for idx, m in enumerate([3, 7, 1, 3, 7, 1, 3, 7, 1]): | ||
s += int(sanitized_value[idx]) * m | ||
return s % 10 == 0 | ||
|
||
@staticmethod | ||
def __sanitize_value(text, replacement_pairs): | ||
for search_string, replacement_string in replacement_pairs: | ||
text = text.replace(search_string, replacement_string) | ||
return text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import pytest | ||
|
||
from tests import assert_result | ||
from presidio_analyzer.predefined_recognizers import AbaRoutingRecognizer | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def recognizer(): | ||
return AbaRoutingRecognizer() | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def entities(): | ||
return ["ABA_ROUTING_NUMBER"] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"text, expected_len, expected_positions, expected_score", | ||
[ | ||
# Bank of America | ||
("121000358", 1, ((0, 9),), 1.0), | ||
# Chase | ||
("3222-7162-7", 1, ((0, 11),), 1.0), | ||
# Wells Fargo | ||
("121042882", 1, ((0, 9),), 1.0), | ||
("0711-0130-7", 1, ((0, 11),), 1.0), | ||
# invalid ABA numbers | ||
("421042111", 0, (), -1.0), | ||
("1234-0000-0", 0, (), -1.0), | ||
], | ||
) | ||
def test_aba_routing_numbers( | ||
text, expected_len, expected_positions, expected_score, recognizer, entities | ||
): | ||
results = recognizer.analyze(text, entities) | ||
assert len(results) == expected_len | ||
for res, (st_pos, fn_pos) in zip(results, expected_positions): | ||
assert_result(res, entities[0], st_pos, fn_pos, expected_score) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters