Skip to content

Anonymize Module

anonipy.anonymize

Module containing the anonymization modules and utility.

The anonymize module provides a set of anonymization modules and utility, including extractors, generators, and strategies. In addition, it provides methods for anonymizing text based on a list of replacements, as well as a pipeline class for automating the anonymization process.

Modules:

Name Description
extractors

The module containing the extractor classes.

generators

The module containing the generator classes.

strategies

The module containing the strategy classes.

pipeline

The module containing the pipeline class.

Functions:

Name Description
anonymize

Anonymize the text based on the replacements.

Functions

anonipy.anonymize.anonymize(text, replacements)

Anonymize a text based on a list of replacements.

Examples:

>>> from anonipy.anonymize import anonymize
>>> anonymize(text, replacements)

Parameters:

Name Type Description Default
text str

The text to anonymize.

required
replacements List[Replacement]

The list of replacements to apply.

required

Returns:

Type Description
str

The anonymized text.

Source code in anonipy/anonymize/helpers.py
def anonymize(text: str, replacements: List[Replacement]) -> str:
    """Anonymize a text based on a list of replacements.

    Examples:
        >>> from anonipy.anonymize import anonymize
        >>> anonymize(text, replacements)

    Args:
        text: The text to anonymize.
        replacements: The list of replacements to apply.

    Returns:
        The anonymized text.

    """

    s_replacements = sorted(replacements, key=lambda x: x["start_index"], reverse=True)

    anonymized_text = text
    for replacement in s_replacements:
        anonymized_text = (
            anonymized_text[: replacement["start_index"]]
            + replacement["anonymized_text"]
            + anonymized_text[replacement["end_index"] :]
        )
    return anonymized_text, s_replacements[::-1]