ukrainian-tn 0.6.0

Ukrainian text normalization, tokenization and sentence splitting: reads numbers, dates, units, currencies and abbreviations as words
Documentation
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! Sentence segmentation.

use super::abbrev;
use super::chars::{
    codepoints, is_alpha, is_digit, is_lower_alpha, is_space, is_upper_one, is_word_cp,
    is_word_letter, lower_ascii_ukrainian, smile_at, Cp,
};
use super::{push_substring, Substring};

const ENDINGS: &str = ".?!…";
const DASHES: &str = "‑–—−-";
const GENERIC_QUOTES: &str = "\"„'";
const CLOSE_QUOTES: &str = "»”’";
const CLOSE_BRACKETS: &str = ")]}";
const DELIMITERS: &str = ".?!…;\"„'»”’)]}";

/// What to do at a candidate sentence boundary.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Action {
    /// Split here.
    Split,
    /// Keep the two halves in one sentence.
    Join,
}

/// The window of context around a candidate boundary.
struct SentSplit<'a> {
    left: &'a str,
    delimiter: &'a str,
    right: &'a str,
    buffer: &'a str,
}

fn first_char(text: &str) -> Option<char> {
    text.chars().next()
}

fn starts_with_space(text: &str) -> bool {
    first_char(text).is_some_and(is_space)
}

fn ends_with_space(text: &str) -> bool {
    text.chars().next_back().is_some_and(is_space)
}

/// Returns the byte offset of the first non-space character and the trimmed slice.
pub(crate) fn trim_view(text: &str) -> (usize, &str) {
    let start = text.find(|c: char| !is_space(c)).unwrap_or(text.len());
    let end = text
        .rfind(|c: char| !is_space(c))
        .map_or(start, |i| i + text[i..].chars().next().map_or(0, char::len_utf8));
    (start, &text[start..end])
}

/// Splits `text` into maximal runs of word letters, of digits, and single other
/// characters, skipping whitespace.
fn tokens_in(text: &str) -> Vec<&str> {
    let cps = codepoints(text);
    let mut out = Vec::new();
    let mut i = 0;
    while i < cps.len() {
        if is_space(cps[i].value) {
            i += 1;
            continue;
        }
        let begin = i;
        if is_word_letter(cps[i].value) {
            while i < cps.len() && is_word_letter(cps[i].value) {
                i += 1;
            }
        } else if is_digit(cps[i].value) {
            while i < cps.len() && is_digit(cps[i].value) {
                i += 1;
            }
        } else {
            i += 1;
        }
        out.push(&text[cps[begin].start..cps[i - 1].stop]);
    }
    out
}

/// The first token of `text`: a run of word letters, a run of digits, or one
/// other character.
fn first_token(text: &str) -> Option<&str> {
    let cps = codepoints(text);
    for i in 0..cps.len() {
        if is_space(cps[i].value) {
            continue;
        }
        return Some(run_forward(text, &cps, i));
    }
    None
}

/// The first run of word letters or digits, ignoring everything else.
fn first_word(text: &str) -> Option<&str> {
    let cps = codepoints(text);
    for i in 0..cps.len() {
        if is_word_letter(cps[i].value) || is_digit(cps[i].value) {
            return Some(run_forward(text, &cps, i));
        }
    }
    None
}

fn run_forward<'a>(text: &'a str, cps: &[Cp], i: usize) -> &'a str {
    let pred: fn(char) -> bool = if is_word_letter(cps[i].value) {
        is_word_letter
    } else if is_digit(cps[i].value) {
        is_digit
    } else {
        return &text[cps[i].start..cps[i].stop];
    };
    let mut j = i + 1;
    while j < cps.len() && pred(cps[j].value) {
        j += 1;
    }
    &text[cps[i].start..cps[j - 1].stop]
}

/// The last token of `text`, mirroring [`first_token`].
fn last_token(text: &str) -> Option<&str> {
    let cps = codepoints(text);
    for i in (0..cps.len()).rev() {
        if is_space(cps[i].value) {
            continue;
        }
        let pred: fn(char) -> bool = if is_word_letter(cps[i].value) {
            is_word_letter
        } else if is_digit(cps[i].value) {
            is_digit
        } else {
            return Some(&text[cps[i].start..cps[i].stop]);
        };
        let mut j = i;
        while j > 0 && pred(cps[j - 1].value) {
            j -= 1;
        }
        return Some(&text[cps[j].start..cps[i].stop]);
    }
    None
}

/// The trailing token when it contains an inner `-`, `.` or `/`, as in `чл.-кор`.
fn last_compound_abbrev_token(text: &str) -> Option<&str> {
    let cps = codepoints(text);
    for i in (0..cps.len()).rev() {
        if is_space(cps[i].value) {
            continue;
        }
        if !is_word_cp(cps[i].value) {
            return None;
        }
        let joiner = |c: char| matches!(c, '-' | '.' | '/');
        let mut saw_compound_mark = false;
        let mut j = i;
        while j > 0 && (is_word_cp(cps[j - 1].value) || joiner(cps[j - 1].value)) {
            saw_compound_mark |= joiner(cps[j - 1].value);
            j -= 1;
        }
        while j <= i && joiner(cps[j].value) {
            j += 1;
        }
        if j > i || !saw_compound_mark {
            return None;
        }
        return Some(&text[cps[j].start..cps[i].stop]);
    }
    None
}

