-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into niwilso/dicom/return-bboxes
- Loading branch information
Showing
15 changed files
with
865 additions
and
509 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
67 changes: 67 additions & 0 deletions
67
presidio-analyzer/presidio_analyzer/predefined_recognizers/in_pan_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,67 @@ | ||
from typing import Optional, List, Tuple | ||
|
||
from presidio_analyzer import Pattern, PatternRecognizer | ||
|
||
|
||
class InPanRecognizer(PatternRecognizer): | ||
""" | ||
Recognizes Indian Permanent Account Number ("PAN"). | ||
The Permanent Account Number (PAN) is a ten digit alpha-numeric code | ||
with the last digit being a check digit calculated using a | ||
modified modulus 10 calculation. | ||
This recognizer identifies PAN using regex and context words. | ||
Reference: https://en.wikipedia.org/wiki/Permanent_account_number, | ||
https://incometaxindia.gov.in/Forms/tps/1.Permanent%20Account%20Number%20(PAN).pdf | ||
:param patterns: List of patterns to be used by this recognizer | ||
:param context: List of context words to increase confidence in detection | ||
:param supported_language: Language this recognizer supports | ||
:param supported_entity: The entity this recognizer can detect | ||
:param replacement_pairs: List of tuples with potential replacement values | ||
for different strings to be used during pattern matching. | ||
This can allow a greater variety in input, for example by removing dashes or spaces. | ||
""" | ||
|
||
PATTERNS = [ | ||
Pattern( | ||
"PAN (High)", | ||
r"\b([A-Za-z]{3}[AaBbCcFfGgHhJjLlPpTt]{1}[A-Za-z]{1}[0-9]{4}[A-Za-z]{1})\b", | ||
0.85, | ||
), | ||
Pattern( | ||
"PAN (Medium)", | ||
r"\b([A-Za-z]{5}[0-9]{4}[A-Za-z]{1})\b", | ||
0.6, | ||
), | ||
Pattern( | ||
"PAN (Low)", | ||
r"\b((?=.*?[a-zA-Z])(?=.*?[0-9]{4})[\w@#$%^?~-]{10})\b", | ||
0.05, | ||
), | ||
] | ||
|
||
CONTEXT = [ | ||
"permanent account number", | ||
"pan", | ||
] | ||
|
||
def __init__( | ||
self, | ||
patterns: Optional[List[Pattern]] = None, | ||
context: Optional[List[str]] = None, | ||
supported_language: str = "en", | ||
supported_entity: str = "IN_PAN", | ||
replacement_pairs: Optional[List[Tuple[str, str]]] = None, | ||
): | ||
self.replacement_pairs = ( | ||
replacement_pairs if replacement_pairs else [("-", ""), (" ", "")] | ||
) | ||
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, | ||
) |
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
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,49 @@ | ||
import pytest | ||
|
||
from tests import assert_result | ||
from presidio_analyzer.predefined_recognizers import InPanRecognizer | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def recognizer(): | ||
return InPanRecognizer() | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def entities(): | ||
return ["IN_PAN"] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"text, expected_len, expected_position, expected_score", | ||
[ | ||
# fmt: off | ||
("AAASA1111R", 1, (0,10), 0.6) , | ||
("ABCPD1234Z", 1, (0, 10), 0.85), | ||
("ABCND1234Z", 1, (0, 10), 0.6), | ||
("A1111DFSFS", 1, (0,10),0.05), | ||
("ABCD1234",0,(),(),), | ||
("My PAN number is ABBPM4567S with a lot of text beyond it", 1, (17,27),.85), | ||
# fmt: on | ||
], | ||
) | ||
def test_when_pan_in_text_then_all_pans_found( | ||
text, | ||
expected_len, | ||
expected_position, | ||
expected_score, | ||
recognizer, | ||
entities, | ||
): | ||
results = recognizer.analyze(text, entities) | ||
print(results) | ||
|
||
assert len(results) == expected_len | ||
if results: | ||
assert_result( | ||
results[0], | ||
entities[0], | ||
expected_position[0], | ||
expected_position[1], | ||
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
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
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
Oops, something went wrong.