Skip to content

A TTS that fits in your CPU (and pocket)

Python

Always end the prompt with sentence-final punctuation (#296)

manu-lm6df75699d69a
diff --git a/pocket_tts/models/text_chunking.py b/pocket_tts/models/text_chunking.py
index 66b3cf5..cae9826 100644
--- a/pocket_tts/models/text_chunking.py
+++ b/pocket_tts/models/text_chunking.py
@@ -33,10 +33,8 @@ def prepare_text_prompt(
if not text[0].isupper():
text = text[0].upper() + text[1:]
- # Let's make sure it ends with some kind of punctuation
- # If it ends with a letter or digit, we add a period.
- if append_terminal_punctuation and text[-1].isalnum():
- text = text + "."
+ if append_terminal_punctuation:
+ text = _ensure_terminal_punctuation(text)
# The model does not perform well when there are very few tokens, so
# we can add empty spaces at the beginning to increase the token count.
@@ -46,6 +44,32 @@ def prepare_text_prompt(
return text, frames_after_eos_guess
+_TERMINAL_PUNCTUATION = ".!?\u2026"
+_WEAK_PUNCTUATION = ",;:-\u2013\u2014"
+_CLOSERS = "\"'\u201d\u2019)]\u00bb"
+
+
+def _ensure_terminal_punctuation(text: str) -> str:
+ """Make sure the prompt ends with sentence-final punctuation.
+
+ The model is trained on sentences that end with a period, a question mark
+ or an exclamation mark. Without one, the last word is often mispronounced
+ or repeated. Text that already ends with one, possibly followed by closing
+ quotes or brackets, is left alone. A trailing comma, colon or dash is
+ replaced by a period. Anything else gets a period appended after the
+ closing quote or bracket: the sentence splitter only recognizes a bare
+ period token, and a quote left alone after a period would become a chunk
+ of its own.
+ """
+ core = text.rstrip(_CLOSERS + " ")
+ closers = text[len(core) :].strip()
+ if not core or core[-1] in _TERMINAL_PUNCTUATION:
+ return text
+ if core[-1] in _WEAK_PUNCTUATION:
+ return core.rstrip(_WEAK_PUNCTUATION + " ") + "." + closers
+ return text + "."
+
+
def _is_decimal_period_boundary(
list_of_tokens: list[int], segment_start_idx: int, tokenizer: SentencePieceTokenizer
) -> bool:
diff --git a/tests/test_split_sentences.py b/tests/test_split_sentences.py
index a9ee0b2..f0cf805 100644
--- a/tests/test_split_sentences.py
+++ b/tests/test_split_sentences.py
@@ -206,3 +206,29 @@ def test_oversized_clause_without_commas_still_returns(tokenizer: SentencePieceT
# prepare_text_prompt capitalizes the first char and adds a trailing period,
# so compare case-insensitively and strip punctuation
assert chunks[0].lower().rstrip(".") == text.lower()
+
+
+ "text, expected",
+ [
+ ("hello world", "Hello world."),
+ ("it costs 42", "It costs 42."),
+ ("hello world!", "Hello world!"),
+ ("wait for it...", "Wait for it..."),
+ ("hello world,", "Hello world."),
+ ("hello world:", "Hello world."),
+ ("hello world -", "Hello world."),
+ ('he said "go home"', 'He said "go home".'),
+ ("she whispered 'run'", "She whispered 'run'."),
+ ('he said "go home."', 'He said "go home."'),
+ ('he said "go home,"', 'He said "go home."'),
+ ("is it (really)?", "Is it (really)?"),
+ ("see the note (below)", "See the note (below)."),
+ ("up by 50%", "Up by 50%."),
+ ],
+)
+def test_terminal_punctuation_is_added_when_missing(text: str, expected: str):
+ got, _ = prepare_text_prompt(
+ text, pad_with_spaces_for_short_inputs=False, remove_semicolons=False
+ )
+ assert got == expected