AdvisoryBuilderWorkshop / frontend /src /lib /ttsSentenceSplit.ts
NeonClary's picture
Deploy tutorial feature and recent fixes
d8808e7 verified
Raw
History Blame Contribute Delete
709 Bytes
/** Split plain text into sentences for chunked TTS (aligned with AskJerry / ExperimentPage). */
export function extractTtsSentences(text: string, includeTrailing = false): string[] {
const t = (text || '').trim()
if (!t) return []
const re = /[^.!?\n]+[.!?\n]+/g
const sentences: string[] = []
let match: RegExpExecArray | null
let lastEnd = 0
while ((match = re.exec(t)) !== null) {
const s = match[0].trim()
if (s) sentences.push(s)
lastEnd = re.lastIndex
}
if (!sentences.length) return includeTrailing ? [t] : []
if (includeTrailing) {
const remainder = t.slice(lastEnd).trim()
if (remainder) sentences.push(remainder)
}
return sentences
}