Pii Detector
Detect PII in text.
The package imports TextAnalyzer and TextAnonymizer on first access, so importing a single
recognizer does not load the analyzer or the anonymizer.
TextAnalyzer(additional_recognizers=None, nlp_configuration=None)
Bases: BaseTextAnalyzer
Detect PII entities in text using Presidio.
Additional recognizers with async def analyze(...) are wrapped so the
public analyze method remains synchronous.
Attributes:
| Name | Type | Description |
|---|---|---|
analyzer |
AnalyzerEngine
|
The analyzer engine that detects PII entities in text. |
Initialize TextAnalyzer and add recognizers.
The predefined recognizers are BankAccountNumberRecognizer, BPJSNumberRecognizer,
IndonesianPhoneNumberRecognizer, GDPLabsEmployeeIdRecognizer, FacebookAccountRecognizer,
FamilyCardNumberRecognizer, KTPRecognizer, LinkedinAccountRecognizer, MoneyRecognizer, NPWPRecognizer,
ProjectNameRecognizer, and CreditCardRecognizer.
The analyzer also adds Presidio EmailRecognizer and PhoneRecognizer for Indonesian.
No predefined recognizer detects organization names. To detect them, add an NER recognizer, such as
GDPLabsNerApiRemoteRecognizer or TransformersRecognizer, in additional_recognizers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
additional_recognizers
|
Sequence[EntityRecognizer] | None
|
Additional recognizers to add. Default is None. Async recognizers are wrapped internally to support sync analysis flow. |
None
|
nlp_configuration
|
dict[str, Any] | None
|
Retained for API compatibility and ignored because the analyzer always uses the model-free NLP engine. |
None
|
analyze(text, language='id', entities=None, score_threshold=0.46, allow_list=None, allow_list_match='exact', regex_flags=re.DOTALL | re.MULTILINE | re.IGNORECASE, **kwargs)
Detect PII entities in text.
The method intentionally stays synchronous for backward compatibility. The analyzer resolves awaitable results from additional recognizers on an internal background event loop.
Note
This method blocks the caller thread. In async application flows, call this method from a worker thread.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to analyze. |
required |
language
|
str
|
The language of the text to analyze. Default is "id". |
'id'
|
entities
|
list[str] | None
|
The entities to detect. If you need to extract other entities, add the recognizer in the constructor. Default is None. |
None
|
score_threshold
|
float | None
|
The minimum score threshold for the extracted entities. Default is 0.46. |
0.46
|
allow_list
|
list | None
|
Words to keep in the text. Default is None. |
None
|
allow_list_match
|
str | None
|
The matching strategy for the allow list. Default is "exact". |
'exact'
|
regex_flags
|
int | None
|
The regex flags for the text analysis. |
DOTALL | MULTILINE | IGNORECASE
|
**kwargs
|
Any
|
Additional arguments for text analysis. |
{}
|
Returns:
| Type | Description |
|---|---|
list[RecognizerResult]
|
list[RecognizerResult]: The detected entities. |
Raises:
| Type | Description |
|---|---|
UnsupportedLanguageError
|
If the language is not supported. |
InvalidInputError
|
If none of the requested entities can be detected in the language. |
TextAnonymizer(text_analyzer, operators=None, add_default_faker_operators=False, faker_seed=42, anonymizer_engine=None, deanonymizer_mapping=None, skip_format_duplicates=False)
Bases: BaseTextAnonymizer
Anonymize text using analyzer results and the Presidio library [1].
Attributes:
| Name | Type | Description |
|---|---|---|
anonymizer |
AnonymizerEngine
|
The anonymizer engine used in the anonymization process. |
operators |
dict[str, OperatorConfig]
|
The operators used in the anonymization process. |
anonymizer_mapping |
MappingDataType
|
The anonymizer mapping. |
deanonymizer_mapping |
MappingDataType
|
The deanonymizer mapping. |
Initialize the TextAnonymizer class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text_analyzer
|
BaseTextAnalyzer
|
The analyzer engine used in the anonymization process |
required |
operators
|
dic[str, OperatorConfig] | None
|
The operators used in the anonymization process see https://microsoft.github.io/presidio/anonymizer/#built-in-operators |
None
|
add_default_faker_operators
|
bool
|
Whether to add default faker operators. Defaults to False. |
False
|
faker_seed
|
int | None
|
The seed used for faker. Defaults to 42. |
42
|
anonymizer_engine
|
AnonymizerEngine
|
The anonymizer engine used in the anonymization process. Defaults to None. |
None
|
deanonymizer_mapping
|
DeanonymizerMapping
|
The deanonymizer mapping used to record mapping between anonymized and original text. Defaults to None. |
None
|
skip_format_duplicates
|
bool
|
Whether to skip formatting duplicated operators. Defaults to False. |
False
|
anonymizer_mapping
property
Return the anonymizer mapping.
Returns:
| Name | Type | Description |
|---|---|---|
MappingDataType |
MappingDataType
|
The anonymizer mapping. |
deanonymizer_mapping
property
Return the deanonymizer mapping.
Returns:
| Name | Type | Description |
|---|---|---|
MappingDataType |
MappingDataType
|
The deanonymizer mapping. |
add_operators(operators)
Add operators to the anonymizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operators
|
dict[str, OperatorConfig]
|
Operators to add to the anonymizer. |
required |
anonymize(text, entities=None, language='id', conflict_resolution=None, analyzer_score_threshold=0.46, allow_list=None, allow_list_match='exact', regex_flags=re.DOTALL | re.MULTILINE | re.IGNORECASE, **kwargs)
Anonymize the text based on the analyzer results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to anonymize. |
required |
entities
|
list[str] | None
|
The entities to anonymize. Defaults to None. |
None
|
language
|
str
|
The language of the text to analyze. Defaults to "id". |
'id'
|
conflict_resolution
|
ConflictResolutionStrategy | None
|
The conflict resolution strategy. Defaults to None. |
None
|
analyzer_score_threshold
|
float
|
The score threshold for the analyzer. Defaults to 0.46. |
0.46
|
allow_list
|
list | None
|
Words to keep in the text. Defaults to None. |
None
|
allow_list_match
|
str | None
|
The matching strategy for the allow list. Defaults to "exact". |
'exact'
|
regex_flags
|
int | None
|
The regex flags for the text analysis. |
DOTALL | MULTILINE | IGNORECASE
|
**kwargs
|
Any
|
Additional arguments for text analysis. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The anonymized text. |
deanonymize(text)
Deanonymize the text based on the anonymizer mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to deanonymize. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The deanonymized text or the original text if no mapping is found or the mapping is empty. |
remove_all_operators()
Remove all operators from the anonymizer.
reset_deanonymizer_mapping()
Reset the deanonymizer mapping.
__dir__()
List the public names alongside the module's own attributes.
__getattr__(name)
Import the analyzer or anonymizer module the first time the package attribute is read.
anonymize_with_mapping(text, anonymizer_mapping)
Anonymize text with an existing mapping without running PII detection.
anonymizer_mapping groups replacements by entity-type string, such as "PERSON". See
:class:Entities for the supported entity types and their matching rules.
Example
anonymizer_mapping = {
"PERSON": {
"John Doe": "<PERSON_1>",
},
}
result = anonymize_with_mapping("Hello John Doe", anonymizer_mapping=anonymizer_mapping)
assert result == "Hello <PERSON_1>"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text whose original values should be replaced. |
required |
anonymizer_mapping
|
dict[str, dict[str, str]]
|
An original-to-anonymized mapping in the form
|
required |
Notes
Matching is case-insensitive. Most entities use word boundaries; PHONE_NUMBER uses phone-number
boundaries instead. PROJECT, ORGANIZATION, and BRAND_NAME matches inside URLs are preserved.
Longer originals take precedence over overlapping shorter originals. Equal-length matches prefer URL-preserved entity types, then lexical order, so mapping insertion order does not affect the result. Every match is found in the original input; anonymized output is never processed by another mapping entry.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Text with matching original values replaced by their anonymized values. |