Skip to content

A TTS that fits in your CPU (and pocket)

Python

Capitalize the first letter after opening quotes and brackets (#437)

Tolga Unal1773f648c845
diff --git a/pocket_tts/models/text_chunking.py b/pocket_tts/models/text_chunking.py
index 6a4b02a..222f957 100644
--- a/pocket_tts/models/text_chunking.py
+++ b/pocket_tts/models/text_chunking.py
@@ -37,10 +37,13 @@ def prepare_text_prompt(
else:
frames_after_eos_guess = 1
- # Make sure it starts with an uppercase letter. Only meaningful for
- # orthographies that have case; see Config.capitalize_first_letter.
- if capitalize_first_letter and not text[0].isupper():
- text = text[0].upper() + text[1:]
+ # Make sure it starts with an uppercase letter, after any opening quotes or brackets
+ # ('"hello," she said' -> '"Hello," she said'). Only meaningful for orthographies that
+ # have case; see Config.capitalize_first_letter.
+ if capitalize_first_letter:
+ start = len(text) - len(text.lstrip(_OPENERS))
+ if start < len(text) and not text[start].isupper():
+ text = text[:start] + text[start].upper() + text[start + 1 :]
if append_terminal_punctuation:
text = _ensure_terminal_punctuation(text)
@@ -59,6 +62,7 @@ _TERMINAL_PUNCTUATION = ".!?\u2026"
_ENDS_WITH_DECIMAL_NUMBER = re.compile(r"\d[.,]\d+\W*$")
_WEAK_PUNCTUATION = ",;:-\u2013\u2014"
_CLOSERS = "\"'\u201d\u2019)]\u00bb"
+_OPENERS = "\"'\u201c\u2018([\u00ab"
def _ensure_terminal_punctuation(text: str) -> str:
diff --git a/rust/src/text.rs b/rust/src/text.rs
index 27c5c4a..71902dc 100644
--- a/rust/src/text.rs
+++ b/rust/src/text.rs
@@ -106,13 +106,17 @@ pub fn prepare_text_prompt(text: &str, options: &TextOptions) -> Result<(String,
let number_of_words = text.split_whitespace().count();
let frames_after_eos_guess = if number_of_words <= 4 { 3 } else { 1 };
- // Make sure it starts with an uppercase letter. Only meaningful for orthographies that have
+ // Make sure it starts with an uppercase letter, after any opening quotes or brackets
+ // ('"hello," she said' -> '"Hello," she said'). Only meaningful for orthographies that have
// case; see the config's capitalize_first_letter.
if options.capitalize_first_letter {
- let mut chars = text.chars();
- let first = chars.next().unwrap();
- if !first.is_uppercase() {
- text = first.to_uppercase().chain(chars).collect();
+ let start = text.len() - text.trim_start_matches(|c| OPENERS.contains(c)).len();
+ let (openers, rest) = text.split_at(start);
+ let mut chars = rest.chars();
+ if let Some(first) = chars.next()
+ && !first.is_uppercase()
+ {
+ text = format!("{openers}{}{}", first.to_uppercase(), chars.as_str());
}
}
@@ -132,6 +136,7 @@ pub fn prepare_text_prompt(text: &str, options: &TextOptions) -> Result<(String,
const TERMINAL_PUNCTUATION: &str = ".!?\u{2026}";
const WEAK_PUNCTUATION: &str = ",;:-\u{2013}\u{2014}";
const CLOSERS: &str = "\"'\u{201d}\u{2019})]\u{bb}";
+const OPENERS: &str = "\"'\u{201c}\u{2018}([\u{ab}";
/// Make sure the prompt ends with sentence-final punctuation.
///
@@ -419,6 +424,17 @@ mod tests {
"Is it \"so?\""
);
assert_eq!(prepare_text_prompt("über", &o).unwrap().0, "Über.");
+ assert_eq!(
+ prepare_text_prompt("\"hello there,\" she said.", &o)
+ .unwrap()
+ .0,
+ "\"Hello there,\" she said."
+ );
+ assert_eq!(
+ prepare_text_prompt("(quietly) she left.", &o).unwrap().0,
+ "(Quietly) she left."
+ );
+ assert_eq!(prepare_text_prompt("“über”", &o).unwrap().0, "“Über”.");
assert_eq!(prepare_text_prompt("a\nb c", &o).unwrap().0, "A b c.");
assert!(prepare_text_prompt(" ", &o).is_err());
@@ -494,7 +510,7 @@ mod tests {
);
assert_eq!(
split("(quietly.) Okay then.", 11),
- ["(quietly.)", "Okay then."]
+ ["(Quietly.)", "Okay then."]
);
// A quote after a space opens the next sentence.
assert_eq!(
diff --git a/tests/test_split_sentences.py b/tests/test_split_sentences.py
index 47d5c35..6320237 100644
--- a/tests/test_split_sentences.py
+++ b/tests/test_split_sentences.py
@@ -285,6 +285,22 @@ def test_capitalization_can_be_switched_off():
assert default == "Hello there."
+ "text, expected",
+ [
+ ('"hello there," she said.', '"Hello there," she said.'),
+ ("“hello there,” she said.", "“Hello there,” she said."),
+ ("(quietly) she left.", "(Quietly) she left."),
+ ("'tis the season.", "'Tis the season."),
+ ('"42 is the answer."', '"42 is the answer."'),
+ ('"Already capitalized."', '"Already capitalized."'),
+ ],
+)
+def test_first_letter_after_opening_quotes_is_capitalized(text: str, expected: str):
+ got, _ = prepare_text_prompt(text, False, False)
+ assert got == expected
+
+
def test_replace_characters_rewrites_unseen_characters_before_capitalizing():
"""Characters a model never saw in training are rewritten, and the text is re-tidied."""
drop = {c: "" for c in '"¡¿«»'}
@@ -438,7 +454,7 @@ def test_sentence_ending_on_an_abbreviation_still_splits(tokenizer: Tokenizer):
["A long first sentence goes right here.", "He said “Stop.”"],
),
("He said 'Stop.' Then he left.", 8, ["He said 'Stop.'", "Then he left."]),
- ("(quietly.) Okay then.", 4, ["(quietly.)", "Okay then."]),
+ ("(quietly.) Okay then.", 4, ["(Quietly.)", "Okay then."]),
("He said (“Stop.”) Then he left.", 8, ["He said (“Stop.”)", "Then he left."]),
(
"“Stop,” he said, and then he left the room very quietly.",