kham-core 0.4.0

Pure Rust Thai word segmentation engine — no_std compatible
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//! Unicode script classifier and pre-tokenizer.
//!
//! Splits raw input into coarse, script-homogeneous [`Token`] spans before
//! the main segmenter runs. The segmenter only needs to apply the expensive
//! DAG algorithm to Thai spans; all other spans pass through unchanged.
//!
//! ## Pipeline position
//!
//! ```text
//! raw text
//!//!//! pre_tokenize()   ← this module
//!    │  splits into [Thai | Latin | Number | Whitespace | Emoji | Punctuation | Unknown]
//!//! segmenter        ← processes Thai spans with tcc + dict
//!//!//! Vec<Token<'_>>
//! ```
//!
//! ## Example
//!
//! ```rust
//! use kham_core::pre_tokenizer::pre_tokenize;
//! use kham_core::TokenKind;
//!
//! let spans = pre_tokenize("ธนาคาร100แห่ง");
//! assert_eq!(spans[0].kind, TokenKind::Thai);   // "ธนาคาร"
//! assert_eq!(spans[1].kind, TokenKind::Number); // "100"
//! assert_eq!(spans[2].kind, TokenKind::Thai);   // "แห่ง"
//! ```

use alloc::vec::Vec;

use crate::token::{Token, TokenKind};

// ---------------------------------------------------------------------------
// Character classification
// ---------------------------------------------------------------------------

/// Classify a single Unicode scalar value into a [`TokenKind`].
///
/// Classification is purely codepoint-based — no context is used. The rules
/// are applied in priority order so that sub-ranges override their parent
/// block (e.g. Thai digits are checked before the broader Thai block).
///
/// ## Classification table
///
/// | Range / set | Kind |
/// |---|---|
/// | U+0E50–U+0E59 (Thai digits ๐–๙) | `Number` |
/// | U+0E00–U+0E7F (Thai block) | `Thai` |
/// | `0`–`9` (ASCII digits) | `Number` |
/// | U+FF10–U+FF19 (fullwidth digits) | `Number` |
/// | `A`–`Z`, `a`–`z` (ASCII letters) | `Latin` |
/// | U+FF21–U+FF5A (fullwidth Latin) | `Latin` |
/// | Space, tab, newline, CR, NBSP, ideographic space | `Whitespace` |
/// | Major emoji blocks (U+1F300–U+1FAFF, U+2600–U+27BF, …) | `Emoji` |
/// | ASCII punctuation (`!`–`/`, `:`–`@`, …) | `Punctuation` |
/// | U+2000–U+206F (Unicode general punctuation) | `Punctuation` |
/// | Everything else | `Unknown` |
#[inline]
pub fn classify_char(c: char) -> TokenKind {
    match c {
        // Thai digits sit inside the Thai block — check them first so they
        // are not misclassified as Thai script.
        '\u{0E50}'..='\u{0E59}' => TokenKind::Number,

        // Remaining Thai Unicode block: consonants, vowels, tone marks, etc.
        '\u{0E00}'..='\u{0E7F}' => TokenKind::Thai,

        // ASCII decimal digits.
        '0'..='9' => TokenKind::Number,

        // Fullwidth digit forms (U+FF10 0 – U+FF19 9).
        '\u{FF10}'..='\u{FF19}' => TokenKind::Number,

        // ASCII basic Latin letters (a–z, A–Z).
        'A'..='Z' | 'a'..='z' => TokenKind::Latin,

        // Fullwidth Latin capital (U+FF21 A – U+FF3A Z) and
        // small (U+FF41 a – U+FF5A z) letter forms.
        '\u{FF21}'..='\u{FF3A}' | '\u{FF41}'..='\u{FF5A}' => TokenKind::Latin,

        // Common whitespace: regular space, horizontal tab, newline, carriage
        // return, non-breaking space (U+00A0), and ideographic space (U+3000).
        ' ' | '\t' | '\n' | '\r' | '\u{00A0}' | '\u{3000}' => TokenKind::Whitespace,

        // Emoji — covers the core emoji blocks in the Supplementary Multilingual
        // Plane and the Miscellaneous Symbols / Dingbats blocks in the BMP.
        // ZWJ (U+200D) and the emoji variation selector (U+FE0F) are also
        // included so that ZWJ emoji sequences stay in one span.
        c if is_emoji(c) => TokenKind::Emoji,

        // ASCII punctuation is split into three non-contiguous ranges:
        //   U+0021–U+002F  ! " # $ % & ' ( ) * + , - . /
        //   U+003A–U+0040  : ; < = > ? @
        //   U+005B–U+0060  [ \ ] ^ _ `
        //   U+007B–U+007E  { | } ~
        '!'..='/' | ':'..='@' | '['..='`' | '{'..='~' => TokenKind::Punctuation,

        // Unicode General Punctuation block (U+2000–U+206F):
        // em-dash, en-dash, ellipsis, quotation marks, etc.
        '\u{2000}'..='\u{206F}' => TokenKind::Punctuation,

        // All other codepoints (Hangul, Arabic, Cyrillic, CJK, etc.).
        _ => TokenKind::Unknown,
    }
}

