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
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
//! Domain traits for sentence boundary detection
//!
//! This module consolidates all pure domain traits that represent business logic
//! without any execution or infrastructure concerns.

use crate::domain::types::PartialState;

// ============================================================================
// Character Classification
// ============================================================================

/// Classification of characters for boundary detection
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CharacterClass {
    /// Sentence-ending punctuation
    SentenceTerminal,
    /// Opening delimiter (quote, parenthesis, etc.)
    DelimiterOpen,
    /// Closing delimiter
    DelimiterClose,
    /// Whitespace character
    Whitespace,
    /// Alphabetic character
    Alphabetic,
    /// Numeric character
    Numeric,
    /// Other punctuation
    OtherPunctuation,
    /// Other character type
    Other,
}

/// Pure character classification logic
pub trait CharacterClassifier: Send + Sync {
    /// Classify a character
    fn classify(&self, ch: char) -> CharacterClass;

    /// Check if character is a sentence terminal
    fn is_sentence_terminal(&self, ch: char) -> bool {
        matches!(self.classify(ch), CharacterClass::SentenceTerminal)
    }

    /// Check if character is an opening delimiter
    fn is_delimiter_open(&self, ch: char) -> bool {
        matches!(self.classify(ch), CharacterClass::DelimiterOpen)
    }

    /// Check if character is a closing delimiter
    fn is_delimiter_close(&self, ch: char) -> bool {
        matches!(self.classify(ch), CharacterClass::DelimiterClose)
    }

    /// Check if character is whitespace
    fn is_whitespace(&self, ch: char) -> bool {
        matches!(self.classify(ch), CharacterClass::Whitespace)
    }

    /// Get the matching closing delimiter for an opening delimiter
    fn get_matching_delimiter(&self, open: char) -> Option<char> {
        match open {
            '(' => Some(')'),
            '[' => Some(']'),
            '{' => Some('}'),
            '"' => Some('"'),
            '\'' => Some('\''),
            '' => Some(''),
            '' => Some(''),
            '' => Some(''),
            '' => Some(''),
            '' => Some(''),
            '' => Some(''),
            '' => Some(''),
            '' => Some(''),
            '' => Some(''),
            _ => None,
        }
    }
}

// ============================================================================
// Language-Specific Rules
// ============================================================================

/// Type of quotation mark
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum QuoteType {
    /// Single quote (')
    Single,
    /// Double quote (")
    Double,
    /// Japanese corner bracket (「」)
    JapaneseCorner,
    /// Japanese double corner bracket (『』)
    JapaneseDoubleCorner,
    /// Other quote type
    Other,
}

/// Behavior for quotes in sentence boundary detection
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum QuoteBehavior {
    /// Quote can contain sentence boundaries
    AllowBoundaries,
    /// Quote suppresses internal boundaries
    SuppressBoundaries,
    /// Context-dependent behavior
    Contextual,
}

/// Language-specific rules (pure logic)
pub trait LanguageSpecificRules: Send + Sync {
    /// Check if a word is an abbreviation
    fn is_abbreviation(&self, word: &str) -> bool;

    /// Get quote behavior for a quote type
    fn quote_behavior(&self, quote_type: QuoteType) -> QuoteBehavior;

    /// Check if a period after a word likely indicates abbreviation
    fn is_abbreviation_context(&self, word_before: &str, char_after: Option<char>) -> bool {
        // Default implementation
        if self.is_abbreviation(word_before) {
            // If known abbreviation, check what follows
            match char_after {
                Some(ch) if ch.is_uppercase() => false, // "Dr. Smith" - might be sentence boundary
                Some(ch) if ch.is_whitespace() => true, // Abbreviation with space
                _ => true,                              // Default to abbreviation
            }
        } else {
            false
        }
    }

    /// Check if sentence boundary should be suppressed based on following context
    fn should_suppress_boundary(&self, text_after: &str) -> bool {
        // Default: don't suppress if followed by uppercase or significant whitespace
        if let Some(first_non_ws) = text_after.chars().find(|c| !c.is_whitespace()) {
            !first_non_ws.is_uppercase()
        } else {
            true // Suppress if nothing follows
        }
    }

    /// Get language code
    fn language_code(&self) -> &str;
}

// ============================================================================
// Boundary Analysis
// ============================================================================

