-
Notifications
You must be signed in to change notification settings - Fork 603
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
5ea004d
commit 41e0202
Showing
6 changed files
with
116 additions
and
4 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
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
57 changes: 57 additions & 0 deletions
57
presidio-analyzer/presidio_analyzer/predefined_recognizers/in_voter_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,57 @@ | ||
from typing import Optional, List | ||
from presidio_analyzer import Pattern, PatternRecognizer | ||
|
||
|
||
class InVoterRecognizer(PatternRecognizer): | ||
""" | ||
Recognize Indian Voter/Election Id(EPIC). | ||
The Elector's Photo Identity Card or Voter id is a ten digit | ||
alpha-numeric code issued by Election Commission of India | ||
to adult domiciles who have reached the age of 18 | ||
Ref: https://en.wikipedia.org/wiki/Voter_ID_(India) | ||
: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( | ||
"VOTER", | ||
r"\b([A-Za-z]{1}[ABCDGHJKMNPRSYabcdghjkmnprsy]{1}[A-Za-z]{1}([0-9]){7})\b", | ||
0.4, | ||
), | ||
Pattern( | ||
"VOTER", | ||
r"\b([A-Za-z]){3}([0-9]){7}\b", | ||
0.3, | ||
), | ||
] | ||
|
||
CONTEXT = [ | ||
"voter", | ||
"epic", | ||
"elector photo identity card", | ||
] | ||
|
||
def __init__( | ||
self, | ||
patterns: Optional[List[Pattern]] = None, | ||
context: Optional[List[str]] = None, | ||
supported_language: str = "en", | ||
supported_entity: str = "IN_VOTER", | ||
): | ||
|
||
patterns = patterns if patterns else self.PATTERNS | ||
context = context if context else self.CONTEXT | ||
super().__init__( | ||
patterns=patterns, | ||
context=context, | ||
supported_language=supported_language, | ||
supported_entity=supported_entity, | ||
) |
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,52 @@ | ||
import pytest | ||
|
||
from tests import assert_result | ||
from presidio_analyzer.predefined_recognizers import InVoterRecognizer | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def recognizer(): | ||
return InVoterRecognizer() | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def entities(): | ||
return ["IN_VOTER"] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"text, expected_len, expected_position, expected_score", | ||
[ | ||
# fmt: off | ||
# valid | ||
("KSD1287349", 1, (0, 10), 0.4), | ||
("my voter: DBJ2289013", 1, (10, 20), (0.4)), | ||
("uzb2345117", 1, (0, 10), 0.3), | ||
("this MUP5632811", 1, (5, 15), 0.3), | ||
("You can vote with your CPJ4467918 number", 1, (23, 33), 0.4), | ||
# invalid | ||
("zxdf8923q1", 0, (), (),), | ||
("A8923571WZ", 0, (), (),), | ||
# fmt: on | ||
], | ||
) | ||
def test_when_voter_in_text_then_all_voter_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