aboutsummaryrefslogtreecommitdiffstats
path: root/crates/asist-core/src/text.rs
blob: 30eef9d37d6f1ee0ed29874491c735157e87cc4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//! Preparing the text that will be read aloud.
//!
//! Two different problems, both on the latency critical path:
//!
//! 1. **Splitting into sentences while the model writes.** Waiting for the
//!    whole answer before synthesizing adds the LLM time to the TTS time.
//!    Cutting per sentence, the assistant starts speaking while still thinking.
//! 2. **Removing what is not pronounced.** The synthesizer reads asterisks and
//!    hashes literally, so markdown that slips out of the model must be
//!    cleaned first.

/// Splits a text stream into speakable sentences.
///
/// The first sentence goes out as soon as it is minimally decent and the next
/// ones wait until they have more body: the start dominates perceived
/// latency, but once the voice is playing, longer sentences get better
/// intonation.
#[derive(Debug)]
pub struct SentenceSplitter {
    buffer: String,
    emitted: usize,
    /// Minimum length of the first sentence.
    first_min: usize,
    /// Minimum length of the following ones.
    rest_min: usize,
    /// Length after which the text is cut even without punctuation, so a
    /// model that does not punctuate does not leave the assistant mute.
    hard_max: usize,
}

impl Default for SentenceSplitter {
    fn default() -> Self {
        Self {
            buffer: String::new(),
            emitted: 0,
            first_min: 12,
            rest_min: 40,
            hard_max: 240,
        }
    }
}

impl SentenceSplitter {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_limits(first_min: usize, rest_min: usize, hard_max: usize) -> Self {
        Self {
            first_min,
            rest_min,
            hard_max,
            ..Self::default()
        }
    }

    pub fn emitted(&self) -> usize {
        self.emitted
    }

    /// Adds newly arrived text and returns the sentences already complete.
    pub fn push(&mut self, delta: &str) -> Vec<String> {
        self.buffer.push_str(delta);
        let mut out = Vec::new();
        while let Some(sentence) = self.take_ready() {
            out.push(sentence);
        }
        out
    }

    /// Delivers whatever is left when the answer ends.
    pub fn flush(&mut self) -> Option<String> {
        let rest = clean_for_speech(&std::mem::take(&mut self.buffer));
        if !is_speakable(&rest) {
            return None;
        }
        self.emitted += 1;
        Some(rest)
    }

    fn min_len(&self) -> usize {
        if self.emitted == 0 {
            self.first_min
        } else {
            self.rest_min
        }
    }

    fn take_ready(&mut self) -> Option<String> {
        let cut = self.boundary()?;
        let head: String = self.buffer.drain(..cut).collect();
        let head = clean_for_speech(&head);
        if !is_speakable(&head) {
            // It was only punctuation or markdown: dropped without spending a
            // synthesis turn, but the cut has been consumed.
            return self.take_ready();
        }
        self.emitted += 1;
        Some(head)
    }

    /// Byte index to cut at, if any.
    fn boundary(&self) -> Option<usize> {
        let min = self.min_len();
        let mut last_soft = None;

        for (i, c) in self.buffer.char_indices() {
            let end = i + c.len_utf8();
            if end < min {
                continue;
            }
            if is_terminator(c) {
                // The dot of an abbreviation or a decimal does not end a sentence.
                if c == '.' && !ends_sentence(&self.buffer, i) {
                    continue;
                }
                // It only closes if a space follows, or if nothing is left:
                // otherwise it would cut in the middle of «3.14».
                match self.buffer[end..].chars().next() {
                    Some(next) if next.is_whitespace() => return Some(end),
                    None => {}
                    Some(_) => continue,
                }
            }
            // A comma or semicolon counts as an emergency cut if the sentence
            // has already run too long.
            if matches!(c, ',' | ';' | ':') {
                last_soft = Some(end);
            }
            if end >= self.hard_max {
                return Some(last_soft.unwrap_or(end));
            }
        }
        None
    }
}

