Skip to main content

Deidentification

A Python module that removes personally identifiable information (PII) from text documents, focusing on personal names and gender-specific pronouns. This tool uses spaCy's Named Entity Recognition (NER) capabilities combined with custom pronoun handling to provide thorough text de-identification.

Key Features

  • Accurately identifies and replaces personal names using spaCy's NER
  • Handles gender-specific pronouns with customizable replacements
  • Supports both plain text and HTML output formats
  • Uses an optimized backward-processing strategy for accurate text replacements
  • Iterative processing ensures comprehensive PII removal
  • Configurable replacement tokens and debug output
  • GPU acceleration support through spaCy

Installation

pip install text-deidentification

# or...

pip install git+https://github.com/jftuga/deidentification.git

Requirements

  • Python 3.10 or higher
  • spaCy's en_core_web_trf model (or another compatible model)

Download the required spaCy model:

python -m spacy download en_core_web_trf

Usage

Command Line Interface

The package includes a command-line tool for quick de-identification of text files:

deidentify input_file [options]
# or:
python -m deidentification.deidentify input_file [options]

Options:

  • -r, --replacement TEXT: Specify replacement text for identified names (default: "PERSON")
  • -o, --output FILE: Output file (defaults to stdout)
  • -H, --html: Output in HTML format with highlighted replacements
  • -d, --debug: Enable debug mode
  • -t, --tokens: Save identified elements to a JSON file (filename--tokens.json)
  • -x, --exclude EXCLUDE: comma-delimited list of entities to exclude from de-identification; or change with DEIDENTIFY_EXCLUDE_DELIM env var
  • -v, --version: Display version information

Example:

# De-identify a text file and save with HTML markup
deidentify input.txt -H -o output.html -r "[REDACTED]"

Python API Usage

from deidentification import Deidentification

# Create a deidentification instance with default settings
deidentifier = Deidentification()

# Process text
text = "John Smith went to the store. He bought some groceries."
deidentified_text = deidentifier.deidentify(text)
print(deidentified_text)
# Output: "PERSON went to the store. HE/SHE bought some groceries."

HTML Output

# Generate HTML output with highlighted replacements
html_output = deidentifier.deidentify_with_wrapped_html(text)

HTML Output Demo

deidentification html demo

Custom Configuration

from deidentification import (
    Deidentification,
    DeidentificationConfig,
    DeidentificationOutputStyle,
)

config = DeidentificationConfig(
    spacy_model="en_core_web_trf",
    output_style=DeidentificationOutputStyle.HTML,
    replacement="[REDACTED]",
    excluded_entities={"Joe Smith","Alice Jones"},
    debug=True
)
deidentifier = Deidentification(config)

Configuration Options

The DeidentificationConfig class supports the following options:

  • spacy_load (bool): Whether to load the spaCy model (default: True)
  • spacy_model (str): Name of the spaCy model to use (default: "en_core_web_trf")
  • output_style (DeidentificationOutputStyle): Output format - TEXT or HTML (default: TEXT)
  • replacement (str): Replacement text for identified names (default: "PERSON")
  • debug (bool): Enable debug output (default: False)

How It Works

The de-identification process follows these steps:

  1. Text is normalized for consistent processing
  2. spaCy processes the text to identify person entities
  3. Gender-specific pronouns are identified using a predefined list
  4. Entities and pronouns are sorted by their position in reverse order
  5. Replacements are made from end to beginning to maintain position accuracy
  6. The process repeats until no new entities are detected

The backward-processing strategy is key to accurate replacements, as it prevents position shifts from affecting subsequent replacements.

Debug Output

When debug mode is enabled, the tool provides detailed information about:

  • Identified person entities
  • Found pronouns
  • Replacement positions and actions
  • Processing iterations

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for text-deidentification 1.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for text-deidentification 1.3.2
File Size Uploaded
text_deidentification-1.3.2.tar.gz 16.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for text-deidentification 1.3.2
File Interpreter ABI Platform
text_deidentification-1.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 33.4 kB

Release files / text_deidentification-1.3.2.tar.gz

Download URL text_deidentification-1.3.2.tar.gz
Size 16.6 kB
Tags Source
SHA-256 checksum
How to use checksums
96832599c72364708aca1f8309c0f609c7889ac76aefb8d0d0e35f3bfd4dcd63
BLAKE2b-256 checksum
How to use checksums
de6d849b07161566d26616d260ae36abf1ad3ae8d678157765d20618ed858594
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8

Release files / text_deidentification-1.3.2-py3-none-any.whl

Download URL text_deidentification-1.3.2-py3-none-any.whl
Size 16.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1674a98aa274e55cdff3ee4281a049c94b18afc8b3c2d76acd1560c1621daeba
BLAKE2b-256 checksum
How to use checksums
7c29bd173071033b57baf96b08a3098d0f41d245cc61283d904bee8f0dad32b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8

Release history Release notifications | RSS feed

This release

1.3.2 This release

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page