A TTS that fits in your CPU (and pocket)
Python
Check decimal periods without decoding the whole text (#399)
quetion66744cb63eaa414
diff --git a/pocket_tts/models/text_chunking.py b/pocket_tts/models/text_chunking.pyindex cf1b903..51f08d6 100644--- a/pocket_tts/models/text_chunking.py+++ b/pocket_tts/models/text_chunking.py@@ -82,15 +82,21 @@ def _ensure_terminal_punctuation(text: str) -> str:def _is_decimal_period_boundary(list_of_tokens: list[int], segment_start_idx: int, tokenizer: Tokenizer) -> bool:- """Return True when segment_start_idx begins right after a decimal period."""- prefix = tokenizer.decode(list_of_tokens[:segment_start_idx])- suffix = tokenizer.decode(list_of_tokens[segment_start_idx:])+ """Return True when segment_start_idx begins right after a decimal period, as in "3.14".++ Only the tokens next to the boundary are decoded, so checking every sentence boundary+ stays linear in the text length. Decoding the period together with the next token keeps+ the space between them: in "I counted to 3. 14 people were there." the period ends a+ sentence.+ """+ before = tokenizer.decode(list_of_tokens[max(0, segment_start_idx - 2) : segment_start_idx])+ after = tokenizer.decode(list_of_tokens[segment_start_idx - 1 : segment_start_idx + 1])return (- len(prefix) >= 2- and prefix[-1] == "."- and prefix[-2].isdigit()- and bool(suffix)- and suffix[0].isdigit()+ len(before) >= 2+ and before[-1] == "."+ and before[-2].isdigit()+ and len(after) >= 2+ and after[1].isdigit())diff --git a/tests/test_split_sentences.py b/tests/test_split_sentences.pyindex 7ee5286..3e5dc83 100644--- a/tests/test_split_sentences.py+++ b/tests/test_split_sentences.py@@ -195,6 +195,36 @@ def test_sentence_period_after_decimal_still_splits(tokenizer: Tokenizer):assert "2. 0" not in chunks[0]+def test_sentence_ending_in_number_before_number_still_splits(tokenizer: Tokenizer):+ """A period followed by a space is a sentence end, even between two numbers."""+ text = "I counted to 3. 14 people were there."+ chunks = split_into_best_sentences(+ tokenizer, text, 10, pad_with_spaces_for_short_inputs=False, remove_semicolons=False+ )+ assert chunks == ["I counted to 3.", "14 people were there."]+++def test_decimal_check_decodes_only_tokens_near_each_period(+ tokenizer: Tokenizer, monkeypatch: pytest.MonkeyPatch+):+ """Checking each period for a decimal must not decode the whole text around it,+ which made splitting quadratic in the text length."""+ text = "Pi is 3.14 and the sky is blue. " * 200+ decoded_lengths = []+ decode = tokenizer.decode++ def counting_decode(tokens: list[int]) -> str:+ decoded_lengths.append(len(tokens))+ return decode(tokens)++ monkeypatch.setattr(tokenizer, "decode", counting_decode)+ chunks = split_into_best_sentences(+ tokenizer, text, 50, pad_with_spaces_for_short_inputs=False, remove_semicolons=False+ )+ assert "3.14" in chunks[0]+ assert sum(decoded_lengths) < 3 * len(tokenizer(text)[0])++def test_oversized_clause_without_commas_still_returns(tokenizer: Tokenizer):"""A long clause with no split points should still be returned (not dropped)."""# 20 words with no punctuation at all - no way to split