Files
presidio/docs/samples/python/example_custom_lambda_anonymizer.py
Omri Mendels 31e8675930 Changes to Anonymizer packages/imports + added repr to classes. (#763)
* modules changes, doc changes

* updates on notebook and e2e

* backward compatibility of OperatorConfig

* revert file structure

* revert customize_presidio_analyzer, doesn't belong in this PR

* update to spark notebook

* updates to notebook

* Update getting_entity_values.ipynb

* Update getting_entity_values.ipynb

* grouped imports

* Update test_context_support.py
2021-09-26 14:49:48 +03:00

55 lines
1.5 KiB
Python

from presidio_analyzer import AnalyzerEngine
from presidio_anonymizer import AnonymizerEngine
from presidio_anonymizer.entities import OperatorConfig
from faker import Faker
from faker.providers import internet
def reverse_string(x):
return x[::-1]
def anonymize_reverse_lambda(analyzer_results, text_to_anonymize):
anonymized_results = anonymizer.anonymize(
text=text_to_anonymize,
analyzer_results=analyzer_results,
operators={
"EMAIL_ADDRESS": OperatorConfig("custom", {"lambda": lambda x: x[::-1]})
},
)
return anonymized_results
def anonymize_faker_lambda(analyzer_results, text_to_anonymize):
anonymized_results = anonymizer.anonymize(
text=text_to_anonymize,
analyzer_results=analyzer_results,
operators={
"EMAIL_ADDRESS": OperatorConfig(
"custom", {"lambda": lambda x: fake.safe_email()}
)
},
)
return anonymized_results
fake = Faker("en_US")
fake.add_provider(internet)
analyzer = AnalyzerEngine()
anonymizer = AnonymizerEngine()
text = (
"The user has the following two emails: email1@contoso.com and email2@contoso.com"
)
analyzer_results = analyzer.analyze(
text=text, entities=["EMAIL_ADDRESS"], language="en"
)
print(f"Original Text: {text}")
print(f"Analyzer result: {analyzer_results}\n")
print(f"Reverse lambda result: {anonymize_reverse_lambda(analyzer_results, text).text}")
print(f"Faker lambda result: {anonymize_faker_lambda(analyzer_results, text).text}")