fix(tts): count the space between two tags when inferring a sentence's language

CodeRabbit found that 0cadb9c4b still dropped text that isn't a fragment
of its own: in "One<break/> <break/>two 一二." the lone space between the
two tags never reached the inference text, so "One" and "two" ran together
and both fragments took the Chinese voice while the paragraph stayed
English. That text is now appended to the previous fragment's inference
text. It holds no letters (only whitespace, punctuation or symbols reach
that branch), so it only separates words.

Verified: the new test `should keep the words apart that a space alone
between two tags separates` fails without the change (expected
[ 'zh', 'zh' ] to deeply equal [ 'en', 'en' ]) and passes with it. SSML
and language suites (164 passed) and pnpm lint; the full suite runs at
push.

Co-Authored-By: Claude Code (Claude Opus 5.5) <noreply@anthropic.com>
This commit is contained in:
Huang Xin 2026-11-28 15:45:47 +08:00
parent 0cadb9c4b5
commit db62f06c6a
No known key found for this signature in database
GPG key ID: 7BEBDF8CB75381EB
2 changed files with 9 additions and 0 deletions

View file

@ -130,6 +130,12 @@ describe('a stray ideograph in an English paragraph', () => {
expect(parseSSMLMarks(ssml).marks.map((mark) => mark.language)).toEqual(['en', 'en', 'en']);
});
it('should keep the words apart that a space alone between two tags separates', () => {
const ssml = ssmlWithLang('en', '<mark name="0"/>One<break/> <break/>two 一二.');
expect(parseSSMLLang(ssml)).toBe('en');
expect(parseSSMLMarks(ssml).marks.map((mark) => mark.language)).toEqual(['en', 'en']);
});
it('should keep an explicit language inside the sentence', () => {
const { marks } = parseSSMLMarks(
ssmlWithLang('en', '<mark name="0"/>She said <lang xml:lang="fr">bonjour</lang> and left.'),

View file

@ -130,6 +130,9 @@ export const parseSSMLMarks = (ssml: string, primaryLang?: string) => {
rawTexts.push(rawText);
} else {
plainText += cleanTextContent(rawText);
// Not a fragment (a space between two tags, say), but it still keeps
// the words around it apart.
if (rawTexts.length) rawTexts[rawTexts.length - 1] += rawText;
}
} else {
const isEnd = match[1] === '/';