Skip to content

deconfuse()

slugany.deconfuse

deconfuse(text: str) -> str

Replace confusable Unicode characters with their Latin equivalents.

Converts Cyrillic homoglyphs and Greek letters that visually resemble Latin characters to their ASCII equivalents.

Parameters:

Name Type Description Default
text str

The text to deconfuse.

required

Returns:

Type Description
str

The text with confusable characters replaced.

Raises:

Type Description
TypeError

If text is not a string.

Examples:

>>> deconfuse("саfe")
'cafe'
>>> deconfuse("αβγ")
'abg'
Source code in slugany/_slugify.py
def deconfuse(text: str) -> str:
    """Replace confusable Unicode characters with their Latin equivalents.

    Converts Cyrillic homoglyphs and Greek letters that visually resemble
    Latin characters to their ASCII equivalents.

    Args:
        text: The text to deconfuse.

    Returns:
        The text with confusable characters replaced.

    Raises:
        TypeError: If ``text`` is not a string.

    Examples:
        >>> deconfuse("саfe")
        'cafe'
        >>> deconfuse("αβγ")
        'abg'
    """
    if not isinstance(text, str):
        msg = f"text must be a string, got {type(text).__name__}"
        raise TypeError(msg)
    from slugany._tables import _CONFUSABLES

    return text.translate(_CONFUSABLES)