Skip to content

Commit e4e751e

Browse files
committed
fix(engine): retain structured findings when NER spans overlap
1 parent fb262c8 commit e4e751e

3 files changed

Lines changed: 21 additions & 3 deletions

File tree

‎datafog/engine.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,9 @@ def scan(
789789

790790
filtered = _filter_entity_types(combined, entity_types)
791791
filtered = _apply_allowlist(filtered, allowlist, allowlist_patterns)
792-
deduped = _suppress_overlapping_entities(_dedupe_entities(filtered))
792+
# Optional NER adds candidates without suppressing structured findings.
793+
# Transformation resolves overlaps after selection, preserving scan labels.
794+
deduped = _dedupe_entities(filtered)
793795
return ScanResult(
794796
entities=deduped,
795797
text=text,

‎tests/corpus/unstructured_pii.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@
168168
{
169169
"type": "LOCATION",
170170
"text": "Jordan",
171-
"start": 0,
172-
"end": 6
171+
"start": 33,
172+
"end": 39
173173
}
174174
]
175175
},

‎tests/test_review_engine.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,19 @@ def test_shared_hash_helper_retains_legacy_widths_and_full_hmac(algorithm):
342342
engine._hash_value(value, algorithm, key, plain_length=8)
343343
== hmac.new(key, value.encode(), algorithm).hexdigest()
344344
)
345+
346+
347+
@pytest.mark.parametrize("mode", ["smart", "spacy", "gliner"])
348+
def test_optional_ner_cannot_remove_overlapping_structured_scan_findings(
349+
monkeypatch, mode
350+
):
351+
text = "Ping 127.0.0.1"
352+
candidates = [engine.Entity("PERSON", text, 0, len(text), 0.8, mode)]
353+
monkeypatch.setattr(engine, "_spacy_entities", lambda _: candidates)
354+
monkeypatch.setattr(engine, "_gliner_entities", lambda _: candidates)
355+
result = engine.scan(text, engine=mode)
356+
assert any(
357+
item.type == "IP_ADDRESS" and item.text == "127.0.0.1"
358+
for item in result.entities
359+
)
360+
assert any(item.type == "PERSON" for item in result.entities)

0 commit comments

Comments
 (0)