Skip to content

symfonic.core.utils.compression

compression

Deterministic local text compression for memory context.

Reduces verbose memory strings to high-density tokens without any API calls. Uses a fixed abbreviation dictionary + whitespace collapsing.

compress_memory

compress_memory(text: str) -> str

Compress memory text by abbreviating common words and collapsing whitespace.

Deterministic and local -- zero API cost. Safe for pre-LLM context.

Parameters:

Name Type Description Default
text str

Input text to compress.

required

Returns:

Type Description
str

Compressed text with abbreviated common terms and collapsed whitespace.

Source code in src/symfonic/core/utils/compression.py
def compress_memory(text: str) -> str:
    """Compress memory text by abbreviating common words and collapsing whitespace.

    Deterministic and local -- zero API cost. Safe for pre-LLM context.

    Args:
        text: Input text to compress.

    Returns:
        Compressed text with abbreviated common terms and collapsed whitespace.
    """
    if not text:
        return text
    result = text
    for pattern, short in _COMPILED_FORWARD:
        result = pattern.sub(short, result)
    result = _WHITESPACE_RE.sub(" ", result).strip()
    return result

decompress_memory

decompress_memory(text: str) -> str

Reverse compression for logging and debugging. Not used in hot path.

Parameters:

Name Type Description Default
text str

Previously compressed text.

required

Returns:

Type Description
str

Text with abbreviations expanded back to full words.

Source code in src/symfonic/core/utils/compression.py
def decompress_memory(text: str) -> str:
    """Reverse compression for logging and debugging. Not used in hot path.

    Args:
        text: Previously compressed text.

    Returns:
        Text with abbreviations expanded back to full words.
    """
    if not text:
        return text
    result = text
    for pattern, full in _COMPILED_REVERSE:
        result = pattern.sub(full, result)
    return result