Skip to content

Commit

Permalink
ReadAloud ignores hyphens that span lines
Browse files Browse the repository at this point in the history
  • Loading branch information
cdrini committed Dec 17, 2019
1 parent 383a025 commit 39f0800
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion BookReader/plugins/plugin.tts.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion BookReader/plugins/plugin.tts.js.map

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion src/js/plugins/tts/PageChunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ export default class PageChunk {
static _fromTextWrapperResponse(leafIndex, chunksResponse) {
return chunksResponse.map((c, i) => {
const correctedLineRects = PageChunk._fixChunkRects(c.slice(1));
return new PageChunk(leafIndex, i, c[0], correctedLineRects);
const correctedText = PageChunk._removeDanglingHyphens(c[0]);
return new PageChunk(leafIndex, i, correctedText, correctedLineRects);
});
}

Expand Down Expand Up @@ -90,6 +91,15 @@ export default class PageChunk {

return rects;
}

/**
* Remove "dangling" hyphens from read aloud text to avoid TTS stuttering
* @param {string} text
* @return {string}
*/
static _removeDanglingHyphens(text) {
return text.replace(/-\s+/g, '');
}
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/plugins/tts/PageChunk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,24 @@ describe('_fromTextWrapperResponse', () => {
expect(chunks).toEqual([new PageChunk(0, 0, 'Line', [[0,100,100,0]])]);
});
});

describe('_removeDanglingHyphens', () => {
const { _removeDanglingHyphens } = PageChunk;

test('No change to empty string', () => {
expect(_removeDanglingHyphens('')).toEqual('');
});

test('No change when no hyphens string', () => {
expect(_removeDanglingHyphens('Hello world!')).toEqual('Hello world!');
});

test('No change to hyphens mid-word', () => {
expect(_removeDanglingHyphens('It is mid-word!')).toEqual('It is mid-word!');
});

test('Removes hyphens followed by spaces', () => {
expect(_removeDanglingHyphens('It is not mid- word!')).toEqual('It is not midword!');
expect(_removeDanglingHyphens('mid- word fid- word')).toEqual('midword fidword');
});
});

0 comments on commit 39f0800

Please sign in to comment.