-
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.
Spanish NIE (Foreigners ID card) recognizer (#1359)
- Loading branch information
1 parent
f29e112
commit e64d8ec
Showing
6 changed files
with
141 additions
and
6 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
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
72 changes: 72 additions & 0 deletions
72
presidio-analyzer/presidio_analyzer/predefined_recognizers/es_nie_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,72 @@ | ||
from typing import List, Tuple, Optional | ||
|
||
from presidio_analyzer import Pattern, PatternRecognizer | ||
|
||
|
||
class EsNieRecognizer(PatternRecognizer): | ||
""" | ||
Recognize NIE number using regex and checksum. | ||
Reference(s): | ||
https://es.wikipedia.org/wiki/N%C3%BAmero_de_identidad_de_extranjero | ||
https://www.interior.gob.es/opencms/ca/servicios-al-ciudadano/tramites-y-gestiones/dni/calculo-del-digito-de-control-del-nif-nie/ | ||
: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( | ||
"NIE", | ||
r"\b[X-Z]?[0-9]?[0-9]{7}[-]?[A-Z]\b", | ||
0.5, | ||
), | ||
] | ||
|
||
CONTEXT = ["número de identificación de extranjero", "NIE"] | ||
|
||
def __init__( | ||
self, | ||
patterns: Optional[List[Pattern]] = None, | ||
context: Optional[List[str]] = None, | ||
supported_language: str = "es", | ||
supported_entity: str = "ES_NIE", | ||
replacement_pairs: Optional[List[Tuple[str, str]]] = None, | ||
): | ||
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: str) -> bool: | ||
"""Validate the pattern by using the control character.""" | ||
|
||
pattern_text = EsNieRecognizer.__sanitize_value(pattern_text) | ||
|
||
letters = "TRWAGMYFPDXBNJZSQVHLCKE" | ||
letter = pattern_text[-1] | ||
|
||
# check last is a letter, and first is in X,Y,Z | ||
if not pattern_text[1:-1].isdigit or pattern_text[:1] not in 'XYZ': | ||
return False | ||
# check size is 8 or 9 | ||
if len(pattern_text) < 8 or len(pattern_text) > 9: | ||
return False | ||
|
||
# replace XYZ with 012, and check the mod 23 | ||
number = int(str('XYZ'.index(pattern_text[0])) + pattern_text[1:-1]) | ||
return letter == letters[number % 23] | ||
|
||
@staticmethod | ||
def __sanitize_value(text: str) -> str: | ||
return text.replace("-", "").replace(" ", "") |
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,45 @@ | ||
import pytest | ||
|
||
from tests import assert_result | ||
from presidio_analyzer.predefined_recognizers import EsNieRecognizer | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def recognizer(): | ||
"""Return an instance of the EsNieRecognizer.""" | ||
return EsNieRecognizer() | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def entities(): | ||
"""Return entities to analyze.""" | ||
return ["ES_NIE"] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"text, expected_len, expected_positions", | ||
[ | ||
# valid NIE scores | ||
("Z8078221M", 1, ((0, 9),),), | ||
("X9613851N", 1, ((0, 9),),), | ||
("Y8063915Z", 1, ((0, 9),),), | ||
("Y8063915-Z", 1, ((0, 10),),), | ||
("Mi NIE es X9613851N", 1, ((10, 19),),), | ||
("Z8078221M en mi NIE", 1, ((0, 9),),), | ||
("Mi Número de identificación de extranjero es Y8063915-Z", 1, \ | ||
((45, 55),),), | ||
# invalid NIE scores | ||
("Y8063915Q", 0, ()), | ||
("Y806391Q", 0, ()), | ||
("58063915Q", 0, ()), | ||
("W8063915Q", 0, ()), | ||
], | ||
) | ||
def test_when_all_es_nie_then_succeed( | ||
text, expected_len, expected_positions, recognizer, entities, max_score | ||
): | ||
"""Tests the ES_NIE recognizer against valid & invalid examples.""" | ||
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, max_score) |