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
//! Tests for parsing strategies

use super::*;
use crate::domain::language::{
    AbbreviationResult, BoundaryContext, BoundaryDecision, LanguageRules, QuotationContext,
    QuotationDecision,
};
use crate::domain::types::BoundaryFlags;
use crate::domain::EnclosureChar;

/// Mock language rules for testing
struct MockParserRules {
    /// Whether to always return boundary
    always_boundary: bool,
    /// Whether to return needs more context
    needs_context: bool,
}

impl MockParserRules {
    fn new() -> Self {
        Self {
            always_boundary: true,
            needs_context: false,
        }
    }

    fn with_needs_context() -> Self {
        Self {
            always_boundary: false,
            needs_context: true,
        }
    }

    fn never_boundary() -> Self {
        Self {
            always_boundary: false,
            needs_context: false,
        }
    }
}

impl LanguageRules for MockParserRules {
    fn detect_sentence_boundary(&self, _context: &BoundaryContext) -> BoundaryDecision {
        if self.needs_context {
            BoundaryDecision::NeedsMoreContext
        } else if self.always_boundary {
            BoundaryDecision::Boundary(BoundaryFlags::STRONG)
        } else {
            BoundaryDecision::NotBoundary
        }
    }

    fn process_abbreviation(&self, _text: &str, _position: usize) -> AbbreviationResult {
        AbbreviationResult {
            is_abbreviation: false,
            confidence: 0.0,
            length: 0,
        }
    }

    fn handle_quotation(&self, _context: &QuotationContext) -> QuotationDecision {
        QuotationDecision::Regular
    }

    fn get_enclosure_char(&self, _ch: char) -> Option<EnclosureChar> {
        None
    }

    fn get_enclosure_type_id(&self, _ch: char) -> Option<usize> {
        None
    }

    fn enclosure_type_count(&self) -> usize {
        1 // Minimal for testing
    }

    fn language_code(&self) -> &str {
        "test"
    }