/// Is there anything to pronounce?
///
/// A fragment with only punctuation (what is left after cleaning a «**» or a
/// stray ellipsis) costs a whole synthesis and sounds like nothing, so it
/// never leaves the splitter.
pub fn is_speakable(text: &str) -> bool {
    text.chars().any(char::is_alphanumeric)
}

fn is_terminator(c: char) -> bool {
    matches!(c, '.' | '!' | '?' | '…' | '\n')
}

/// Does the dot at `idx` really end a sentence?
///
/// Rules out decimals («3.14»), half-written ellipses and the most common
/// Spanish abbreviations, which would otherwise split the sentence right
/// before the name.
fn ends_sentence(text: &str, idx: usize) -> bool {
    let before = &text[..idx];
    let after = &text[idx + 1..];

    if after.starts_with(|c: char| c.is_ascii_digit())
        && before.ends_with(|c: char| c.is_ascii_digit())
    {
        return false;
    }
    if after.starts_with('.') || before.ends_with('.') {
        return false;
    }
    let word = before
        .rsplit(|c: char| c.is_whitespace())
        .next()
        .unwrap_or("")
        .to_lowercase();
    const ABBREVIATIONS: &[&str] = &[
        "sr", "sra", "srta", "dr", "dra", "ud", "uds", "etc", "ej", "p.ej", "av", "núm", "num",
        "pág", "pag", "vol", "art", "ap", "aprox", "ee.uu", "d", "dña",
    ];
    !ABBREVIATIONS.contains(&word.as_str())
}

/// Gets the text ready for the synthesizer.
///
/// It only removes what is not pronounced; it neither rewrites nor
/// summarizes, because what is heard must be what the model said.
pub fn clean_for_speech(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    let mut chars = text.chars().peekable();
    let mut at_line_start = true;

    while let Some(c) = chars.next() {
        match c {
            // Emphasis and code: the delimiters would be read aloud.
            '*' | '_' | '`' | '~' => continue,
            // Headings and bullets, only at the start of a line: a hash or a
            // dash in the middle of a sentence does mean something.
            '#' if at_line_start => {
                while chars.peek() == Some(&'#') {
                    chars.next();
                }
                while chars.peek().is_some_and(|c| *c == ' ') {
                    chars.next();
                }
                continue;
            }
            '-' | '•' | '–' if at_line_start && chars.peek() == Some(&' ') => {
                chars.next();
                continue;
            }
            '>' if at_line_start => {
                while chars.peek().is_some_and(|c| *c == ' ') {
                    chars.next();
                }
                continue;
            }
            // Line breaks are spoken as pauses.
            '\n' | '\r' | '\t' => {
                at_line_start = c == '\n';
                if !out.ends_with(' ') && !out.is_empty() {
                    out.push(' ');
                }
                continue;
            }
            _ => {}
        }
        at_line_start = false;
        out.push(c);
    }

    // Collapses the spaces the cleanup leaves behind.
    let mut collapsed = String::with_capacity(out.len());
    let mut space = false;
    for c in out.chars() {
        if c == ' ' {
            space = true;
            continue;
        }
        if space && !collapsed.is_empty() {
            collapsed.push(' ');
        }
        space = false;
        collapsed.push(c);
    }
    collapsed.trim().to_string()
}

#[cfg(test)]
mod tests {
    use super::*;

    fn split_all(chunks: &[&str]) -> Vec<String> {
        let mut splitter = SentenceSplitter::new();
        let mut out: Vec<String> = chunks.iter().flat_map(|c| splitter.push(c)).collect();
        out.extend(splitter.flush());
        out
    }

    #[test]
    fn first_sentence_comes_out_before_the_rest() {
        // The first one only needs to be short; the second waits for body.
        let out = split_all(&["Claro que sí. ", "Vale. ", "Aún no.", ""]);
        assert_eq!(out[0], "Claro que sí.");
        assert_eq!(out[1], "Vale. Aún no.");
    }

    #[test]
    fn chunks_as_deltas_arrive() {
        let mut splitter = SentenceSplitter::new();
        assert!(splitter.push("Hola, ¿qué ").is_empty());