Creates and configures an anonymizer instance for PII detection and replacement.
Signature
function createAnonymizer(config?: AnonymizerConfig): Anonymizer
Parameters
config (optional)
NER model configurationShow NERConfig properties
Model mode: 'disabled' | 'quantized' | 'standard' | 'custom'
Custom model path (required when mode is 'custom')
Custom vocab path (required when mode is 'custom')
Auto-download model if not present
Confidence thresholds per PII type (0.0-1.0)
Download progress callback
Semantic enrichment configurationShow SemanticConfig properties
Enable semantic enrichment (gender/scope attributes)
Auto-download semantic data if not present
Download progress callback
Encryption key provider. If not provided, generates a random key.
Storage provider for session-based PII map persistence
Default anonymization policy
Custom recognizer registry (uses default if not provided)
Returns
An Anonymizer instance with these methods:
| Method | Description |
|---|
initialize() | Initialize the anonymizer (loads models) |
anonymize(text, locale?, policy?) | Anonymize text |
session(sessionId) | Create a session for persistent storage |
dispose() | Release resources |
getRegistry() | Get the recognizer registry |
getNERModel() | Get the NER model instance |
isInitialized | Check if initialized |
Examples
Basic (Regex Only)
import { createAnonymizer } from 'rehydra';
const anonymizer = createAnonymizer();
await anonymizer.initialize();
const result = await anonymizer.anonymize('Contact: [email protected]');
console.log(result.anonymizedText);
// "Contact: <PII type="EMAIL" id="1"/>"
await anonymizer.dispose();
With NER
const anonymizer = createAnonymizer({
ner: {
mode: 'quantized',
onStatus: (status) => console.log(status)
}
});
await anonymizer.initialize();
const result = await anonymizer.anonymize('Hello John Smith!');
// "Hello <PII type="PERSON" id="1"/>!"
With Semantic Enrichment
const anonymizer = createAnonymizer({
ner: { mode: 'quantized' },
semantic: { enabled: true }
});
await anonymizer.initialize();
const result = await anonymizer.anonymize('Contact Maria in Berlin');
// "Contact <PII type="PERSON" gender="female" id="1"/> in <PII type="LOCATION" scope="city" id="2"/>"
With Storage
import {
createAnonymizer,
InMemoryKeyProvider,
SQLitePIIStorageProvider
} from 'rehydra';
const storage = new SQLitePIIStorageProvider('./pii.db');
await storage.initialize();
const anonymizer = createAnonymizer({
ner: { mode: 'quantized' },
keyProvider: new InMemoryKeyProvider(),
piiStorageProvider: storage,
});
await anonymizer.initialize();
// Use sessions for automatic persistence
const session = anonymizer.session('chat-123');
await session.anonymize('Hello John!');
With Custom Thresholds
import { createAnonymizer, PIIType } from 'rehydra';
const anonymizer = createAnonymizer({
ner: {
mode: 'quantized',
thresholds: {
[PIIType.PERSON]: 0.8,
[PIIType.ORG]: 0.7,
}
}
});
With Custom Policy
import { createAnonymizer, PIIType, createDefaultPolicy } from 'rehydra';
const policy = createDefaultPolicy();
policy.enabledTypes = new Set([PIIType.EMAIL, PIIType.PHONE, PIIType.PERSON]);
policy.allowlistTerms = new Set(['Support Team', 'Help Desk']);
policy.enableLeakScan = true;
const anonymizer = createAnonymizer({
ner: { mode: 'quantized' },
defaultPolicy: policy,
});