/// Returns `true` if `c` belongs to one of the major Unicode emoji blocks.
///
/// This function is intentionally conservative: it matches codepoints that
/// are nearly always emoji (Emoticons, Miscellaneous Symbols and Pictographs,
/// Transport and Map Symbols, supplemental emoji blocks), plus the two glue
/// codepoints used to build emoji sequences — ZWJ (U+200D) and the emoji
/// variation selector (U+FE0F).
///
/// Full ZWJ-sequence detection (e.g. 👨‍👩‍👧) requires multi-codepoint
/// lookahead and is left to a dedicated Unicode segmenter; this function
/// ensures that the individual codepoints in such sequences are at least
/// classified as `Emoji` so they land in the same pre-token span.
#[inline]
pub fn is_emoji(c: char) -> bool {
    matches!(c,
        // Zero-width joiner — glue used in multi-person / flag emoji sequences.
        '\u{200D}'
        // Variation Selector-16: forces emoji (graphic) presentation.
        | '\u{FE0F}'
        // Miscellaneous Symbols and Dingbats (BMP).
        | '\u{2600}'..='\u{27BF}'
        // Supplemental Symbols and Pictographs — the large SMP emoji block.
        // Covers Emoticons (1F600), Misc Symbols & Pictographs (1F300),
        // Transport (1F680), Activities (1F3C0), Objects (1F4A0), etc.
        | '\u{1F300}'..='\u{1F9FF}'
        // Symbols and Pictographs Extended-A (chess, medical symbols, …).
        | '\u{1FA00}'..='\u{1FAFF}'
    )
}

// ---------------------------------------------------------------------------
// Pre-tokenizer
// ---------------------------------------------------------------------------

/// Split `text` into a sequence of script-homogeneous [`Token`] spans.
///
/// Each span groups consecutive characters that share the same [`TokenKind`]
/// as determined by [`classify_char`]. Spans never overlap and their union
/// is exactly `text` — i.e. joining `token.text` values reconstructs the
/// original string.
///
/// The function is O(n) in the number of Unicode scalar values in `text`.
/// No allocation beyond the output `Vec` is performed.
///
/// # Returns
///
/// An empty `Vec` when `text` is empty.
///
/// # Example
///
/// ```rust
/// use kham_core::pre_tokenizer::pre_tokenize;
/// use kham_core::TokenKind;
///
/// // Mixed Thai / number / Thai
/// let tokens = pre_tokenize("ธนาคาร100แห่ง");
/// assert_eq!(tokens.len(), 3);
/// assert_eq!(tokens[0].text, "ธนาคาร");
/// assert_eq!(tokens[0].kind, TokenKind::Thai);
/// assert_eq!(tokens[1].text, "100");
/// assert_eq!(tokens[1].kind, TokenKind::Number);
/// assert_eq!(tokens[2].text, "แห่ง");
/// assert_eq!(tokens[2].kind, TokenKind::Thai);
/// ```
pub fn pre_tokenize(text: &str) -> Vec<Token<'_>> {
    if text.is_empty() {
        return Vec::new();
    }

    // Capacity hint: most real text averages > 3 bytes per token, so
    // `text.len() / 4` avoids most reallocations without over-allocating.
    let mut tokens: Vec<Token<'_>> = Vec::with_capacity(text.len() / 4 + 1);

    // `span_start`/`char_span_start` track the byte/char offset where the
    // current span began. `span_kind` is `None` only before the first char.
    let mut span_start = 0usize;
    let mut char_span_start = 0usize;
    let mut span_kind: Option<TokenKind> = None;
    let mut char_pos = 0usize;

    for (byte_pos, c) in text.char_indices() {
        let kind = classify_char(c);

        match span_kind {
            // No span open yet — start the first one.
            None => {
                span_start = byte_pos;
                char_span_start = char_pos;
                span_kind = Some(kind);
            }

            // Same kind as the running span — extend it silently.
            Some(k) if k == kind => {}

            // Different kind — flush the completed span and open a new one.
            Some(k) => {
                push_token(
                    &mut tokens,
                    text,
                    span_start,
                    byte_pos,
                    char_span_start,
                    char_pos,
                    k,
                );
                span_start = byte_pos;
                char_span_start = char_pos;
                span_kind = Some(kind);
            }
        }

        char_pos += 1;
    }

    // Flush the final span (always non-empty because text is non-empty).
    if let Some(k) = span_kind {
        push_token(
            &mut tokens,
            text,
            span_start,
            text.len(),
            char_span_start,
            char_pos,
            k,
        );
    }

    tokens
}

/// Construct a [`Token`] from byte and char ranges of `text` and push it onto `out`.
#[inline]
fn push_token<'t>(
    out: &mut Vec<Token<'t>>,
    text: &'t str,
    start: usize,
    end: usize,
    char_start: usize,
    char_end: usize,
    kind: TokenKind,
) {
    out.push(Token::new(
        &text[start..end],
        start..end,
        char_start..char_end,
        kind,
    ));
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::string::{String, ToString};

    // ── helpers ──────────────────────────────────────────────────────────────

    /// Assert that `pre_tokenize(text)` produces tokens with the given
    /// `(text, kind)` pairs, in order.
    fn assert_tokens(text: &str, expected: &[(&str, TokenKind)]) {
        let tokens = pre_tokenize(text);
        assert_eq!(
            tokens.len(),
            expected.len(),
            "token count mismatch for {text:?}\ngot: {tokens:?}"
        );
        for (i, (tok, &(exp_text, exp_kind))) in tokens.iter().zip(expected.iter()).enumerate() {
            assert_eq!(tok.text, exp_text, "token[{i}].text");
            assert_eq!(tok.kind, exp_kind, "token[{i}].kind");
        }
    }

    // ── edge cases ───────────────────────────────────────────────────────────

    #[test]
    fn empty_input_returns_empty_vec() {
        assert!(pre_tokenize("").is_empty());
    }

    #[test]
    fn single_char_each_kind() {
        assert_tokens("", &[("", TokenKind::Thai)]);
        assert_tokens("A", &[("A", TokenKind::Latin)]);
        assert_tokens("1", &[("1", TokenKind::Number)]);
        assert_tokens(" ", &[(" ", TokenKind::Whitespace)]);
        assert_tokens("!", &[("!", TokenKind::Punctuation)]);
        assert_tokens("😀", &[("😀", TokenKind::Emoji)]);
    }

    // ── Thai ─────────────────────────────────────────────────────────────────

    #[test]
    fn thai_run_stays_one_span() {
        assert_tokens("สวัสดี", &[("สวัสดี", TokenKind::Thai)]);
    }

    #[test]
    fn thai_digits_split_from_thai_script() {
        // Thai digits ๑๒๓ are Number, not Thai.
        assert_tokens("ก๑", &[("", TokenKind::Thai), ("", TokenKind::Number)]);
    }

    #[test]
    fn thai_digits_grouped_as_number() {
        assert_tokens("๑๒๓", &[("๑๒๓", TokenKind::Number)]);
    }

    // ── Latin ─────────────────────────────────────────────────────────────────

    #[test]
    fn latin_run_stays_one_span() {
        assert_tokens("hello", &[("hello", TokenKind::Latin)]);
    }

    #[test]
    fn latin_case_mixed_stays_one_span() {
        assert_tokens("Hello", &[("Hello", TokenKind::Latin)]);
    }

    #[test]
    fn fullwidth_latin_classified_as_latin() {
        // A = U+FF21, a = U+FF41
        assert_tokens("Aa", &[("Aa", TokenKind::Latin)]);
    }

    // ── Number ───────────────────────────────────────────────────────────────

    #[test]
    fn ascii_digits_grouped() {
        assert_tokens("100", &[("100", TokenKind::Number)]);
    }

    #[test]
    fn fullwidth_digits_classified_as_number() {
        // 0 = U+FF10
        assert_tokens("123", &[("123", TokenKind::Number)]);
    }

    // ── Whitespace ────────────────────────────────────────────────────────────

    #[test]
    fn space_tab_newline_grouped() {
        assert_tokens(" \t\n", &[(" \t\n", TokenKind::Whitespace)]);
    }

    #[test]
    fn nbsp_classified_as_whitespace() {
        // U+00A0 non-breaking space
        let nbsp = "\u{00A0}";
        assert_tokens(nbsp, &[(nbsp, TokenKind::Whitespace)]);
    }

    #[test]
    fn ideographic_space_classified_as_whitespace() {
        // U+3000 ideographic space
        let is = "\u{3000}";
        assert_tokens(is, &[(is, TokenKind::Whitespace)]);
    }

    // ── Punctuation ───────────────────────────────────────────────────────────

    #[test]
    fn ascii_punctuation_classified() {
        for ch in "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~".chars() {
            let s = ch.to_string();
            let tokens = pre_tokenize(&s);
            assert_eq!(tokens.len(), 1, "expected 1 token for {ch:?}");
            assert_eq!(
                tokens[0].kind,
                TokenKind::Punctuation,
                "wrong kind for {ch:?}"
            );
        }
    }

    #[test]
    fn unicode_punctuation_em_dash() {
        // U+2014 EM DASH is in the General Punctuation block.
        assert_tokens("", &[("", TokenKind::Punctuation)]);
    }

    #[test]
    fn unicode_punctuation_ellipsis() {
        assert_tokens("", &[("", TokenKind::Punctuation)]);
    }

    // ── Emoji ─────────────────────────────────────────────────────────────────

    #[test]
    fn basic_emoji_span() {
        assert_tokens("😀", &[("😀", TokenKind::Emoji)]);
    }

    #[test]
    fn emoji_run_stays_one_span() {
        assert_tokens("😀🎉", &[("😀🎉", TokenKind::Emoji)]);
    }

    #[test]
    fn misc_symbol_emoji() {
        // U+2764 ❤ is in the Miscellaneous Symbols block.
        assert_tokens("", &[("", TokenKind::Emoji)]);
    }

    // ── Mixed script ──────────────────────────────────────────────────────────

    #[test]
    fn bank_example() {
        // Classic mixed-script Thai example from CLAUDE.md.
        assert_tokens(
            "ธนาคาร100แห่ง",
            &[
                ("ธนาคาร", TokenKind::Thai),
                ("100", TokenKind::Number),
                ("แห่ง", TokenKind::Thai),
            ],
        );
    }

    #[test]
    fn thai_space_latin() {
        assert_tokens(
            "สวัสดี hello",
            &[
                ("สวัสดี", TokenKind::Thai),
                (" ", TokenKind::Whitespace),
                ("hello", TokenKind::Latin),
            ],
        );
    }

    #[test]
    fn latin_number_thai() {
        assert_tokens(
            "hello123สวัสดี",
            &[
                ("hello", TokenKind::Latin),
                ("123", TokenKind::Number),
                ("สวัสดี", TokenKind::Thai),
            ],
        );
    }

    #[test]
    fn all_kinds_in_sequence() {
        assert_tokens(
            "กิน 1 A!😀",
            &[
                ("กิน", TokenKind::Thai),
                (" ", TokenKind::Whitespace),
                ("1", TokenKind::Number),
                (" ", TokenKind::Whitespace),
                ("A", TokenKind::Latin),
                ("!", TokenKind::Punctuation),
                ("😀", TokenKind::Emoji),
            ],
        );
    }

    // ── Structural invariants ─────────────────────────────────────────────────

    #[test]
    fn spans_cover_full_input() {
        // Joining all token texts must reconstruct the original string exactly.
        let inputs = [
            "ธนาคาร100แห่ง",
            "hello world",
            "สวัสดี 😀 123!",
            "กิน\tข้าว\n",
            "",
        ];
        for input in inputs {
            let rebuilt: String = pre_tokenize(input).iter().map(|t| t.text).collect();
            assert_eq!(rebuilt, input, "coverage failed for {input:?}");
        }
    }

    #[test]
    fn span_byte_offsets_are_correct() {
        // Every span's byte range must match the string it refers to.
        let text = "ธนาคาร100แห่ง";
        for tok in pre_tokenize(text) {
            assert_eq!(
                &text[tok.span.clone()],
                tok.text,
                "span mismatch: {:?}",
                tok
            );
            assert!(
                text.is_char_boundary(tok.span.start),
                "span.start is not a char boundary"
            );
            assert!(
                text.is_char_boundary(tok.span.end),
                "span.end is not a char boundary"
            );
        }
    }

    #[test]
    fn no_empty_tokens() {
        // The pre-tokenizer must never emit a zero-length token.
        let text = "กิน hello 123";
        for tok in pre_tokenize(text) {
            assert!(!tok.text.is_empty(), "empty token: {tok:?}");
        }
    }

    #[test]
    fn adjacent_spans_are_contiguous() {
        // The end of span[i] must equal the start of span[i+1].
        let text = "กิน hello 123!😀";
        let tokens = pre_tokenize(text);
        for pair in tokens.windows(2) {
            assert_eq!(
                pair[0].span.end, pair[1].span.start,
                "gap between {:?} and {:?}",
                pair[0], pair[1]
            );
        }
    }

    #[test]
    fn char_spans_are_contiguous() {
        let text = "กิน hello 123!😀";
        let tokens = pre_tokenize(text);
        for pair in tokens.windows(2) {
            assert_eq!(
                pair[0].char_span.end, pair[1].char_span.start,
                "char_span gap between {:?} and {:?}",
                pair[0].text, pair[1].text
            );
        }
    }

    #[test]
    fn char_span_len_matches_char_count() {
        let text = "ธนาคาร100แห่ง";
        for tok in pre_tokenize(text) {
            assert_eq!(
                tok.char_span.end - tok.char_span.start,
                tok.text.chars().count(),
                "char_span mismatch for {:?}",
                tok.text
            );
        }
    }

    #[test]
    fn char_span_mixed_script_offsets() {
        // "ธนาคาร100แห่ง": ธนาคาร=6 chars, 100=3 chars, แห่ง=4 chars
        let tokens = pre_tokenize("ธนาคาร100แห่ง");
        assert_eq!(tokens[0].char_span, 0..6);
        assert_eq!(tokens[1].char_span, 6..9);
        assert_eq!(tokens[2].char_span, 9..13);
    }

    #[test]
    fn char_span_emoji_counts_as_one_char() {
        // 😀 is 4 bytes but 1 Unicode scalar value.
        let tokens = pre_tokenize("😀");
        assert_eq!(tokens[0].char_span, 0..1);
        assert_eq!(tokens[0].span, 0..4);
    }

    // ── classify_char direct tests ────────────────────────────────────────────

    #[test]
    fn classify_char_spot_checks() {
        assert_eq!(classify_char(''), TokenKind::Thai);
        assert_eq!(classify_char(''), TokenKind::Number); // Thai digit
        assert_eq!(classify_char('a'), TokenKind::Latin);
        assert_eq!(classify_char('Z'), TokenKind::Latin);
        assert_eq!(classify_char('5'), TokenKind::Number);
        assert_eq!(classify_char(' '), TokenKind::Whitespace);
        assert_eq!(classify_char('\n'), TokenKind::Whitespace);
        assert_eq!(classify_char('!'), TokenKind::Punctuation);
        assert_eq!(classify_char('.'), TokenKind::Punctuation);
        assert_eq!(classify_char('😀'), TokenKind::Emoji);
        assert_eq!(classify_char(''), TokenKind::Emoji);
    }
}