fix(tts): infer one language per sentence, not per tag fragment

Two gaps CodeRabbit found in the previous commit:

- parseSSMLMarks inferred a language for each text fragment between tags,
  so a fragment holding only a stray ideograph still took a Chinese voice.
  #preprocessSSML replaces only the first <break/>, so a line that is just
  一 between two <br>s reached it that way. The language now comes from all
  of a sentence's text within one language scope; an explicit <lang> scope
  keeps its own.
- parseSSMLLang replaced tags with a space, which split a word cut by an
  inline tag: "你好 i<emphasis>Phone</emphasis>" counted two English words
  and came out en. Tags are now removed without a space, the way the marks
  parser joins fragments.

Verified:
- New tests in ssml.test.ts fail without the change (expected
  [ 'en', 'zh', 'en' ] to deeply equal [ 'en', 'en', 'en' ]; expected 'en'
  to be 'zh') and pass with it. A third pins an explicit <lang> fragment
  inside an English sentence to its own language; it passes before and
  after.
- dev-web with Edge TTS on the reported book: 11 requests, all
  `en | en-US-BrianNeural`, including two sentences ending in 一.
- pnpm test (SSML and language suites; the full suite runs at push),
  pnpm lint, pnpm format:check.

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

View file

@ -105,6 +105,30 @@ describe('a stray ideograph in an English paragraph', () => {
const { marks } = parseSSMLMarks(ssml, 'en');
expect(marks.map((mark) => mark.language)).toEqual(['en', 'en', 'en']);
});
// A tag splits a sentence into fragments; one holding only the ideograph
// must not take a voice of its own.
it('should keep a fragment holding only the ideograph in English', () => {
const { marks } = parseSSMLMarks(
ssmlWithLang('en', '<mark name="0"/>He paused<break/>一<break/>and went on.'),
'en',
);
expect(marks.map((mark) => mark.language)).toEqual(['en', 'en', 'en']);
});
it('should keep a word split by an inline tag whole', () => {
expect(
parseSSMLLang(ssmlWithLang('en', '<mark name="0"/>你好 i<emphasis>Phone</emphasis>')),
).toBe('zh');
});
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.'),
'en',
);
expect(marks.map((mark) => mark.language)).toEqual(['en', 'fr', 'en']);
});
});
describe('parseSSMLMarks', () => {

View file

@ -85,7 +85,7 @@ export const parseSSMLLang = (ssml: string, primaryLang?: string): string => {
if (isDefaultEnglish && primaryLang && !isSameLang(lang, primaryLang)) {
lang = primaryLang.split('-')[0]!.toLowerCase();
}
const text = removeLanguageBlocks(ssml, () => false).replace(/<[^>]*>/g, ' ');
const text = removeLanguageBlocks(ssml, () => false).replace(/<[^>]*>/g, '');
return inferLangFromScript(text, lang);
};
@ -121,12 +121,7 @@ export const parseSSMLMarks = (ssml: string, primaryLang?: string) => {
if (text && activeMark && isValidMark(text)) {
const offset = plainText.length;
plainText += text;
marks.push({
offset,
name: activeMark,
text,
language: inferLangFromScript(text, currentLang) || currentLang,
});
marks.push({ offset, name: activeMark, text, language: currentLang });
} else {
plainText += cleanTextContent(rawText);
}
@ -154,6 +149,18 @@ export const parseSSMLMarks = (ssml: string, primaryLang?: string) => {
}
}
// A tag cuts a sentence into fragments. The language comes from all of the
// sentence's text in one language scope, so a fragment holding only a stray
// ideograph (between two <break/>s, say) doesn't get a voice of its own.
const scopes = marks.map((mark) => `${mark.name}\u0000${mark.language}`);
const scopeText = new Map<string, string>();
marks.forEach((mark, i) =>
scopeText.set(scopes[i]!, (scopeText.get(scopes[i]!) ?? '') + mark.text),
);
marks.forEach((mark, i) => {
mark.language = inferLangFromScript(scopeText.get(scopes[i]!)!, mark.language) || mark.language;
});
return { plainText, marks };
};