PatternPairAggregator
Text aggregator that identifies and processes content between pattern pairs in streaming text
Overview
PatternPairAggregator
is a specialized text aggregator that buffers streaming text until it can identify complete pattern pairs (like XML tags, markdown formatting, or custom delimiters). It processes the content between these patterns using registered handlers and returns text at sentence boundaries (therefore allowing normal TTS processing to occur).
This aggregator is particularly useful for applications like voice switching, structured content processing, and extracting metadata from LLM outputs, ensuring that patterns spanning multiple text chunks are correctly identified.
Want to see it in action? Check out the voice switching demo.
Constructor
No parameters are required for initialization. The aggregator starts with an empty buffer and no registered patterns.
Methods
add_pattern_pair
Registers a new pattern pair to detect in the text.
Unique identifier for this pattern pair
Pattern that marks the beginning of content
Pattern that marks the end of content
Whether to remove the matched patterns from the output text
on_pattern_match
Registers a handler function to be called when a specific pattern pair is matched.
ID of the pattern pair to match
Function to call when the pattern is matched. The function should accept a PatternMatch object.
Pattern Match Object
When a pattern is matched, the handler function receives a PatternMatch
object with these attributes:
The identifier of the matched pattern pair
The complete text including start and end patterns
The text content between the start and end patterns
Usage Examples
Voice Switching in TTS
Extracting Structured Data from LLM Outputs
Concept Explanation with Multiple Patterns
How It Works
Notes
- Patterns are processed in the order they appear in the text
- Handlers are called when complete patterns are found
- Patterns can span multiple sentences of text, but be aware that encoding many “reasoning” tokens may slow down the LLM response