Anonymizer
Anonymization engine with token, synthetic, and hybrid strategies
Replaces detected entities with anonymized substitutes using configurable strategies.
from rdakt_ai import Anonymizer, AnonymizationStrategy
class AnonymizationStrategy(Enum):
TOKEN = "token"
SYNTHETIC = "synthetic"
HYBRID = "hybrid"
| Value | Replacement | Example |
|---|
TOKEN | <TYPE_N> tokens | "John Smith" → "<PERSON_1>" |
SYNTHETIC | Faker-generated values | "John Smith" → "Robert Johnson" |
HYBRID | Synthetic for names/orgs, token for the rest | Default strategy |
Anonymizer(
*,
default_strategy: AnonymizationStrategy = AnonymizationStrategy.HYBRID,
type_strategies: dict[str, AnonymizationStrategy] | None = None,
)
| Parameter | Type | Default | Description |
|---|
default_strategy | AnonymizationStrategy | HYBRID | Default strategy when type not in type_strategies |
type_strategies | dict[str, AnonymizationStrategy] | None | None | Per-entity-type strategy overrides |
def anonymize(
self,
text: str,
entities: list[Entity],
*,
session: RdaktSession | None = None,
) -> tuple[str, dict[str, str]]
Anonymize detected entities in text. Returns (anonymized_text, new_mappings).
| Parameter | Type | Description |
|---|
text | str | Original text |
entities | list[Entity] | Detected entities with positions |
session | RdaktSession | None | Session to reuse existing mappings |
| Attribute | Type | Value |
|---|
hybrid_synthetic_types | set[str] | {"PERSON", "ORGANIZATION", "LOCATION", "ADDRESS"} |
from rdakt_ai import Anonymizer, AnonymizationStrategy, Entity, RdaktSession
anonymizer = Anonymizer(
default_strategy=AnonymizationStrategy.TOKEN,
type_strategies={"PERSON": AnonymizationStrategy.SYNTHETIC},
)
entities = [
Entity(value="John Smith", type="PERSON", start=0, end=10),
Entity(value="john@example.com", type="EMAIL", start=23, end=39),
]
session = RdaktSession()
text, mapping = anonymizer.anonymize(
"John Smith — email: john@example.com",
entities,
session=session,
)
# text: "Robert Johnson — email: <EMAIL_1>"
# mapping: {"Robert Johnson": "John Smith", "<EMAIL_1>": "john@example.com"}