Spaces:
Running
Running
Commit ·
ffd8d4a
1
Parent(s): a527147
test(recognition): reproduce Dutch address overcapture contexts
Browse files
tests/test_dutch_address_span_precision.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from presidio_analyzer import RecognizerResult
|
| 2 |
+
|
| 3 |
+
from dutch_address_span_precision import tighten_dutch_address_results
|
| 4 |
+
from dutch_recognizers import get_dutch_entity_names, get_dutch_recognizers
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
LIVE_CONTEXTS = [
|
| 8 |
+
"Beschrijving Polderweg 8",
|
| 9 |
+
"De inspectie bezoekt Polderweg 8",
|
| 10 |
+
"Nu Polderweg 8 de",
|
| 11 |
+
"Op Polderweg 8",
|
| 12 |
+
"Polderweg 8 een",
|
| 13 |
+
"Polderweg 8 en",
|
| 14 |
+
"Polderweg 8 in",
|
| 15 |
+
"Polderweg 8 is",
|
| 16 |
+
"Polderweg 8 na",
|
| 17 |
+
"Polderweg 8 op",
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def _raw_address_results(text: str):
|
| 22 |
+
entities = get_dutch_entity_names(include_legal=True)
|
| 23 |
+
results = []
|
| 24 |
+
for recognizer in get_dutch_recognizers(supported_language="en"):
|
| 25 |
+
if "NL_ADDRESS" not in getattr(recognizer, "supported_entities", []):
|
| 26 |
+
continue
|
| 27 |
+
if hasattr(recognizer, "load"):
|
| 28 |
+
recognizer.load()
|
| 29 |
+
results.extend(recognizer.analyze(text, entities=entities, nlp_artifacts=None))
|
| 30 |
+
return [result for result in results if result.entity_type == "NL_ADDRESS"]
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def _values(text: str, results):
|
| 34 |
+
return [text[result.start : result.end] for result in results]
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def test_live_polderweg_contexts_resolve_to_exact_address_only():
|
| 38 |
+
for text in LIVE_CONTEXTS:
|
| 39 |
+
raw = _raw_address_results(text)
|
| 40 |
+
assert raw, f"baseline recognizer unexpectedly missed address in: {text!r}"
|
| 41 |
+
resolved = tighten_dutch_address_results(text, raw)
|
| 42 |
+
values = _values(text, resolved)
|
| 43 |
+
assert "Polderweg 8" in values, (text, values)
|
| 44 |
+
assert all(value == "Polderweg 8" for value in values), (text, values)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def test_house_number_does_not_absorb_short_adjacent_words():
|
| 48 |
+
for suffix in ("een", "en", "in", "is", "na", "op", "de"):
|
| 49 |
+
text = f"Polderweg 8 {suffix}"
|
| 50 |
+
broad = RecognizerResult(entity_type="NL_ADDRESS", start=0, end=len(text), score=0.66)
|
| 51 |
+
[resolved] = tighten_dutch_address_results(text, [broad])
|
| 52 |
+
assert text[resolved.start : resolved.end] == "Polderweg 8"
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def test_legitimate_prefix_address_with_postcode_and_city_is_preserved():
|
| 56 |
+
text = "Verweerder woont aan Laan van Meerdervoort 55, 2517 AM Den Haag."
|
| 57 |
+
start = text.index("Laan")
|
| 58 |
+
end = text.index(".")
|
| 59 |
+
broad = RecognizerResult(entity_type="NL_ADDRESS", start=start, end=end, score=0.70)
|
| 60 |
+
[resolved] = tighten_dutch_address_results(text, [broad])
|
| 61 |
+
assert text[resolved.start : resolved.end] == "Laan van Meerdervoort 55, 2517 AM Den Haag"
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def test_common_suffix_street_forms_remain_detectable():
|
| 65 |
+
examples = (
|
| 66 |
+
"Polderweg 8",
|
| 67 |
+
"Kerkstraat 12A",
|
| 68 |
+
"Nieuwe Kerkstraat 14",
|
| 69 |
+
"Westersingel 101-2",
|
| 70 |
+
)
|
| 71 |
+
for address in examples:
|
| 72 |
+
broad = RecognizerResult(entity_type="NL_ADDRESS", start=0, end=len(address), score=0.66)
|
| 73 |
+
[resolved] = tighten_dutch_address_results(address, [broad])
|
| 74 |
+
assert address[resolved.start : resolved.end] == address
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def test_unknown_address_shape_is_preserved_fail_safe_instead_of_dropped():
|
| 78 |
+
text = "Onbekende adresvorm 42"
|
| 79 |
+
original = RecognizerResult(entity_type="NL_ADDRESS", start=0, end=len(text), score=0.66)
|
| 80 |
+
[resolved] = tighten_dutch_address_results(text, [original])
|
| 81 |
+
assert resolved.start == original.start
|
| 82 |
+
assert resolved.end == original.end
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def test_non_address_results_are_untouched():
|
| 86 |
+
text = "Polderweg 8"
|
| 87 |
+
original = RecognizerResult(entity_type="LOCATION", start=0, end=len(text), score=0.7)
|
| 88 |
+
[resolved] = tighten_dutch_address_results(text, [original])
|
| 89 |
+
assert resolved is original
|