{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "bcddce7b", "metadata": { "scrolled": true }, "outputs": [], "source": [ "# download presidio\n", "!pip install presidio_analyzer presidio_anonymizer\n", "!python -m spacy download en_core_web_lg" ] }, { "cell_type": "markdown", "id": "3345f1c4", "metadata": {}, "source": [ "###### Path to notebook: [https://www.github.com/microsoft/presidio/blob/main/docs/samples/python/keep_entities.ipynb](https://www.github.com/microsoft/presidio/blob/main/docs/samples/python/keep_entities.ipynb)" ] }, { "cell_type": "markdown", "id": "gothic-trademark", "metadata": {}, "source": [ "# Keeping some PIIs from being anonymized\n", "\n", "This sample shows how to use Presidio's `keep` anonymizer to keep some of the identified PIIs in the output string" ] }, { "cell_type": "markdown", "id": "roman-allergy", "metadata": {}, "source": [ "### Set up imports" ] }, { "cell_type": "code", "execution_count": 1, "id": "extensive-greensboro", "metadata": {}, "outputs": [], "source": [ "from presidio_anonymizer import AnonymizerEngine\n", "from presidio_anonymizer.entities import RecognizerResult, OperatorConfig" ] }, { "cell_type": "markdown", "id": "metropolitan-atlantic", "metadata": {}, "source": [ "### Presidio Anonymizer: Keep person names\n", "\n", "This example input has 2 PIIs, an person name and a location. We configure the anonymizer to replace the location name with a placeholder, but keep the person name unmodified." ] }, { "cell_type": "code", "execution_count": 2, "id": "medium-ridge", "metadata": {}, "outputs": [], "source": [ "engine = AnonymizerEngine()\n", "\n", "# Invoke the anonymize function with the text,\n", "# analyzer results (potentially coming from presidio-analyzer)\n", "# and 'keep' operator on PIIs\n", "anonymize_result = engine.anonymize(\n", " text=\"My name is James Bond, I live in London\",\n", " analyzer_results=[\n", " RecognizerResult(entity_type=\"PERSON\", start=11, end=21, score=0.8),\n", " RecognizerResult(entity_type=\"LOCATION\", start=33, end=39, score=0.8),\n", " ],\n", " operators={\n", " \"PERSON\": OperatorConfig(\"keep\"),\n", " \"DEFAULT\": OperatorConfig(\"replace\"),\n", " },\n", ")" ] }, { "cell_type": "markdown", "id": "1d2cabaa-4aa6-49cf-875d-4bdf407215b4", "metadata": {}, "source": [ "### Result: Name unmodified, but tracked\n", "\n", "The person name is preserved in the result text, but remains tracked in the items list." ] }, { "cell_type": "code", "execution_count": 3, "id": "421c2914-9b75-4c33-a270-e410d91d036b", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "text: My name is James Bond, I live in \n", "items:\n", "[\n", " {'start': 33, 'end': 43, 'entity_type': 'LOCATION', 'text': '', 'operator': 'replace'},\n", " {'start': 11, 'end': 21, 'entity_type': 'PERSON', 'text': 'James Bond', 'operator': 'keep'}\n", "]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "anonymize_result" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.10" } }, "nbformat": 4, "nbformat_minor": 5 }