/// The word before a trailing period, as in `... тис.` -> `тис`.
fn trailing_dot_abbrev_token(text: &str) -> Option<&str> {
    let cps = codepoints(text);
    let mut i = cps.len();
    while i > 0 && is_space(cps[i - 1].value) {
        i -= 1;
    }
    if i == 0 || cps[i - 1].value != '.' {
        return None;
    }
    i -= 1;
    while i > 0 && is_space(cps[i - 1].value) {
        i -= 1;
    }
    let stop = i;
    while i > 0 && is_word_cp(cps[i - 1].value) {
        i -= 1;
    }
    (i != stop).then(|| &text[cps[i].start..cps[stop - 1].stop])
}

/// Splits a trailing `<word>. <word>` into its two words, as in `т. д`.
fn left_abbreviation_pair(text: &str) -> Option<(&str, &str)> {
    let cps = codepoints(text);
    let mut i = cps.len();
    while i > 0 && is_space(cps[i - 1].value) {
        i -= 1;
    }
    let b_stop = i;
    while i > 0 && is_word_cp(cps[i - 1].value) {
        i -= 1;
    }
    if i == b_stop {
        return None;
    }
    let b_start = i;
    while i > 0 && is_space(cps[i - 1].value) {
        i -= 1;
    }
    if i == 0 || cps[i - 1].value != '.' {
        return None;
    }
    i -= 1;
    while i > 0 && is_space(cps[i - 1].value) {
        i -= 1;
    }
    let a_stop = i;
    while i > 0 && is_word_cp(cps[i - 1].value) {
        i -= 1;
    }
    if i == a_stop {
        return None;
    }
    Some((
        &text[cps[i].start..cps[a_stop - 1].stop],
        &text[cps[b_start].start..cps[b_stop - 1].stop],
    ))
}

fn starts_with_letter_bullet(text: &str) -> bool {
    let cps = codepoints(text);
    let mut i = 0;
    while i < cps.len() && is_space(cps[i].value) {
        i += 1;
    }
    if i + 1 >= cps.len() || !is_alpha(cps[i].value) || cps[i + 1].value != ')' {
        return false;
    }
    i + 2 >= cps.len() || is_space(cps[i + 2].value)
}

/// True when `token` can legitimately follow an abbreviation's period.
fn can_follow_abbreviation(token: &str) -> bool {
    if token.is_empty() {
        return false;
    }
    if token.chars().all(is_digit) {
        return true;
    }
    if !token.chars().all(is_alpha) {
        return true;
    }
    is_lower_alpha(token)
}

fn is_roman_token(token: &str) -> bool {
    !token.is_empty() && token.chars().all(|c| "IVXLCDM".contains(c))
}

fn is_article_abbrev_right(token: &str) -> bool {
    !token.is_empty() && (token.chars().all(is_digit) || is_roman_token(token))
}

fn is_bullet(token: &str) -> bool {
    if token.is_empty() {
        return false;
    }
    if token.bytes().all(|b| b.is_ascii_digit()) || token == "." || token == ")" {
        return true;
    }
    let lower = lower_ascii_ukrainian(token);
    "§абвгдеabcdef".contains(&lower) || (token.chars().all(|c| "IVXML".contains(c)))
}

fn smile_prefix(text: &str) -> bool {
    let offset = text.find(|c: char| !is_space(c)).unwrap_or(text.len());
    smile_at(text, offset).is_some()
}

