docling icon indicating copy to clipboard operation
docling copied to clipboard

Add Microsoft Presidio as a third engine option in the PII obfuscation example

Open scks1 opened this issue 1 month ago • 0 comments

Requested feature

docs/examples/pii_obfuscate.py currently offers two detection engines behind --engine: a Hugging Face token-classification pipeline (default) and GLiNER (optional). I would like to propose adding Microsoft Presidio (presidio-analyzer + presidio-anonymizer, spaCy-backed) as a third optional engine in the same example: --engine presidio / PII_ENGINE=presidio.

Presidio is a dedicated PII detection/anonymization library rather than a general NER model: it combines NER with pattern- and checksum-based recognizers for structured PII (email addresses, phone numbers, US SSNs, credit cards with Luhn validation), and its anonymizer resolves overlapping/conflicting detections. That makes it a natural third point of comparison next to the two model-based engines the example already demonstrates.

I have a working prototype of this as a post-conversion stage over TextItem text and TableItem cells, and four properties of the Presidio path differ from what the example does today:

  1. Counts-and-types-only reporting. The example currently ends by logging the obfuscation map — the actual detected values paired with their replacement IDs (entity_map, tabulated in main()). In a PII example, that log line writes the very values the example exists to hide into application logs. The Presidio path emits only counts per entity type per location class (text vs. table) and never a value. I verified this by string-searching both the redacted Markdown export and the serialized report for every planted fixture value: 0 of 28 values present in either output.

  2. Caller-selectable entity types. An entities=[...] parameter restricts both detection and replacement (e.g. only EMAIL_ADDRESS), rather than only the fixed label allowlist the current classes use.

  3. A custom STREET_ADDRESS recognizer. Presidio ships no street-address recognizer, so the prototype registers a documented regex PatternRecognizer for " <Street|Avenue|…>" shapes, with its recall measured separately like every other type.

  4. Measured fixture evidence. The prototype was measured on a synthetic fixture of 28 planted entities — 16 in body text, 12 in table cells (PERSON ×5, EMAIL ×5, PHONE ×4, US SSN ×4, Luhn-valid published test card numbers ×4, DATE_OF_BIRTH ×4, STREET_ADDRESS ×2) — on both a constructed DoclingDocument and a real pipeline conversion of the fixture PDF, with identical outcomes:

    • Recall 28/28 (text 16/16, table cells 12/12).
    • Replacement-level precision 28/32 (0.875) — the anonymizer's conflict resolution suppresses most spurious detections inside stronger spans. Four spurious replacements survive, including the table's "Email" header cell replaced by <PERSON>.
    • Detection precision 28/38 (0.737); the weak class is DATE_TIME at 4/12 — it fires on "spring", "the same day", and a distractor batch number read as a year, so the date recognizer over-redacts ordinary prose as measured.
    • 4 tests pass (placeholder replacement inside a table cell; counts-only report contains none of the planted values; input document not mutated; entity-type selection restricts both counts and replacements).

    Limits, stated plainly: the evidence is one small, clean, born-digital English document with the small spaCy model (en_core_web_sm). 28/28 recall is a smoke result, not a robustness claim — it says nothing about scanned pages, noisy OCR text, other languages, or other name distributions. The street-address regex is narrow by construction (it misses ZIP-bearing, lowercase, and non-US shapes).

If the maintainers would like this, I am happy to implement it as a PR against the existing example — Presidio imported lazily like GLiNER, the existing engines and the default behaviour untouched, DCO sign-off on the commits — and to adjust the shape (e.g. placeholder style: Presidio's <PERSON> typed placeholders vs. the example's stable person-1 IDs) to maintainer preference.

Alternatives

  • A standalone new example for Presidio redaction. I built the prototype in that shape first, but it would duplicate this example's function and placement; extending the existing example keeps a single PII example with an engine choice, consistent with how GLiNER was added.
  • Status quo. The HF and GLiNER engines remain the only options; structured-PII coverage then depends entirely on what the chosen NER model happens to label, and the value-logging map remains in the example's output.

Disclosure: this contribution was written by Árbol, an AI assistant working under the direction of the account holder, who set the acceptance criteria and verified the results before submission.

scks1 avatar Oct 30 '26 14:10 scks1