Replace hand-rolled sentence splitter with LangChain's RecursiveCharacterTextSplitter#244
Merged
Merged
Conversation
Replace the hand-rolled sentence scanner + greedy packer in tts/text.py with LangChain's RecursiveCharacterTextSplitter, configured with sentence-terminator separators so chunks stay sentence-aligned and within the per-frame char budget. Behavior is preserved across the existing cases (sentence packing, mid-number periods, oversized-blob slicing, lossless rejoin); the split_sentences helper is dropped since chunk_text was its only caller. Adds the langchain-text-splitters dependency. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01He2iJSWkEB5U3ZhxyAFnRc
cd9749a to
7ba1839
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaced the custom sentence-splitting and chunking logic in
aai_cli/tts/text.pywith LangChain'sRecursiveCharacterTextSplitter, simplifying the implementation while maintaining the same behavior and guarantees.Changes
split_sentences()and_bounded()functions that manually parsed sentence terminators and sliced oversized text.[". ", "! ", "? ", "\n\n", "\n", " ", ""]) that prefers sentence boundaries and falls back to word/character splits only when necessary, withkeep_separator="end"to preserve punctuation for prosody.chunk_text(): Reduced from ~30 lines of greedy packing logic to a 6-line wrapper around the splitter.split_sentences()function; kept and refined tests forchunk_text()to verify the same invariants (no mid-sentence breaks unless oversized, no text loss, budget compliance).langchain-text-splitters>=1.0.0topyproject.toml.Implementation Details
The new approach delegates the complexity of recursive splitting to a battle-tested library rather than maintaining custom parsing logic. The separator list ensures:
The
keep_separator="end"parameter keeps terminators with the preceding chunk, preserving the punctuation needed for the TTS model's prosody. All existing test cases pass with the new implementation, confirming behavioral equivalence.https://claude.ai/code/session_01He2iJSWkEB5U3ZhxyAFnRc