mirror of
https://github.com/readest/readest.git
synced 2026-12-01 13:36:39 +00:00
fix(tts): infer a sentence's language from its untrimmed text
CodeRabbit found that 53d3e1f18 joined the sentence's fragments after
cleanTextContent had trimmed each one's leading space, which runs the words
on either side of a tag together: "One<break/> two<break/> three 一二." was
counted as "Onetwothree 一二." (one English word against two Han characters),
so every fragment took the Chinese voice while the paragraph stayed English.
The inference now reads each fragment's untrimmed text. Tags still add no
space, so "i<emphasis>Phone</emphasis>" stays one word.
Verified: the new test `should keep the words apart that a tag and a space
separate` fails without the change (expected [ 'zh', 'zh', 'zh' ] to deeply
equal [ 'en', 'en', 'en' ]) and passes with it. The iPhone test now also
checks the marks (['zh', 'zh']); it passes before and after. SSML and
language suites (163 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:
parent
f7ca65ae09
commit
0cadb9c4b5
2 changed files with 17 additions and 5 deletions
|
|
@ -117,9 +117,17 @@ describe('a stray ideograph in an English paragraph', () => {
|
|||
});
|
||||
|
||||
it('should keep a word split by an inline tag whole', () => {
|
||||
expect(
|
||||
parseSSMLLang(ssmlWithLang('en', '<mark name="0"/>你好 i<emphasis>Phone</emphasis>')),
|
||||
).toBe('zh');
|
||||
const ssml = ssmlWithLang('en', '<mark name="0"/>你好 i<emphasis>Phone</emphasis>');
|
||||
expect(parseSSMLLang(ssml)).toBe('zh');
|
||||
expect(parseSSMLMarks(ssml).marks.map((mark) => mark.language)).toEqual(['zh', 'zh']);
|
||||
});
|
||||
|
||||
// Each fragment's leading space is trimmed for speech; the words it
|
||||
// separated must still count apart.
|
||||
it('should keep the words apart that a tag and a space separate', () => {
|
||||
const ssml = ssmlWithLang('en', '<mark name="0"/>One<break/> two<break/> three 一二.');
|
||||
expect(parseSSMLLang(ssml)).toBe('en');
|
||||
expect(parseSSMLMarks(ssml).marks.map((mark) => mark.language)).toEqual(['en', 'en', 'en']);
|
||||
});
|
||||
|
||||
it('should keep an explicit language inside the sentence', () => {
|
||||
|
|
|
|||
|
|
@ -110,6 +110,7 @@ export const parseSSMLMarks = (ssml: string, primaryLang?: string) => {
|
|||
|
||||
let plainText = '';
|
||||
const marks: TTSMark[] = [];
|
||||
const rawTexts: string[] = [];
|
||||
|
||||
let activeMark: string | null = null;
|
||||
let currentLang = defaultLang;
|
||||
|
|
@ -126,6 +127,7 @@ export const parseSSMLMarks = (ssml: string, primaryLang?: string) => {
|
|||
const offset = plainText.length;
|
||||
plainText += text;
|
||||
marks.push({ offset, name: activeMark, text, language: currentLang });
|
||||
rawTexts.push(rawText);
|
||||
} else {
|
||||
plainText += cleanTextContent(rawText);
|
||||
}
|
||||
|
|
@ -156,10 +158,12 @@ 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.
|
||||
// The text is untrimmed: the space a fragment starts with keeps the words on
|
||||
// either side of the tag apart.
|
||||
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((_, i) =>
|
||||
scopeText.set(scopes[i]!, (scopeText.get(scopes[i]!) ?? '') + rawTexts[i]),
|
||||
);
|
||||
marks.forEach((mark, i) => {
|
||||
mark.language = inferLangFromScript(scopeText.get(scopes[i]!)!, mark.language) || mark.language;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue