sakurs-core 0.1.2

High-performance sentence boundary detection using Delta-Stack Monoid algorithm
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
//! Text parsing implementation for sentence boundary detection
//!
//! This module implements the character-by-character parsing logic in the
//! application layer, converting text into partial states for the domain layer.

use crate::domain::{
    enclosure::{EnclosureRules, StandardEnclosureRules},
    language::{BoundaryContext, BoundaryDecision, LanguageRules},
    types::{AbbreviationState, BoundaryFlags, DeltaEntry, DepthVec, PartialState},
};

mod strategies;

#[cfg(test)]
mod tests;

pub use strategies::{
    ParseError, ParseStrategy, ParsingInput, ParsingOutput, SequentialParser, StreamingParser,
};

/// Default context window size for boundary detection
const DEFAULT_CONTEXT_WINDOW: usize = 10;

/// Parser configuration options.
pub struct ParserConfig {
    /// Rules for handling enclosures (quotes, parentheses, etc.)
    pub enclosure_rules: Box<dyn EnclosureRules>,
}

impl Default for ParserConfig {
    fn default() -> Self {
        Self {
            enclosure_rules: Box::new(StandardEnclosureRules::new()),
        }
    }
}

/// Text parser for converting text into partial states
///
/// This parser lives in the application layer and orchestrates
/// the parsing process using different strategies.
pub struct TextParser {
    #[allow(dead_code)]
    config: ParserConfig,
}

impl TextParser {
    /// Creates a new parser with default configuration.
    pub fn new() -> Self {
        Self {
            config: ParserConfig::default(),
        }
    }

    /// Creates a new parser with custom configuration.
    pub fn with_config(config: ParserConfig) -> Self {
        Self { config }
    }

    /// Scans a chunk of text and produces a partial state with boundary candidates.
    ///
    /// Assumes the chunk starts at the beginning of a line; use
    /// [`Self::scan_chunk_with_context`] when the chunk starts mid-line.
    pub fn scan_chunk(&self, text: &str, language_rules: &dyn LanguageRules) -> PartialState {
        self.scan_chunk_with_context(text, language_rules, 0)
    }

