Files
presidio/docs/samples/python/getting_entity_values.ipynb
2026-06-28 10:27:33 +03:00

220 lines
5.6 KiB
Plaintext

{
"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/data-privacy-stack/presidio/blob/main/docs/samples/python/getting_entity_values.ipynb](https://www.github.com/data-privacy-stack/presidio/blob/main/docs/samples/python/getting_entity_values.ipynb)"
]
},
{
"cell_type": "markdown",
"id": "adjusted-jurisdiction",
"metadata": {},
"source": [
"# Getting a list of all identified texts\n",
"\n",
"This sample illustrates how to get a list of all the identified PII entities using Presidio Analyzer for detection and a custom Presidio Anonymizer operator."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "noted-lounge",
"metadata": {},
"outputs": [],
"source": [
"from presidio_analyzer import AnalyzerEngine\n",
"from presidio_anonymizer import AnonymizerEngine\n",
"from presidio_anonymizer.entities import OperatorConfig"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "clinical-surface",
"metadata": {},
"outputs": [],
"source": [
"analyzer = AnalyzerEngine()\n",
"anonymizer = AnonymizerEngine()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "referenced-argument",
"metadata": {},
"outputs": [],
"source": [
"text_to_analyze = \"Hi my name is Charles Darwin and my email is cdarwin@hmsbeagle.org\"\n",
"analyzer_results = analyzer.analyze(text_to_analyze, language=\"en\")\n"
]
},
{
"cell_type": "markdown",
"id": "innovative-audio",
"metadata": {},
"source": [
"A naive approach for getting the text values:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "congressional-wiring",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('cdarwin@hmsbeagle.org', 45, 66),\n",
" ('Charles Darwin', 14, 28),\n",
" ('hmsbeagle.org', 53, 66)]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[(text_to_analyze[res.start:res.end], res.start, res.end) for res in analyzer_results]"
]
},
{
"cell_type": "markdown",
"id": "informal-evanescence",
"metadata": {},
"source": [
"Another option is to set up a custom operator* which runs an identity function (`lambda x: x`). This operator doesn't really anonymize, but replaces the identified value with itself. This is useful as the Anonymizer handles the overlaps automatically. \n",
"\n",
"> In this example, the URL (hmsbeagle.org) is contained in the email address, so it's ommitted from the final result.\n",
"\n",
"\\* an `Operator` is usually either an `Anonymizer` or `Deanonymizer` on the presidio-anonymizer library/"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "growing-motivation",
"metadata": {},
"outputs": [],
"source": [
"anonymized_results = anonymizer.anonymize(\n",
" text=text_to_analyze,\n",
" analyzer_results=analyzer_results, \n",
" operators={\"DEFAULT\": OperatorConfig(\"custom\", {\"lambda\": lambda x: x})} \n",
" )"
]
},
{
"cell_type": "markdown",
"id": "hydraulic-association",
"metadata": {},
"source": [
"The operator defined here is `DEFAULT`, meaning it will be used for all entities. The `OperatorConfig` is a custom one and the labmda is the identity function."
]
},
{
"cell_type": "markdown",
"id": "according-rates",
"metadata": {},
"source": [
"Output text, start and end locations for each detected entity"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "serial-arcade",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('cdarwin@hmsbeagle.org', 45, 66), ('Charles Darwin', 14, 28)]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[(item.text, item.start, item.end) for item in anonymized_results.items]"
]
},
{
"cell_type": "markdown",
"id": "37e64444",
"metadata": {},
"source": [
"A third option would be to use the `keep` operator:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "3833bc43",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('cdarwin@hmsbeagle.org', 45, 66), ('Charles Darwin', 14, 28)]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anonymized_results_with_keep = anonymizer.anonymize(\n",
" text=text_to_analyze,\n",
" analyzer_results=analyzer_results, \n",
" operators={\"DEFAULT\": OperatorConfig(\"keep\")} \n",
" )\n",
"[(item.text, item.start, item.end) for item in anonymized_results_with_keep.items]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "presidio",
"language": "python",
"name": "presidio"
},
"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.9.16"
}
},
"nbformat": 4,
"nbformat_minor": 5
}