sanitize_for_tts (src/text_utils.py)

robot-bridge · src/text_utils.py · python · MIT
voice

Cleans text by stripping markdown, emojis, and special characters to ensure smooth text-to-speech synthesis from LLM outputs.

# Source: https://github.com/waynecc-at/robot-bridge | License: MIT | Copyright 2024 Robot Bridge Contributors
# Provided via justclaw.it.com; keep this notice with the code.
"""Text sanitization for TTS — strip markdown, emoji, and special characters."""

import re


# Unicode ranges for emojis and symbols
_EMOJI_PATTERN = re.compile(
    "["
    "\U0001F600-\U0010FFFF"  # Emoticons, symbols, supplemental
    "☀-➿"           # Misc symbols, dingbats
    "⌀-⏿"           # Misc technical (including ⏰ etc.)
    "─-╿"           # Box drawing
    "⬀-⯿"           # Misc symbols and arrows
    "✀-➿"           # Dingbats
    "︀-️"           # Variation selectors
    "‍"                  # Zero-width joiner
    "]"
)

# Markdown patterns to strip
_MARKDOWN_PATTERNS = [
    re.compile(r"```[\s\S]*?

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key