FastPII Docs
Guides

Detection

How to detect PII in text, filter by detector, and work with DetectionResult.

Detection

Use detect() to scan text for PII entities. It returns a DetectionResult containing all findings with their positions, confidence scores, and metadata.

Basic detection

from fastpii import FastPII, DEFAULT_PRIORITY
from fastpii.countries.cz import CzechPack

engine = FastPII(priority=DEFAULT_PRIORITY)
engine.register(CzechPack())

result = engine.detect("Email: jan.novak@example.cz, RČ: 8001011238")

for finding in result.findings:
    print(f"{finding.type}: {finding.value} at [{finding.start}:{finding.end}]")
    print(f"  confidence: {finding.confidence:.0%}, region: {finding.region}")
    if finding.metadata:
        print(f"  metadata: {finding.metadata}")

Multi-region detection

from fastpii import FastPII, DEFAULT_PRIORITY
from fastpii.countries.cz import CzechPack
from fastpii.countries.pl import PolishPack
from fastpii.countries.de import GermanPack
from fastpii.countries.fr import FrenchPack

engine = FastPII(priority=DEFAULT_PRIORITY)
engine.register_many([CzechPack(), PolishPack(), GermanPack(), FrenchPack()])

text = "PESEL: 44051401458, Steuer-ID: 86095742719, SIREN: 552120222"
result = engine.detect(text)

for finding in result.findings:
    print(f"[{finding.region}] {finding.type}: {finding.value}")

Selective detection

Pass detector_names to run only specific detectors:

result = engine.detect(text, detector_names=["rodne_cislo", "email"])

This is useful when you only need to find certain types of PII and want to avoid unnecessary processing.

Working with DetectionResult

The DetectionResult object contains:

FieldTypeDescription
textstrThe original input text
findingslist[Finding]All detected PII findings
detector_nameslist[str]Unique list of detector types that triggered
processing_time_msintProcessing time in milliseconds

Working with Finding

Each Finding contains:

FieldTypeDescription
typestrDetector type (e.g., "rodne_cislo", "email")
valuestrThe matched text
startintCharacter offset of match start
endintCharacter offset of match end
confidencefloatConfidence score between 0.0 and 1.0
regionstrRegion code (e.g., "cz", "pl")
metadatadictDetector-specific metadata

Overlapping findings

When multiple detectors match overlapping text, FastPII resolves conflicts using the priority dict. Higher-priority detector types win:

from fastpii import FastPII, DEFAULT_PRIORITY

# DEFAULT_PRIORITY assigns rodne_cislo=100, phone=20
# If both match the same span, rodne_cislo wins
engine = FastPII(priority=DEFAULT_PRIORITY)

You can customize priorities:

priority = {
    "rodne_cislo": 100, "pesel": 100, "steuer_id": 100,
    "ico": 95, "email": 70, "name": 50, "phone": 20,
}
engine = FastPII(priority=priority)

Filtering by confidence

for finding in result.findings:
    if finding.confidence >= 0.9:
        print(f"High confidence: {finding.type} = {finding.value}")

Confidence scores are determined by:

  • Checksum validated: 1.0
  • Context match: 0.95
  • Pattern match: 0.85
  • No context: 0.70

With a context boost of 0.10 applied when context matches but checksum is not available.

On this page