mirror of
https://github.com/data-privacy-stack/presidio.git
synced 2026-10-01 18:37:56 -05:00
8.1 KiB
8.1 KiB
In [ ]:
# download presidio
!pip install presidio_analyzer presidio_anonymizer
!python -m spacy download en_core_web_lgIn [ ]:
from presidio_analyzer import AnalyzerEngine, PatternRecognizer
from presidio_anonymizer import AnonymizerEngine
from presidio_anonymizer.entities import OperatorConfig
import json
from pprint import pprintIn [ ]:
text_to_anonymize = "His name is Mr. Jones and his phone number is 212-555-5555"In [ ]:
analyzer = AnalyzerEngine()
analyzer_results = analyzer.analyze(text=text_to_anonymize, entities=["PHONE_NUMBER"], language='en')
print(analyzer_results)In [ ]:
titles_recognizer = PatternRecognizer(supported_entity="TITLE",
deny_list=["Mr.","Mrs.","Miss"])
pronoun_recognizer = PatternRecognizer(supported_entity="PRONOUN",
deny_list=["he", "He", "his", "His", "she", "She", "hers", "Hers"])
analyzer.registry.add_recognizer(titles_recognizer)
analyzer.registry.add_recognizer(pronoun_recognizer)
analyzer_results = analyzer.analyze(text=text_to_anonymize,
entities=["TITLE", "PRONOUN"],
language="en")
print(analyzer_results)
In [ ]:
analyzer_results = analyzer.analyze(text=text_to_anonymize, language='en')
analyzer_resultsIn [ ]:
anonymizer = AnonymizerEngine()
anonymized_results = anonymizer.anonymize(
text=text_to_anonymize,
analyzer_results=analyzer_results,
operators={"DEFAULT": OperatorConfig("replace", {"new_value": "<ANONYMIZED>"}),
"PHONE_NUMBER": OperatorConfig("mask", {"type": "mask", "masking_char" : "*", "chars_to_mask" : 12, "from_end" : True}),
"TITLE": OperatorConfig("redact", {})}
)
print(f"text: {anonymized_results.text}")
print("detailed response:")
pprint(json.loads(anonymized_results.to_json()))In [ ]: