oryksaTvSplitForSpeech function

List<String> oryksaTvSplitForSpeech(
  1. String text,
  2. int max
)

Splits text into pieces of at most max characters for the voice, at the end of a sentence when possible, then at a comma, then at a space.

Implementation

List<String> oryksaTvSplitForSpeech(String text, int max) {
  final t = text.replaceAll(RegExp(r'\s+'), ' ').trim();
  if (t.isEmpty) return const [];
  if (t.length <= max) return [t];
  final out = <String>[];
  var cur = '';
  for (final s in oryksaTvSentences(t)) {
    for (final piece in _cut(s, max)) {
      if (cur.isEmpty) {
        cur = piece;
      } else if (cur.length + 1 + piece.length <= max) {
        cur = '$cur $piece';
      } else {
        out.add(cur);
        cur = piece;
      }
    }
  }
  if (cur.isNotEmpty) out.add(cur);
  return out;
}