    /// Scans a chunk of text and produces a partial state with boundary candidates.
    ///
    /// This implements the scan phase of the Δ-Stack Monoid algorithm.
    /// It tracks local enclosure depth and records boundary candidates without
    /// determining if they are valid (that happens in the reduce phase).
    ///
    /// `line_offset_at_start` is the number of characters between the last
    /// newline in the full text and the start of this chunk. Without it, a
    /// chunk starting mid-line would look like a line start to suppression
    /// rules (e.g. the list-item rule for `)`), silently corrupting enclosure
    /// tracking for the rest of the text. Values above the rules' threshold
    /// may be capped by the caller.
    pub fn scan_chunk_with_context(
        &self,
        text: &str,
        language_rules: &dyn LanguageRules,
        line_offset_at_start: usize,
    ) -> PartialState {
        // Initialize state with the number of enclosure types from language rules
        let enclosure_count = language_rules.enclosure_type_count();
        let mut state = PartialState::new(enclosure_count);

        // Local depth tracking (relative to chunk start)
        let mut local_depths = vec![0i32; enclosure_count];
        let mut min_depths = vec![0i32; enclosure_count];

        let mut position = 0;
        let mut chars = text.chars().peekable();

        // Track parsing context
        let mut last_char: Option<char> = None;
        let mut consecutive_dots = 0;
        let mut first_word_captured = false;

        while let Some(ch) = chars.next() {
            let char_len = ch.len_utf8();

            // Capture first word of chunk if not yet captured
            if !first_word_captured && ch.is_alphabetic() {
                // Found the start of the first word
                let mut word = String::new();
                word.push(ch);

                // Collect the rest of the word
                while let Some(&next_ch) = chars.peek() {
                    if next_ch.is_alphabetic() {
                        word.push(next_ch);
                        chars.next();
                        position += next_ch.len_utf8();
                    } else {
                        break;
                    }
                }

                state.abbreviation.first_word = Some(word);
                first_word_captured = true;

                // Don't skip the rest of the processing for this character
                // Fall through to normal character processing
            }

            // Check for enclosure characters using language rules
            if let Some(enc_char) = language_rules.get_enclosure_char(ch) {
                // Check if this enclosure should be suppressed
                let should_suppress =
                    if let Some(suppressor) = language_rules.enclosure_suppressor() {
                        // Build context for suppression check
                        let context = build_enclosure_context(
                            text,
                            position,
                            ch,
                            &chars,
                            last_char,
                            line_offset_at_start,
                        );
                        suppressor.should_suppress_enclosure(ch, &context)
                    } else {
                        false
                    };

                // Only track enclosure if not suppressed
                if !should_suppress {
                    if let Some(type_id) = language_rules.get_enclosure_type_id(ch) {
                        if type_id < enclosure_count {
                            if enc_char.is_symmetric {
                                // Symmetric enclosures are tracked as a parity
                                // count: every occurrence adds one, and
                                // "outside" means an even cumulative count.
                                // Deciding open vs. close from the chunk-local
                                // depth would be wrong whenever a chunk starts
                                // inside the enclosure (a chunk with an odd
                                // number of quotes would always report net +1,
                                // permanently corrupting the cumulative
                                // depth), so the decision is deferred to the
                                // reduce phase, which checks parity for these
                                // types.
                                local_depths[type_id] += 1;
                            } else {
                                // For asymmetric enclosures: use is_opening flag
                                if enc_char.is_opening {
                                    local_depths[type_id] += 1;
                                } else {
                                    local_depths[type_id] -= 1;
                                    min_depths[type_id] =
                                        min_depths[type_id].min(local_depths[type_id]);
                                }
                            }
                        }
                    }
                }
            }

            // Track consecutive dots for abbreviations
            if ch == '.' {
                consecutive_dots += 1;
            } else {
                consecutive_dots = 0;
            }

            // Check for potential sentence terminators (record as candidate regardless of depth)
            if language_rules.is_potential_terminator(ch) {
                // Build context for language rules
                let context =
                    build_boundary_context(text, position, ch, &chars, last_char, consecutive_dots);

                // Ask language rules for decision
                let decision = language_rules.detect_sentence_boundary(&context);

                match decision {
                    BoundaryDecision::Boundary(flags) => {
                        // Record as boundary candidate with current local depths
                        state.add_boundary_candidate(
                            position + char_len,
                            DepthVec::from_vec(local_depths.clone()),
                            flags,
                        );

                        // Track abbreviation state
                        if ch == '.' {
                            let abbr_result = language_rules.process_abbreviation(text, position);
                            if abbr_result.is_abbreviation {
                                state.abbreviation = AbbreviationState::with_first_word(
                                    true, // dangling_dot
                                    context
                                        .following_context
                                        .chars()
                                        .next()
                                        .is_some_and(|c| c.is_alphabetic()), // head_alpha
                                    state.abbreviation.first_word.clone(), // preserve first_word
                                );
                            }
                        }
                    }
                    BoundaryDecision::NotBoundary => {
                        // No action needed
                    }
                    BoundaryDecision::NeedsMoreContext => {
                        // Record as weak boundary candidate
                        state.add_boundary_candidate(
                            position + char_len,
                            DepthVec::from_vec(local_depths.clone()),
                            BoundaryFlags::WEAK,
                        );
                    }
                }
            }

            last_char = Some(ch);
            position += char_len;
        }

        // Update final chunk length
        state.chunk_length = position;

        // Calculate final deltas
        for i in 0..enclosure_count {
            state.deltas[i] = DeltaEntry {
                net: local_depths[i],
                min: min_depths[i],
            };
        }

        // Note: unclosed enclosures are handled through the delta representation

        state
    }
}

impl Default for TextParser {
    fn default() -> Self {
        Self::new()
    }
}

/// Builds a boundary context for language rule evaluation.
fn build_boundary_context(
    text: &str,
    position: usize,
    terminator: char,
    chars_iter: &std::iter::Peekable<std::str::Chars>,
    _last_char: Option<char>,
    _consecutive_dots: usize,
) -> BoundaryContext {
    // Extract text before the boundary (up to DEFAULT_CONTEXT_WINDOW chars)
    // Need to find valid UTF-8 boundary
    let mut start = position.saturating_sub(DEFAULT_CONTEXT_WINDOW);
    while start > 0 && !text.is_char_boundary(start) {
        start -= 1;
    }
    let preceding_context = text[start..position].to_string();

    // Peek at upcoming characters (up to DEFAULT_CONTEXT_WINDOW chars)
    let following_context = chars_iter
        .clone()
        .take(DEFAULT_CONTEXT_WINDOW)
        .collect::<String>();

    BoundaryContext {
        text: text.to_string(),
        position,
        boundary_char: terminator,
        preceding_context,
        following_context,
    }
}

