mirror of
https://github.com/data-privacy-stack/presidio.git
synced 2026-07-26 12:50:56 -05:00
added logic to handle phonenumbers with country code (#1426)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from typing import List, Optional
|
||||
|
||||
import phonenumbers
|
||||
from phonenumbers.phonenumberutil import NumberParseException
|
||||
|
||||
from presidio_analyzer import (
|
||||
AnalysisExplanation,
|
||||
@@ -66,9 +67,16 @@ class PhoneRecognizer(LocalRecognizer):
|
||||
for match in phonenumbers.PhoneNumberMatcher(
|
||||
text, region, leniency=self.leniency
|
||||
):
|
||||
results += [
|
||||
try:
|
||||
parsed_number = phonenumbers.parse(text[match.start:match.end])
|
||||
region = phonenumbers.region_code_for_number(parsed_number)
|
||||
results += [
|
||||
self._get_recognizer_result(match, text, region, nlp_artifacts)
|
||||
]
|
||||
except NumberParseException:
|
||||
results += [
|
||||
self._get_recognizer_result(match, text, region, nlp_artifacts)
|
||||
]
|
||||
|
||||
return EntityRecognizer.remove_duplicates(results)
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
from .assertions import assert_result, assert_result_within_score_range # noqa
|
||||
from .assertions import assert_result, assert_result_within_score_range, assert_result_with_textual_explanation # noqa
|
||||
|
||||
@@ -32,3 +32,12 @@ def assert_result_within_score_range(
|
||||
min_score = max(0, expected_score_min - error)
|
||||
max_score = min(1, expected_score_max + error)
|
||||
assert result.score >= min_score and result.score <= max_score
|
||||
|
||||
|
||||
def assert_result_with_textual_explanation(
|
||||
result, expected_entity_type, expected_start, expected_end, expected_score, expected_textual_explanation
|
||||
):
|
||||
assert_result(
|
||||
result, expected_entity_type, expected_start, expected_end, expected_score
|
||||
)
|
||||
assert result.analysis_explanation.textual_explanation == expected_textual_explanation
|
||||
@@ -1,7 +1,7 @@
|
||||
import pytest
|
||||
|
||||
from presidio_analyzer.predefined_recognizers.phone_recognizer import PhoneRecognizer
|
||||
from tests import assert_result
|
||||
from tests import assert_result, assert_result_with_textual_explanation
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
@@ -84,6 +84,53 @@ def test_when_phone_with_leniency_then_succeed(
|
||||
assert_result(res, entities[i], st_pos, fn_pos, score)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"text, expected_len, entities, expected_positions, score, expected_textual_explanations",
|
||||
[
|
||||
# fmt: off
|
||||
("My US number is (415) 555-0132, and my international one is +44 (20) 7123 4567",
|
||||
2, ["PHONE_NUMBER", "PHONE_NUMBER"],
|
||||
((16, 30), (60, 78),), 0.4,
|
||||
['Recognized as US region phone number, using PhoneRecognizer','Recognized as GB region phone number, using PhoneRecognizer']),
|
||||
("My US number is (415) 555-0132, and my international one is +91 4155550132",
|
||||
2, ["PHONE_NUMBER", "PHONE_NUMBER"],
|
||||
((16, 30), (60, 74),), 0.4,
|
||||
['Recognized as US region phone number, using PhoneRecognizer','Recognized as IN region phone number, using PhoneRecognizer']),
|
||||
("My US number is (415) 555-0132, and my international one is +55 11 98456 5666",
|
||||
2, ["PHONE_NUMBER", "PHONE_NUMBER"],
|
||||
((16, 30), (60, 77),), 0.4,
|
||||
['Recognized as US region phone number, using PhoneRecognizer','Recognized as BR region phone number, using PhoneRecognizer']),
|
||||
("My US number is (415) 555-0132, and my international one is +49 30 1234567",
|
||||
2, ["PHONE_NUMBER", "PHONE_NUMBER"],
|
||||
((16, 30), (60, 74),), 0.4,
|
||||
['Recognized as US region phone number, using PhoneRecognizer','Recognized as DE region phone number, using PhoneRecognizer']),
|
||||
("My US number is (415) 555-0132, and my international one is +39 06 678 4343",
|
||||
2, ["PHONE_NUMBER", "PHONE_NUMBER"],
|
||||
((16, 30), (60, 75),), 0.4,
|
||||
['Recognized as US region phone number, using PhoneRecognizer','Recognized as IT region phone number, using PhoneRecognizer']),
|
||||
("My US number is (415) 555-0132, and my international one is +30 21 0 1234567",
|
||||
2, ["PHONE_NUMBER", "PHONE_NUMBER"],
|
||||
((16, 30), (60, 76),), 0.4,
|
||||
['Recognized as US region phone number, using PhoneRecognizer','Recognized as GR region phone number, using PhoneRecognizer']),
|
||||
# fmt: on
|
||||
],
|
||||
)
|
||||
def test_when_phone_with_textual_explanation_then_succeed(
|
||||
spacy_nlp_engine,
|
||||
text,
|
||||
expected_len,
|
||||
entities,
|
||||
expected_positions,
|
||||
score,
|
||||
expected_textual_explanations,
|
||||
):
|
||||
nlp_artifacts = spacy_nlp_engine.process_text(text, "en")
|
||||
recognizer = PhoneRecognizer()
|
||||
results = recognizer.analyze(text, entities, nlp_artifacts=nlp_artifacts)
|
||||
assert len(results) == expected_len
|
||||
for i, (res, (st_pos, fn_pos)) in enumerate(zip(results, expected_positions)):
|
||||
assert_result_with_textual_explanation(res, entities[i], st_pos, fn_pos, score, expected_textual_explanations[i])
|
||||
|
||||
def test_get_analysis_explanation():
|
||||
phone_recognizer = PhoneRecognizer()
|
||||
test_region = "US"
|
||||
|
||||
Reference in New Issue
Block a user