A TTS that fits in your CPU (and pocket)
Python
Never end a text chunk on a decimal number (#409)
manu-lm4cc5753319ce
diff --git a/pocket_tts/models/text_chunking.py b/pocket_tts/models/text_chunking.pyindex 51f08d6..8f25410 100644--- a/pocket_tts/models/text_chunking.py+++ b/pocket_tts/models/text_chunking.py@@ -54,6 +54,8 @@ def prepare_text_prompt(_TERMINAL_PUNCTUATION = ".!?\u2026"+# A segment ending on a decimal number, as in "... a fee of $822.95," or "... is 3.14."+_ENDS_WITH_DECIMAL_NUMBER = re.compile(r"\d\.\d+\W*$")_WEAK_PUNCTUATION = ",;:-\u2013\u2014"_CLOSERS = "\"'\u201d\u2019)]\u00bb"@@ -191,6 +193,18 @@ def split_into_best_sentences(else:refined_segments.append((nb_tokens, text))+ # Never end a chunk on a decimal number: "... $822.95," would become the chunk "... $822.95."+ # and the model takes the decimal point for the sentence end, stopping before the decimals.+ # Such a segment stays with the next one.+ glued_segments: list[tuple[int, str]] = []+ for nb_tokens, text in refined_segments:+ if glued_segments and _ENDS_WITH_DECIMAL_NUMBER.search(glued_segments[-1][1]):+ prev_tokens, prev_text = glued_segments[-1]+ glued_segments[-1] = (prev_tokens + nb_tokens, prev_text.rstrip() + " " + text.strip())+ else:+ glued_segments.append((nb_tokens, text))+ refined_segments = glued_segments+max_nb_tokens_in_a_chunk = max_tokenschunks = []current_chunk = ""diff --git a/tests/test_split_sentences.py b/tests/test_split_sentences.pyindex 3e5dc83..28e9207 100644--- a/tests/test_split_sentences.py+++ b/tests/test_split_sentences.py@@ -308,3 +308,69 @@ def test_replace_characters_rewrites_unseen_characters_before_capitalizing():# Empty by default: shipped configs that don't set it are unchanged.got, _ = prepare_text_prompt('"Yes," she said.', False, False)assert got == '"Yes," she said.'+++def _split(tokenizer: Tokenizer, text: str, max_tokens: int) -> list[str]:+ return split_into_best_sentences(+ tokenizer, text, max_tokens, pad_with_spaces_for_short_inputs=False, remove_semicolons=False+ )+++REIMBURSEMENT = (+ "The decision will be processed by the S C H E L L I N G department and the result "+ "will be a reimbursement of $822.95, which should be reflected in your account within "+ "a few business days."+)+SERVICE_FEE = (+ "Sure, I can walk you through the procedure, the solution we typically offer includes "+ "an onsite technician visit, and the standard service fee is $286.50, our team "+ "coordinates with the IRM and CPAM divisions to ensure compliance."+)+++ "text, max_tokens, number",+ [+ (REIMBURSEMENT, 50, "$822.95,"),+ (SERVICE_FEE, 50, "$286.50,"),+ (+ "The value is 3.14. The next value is larger. And the third one is the largest.",+ 10,+ "3.14.",+ ),+ ],+)+def test_no_chunk_ends_on_a_decimal_number(+ tokenizer: Tokenizer, text: str, max_tokens: int, number: str+):+ """A chunk ending on "$822.95," would get a terminal period appended, and the model then takes+ the decimal point for the sentence end and drops the decimals: keep it with what follows."""+ chunks = _split(tokenizer, text, max_tokens)+ assert any(number in chunk and not chunk.endswith(number) for chunk in chunks), chunks+ assert " ".join(chunks).split() == text.split()+++def test_splits_without_decimal_numbers_are_unchanged(tokenizer: Tokenizer):+ assert _split(tokenizer, "First sentence here. Second sentence here. Third one.", 10) == [+ "First sentence here.",+ "Second sentence here.",+ "Third one.",+ ]+ text = (+ "It was the best of times, it was the worst of times, it was the age of wisdom, it was the "+ "age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was "+ "the season of Light, it was the season of Darkness"+ )+ assert _split(tokenizer, text, 20) == [+ "It was the best of times, it was the worst of times,",+ "it was the age of wisdom, it was the age of foolishness,",+ "it was the epoch of belief,",+ "it was the epoch of incredulity,",+ "it was the season of Light, it was the season of Darkness.",+ ]+ # Integers and sentence-final years are not decimal numbers.+ assert _split(tokenizer, "We met in 2008. It was raining. The bus was late.", 8) == [+ "We met in 2008.",+ "It was raining.",+ "The bus was late.",+ ]