FastPII Docs
Guides

Pattern Registry

Work with PatternDefinition, PatternRegistry, and the shared registry used by FastPII detectors.

FastPII includes a pattern registry for declarative regex-based pattern loading.

PatternDefinition

PatternDefinition is the dataclass used to describe a pattern.

from fastpii.patterns.base import PatternDefinition

pattern = PatternDefinition(
    entity_type="employee_id",
    name="standard",
    regex=r"\b(EMP-\d{4})\b",
    region="internal",
    score=1.0,
    context_words=["employee", "staff", "internal"],
    checksum_algo=None,
)

Key fields:

FieldDescription
entity_typeLogical type such as rodne_cislo or employee_id
namePattern variant name
regexRegex used for detection
regionRegion code
scoreConfidence score applied to matches
context_wordsNearby words that can help the detector
checksum_algoOptional validation algorithm name

Create a registry

from fastpii.patterns import PatternRegistry

registry = PatternRegistry()
print(registry.get_available_regions())

With the current package, the available region is:

["cz"]

Load patterns by region

from fastpii.patterns import PatternRegistry

registry = PatternRegistry()
registry.load_patterns("cz")

print(registry.get_loaded_regions())
print(registry.get_available_entities("cz"))

Example output:

["cz"]
["rodne_cislo", "ico", "dic", "bank_account", "postal_code", "phone", "email"]

Access registered patterns

from fastpii.patterns import PatternRegistry

registry = PatternRegistry()
registry.load_patterns("cz")

patterns = registry.get_patterns("rodne_cislo", "cz")
pattern = registry.get_pattern("rodne_cislo", "standard", "cz")

print(len(patterns))
print(pattern.name)
print(pattern.score)

Shared registry

Use get_shared_registry() when you want the singleton registry instance used across detectors.

from fastpii.patterns import get_shared_registry

registry = get_shared_registry()
registry.load_patterns("cz")

print(type(registry).__name__)

Add a new pattern manually

from fastpii.patterns import PatternRegistry
from fastpii.patterns.base import PatternDefinition

registry = PatternRegistry()

registry.register(
    PatternDefinition(
        entity_type="employee_id",
        name="standard",
        regex=r"\b(EMP-\d{4})\b",
        region="internal",
        score=1.0,
    )
)

Add a new region with PatternLoader

PatternRegistry.load_patterns(region_code) relies on region loaders registered in fastpii.patterns.regions.

The loader protocol is:

from fastpii.patterns.base import PatternDefinition


class InternalPatternLoader:
    @staticmethod
    def load() -> list[PatternDefinition]:
        return [
            PatternDefinition(
                entity_type="employee_id",
                name="standard",
                regex=r"\b(EMP-\d{4})\b",
                region="internal",
                score=1.0,
            )
        ]

To expose the region, add it to the REGION_LOADERS mapping in fastpii.patterns.regions.

After that, PatternRegistry().load_patterns("internal") can load the new region.

On this page