Split text into chunks of 5000 characters in paragraph

This commit is contained in:
Hongbo Wu 2022-08-17 12:25:23 +08:00
parent c9d26acb25
commit 83c1930378
2 changed files with 12 additions and 6 deletions

View file

@ -120,7 +120,7 @@ export function articleRouter() {
return res.status(200).send('Page not found')
}
const text = parseHTML(page.content).document.documentElement.textContent
const text = parseHTML(page.content).document.documentElement.innerText
if (!text) {
return res.status(200).send('Page has no text')
}

View file

@ -129,13 +129,19 @@ export const synthesizeTextToSpeech = async (
)
})
}
// slice the text into chunks of 1,000 characters
const textChunks = input.text.match(/(.|[\r\n]){1,1000}/g) || []
// slice the text into chunks of 5,000 characters
let currentTextChunk = ''
const textChunks = input.text.split('\n')
for (const textChunk of textChunks) {
logger.debug(`synthesizing ${textChunk}`)
const result = await speakTextAsyncPromise(textChunk)
currentTextChunk += textChunk + '\n'
if (currentTextChunk.length < 5000) {
continue
}
logger.debug(`synthesizing ${currentTextChunk}`)
const result = await speakTextAsyncPromise(currentTextChunk)
timeOffset = timeOffset + result.audioDuration
characterOffset = characterOffset + textChunk.length
characterOffset = characterOffset + currentTextChunk.length
currentTextChunk = ''
}
writeStream.end()
synthesizer.close()