/// Context for boundary analysis
#[derive(Clone, Debug)]
pub struct BoundaryContext {
    /// Text content around the potential boundary
    pub text_before: String,
    pub text_after: String,
    /// Position in the original text
    pub position: usize,
    /// Character at the boundary position
    pub boundary_char: char,
    /// Current enclosure depth
    pub enclosure_depth: i32,
}

/// Represents a potential sentence boundary (trait version)
#[derive(Clone, Debug)]
pub struct BoundaryCandidateInfo {
    /// Position in the text
    pub position: usize,
    /// Confidence level (0.0 to 1.0)
    pub confidence: f32,
    /// Context at this position
    pub context: BoundaryContext,
    /// Type of boundary marker
    pub marker_type: BoundaryMarkerType,
}

/// Types of boundary markers
#[derive(Clone, Debug, PartialEq)]
pub enum BoundaryMarkerType {
    /// Period (.)
    Period,
    /// Question mark (?)
    Question,
    /// Exclamation mark (!)
    Exclamation,
    /// Other punctuation
    Other(char),
}

/// Decision about a boundary candidate
#[derive(Clone, Debug, PartialEq)]
pub enum BoundaryDecision {
    /// Confirmed sentence boundary
    Confirmed { confidence: f32 },
    /// Rejected as boundary
    Rejected { reason: RejectionReason },
    /// Needs more context to decide
    Pending,
}

/// Reasons for rejecting a boundary
#[derive(Clone, Debug, PartialEq)]
pub enum RejectionReason {
    /// Part of an abbreviation
    Abbreviation,
    /// Inside quotes or parentheses
    InsideEnclosure,
    /// Not followed by appropriate spacing/capitalization
    InvalidFollowing,
    /// Other language-specific reason
    LanguageSpecific(String),
}

/// Pure boundary detection logic
pub trait BoundaryAnalyzer: Send + Sync {
    /// Analyze a potential boundary position
    fn analyze_candidate(&self, context: &BoundaryContext) -> BoundaryCandidateInfo;

    /// Evaluate a boundary candidate with accumulated state
    fn evaluate_boundary(
        &self,
        candidate: &BoundaryCandidateInfo,
        state: &PartialState,
    ) -> BoundaryDecision;

