mirror of
https://github.com/data-privacy-stack/presidio.git
synced 2026-09-30 18:08:36 -05:00
5.6 KiB
5.6 KiB
In [ ]:
# download presidio
!pip install presidio_analyzer presidio_anonymizer
!python -m spacy download en_core_web_lgIn [1]:
from presidio_analyzer import AnalyzerEngine
from presidio_anonymizer import AnonymizerEngine
from presidio_anonymizer.entities import OperatorConfigIn [2]:
analyzer = AnalyzerEngine()
anonymizer = AnonymizerEngine()In [3]:
text_to_analyze = "Hi my name is Charles Darwin and my email is cdarwin@hmsbeagle.org"
analyzer_results = analyzer.analyze(text_to_analyze, language="en")
In [4]:
[(text_to_analyze[res.start:res.end], res.start, res.end) for res in analyzer_results]Out [4]:
[('cdarwin@hmsbeagle.org', 45, 66),
('Charles Darwin', 14, 28),
('hmsbeagle.org', 53, 66)]In [7]:
anonymized_results = anonymizer.anonymize(
text=text_to_analyze,
analyzer_results=analyzer_results,
operators={"DEFAULT": OperatorConfig("custom", {"lambda": lambda x: x})}
)In [8]:
[(item.text, item.start, item.end) for item in anonymized_results.items]Out [8]:
[('cdarwin@hmsbeagle.org', 45, 66), ('Charles Darwin', 14, 28)]In [9]:
anonymized_results_with_keep = anonymizer.anonymize(
text=text_to_analyze,
analyzer_results=analyzer_results,
operators={"DEFAULT": OperatorConfig("keep")}
)
[(item.text, item.start, item.end) for item in anonymized_results_with_keep.items]Out [9]:
[('cdarwin@hmsbeagle.org', 45, 66), ('Charles Darwin', 14, 28)]