/// Builds an enclosure context for suppression evaluation.
fn build_enclosure_context<'a>(
    text: &'a str,
    position: usize,
    _ch: char,
    chars_iter: &std::iter::Peekable<std::str::Chars>,
    last_char: Option<char>,
    line_offset_at_start: usize,
) -> crate::domain::enclosure_suppressor::EnclosureContext<'a> {
    use smallvec::SmallVec;

    // Get preceding characters (up to 3)
    let mut preceding_chars = SmallVec::<[char; 3]>::new();

    // Add last_char if available
    if let Some(ch) = last_char {
        preceding_chars.push(ch);
    }

    // Try to get more preceding characters from text, walking backwards only
    // as far as needed (this runs for every enclosure character, so it must
    // not scan back to the chunk start).
    if position > 0 {
        // Skip the first char if we already have it from last_char
        let skip = if last_char.is_some() { 1 } else { 0 };

        for ch in text[..position]
            .chars()
            .rev()
            .skip(skip)
            .take(3 - preceding_chars.len())
        {
            preceding_chars.insert(0, ch);
        }
    }

    // Get following characters (up to 3)
    let following_chars: SmallVec<[char; 3]> = chars_iter.clone().take(3).collect();

    // Calculate line offset (simple approximation). If no newline exists in
    // this chunk before the position, the line continues from the previous
    // chunk, so add the carried-in offset at chunk start. Consumers only
    // compare the offset against a small threshold (currently 10), so the
    // backward scan stops once the value is decidable instead of walking to
    // the chunk start.
    const LINE_OFFSET_DECIDABLE: usize = 11;
    let mut line_offset = 0;
    let mut saw_newline = false;
    for c in text[..position].chars().rev() {
        if c == '\n' {
            saw_newline = true;
            break;
        }
        line_offset += 1;
        if line_offset >= LINE_OFFSET_DECIDABLE {
            break;
        }
    }
    if !saw_newline && line_offset < LINE_OFFSET_DECIDABLE {
        line_offset += line_offset_at_start;
    }

    crate::domain::enclosure_suppressor::EnclosureContext {
        position,
        preceding_chars,
        following_chars,
        line_offset,
        chunk_text: text,
    }
}

/// Convenient function for scanning a chunk of text with default settings.
pub fn scan_chunk(text: &str, language_rules: &dyn LanguageRules) -> PartialState {
    let parser = TextParser::new();
    parser.scan_chunk(text, language_rules)
}

#[cfg(test)]
mod parser_tests {
    use super::*;
    use crate::domain::language::MockLanguageRules;

    #[test]
    fn test_basic_parsing() {
        let parser = TextParser::new();
        let rules = MockLanguageRules::english();

        let text = "Hello world. This is a test.";
        let state = parser.scan_chunk(text, &rules);

        // Should have detected two boundary candidates
        assert_eq!(state.boundary_candidates.len(), 2);
        assert_eq!(state.chunk_length, text.len());
    }

    #[test]
    fn test_enclosure_handling() {
        let parser = TextParser::new();
        let rules = MockLanguageRules::english();

        let text = "He said (Hello. World). Done.";
        let state = parser.scan_chunk(text, &rules);

        // Should record all boundary candidates (even inside parentheses)
        // The reduce phase will determine which are valid
        assert!(state
            .boundary_candidates
            .iter()
            .any(|b| b.local_offset == 23)); // After ).
        assert!(state
            .boundary_candidates
            .iter()
            .any(|b| b.local_offset == 29)); // After Done.
    }

    #[test]
    fn test_delta_stack_building() {
        let parser = TextParser::new();
        let rules = MockLanguageRules::english();

        let text = "Test (nested) text.";
        let state = parser.scan_chunk(text, &rules);

        // Should have delta entries for the parentheses
        assert!(!state.deltas.is_empty());

        // Verify the delta has correct net values (should be balanced)
        let total_net: i32 = state.deltas.iter().map(|d| d.net).sum();
        assert_eq!(total_net, 0); // Balanced parentheses
    }

    #[test]
    fn test_abbreviation_detection() {
        let parser = TextParser::new();
        let rules = MockLanguageRules::english();

        let text = "Dr. Smith arrived.";
        let state = parser.scan_chunk(text, &rules);

        // The parser detects all potential boundaries (periods)
        // Both "Dr." and "arrived." create boundary candidates
        assert_eq!(state.boundary_candidates.len(), 2);
        assert!(state
            .boundary_candidates
            .iter()
            .any(|b| b.local_offset == 18)); // After "arrived."
    }