    /// Check if a character could be a sentence boundary
    fn is_potential_boundary(&self, ch: char) -> bool {
        matches!(ch, '.' | '?' | '!' | '' | '' | '' | '' | '')
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    // ============================================================================
    // Mock Implementations for Testing
    // ============================================================================

    /// Mock implementation of CharacterClassifier for testing
    struct MockCharacterClassifier {
        custom_terminals: Vec<char>,
    }

    impl MockCharacterClassifier {
        fn new() -> Self {
            Self {
                custom_terminals: vec!['.', '?', '!', '', '', ''],
            }
        }

        fn with_custom_terminals(terminals: Vec<char>) -> Self {
            Self {
                custom_terminals: terminals,
            }
        }
    }

    impl CharacterClassifier for MockCharacterClassifier {
        fn classify(&self, ch: char) -> CharacterClass {
            if self.custom_terminals.contains(&ch) {
                CharacterClass::SentenceTerminal
            } else if matches!(
                ch,
                '(' | '['
                    | '{'
                    | '"'
                    | '\''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
            ) {
                CharacterClass::DelimiterOpen
            } else if matches!(
                ch,
                ')' | ']'
                    | '}'
                    | '"'
                    | '\''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
            ) {
                CharacterClass::DelimiterClose
            } else if ch.is_whitespace() {
                CharacterClass::Whitespace
            } else if ch.is_alphabetic() {
                CharacterClass::Alphabetic
            } else if ch.is_numeric() {
                CharacterClass::Numeric
            } else if ch.is_ascii_punctuation() {
                CharacterClass::OtherPunctuation
            } else {
                CharacterClass::Other
            }
        }
    }

    // ============================================================================
    // CharacterClass Tests
    // ============================================================================

    #[test]
    fn test_character_class_variants() {
        // Ensure all variants are distinct
        let variants = [
            CharacterClass::SentenceTerminal,
            CharacterClass::DelimiterOpen,
            CharacterClass::DelimiterClose,
            CharacterClass::Whitespace,
            CharacterClass::Alphabetic,
            CharacterClass::Numeric,
            CharacterClass::OtherPunctuation,
            CharacterClass::Other,
        ];

        // Test equality and distinctness
        for (i, v1) in variants.iter().enumerate() {
            for (j, v2) in variants.iter().enumerate() {
                if i == j {
                    assert_eq!(v1, v2);
                } else {
                    assert_ne!(v1, v2);
                }
            }
        }
    }

    #[test]
    fn test_character_class_debug() {
        // Ensure Debug is implemented and produces expected format
        assert_eq!(
            format!("{:?}", CharacterClass::SentenceTerminal),
            "SentenceTerminal"
        );
        assert_eq!(
            format!("{:?}", CharacterClass::DelimiterOpen),
            "DelimiterOpen"
        );
    }

    // ============================================================================
    // CharacterClassifier Tests
    // ============================================================================

    #[test]
    fn test_basic_classification() {
        let classifier = MockCharacterClassifier::new();

        // Test sentence terminals
        assert_eq!(classifier.classify('.'), CharacterClass::SentenceTerminal);
        assert_eq!(classifier.classify('?'), CharacterClass::SentenceTerminal);
        assert_eq!(classifier.classify('!'), CharacterClass::SentenceTerminal);

        // Test whitespace
        assert_eq!(classifier.classify(' '), CharacterClass::Whitespace);
        assert_eq!(classifier.classify('\t'), CharacterClass::Whitespace);
        assert_eq!(classifier.classify('\n'), CharacterClass::Whitespace);

        // Test alphabetic
        assert_eq!(classifier.classify('a'), CharacterClass::Alphabetic);
        assert_eq!(classifier.classify('Z'), CharacterClass::Alphabetic);

        // Test numeric
        assert_eq!(classifier.classify('0'), CharacterClass::Numeric);
        assert_eq!(classifier.classify('9'), CharacterClass::Numeric);

        // Test delimiters
        assert_eq!(classifier.classify('('), CharacterClass::DelimiterOpen);
        assert_eq!(classifier.classify(')'), CharacterClass::DelimiterClose);
        assert_eq!(classifier.classify('['), CharacterClass::DelimiterOpen);
        assert_eq!(classifier.classify(']'), CharacterClass::DelimiterClose);
    }

    #[test]
    fn test_japanese_character_classification() {
        let classifier = MockCharacterClassifier::new();

        // Japanese sentence terminals
        assert_eq!(classifier.classify(''), CharacterClass::SentenceTerminal);
        assert_eq!(classifier.classify(''), CharacterClass::SentenceTerminal);
        assert_eq!(classifier.classify(''), CharacterClass::SentenceTerminal);

        // Japanese brackets
        assert_eq!(classifier.classify(''), CharacterClass::DelimiterOpen);
        assert_eq!(classifier.classify(''), CharacterClass::DelimiterClose);
        assert_eq!(classifier.classify(''), CharacterClass::DelimiterOpen);
        assert_eq!(classifier.classify(''), CharacterClass::DelimiterClose);
        assert_eq!(classifier.classify(''), CharacterClass::DelimiterOpen);
        assert_eq!(classifier.classify(''), CharacterClass::DelimiterClose);

        // Japanese alphabetic (hiragana, katakana, kanji)
        assert_eq!(classifier.classify(''), CharacterClass::Alphabetic);
        assert_eq!(classifier.classify(''), CharacterClass::Alphabetic);
        assert_eq!(classifier.classify(''), CharacterClass::Alphabetic);
    }

    #[test]
    fn test_unicode_edge_cases() {
        let classifier = MockCharacterClassifier::new();

        // Zero-width characters
        assert_eq!(classifier.classify('\u{200B}'), CharacterClass::Other); // Zero-width space
        assert_eq!(classifier.classify('\u{200C}'), CharacterClass::Other); // Zero-width non-joiner
        assert_eq!(classifier.classify('\u{200D}'), CharacterClass::Other); // Zero-width joiner

        // Combining marks
        assert_eq!(classifier.classify('\u{0301}'), CharacterClass::Other); // Combining acute accent

        // Emojis
        assert_eq!(classifier.classify('😀'), CharacterClass::Other);
        assert_eq!(classifier.classify('🎉'), CharacterClass::Other);

        // Control characters
        assert_eq!(classifier.classify('\u{0000}'), CharacterClass::Other);
        assert_eq!(classifier.classify('\u{001F}'), CharacterClass::Other);
    }

    #[test]
    fn test_helper_methods_consistency() {
        let classifier = MockCharacterClassifier::new();

        // Test is_sentence_terminal
        assert!(classifier.is_sentence_terminal('.'));
        assert!(classifier.is_sentence_terminal('?'));
        assert!(classifier.is_sentence_terminal(''));
        assert!(!classifier.is_sentence_terminal('a'));
        assert!(!classifier.is_sentence_terminal(' '));

        // Test is_delimiter_open
        assert!(classifier.is_delimiter_open('('));
        assert!(classifier.is_delimiter_open(''));
        assert!(!classifier.is_delimiter_open(')'));
        assert!(!classifier.is_delimiter_open('a'));

        // Test is_delimiter_close
        assert!(classifier.is_delimiter_close(')'));
        assert!(classifier.is_delimiter_close(''));
        assert!(!classifier.is_delimiter_close('('));
        assert!(!classifier.is_delimiter_close('a'));

        // Test is_whitespace
        assert!(classifier.is_whitespace(' '));
        assert!(classifier.is_whitespace('\t'));
        assert!(classifier.is_whitespace('\n'));
        assert!(!classifier.is_whitespace('a'));
    }

    #[test]
    fn test_delimiter_matching() {
        let classifier = MockCharacterClassifier::new();

        // ASCII delimiters
        assert_eq!(classifier.get_matching_delimiter('('), Some(')'));
        assert_eq!(classifier.get_matching_delimiter('['), Some(']'));
        assert_eq!(classifier.get_matching_delimiter('{'), Some('}'));
        assert_eq!(classifier.get_matching_delimiter('"'), Some('"'));
        assert_eq!(classifier.get_matching_delimiter('\''), Some('\''));

        // Japanese delimiters
        assert_eq!(classifier.get_matching_delimiter(''), Some(''));
        assert_eq!(classifier.get_matching_delimiter(''), Some(''));
        assert_eq!(classifier.get_matching_delimiter(''), Some(''));
        assert_eq!(classifier.get_matching_delimiter(''), Some(''));
        assert_eq!(classifier.get_matching_delimiter(''), Some(''));

        // Non-delimiters
        assert_eq!(classifier.get_matching_delimiter('a'), None);
        assert_eq!(classifier.get_matching_delimiter('.'), None);
        assert_eq!(classifier.get_matching_delimiter(' '), None);
    }

    #[test]
    fn test_custom_terminals() {
        let classifier = MockCharacterClassifier::with_custom_terminals(vec!['§', '']);

        assert_eq!(classifier.classify('§'), CharacterClass::SentenceTerminal);
        assert_eq!(classifier.classify(''), CharacterClass::SentenceTerminal);
        assert_ne!(classifier.classify('.'), CharacterClass::SentenceTerminal);
    }

    // ============================================================================
    // QuoteType and QuoteBehavior Tests
    // ============================================================================

    #[test]
    fn test_quote_type_variants() {
        let variants = [
            QuoteType::Single,
            QuoteType::Double,
            QuoteType::JapaneseCorner,
            QuoteType::JapaneseDoubleCorner,
            QuoteType::Other,
        ];

        // Test distinctness
        for (i, v1) in variants.iter().enumerate() {
            for (j, v2) in variants.iter().enumerate() {
                if i == j {
                    assert_eq!(v1, v2);
                } else {
                    assert_ne!(v1, v2);
                }
            }
        }
    }

    #[test]
    fn test_quote_behavior_variants() {
        let variants = [
            QuoteBehavior::AllowBoundaries,
            QuoteBehavior::SuppressBoundaries,
            QuoteBehavior::Contextual,
        ];

        // Test distinctness
        for (i, v1) in variants.iter().enumerate() {
            for (j, v2) in variants.iter().enumerate() {
                if i == j {
                    assert_eq!(v1, v2);
                } else {
                    assert_ne!(v1, v2);
                }
            }
        }
    }

    // ============================================================================
    // LanguageSpecificRules Tests
    // ============================================================================

    struct MockLanguageRules {
        abbreviations: Vec<String>,
        language: String,
    }

    impl MockLanguageRules {
        fn new_english() -> Self {
            Self {
                abbreviations: vec![
                    "Dr".to_string(),
                    "Mr".to_string(),
                    "Mrs".to_string(),
                    "Ms".to_string(),
                    "Prof".to_string(),
                    "Inc".to_string(),
                    "Ltd".to_string(),
                ],
                language: "en".to_string(),
            }
        }

        fn new_japanese() -> Self {
            Self {
                abbreviations: vec![],
                language: "ja".to_string(),
            }
        }
    }

    impl LanguageSpecificRules for MockLanguageRules {
        fn is_abbreviation(&self, word: &str) -> bool {
            self.abbreviations.iter().any(|abbr| abbr == word)
        }

        fn quote_behavior(&self, quote_type: QuoteType) -> QuoteBehavior {
            match (self.language.as_str(), quote_type) {
                ("en", QuoteType::Double) => QuoteBehavior::AllowBoundaries,
                ("ja", QuoteType::JapaneseCorner) => QuoteBehavior::SuppressBoundaries,
                _ => QuoteBehavior::Contextual,
            }
        }

        fn language_code(&self) -> &str {
            &self.language
        }
    }

    #[test]
    fn test_abbreviation_detection() {
        let rules = MockLanguageRules::new_english();

        // Known abbreviations
        assert!(rules.is_abbreviation("Dr"));
        assert!(rules.is_abbreviation("Mr"));
        assert!(rules.is_abbreviation("Inc"));

        // Not abbreviations
        assert!(!rules.is_abbreviation("Doctor"));
        assert!(!rules.is_abbreviation("Mister"));
        assert!(!rules.is_abbreviation(""));

        // Case sensitivity
        assert!(!rules.is_abbreviation("dr")); // Mock is case-sensitive
        assert!(!rules.is_abbreviation("DR"));
    }

    #[test]
    fn test_abbreviation_context() {
        let rules = MockLanguageRules::new_english();

        // Known abbreviation followed by space
        assert!(rules.is_abbreviation_context("Dr", Some(' ')));

        // Known abbreviation followed by uppercase (potential sentence boundary)
        assert!(!rules.is_abbreviation_context("Dr", Some('S')));

        // Not an abbreviation
        assert!(!rules.is_abbreviation_context("Hello", Some(' ')));
        assert!(!rules.is_abbreviation_context("Hello", Some('W')));

        // Edge cases
        assert!(rules.is_abbreviation_context("Dr", None));
        assert!(!rules.is_abbreviation_context("", Some(' ')));
    }

    #[test]
    fn test_should_suppress_boundary() {
        let rules = MockLanguageRules::new_english();

        // Should not suppress if followed by uppercase
        assert!(!rules.should_suppress_boundary(" World"));
        assert!(!rules.should_suppress_boundary("  Hello"));

        // Should suppress if followed by lowercase
        assert!(rules.should_suppress_boundary(" world"));
        assert!(rules.should_suppress_boundary("  hello"));

        // Should suppress if nothing follows
        assert!(rules.should_suppress_boundary(""));
        assert!(rules.should_suppress_boundary("   "));
    }

    #[test]
    fn test_language_specific_quote_behavior() {
        let en_rules = MockLanguageRules::new_english();
        let ja_rules = MockLanguageRules::new_japanese();

        // English rules
        assert_eq!(
            en_rules.quote_behavior(QuoteType::Double),
            QuoteBehavior::AllowBoundaries
        );
        assert_eq!(
            en_rules.quote_behavior(QuoteType::JapaneseCorner),
            QuoteBehavior::Contextual
        );

        // Japanese rules
        assert_eq!(
            ja_rules.quote_behavior(QuoteType::JapaneseCorner),
            QuoteBehavior::SuppressBoundaries
        );
        assert_eq!(
            ja_rules.quote_behavior(QuoteType::Double),
            QuoteBehavior::Contextual
        );
    }

    // ============================================================================
    // BoundaryAnalyzer Tests
    // ============================================================================

    #[test]
    fn test_boundary_context_creation() {
        let context = BoundaryContext {
            text_before: "Hello".to_string(),
            text_after: " World".to_string(),
            position: 5,
            boundary_char: '.',
            enclosure_depth: 0,
        };

        assert_eq!(context.text_before, "Hello");
        assert_eq!(context.text_after, " World");
        assert_eq!(context.position, 5);
        assert_eq!(context.boundary_char, '.');
        assert_eq!(context.enclosure_depth, 0);
    }

    #[test]
    fn test_boundary_context_clone() {
        let context = BoundaryContext {
            text_before: "Test".to_string(),
            text_after: " text".to_string(),
            position: 4,
            boundary_char: '!',
            enclosure_depth: 2,
        };

        let cloned = context.clone();
        assert_eq!(cloned.text_before, context.text_before);
        assert_eq!(cloned.text_after, context.text_after);
        assert_eq!(cloned.position, context.position);
        assert_eq!(cloned.boundary_char, context.boundary_char);
        assert_eq!(cloned.enclosure_depth, context.enclosure_depth);
    }

    #[test]
    fn test_boundary_context_with_unicode() {
        let context = BoundaryContext {
            text_before: "こんにちは".to_string(),
            text_after: " 世界".to_string(),
            position: 15, // byte position
            boundary_char: '',
            enclosure_depth: 0,
        };

        assert_eq!(context.text_before, "こんにちは");
        assert_eq!(context.text_after, " 世界");
        assert_eq!(context.boundary_char, '');
    }

    #[test]
    fn test_boundary_marker_types() {
        let period = BoundaryMarkerType::Period;
        let question = BoundaryMarkerType::Question;
        let _exclamation = BoundaryMarkerType::Exclamation;
        let other = BoundaryMarkerType::Other('');

        // Test equality
        assert_eq!(period, BoundaryMarkerType::Period);
        assert_ne!(period, question);

        // Test Debug
        assert_eq!(format!("{:?}", period), "Period");
        assert_eq!(format!("{:?}", other), "Other('…')");
    }

    #[test]
    fn test_boundary_decision_variants() {
        let confirmed = BoundaryDecision::Confirmed { confidence: 0.9 };
        let rejected = BoundaryDecision::Rejected {
            reason: RejectionReason::Abbreviation,
        };
        let pending = BoundaryDecision::Pending;

        // Test pattern matching
        match confirmed {
            BoundaryDecision::Confirmed { confidence } => assert_eq!(confidence, 0.9),
            _ => panic!("Wrong variant"),
        }

        match rejected {
            BoundaryDecision::Rejected { reason } => {
                assert_eq!(reason, RejectionReason::Abbreviation)
            }
            _ => panic!("Wrong variant"),
        }

        match pending {
            BoundaryDecision::Pending => (),
            _ => panic!("Wrong variant"),
        }
    }

    #[test]
    fn test_rejection_reasons() {
        let reasons = [
            RejectionReason::Abbreviation,
            RejectionReason::InsideEnclosure,
            RejectionReason::InvalidFollowing,
            RejectionReason::LanguageSpecific("Custom reason".to_string()),
        ];

        // Test equality
        assert_eq!(reasons[0], RejectionReason::Abbreviation);
        assert_ne!(reasons[0], reasons[1]);

        // Test LanguageSpecific with different strings
        let ls1 = RejectionReason::LanguageSpecific("A".to_string());
        let ls2 = RejectionReason::LanguageSpecific("B".to_string());
        let ls3 = RejectionReason::LanguageSpecific("A".to_string());
        assert_ne!(ls1, ls2);
        assert_eq!(ls1, ls3);
    }

    struct MockBoundaryAnalyzer;

    impl BoundaryAnalyzer for MockBoundaryAnalyzer {
        fn analyze_candidate(&self, context: &BoundaryContext) -> BoundaryCandidateInfo {
            let marker_type = match context.boundary_char {
                '.' | '' => BoundaryMarkerType::Period,
                '?' | '' => BoundaryMarkerType::Question,
                '!' | '' => BoundaryMarkerType::Exclamation,
                ch => BoundaryMarkerType::Other(ch),
            };

            let base_confidence = match marker_type {
                BoundaryMarkerType::Question | BoundaryMarkerType::Exclamation => 0.9,
                BoundaryMarkerType::Period => 0.7,
                BoundaryMarkerType::Other(_) => 0.5,
            };

            BoundaryCandidateInfo {
                position: context.position,
                confidence: base_confidence,
                context: context.clone(),
                marker_type,
            }
        }

        fn evaluate_boundary(
            &self,
            candidate: &BoundaryCandidateInfo,
            _state: &PartialState,
        ) -> BoundaryDecision {
            if candidate.context.enclosure_depth > 0 {
                BoundaryDecision::Rejected {
                    reason: RejectionReason::InsideEnclosure,
                }
            } else if candidate.confidence >= 0.8 {
                BoundaryDecision::Confirmed {
                    confidence: candidate.confidence,
                }
            } else if candidate.confidence >= 0.5 {
                BoundaryDecision::Pending
            } else {
                BoundaryDecision::Rejected {
                    reason: RejectionReason::InvalidFollowing,
                }
            }
        }
    }

    #[test]
    fn test_boundary_analyzer_implementation() {
        let analyzer = MockBoundaryAnalyzer;

        // Test is_potential_boundary
        assert!(analyzer.is_potential_boundary('.'));
        assert!(analyzer.is_potential_boundary('?'));
        assert!(analyzer.is_potential_boundary('!'));
        assert!(analyzer.is_potential_boundary(''));
        assert!(analyzer.is_potential_boundary(''));
        assert!(analyzer.is_potential_boundary(''));
        assert!(analyzer.is_potential_boundary(''));
        assert!(analyzer.is_potential_boundary(''));
        assert!(!analyzer.is_potential_boundary('a'));
        assert!(!analyzer.is_potential_boundary(' '));
    }

    #[test]
    fn test_boundary_candidate_analysis() {
        let analyzer = MockBoundaryAnalyzer;

        let context = BoundaryContext {
            text_before: "Hello".to_string(),
            text_after: " World".to_string(),
            position: 5,
            boundary_char: '.',
            enclosure_depth: 0,
        };

        let candidate = analyzer.analyze_candidate(&context);
        assert_eq!(candidate.position, 5);
        assert_eq!(candidate.confidence, 0.7); // Period gets 0.7
        assert!(matches!(candidate.marker_type, BoundaryMarkerType::Period));

        // Test with question mark
        let context_q = BoundaryContext {
            boundary_char: '?',
            ..context.clone()
        };
        let candidate_q = analyzer.analyze_candidate(&context_q);
        assert_eq!(candidate_q.confidence, 0.9); // Question gets 0.9
    }

    #[test]
    fn test_boundary_evaluation_with_enclosure() {
        let analyzer = MockBoundaryAnalyzer;
        let state = PartialState::default();

        // Inside enclosure - should be rejected
        let context = BoundaryContext {
            text_before: "Hello".to_string(),
            text_after: " World".to_string(),
            position: 5,
            boundary_char: '.',
            enclosure_depth: 1,
        };

        let candidate = analyzer.analyze_candidate(&context);
        let decision = analyzer.evaluate_boundary(&candidate, &state);

        match decision {
            BoundaryDecision::Rejected { reason } => {
                assert_eq!(reason, RejectionReason::InsideEnclosure);
            }
            _ => panic!("Expected rejection due to enclosure"),
        }
    }

    #[test]
    fn test_boundary_evaluation_confidence_thresholds() {
        let analyzer = MockBoundaryAnalyzer;
        let state = PartialState::default();

        // High confidence - should be confirmed
        let context_high = BoundaryContext {
            text_before: "Hello".to_string(),
            text_after: " World".to_string(),
            position: 5,
            boundary_char: '!', // 0.9 confidence
            enclosure_depth: 0,
        };

        let candidate_high = analyzer.analyze_candidate(&context_high);
        let decision_high = analyzer.evaluate_boundary(&candidate_high, &state);

        match decision_high {
            BoundaryDecision::Confirmed { confidence } => {
                assert_eq!(confidence, 0.9);
            }
            _ => panic!("Expected confirmation with high confidence"),
        }

        // Medium confidence - should be pending
        let context_med = BoundaryContext {
            boundary_char: '.', // 0.7 confidence
            ..context_high.clone()
        };

        let candidate_med = analyzer.analyze_candidate(&context_med);
        let decision_med = analyzer.evaluate_boundary(&candidate_med, &state);

        match decision_med {
            BoundaryDecision::Pending => (),
            _ => panic!("Expected pending decision"),
        }
    }
}