    fn language_name(&self) -> &str {
        "Test Language"
    }
}

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

    #[test]
    fn test_parse_error_variants() {
        // Test all error variants can be created
        let errors = vec![
            ParseError::InvalidUtf8,
            ParseError::EmptyInput,
            ParseError::ParsingFailed("Test failure".to_string()),
        ];

        for error in &errors {
            // Test Display trait
            let display = format!("{}", error);
            assert!(!display.is_empty());

            // Test Debug trait
            let debug = format!("{:?}", error);
            assert!(!debug.is_empty());
        }
    }

    #[test]
    fn test_parse_error_messages() {
        // Test specific error messages
        let utf8_err = ParseError::InvalidUtf8;
        assert_eq!(format!("{}", utf8_err), "Invalid UTF-8 in input");

        let empty_err = ParseError::EmptyInput;
        assert_eq!(format!("{}", empty_err), "Empty input");

        let parsing_err = ParseError::ParsingFailed("Custom error".to_string());
        assert_eq!(format!("{}", parsing_err), "Parsing failed: Custom error");
    }
}

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

    #[test]
    fn test_sequential_parser_creation() {
        // Test default creation
        let parser = SequentialParser::new();
        assert_eq!(parser.optimal_chunk_size(), 65536);
        assert!(!parser.supports_streaming());

        // Test with custom chunk size
        let parser = SequentialParser::with_chunk_size(1024);
        assert_eq!(parser.optimal_chunk_size(), 1024);

        // Test Default trait
        let parser = SequentialParser::default();
        assert_eq!(parser.optimal_chunk_size(), 65536);
    }

    #[test]
    fn test_sequential_parse_text() {
        let parser = SequentialParser::new();
        let rules = MockParserRules::new();

        // Test with text input
        let input = ParsingInput::Text("Hello world. This is a test.");
        let result = parser.parse(input, &rules).unwrap();

        match result {
            ParsingOutput::State(state) => {
                // Should have parsed the text
                assert!(state.chunk_length > 0);
            }
            ParsingOutput::States(_) => panic!("Expected single state for text input"),
        }
    }

    #[test]
    fn test_sequential_parse_empty_text() {
        let parser = SequentialParser::new();
        let rules = MockParserRules::new();

        // Test with empty text
        let input = ParsingInput::Text("");
        let result = parser.parse(input, &rules);

        assert!(matches!(result, Err(ParseError::EmptyInput)));
    }

    #[test]
    fn test_sequential_parse_chunks() {
        let parser = SequentialParser::new();
        let rules = MockParserRules::new();

        // Test with chunk input
        let chunks = vec!["Hello world.", " This is", " a test."];
        let input = ParsingInput::Chunks(Box::new(chunks.into_iter()));
        let result = parser.parse(input, &rules).unwrap();

        match result {
            ParsingOutput::States(states) => {
                // Should have multiple states
                assert_eq!(states.len(), 3);
                // Each state should have content
                for state in &states {
                    assert!(state.chunk_length > 0);
                }
            }
            ParsingOutput::State(_) => panic!("Expected multiple states for chunk input"),
        }
    }

    #[test]
    fn test_sequential_parse_empty_chunks() {
        let parser = SequentialParser::new();
        let rules = MockParserRules::new();

        // Test with empty chunks
        let chunks: Vec<&str> = vec![];
        let input = ParsingInput::Chunks(Box::new(chunks.into_iter()));
        let result = parser.parse(input, &rules);

        assert!(matches!(result, Err(ParseError::EmptyInput)));
    }

    #[test]
    fn test_sequential_parse_mixed_chunks() {
        let parser = SequentialParser::new();
        let rules = MockParserRules::new();

        // Test with some empty chunks
        let chunks = vec!["Hello", "", "world", "", "."];
        let input = ParsingInput::Chunks(Box::new(chunks.into_iter()));
        let result = parser.parse(input, &rules).unwrap();

        match result {
            ParsingOutput::States(states) => {
                // Should skip empty chunks
                assert_eq!(states.len(), 3);
            }
            _ => panic!("Expected states output"),
        }
    }

    #[test]
    fn test_sequential_parser_trait_impl() {
        let parser = SequentialParser::new();

        // Test trait methods
        assert!(!parser.supports_streaming());
        assert_eq!(parser.optimal_chunk_size(), 65536);

        // Test as trait object
        let parser_ref: &dyn ParseStrategy = &parser;
        assert!(!parser_ref.supports_streaming());
        assert_eq!(parser_ref.optimal_chunk_size(), 65536);
    }
}

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

    #[test]
    fn test_streaming_parser_creation() {
        // Test default creation
        let parser = StreamingParser::new();
        assert_eq!(parser.optimal_chunk_size(), 1_048_576);
        assert!(parser.supports_streaming());

        // Test with custom buffer size
        let parser = StreamingParser::with_buffer_size(4096, 128);
        assert_eq!(parser.optimal_chunk_size(), 4096);

        // Test Default trait
        let parser = StreamingParser::default();
        assert_eq!(parser.optimal_chunk_size(), 1_048_576);
    }

    #[test]
    fn test_streaming_parse_text() {
        let parser = StreamingParser::new();
        let rules = MockParserRules::new();

        // Test with text input
        let input = ParsingInput::Text("Hello world. This is a test.");
        let result = parser.parse(input, &rules).unwrap();

        match result {
            ParsingOutput::State(state) => {
                // Should have parsed the text
                assert!(state.chunk_length > 0);
            }
            ParsingOutput::States(_) => panic!("Expected single state for text input"),
        }
    }

    #[test]
    fn test_streaming_parse_empty_text() {
        let parser = StreamingParser::new();
        let rules = MockParserRules::new();

        // Test with empty text
        let input = ParsingInput::Text("");
        let result = parser.parse(input, &rules);

        assert!(matches!(result, Err(ParseError::EmptyInput)));
    }

    #[test]
    fn test_streaming_parse_chunks_with_overlap() {
        let parser = StreamingParser::with_buffer_size(1024, 5);
        let rules = MockParserRules::new();

        // Test with chunks that should overlap
        let chunks = vec!["Hello world.", " This is", " a test."];
        let input = ParsingInput::Chunks(Box::new(chunks.into_iter()));
        let result = parser.parse(input, &rules).unwrap();

        match result {
            ParsingOutput::States(states) => {
                // Should have states for each chunk
                assert_eq!(states.len(), 3);
                // Each state should include overlap from previous
                // (except the first one)
                assert!(states[0].chunk_length > 0);
                assert!(states[1].chunk_length > " This is".len());
                assert!(states[2].chunk_length > " a test.".len());
            }
            _ => panic!("Expected states output"),
        }
    }

    #[test]
    fn test_streaming_parse_small_chunks() {
        let parser = StreamingParser::with_buffer_size(1024, 10);
        let rules = MockParserRules::new();

        // Test with chunks smaller than overlap size
        let chunks = vec!["Hi", ".", " ", "Bye", "."];
        let input = ParsingInput::Chunks(Box::new(chunks.into_iter()));
        let result = parser.parse(input, &rules).unwrap();

        match result {
            ParsingOutput::States(states) => {
                // Should handle small chunks correctly
                assert_eq!(states.len(), 5);
                // Small chunks should be preserved entirely as overlap
                for (i, state) in states.iter().enumerate() {
                    assert!(state.chunk_length > 0, "State {} has no content", i);
                }
            }
            _ => panic!("Expected states output"),
        }
    }

    #[test]
    fn test_streaming_parse_empty_chunks() {
        let parser = StreamingParser::new();
        let rules = MockParserRules::new();

        // Test with empty chunks
        let chunks: Vec<&str> = vec![];
        let input = ParsingInput::Chunks(Box::new(chunks.into_iter()));
        let result = parser.parse(input, &rules);

        assert!(matches!(result, Err(ParseError::EmptyInput)));
    }

    #[test]
    fn test_streaming_parser_trait_impl() {
        let parser = StreamingParser::new();

        // Test trait methods
        assert!(parser.supports_streaming());
        assert_eq!(parser.optimal_chunk_size(), 1_048_576);

        // Test as trait object
        let parser_ref: &dyn ParseStrategy = &parser;
        assert!(parser_ref.supports_streaming());
        assert_eq!(parser_ref.optimal_chunk_size(), 1_048_576);
    }

    #[test]
    fn test_streaming_overlap_behavior() {
        let parser = StreamingParser::with_buffer_size(1024, 3);
        let rules = MockParserRules::new();

        // Test overlap preservation
        let chunks = vec!["12345", "67890", "ABCDE"];
        let input = ParsingInput::Chunks(Box::new(chunks.into_iter()));
        let result = parser.parse(input, &rules).unwrap();

        match result {
            ParsingOutput::States(states) => {
                assert_eq!(states.len(), 3);

                // First chunk: just "12345"
                assert_eq!(states[0].chunk_length, 5);

                // Second chunk: "345" (overlap) + "67890" = 8 chars
                assert_eq!(states[1].chunk_length, 8);

                // Third chunk: "890" (overlap) + "ABCDE" = 8 chars
                assert_eq!(states[2].chunk_length, 8);
            }
            _ => panic!("Expected states output"),
        }
    }
}

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

    #[test]
    fn test_parse_strategy_as_trait_object() {
        // Test that both parsers can be used as trait objects
        let parsers: Vec<Box<dyn ParseStrategy>> = vec![
            Box::new(SequentialParser::new()),
            Box::new(StreamingParser::new()),
        ];

        let rules = MockParserRules::new();
        let text = "Test text.";

        for parser in &parsers {
            // Test parsing
            let input = ParsingInput::Text(text);
            let result = parser.parse(input, &rules);
            assert!(result.is_ok());

            // Test trait methods
            let _ = parser.supports_streaming();
            let _ = parser.optimal_chunk_size();
        }
    }

    #[test]
    fn test_different_rule_behaviors() {
        let parser = SequentialParser::new();

        // Test with always boundary rules
        let rules = MockParserRules::new();
        let input = ParsingInput::Text("Test. Text.");
        let result = parser.parse(input, &rules).unwrap();
        match result {
            ParsingOutput::State(state) => {
                // Should have boundary candidates
                assert!(!state.boundary_candidates.is_empty());
            }
            _ => panic!("Expected state output"),
        }

        // Test with never boundary rules
        let rules = MockParserRules::never_boundary();
        let input = ParsingInput::Text("Test. Text.");
        let result = parser.parse(input, &rules).unwrap();
        match result {
            ParsingOutput::State(state) => {
                // Should have no boundary candidates
                assert!(state.boundary_candidates.is_empty());
            }
            _ => panic!("Expected state output"),
        }

        // Test with needs context rules
        let rules = MockParserRules::with_needs_context();
        let input = ParsingInput::Text("Test. Text.");
        let result = parser.parse(input, &rules).unwrap();
        match result {
            ParsingOutput::State(state) => {
                // Should have weak boundary candidates
                assert!(!state.boundary_candidates.is_empty());
                for candidate in &state.boundary_candidates {
                    assert!(candidate.flags.contains(BoundaryFlags::WEAK));
                }
            }
            _ => panic!("Expected state output"),
        }
    }

    #[test]
    fn test_send_sync_constraints() {
        // Verify that parsers implement Send + Sync
        fn assert_send_sync<T: Send + Sync>() {}

        assert_send_sync::<SequentialParser>();
        assert_send_sync::<StreamingParser>();

        // Verify trait objects are Send + Sync
        assert_send_sync::<Box<dyn ParseStrategy>>();
    }

    #[test]
    fn test_parsing_unicode_text() {
        let parser = SequentialParser::new();
        let rules = MockParserRules::new();

        // Test with Unicode text
        let texts = vec![
            "Hello 世界。",
            "Привет мир!",
            "مرحبا بالعالم.",
            "🌍 Hello world! 🌎",
        ];

        for text in texts {
            let input = ParsingInput::Text(text);
            let result = parser.parse(input, &rules);
            assert!(result.is_ok(), "Failed to parse: {}", text);

            match result.unwrap() {
                ParsingOutput::State(state) => {
                    assert_eq!(state.chunk_length, text.len());
                }
                _ => panic!("Expected state output"),
            }
        }
    }

    #[test]
    fn test_chunk_iterator_handling() {
        let parser = StreamingParser::new();
        let rules = MockParserRules::new();

        // Test with custom iterator
        let data = vec!["Part1", "Part2", "Part3"];
        let chunks = data.into_iter().filter(|s| !s.is_empty());

        let input = ParsingInput::Chunks(Box::new(chunks));
        let result = parser.parse(input, &rules);
        assert!(result.is_ok());

        match result.unwrap() {
            ParsingOutput::States(states) => {
                assert_eq!(states.len(), 3);
            }
            _ => panic!("Expected states output"),
        }
    }
}