markup_utils
Shared XML/SSML markup utilities for text matching and cursor advancement.
Two notions of “what is markup” live here, and they differ on a single point –
a '<' with no later '>':
strip_markup()treats it as an open tag and swallows the rest of the string. Correct for a fragment that may have been cut mid-tag, such as a single token from a TTS word-timestamp stream.strip_complete_markup()treats it as content. Correct for a complete text, where a lone'<'is real ("5 < 10","<3").
Every markup decision in the word-timestamp path routes through one of the two, so the callers can’t disagree about which characters are a tag.
- pipecat.utils.text.markup_utils.strip_markup(text: str) str[source]
Remove XML/SSML-like markup from a possibly-truncated text fragment.
Syntax-based, not tag-name based: treats anything between ‘<’ and ‘>’ as markup and preserves text outside it. An unclosed ‘<’ swallows the rest of text, matching how a raw word-timestamp token can arrive mid-tag (see
_iter_clean_chars()).For a complete text, use
strip_complete_markup()instead.Used by
TextSegmentMapto match a word against a segment, where the incoming word may be a fragment of a still-open tag.
- pipecat.utils.text.markup_utils.strip_complete_markup(text: str) str[source]
Remove well-formed ‘<…>’ markup from a complete, static text.
Unlike
strip_markup(), only strips matched ‘<…>’ pairs – a lone ‘<’ with no later ‘>’ is left in place as real content rather than swallowing the rest of text, since there is no streamed fragment here that could be mid-tag.Used by
is_transformed, byalnum_only(), and byWordCompletionTrackerto defaultuser_facing_textto a tag-free string.
- pipecat.utils.text.markup_utils.raw_offset_after_clean_chars(text: str, n: int) int[source]
Return the raw offset into text just past its n-th markup-stripped char.
Inverse of
strip_markup()for a prefix: wherestrip_markupcollects every non-markup char, this finds the raw index one past the n-th of them – converting a match measured in markup-stripped space back to a raw offset. Returnslen(text)when text has fewer than n non-markup chars.
- pipecat.utils.text.markup_utils.split_markup_runs(text: str) list[str][source]
Split text into alternating runs of tagged and untagged words.
A word is considered tagged if it overlaps a complete
'<...>'pair. A lone'<'is treated as content, not as the start of a tag (seestrip_complete_markup()). Consecutive words with the same classification form a single run, so whitespace inside a tag such as<phoneme alphabet="ipa">never splits the words it spans across runs.Example:
split_markup_runs("I love to count <spell>1234</spell>.") # -> ["I love to count ", "<spell>1234</spell>."]
Text with no markup yields a single run, unchanged.
Used by
TextSegmentMapwhen it builds its segments, to give a tag one of its own.