fn sent_join(split: &SentSplit<'_>) -> Action {
    let (Some(left), Some(right)) = (last_token(split.left), first_token(split.right)) else {
        return Action::Join;
    };
    if !starts_with_space(split.right) {
        return Action::Join;
    }

    let first_non_space = split.right.find(|c: char| !matches!(c, ' ' | '\t' | '\r' | '\n'));
    let leading_space = &split.right[..first_non_space.unwrap_or(split.right.len())];

    // A wiki-style `== heading ==` never ends mid-way.
    let current_line =
        split.buffer.rfind(['\r', '\n']).map_or(split.buffer, |i| &split.buffer[i + 1..]);
    let heading = current_line
        .find(|c: char| !matches!(c, ' ' | '\t'))
        .is_some_and(|i| current_line[i..].starts_with("=="));
    if heading && first_non_space.is_some_and(|i| split.right[i..].starts_with("==")) {
        return Action::Join;
    }

    if leading_space.contains(['\n', '\r']) {
        return Action::Split;
    }
    if starts_with_letter_bullet(split.right) {
        return Action::Split;
    }
    if is_lower_alpha(right) {
        return Action::Join;
    }

    let right_cp = first_char(right).unwrap_or('\0');
    if !GENERIC_QUOTES.contains(right_cp)
        && (DELIMITERS.contains(right_cp) || smile_prefix(split.right))
    {
        return Action::Join;
    }

    let delimiter_cp = first_char(split.delimiter).unwrap_or('\0');
    let left_lower = lower_ascii_ukrainian(last_compound_abbrev_token(split.left).unwrap_or(left));

    if split.delimiter == "." {
        // `буд. 5 м. Київ`: a measurement abbreviation after a number is not a boundary.
        if left_lower == "м" || left_lower == "с" {
            let tokens = tokens_in(split.left);
            if tokens.len() >= 2 && tokens[tokens.len() - 2].chars().all(is_digit) {
                return Action::Split;
            }
        }
        if left.chars().all(is_digit) && right.chars().all(is_digit) {
            return Action::Join;
        }
        if let Some(dotted) = trailing_dot_abbrev_token(split.left) {
            let dotted_lower = lower_ascii_ukrainian(dotted);
            if abbrev::is_known(&dotted_lower) && can_follow_abbreviation(right) {
                return Action::Join;
            }
        }
        let mut skip_single_abbreviation = false;
        if let Some((a, b)) = left_abbreviation_pair(split.left) {
            let pair = format!("{} {}", lower_ascii_ukrainian(a), lower_ascii_ukrainian(b));
            if abbrev::LEADING_PAIRS.contains(pair.as_str()) {
                return Action::Join;
            }
            if abbrev::PAIRS.contains(pair.as_str()) {
                if can_follow_abbreviation(right) {
                    return Action::Join;
                }
                skip_single_abbreviation = true;
            }
        }
        if !skip_single_abbreviation {
            let known = if left_lower == "ст" {
                is_article_abbrev_right(right)
            } else {
                abbrev::LEADING.contains(left_lower.as_str())
            };
            if known || (abbrev::is_known(&left_lower) && can_follow_abbreviation(right)) {
                return Action::Join;
            }
            let pair = format!("{} {}", left_lower, lower_ascii_ukrainian(right));
            if abbrev::PAIRS.contains(pair.as_str()) {
                return Action::Join;
            }
        }
        if is_upper_one(left) || abbrev::INITIALS.contains(left_lower.as_str()) {
            return Action::Join;
        }
    }

    // A short run of bullet markers such as `1.` or `а)` is a list item, not a sentence.
    if (split.delimiter == "." || split.delimiter == ")") && split.buffer.len() <= 20 {
        let toks = tokens_in(split.buffer);
        if !toks.is_empty() && toks.iter().all(|t| is_bullet(t)) {
            return Action::Join;
        }
    }

    if CLOSE_QUOTES.contains(delimiter_cp)
        || GENERIC_QUOTES.contains(delimiter_cp)
        || CLOSE_BRACKETS.contains(delimiter_cp)
    {
        let left_cp = first_char(left).unwrap_or('\0');
        if !ENDINGS.contains(left_cp) {
            return Action::Join;
        }
        if GENERIC_QUOTES.contains(delimiter_cp) && ends_with_space(split.left) {
            return Action::Join;
        }
    }

    if DASHES.contains(right_cp) && first_word(split.right).is_some_and(is_lower_alpha) {
        return Action::Join;
    }

    Action::Split
}

/// Splits `text` into sentences.
pub fn split_sentences(text: &str) -> Vec<Substring<'_>> {
    if text.chars().all(is_space) {
        return Vec::new();
    }
    let cps = codepoints(text);
    let mut out = Vec::new();
    let mut current_start = 0;
    let mut ci = 0;
    while ci < cps.len() {
        let mut stop = cps[ci].stop;
        let mut is_delim = DELIMITERS.contains(cps[ci].value);
        let mut delimiter_end_index = ci + 1;
        if let Some(smile_stop) = smile_at(text, cps[ci].start) {
            stop = smile_stop;
            is_delim = true;
            while delimiter_end_index < cps.len() && cps[delimiter_end_index].start < stop {
                delimiter_end_index += 1;
            }
        }
        if !is_delim {
            ci += 1;
            continue;
        }

        let start = cps[ci].start;
        // The decision only looks at ten characters on either side.
        let left_start = if ci > 10 { cps[ci - 10].start } else { 0 };
        let right_stop = if delimiter_end_index < cps.len() {
            cps[cps.len().min(delimiter_end_index + 10) - 1].stop
        } else {
            text.len()
        };
        let split = SentSplit {
            left: &text[left_start..start],
            delimiter: &text[start..stop],
            right: &text[stop..right_stop],
            buffer: &text[current_start..start],
        };
        if sent_join(&split) != Action::Join {
            push_substring(&mut out, text, current_start, stop, true);
            current_start = stop;
        }
        ci = delimiter_end_index;
    }
    push_substring(&mut out, text, current_start, text.len(), true);
    out
}