    #[test]
    fn test_quotation_handling() {
        let parser = TextParser::new();
        let rules = MockLanguageRules::english();

        let text = r#"She said "Hello." Then left."#;
        let state = parser.scan_chunk(text, &rules);

        // Should have delta tracking for quotes
        assert!(!state.deltas.is_empty());
        // With the original quote handling logic
        // Note: The specific delta values depend on the quote direction logic
        // Parsing should complete successfully
        assert_eq!(state.chunk_length, text.len());
    }

    #[test]
    fn test_integration_with_english_rules() {
        use crate::domain::language::ConfigurableLanguageRules;

        let parser = TextParser::new();
        let rules = ConfigurableLanguageRules::from_code("en").unwrap();

        // Complex text with abbreviations, numbers, and nested punctuation
        let text = "Dr. Smith (born 1965) earned his Ph.D. He works at Tech Corp. The company is valued at $2.5 billion! Amazing.";
        let state = parser.scan_chunk(text, &rules);

        // Should handle abbreviations properly - no boundary candidates after "Dr." or "Ph.D."
        let boundary_positions: Vec<usize> = state
            .boundary_candidates
            .iter()
            .map(|b| b.local_offset)
            .collect();

        // With use_uppercase_fallback=false, "Smith" is NOT a sentence starter
        // So NO boundary should be created after "Dr."
        assert!(!boundary_positions.contains(&3)); // After "Dr." (followed by "Smith", not a sentence starter)
        assert!(!boundary_positions.contains(&95)); // After "$2.5" decimal

        // But SHOULD create boundaries after abbreviations followed by sentence starters
        let phd_pos = text.find("Ph.D.").unwrap() + 5; // Position after "Ph.D."
        let corp_pos = text.find("Corp.").unwrap() + 5; // Position after "Corp."

        assert!(
            boundary_positions.contains(&phd_pos),
            "Expected boundary after 'Ph.D.' at position {}",
            phd_pos
        );
        assert!(
            boundary_positions.contains(&corp_pos),
            "Expected boundary after 'Corp.' at position {}",
            corp_pos
        );

        // Should create boundary candidates after real sentence endings:
        // - After "Ph.D." when followed by "He" (sentence starter)
        // - After "Corp." when followed by "The" (sentence starter)
        // - After "billion!" (exclamation mark)
        // - After "Amazing." (period at end)
        // Note: With proper multi-period abbreviation handling, Ph.D. creates only one boundary
        assert_eq!(boundary_positions.len(), 4);
    }

    #[test]
    fn test_complex_enclosure_nesting() {
        let parser = TextParser::new();
        let rules = MockLanguageRules::english();

        // Text with nested enclosures and a sentence boundary outside them
        let text = r#"He said (and I quote: "Hello world.") to everyone. That was nice."#;
        let state = parser.scan_chunk(text, &rules);

        // Should track enclosures
        assert!(!state.deltas.is_empty());
        // Should handle parsing successfully
        assert_eq!(state.chunk_length, text.len());

        // Should handle parsing without errors - this is the main goal
        // The boundary detection depends on language rules implementation

        // For now, just check that parsing completed successfully
        // The specific boundary detection behavior depends on the language rules implementation
        assert_eq!(state.chunk_length, text.len());
    }

    #[test]
    fn test_parser_determinism() {
        let parser = TextParser::new();
        let rules = MockLanguageRules::english();

        let text = "First sentence. Second sentence! Third sentence?";

        // Parse the same text multiple times
        let state1 = parser.scan_chunk(text, &rules);
        let state2 = parser.scan_chunk(text, &rules);
        let state3 = parser.scan_chunk(text, &rules);

        // Results should be identical
        assert_eq!(state1.boundary_candidates, state2.boundary_candidates);
        assert_eq!(state2.boundary_candidates, state3.boundary_candidates);
        assert_eq!(state1.deltas, state2.deltas);
        assert_eq!(state1.chunk_length, state2.chunk_length);
    }

    #[test]
    fn test_empty_and_edge_cases() {
        let parser = TextParser::new();
        let rules = MockLanguageRules::english();

        // Empty text
        let state = parser.scan_chunk("", &rules);
        assert!(state.boundary_candidates.is_empty());
        assert_eq!(state.chunk_length, 0);

        // Single character
        let state = parser.scan_chunk(".", &rules);
        assert_eq!(state.chunk_length, 1);

        // Only spaces
        let state = parser.scan_chunk("   ", &rules);
        assert!(state.boundary_candidates.is_empty());
        assert_eq!(state.chunk_length, 3);

        // Unclosed quote
        let text = r#"He said "Hello world."#;
        let state = parser.scan_chunk(text, &rules);
        // Should handle gracefully without panicking
        assert_eq!(state.chunk_length, text.len());
    }
}