torsh-text 0.1.2

Natural language processing utilities for ToRSh deep learning framework
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
//! Comprehensive backward compatibility tests for structural coherence modular implementation
//!
//! This test suite validates that the new modular implementation maintains 100% backward
//! compatibility with the original monolithic structural_coherence.rs API.
//!
//! Tests cover:
//! - All original struct/enum initialization patterns
//! - Complete method API compatibility
//! - Result type equivalence and serialization
//! - Edge case handling consistency
//! - Error condition matching
//! - Performance characteristics preservation
//! - Integration with existing torsh-text workflows

#[cfg(test)]
mod structural_coherence_backward_compatibility_tests {
    use std::collections::HashMap;
    use std::env;
    use std::fs;
    use std::path::PathBuf;
    use std::time::Instant;

    use approx::assert_relative_eq;
    use serde_json;
    use tempfile::TempDir;

    // Import the backward compatibility layer (should work identically to original)
    use crate::metrics::coherence::structural_coherence::{
        AdvancedAnalysisConfig, AdvancedStructuralAnalysis, BoundaryDetection,
        BoundaryDetectionConfig, BoundaryDetectionResult, CoherenceCalculation,
        CoherenceCalculationConfig, CoherenceCalculationResult, DetailedStructuralMetrics,
        DiscoursePattern, DiscoursePatternAnalysis, DiscoursePatternConfig, DiscoursePatternMatch,
        DocumentStructureAnalysis, GeneralAnalysisConfig, HierarchicalAnalysisConfig,
        HierarchicalLevel, HierarchicalStructureAnalysis, HierarchicalStructureNode,
        StructuralCoherenceAnalyzer, StructuralCoherenceConfig, StructuralCoherenceError,
        StructuralCoherenceResult, StructuralMarker, StructuralMarkerAnalysis,
        StructuralMarkerConfig, StructuralMarkerMatch,
    };

    /// Test data for comprehensive compatibility validation
    struct TestDataSuite {
        simple_text: Vec<String>,
        complex_academic_paper: Vec<String>,
        technical_documentation: Vec<String>,
        narrative_text: Vec<String>,
        fragmented_text: Vec<String>,
        empty_text: Vec<String>,
        single_paragraph: Vec<String>,
        hierarchical_structure: Vec<String>,
        discourse_heavy: Vec<String>,
        marker_rich_text: Vec<String>,
    }

    impl TestDataSuite {
        fn new() -> Self {
            Self {
                simple_text: vec![
                    "This is a simple introduction paragraph.".to_string(),
                    "This paragraph contains the main content.".to_string(),
                    "Finally, this paragraph provides a conclusion.".to_string(),
                ],
                complex_academic_paper: vec![
                    "# Abstract\n\nThis paper examines the complex relationship between discourse coherence and structural organization.".to_string(),
                    "## Introduction\n\nDespite significant advances in computational linguistics, the problem of measuring structural coherence remains challenging.".to_string(),
                    "### Background\n\nPrevious research has focused on lexical coherence metrics, but structural approaches offer additional insights.".to_string(),
                    "#### Related Work\n\nSmith et al. (2020) proposed hierarchical analysis methods.".to_string(),
                    "## Methodology\n\nOur approach combines hierarchical structure detection with discourse pattern analysis.".to_string(),
                    "### Data Collection\n\nWe collected a corpus of 10,000 academic papers across multiple domains.".to_string(),
                    "### Analysis Framework\n\nThe framework consists of three main components: structure detection, pattern analysis, and coherence calculation.".to_string(),
                    "## Results\n\nOur results demonstrate significant improvements over baseline methods.".to_string(),
                    "### Quantitative Analysis\n\nStructural coherence scores increased by 23% on average.".to_string(),
                    "### Qualitative Assessment\n\nHuman evaluators rated the improved texts as more coherent and easier to follow.".to_string(),
                    "## Discussion\n\nThese findings suggest that structural analysis provides valuable insights into text coherence.".to_string(),
                    "### Implications\n\nThe implications for automated text assessment are significant.".to_string(),
                    "### Limitations\n\nHowever, several limitations should be acknowledged.".to_string(),
                    "## Conclusion\n\nIn conclusion, our structural coherence analysis framework offers a promising approach to text quality assessment.".to_string(),
                ],
                technical_documentation: vec![
                    "Installation Guide".to_string(),
                    "First, ensure that all prerequisites are installed on your system.".to_string(),
                    "Step 1: Download the software package from the official repository.".to_string(),
                    "Step 2: Extract the archive to your desired installation directory.".to_string(),
                    "Step 3: Configure the environment variables as described below.".to_string(),
                    "Configuration".to_string(),
                    "The configuration file contains several important settings.".to_string(),
                    "For example, the timeout setting controls connection timeouts.".to_string(),
                    "In contrast, the buffer_size setting affects memory usage.".to_string(),
                    "Troubleshooting".to_string(),
                    "If installation fails, check the following common issues:".to_string(),
                    "1. Insufficient disk space".to_string(),
                    "2. Missing dependencies".to_string(),
                    "3. Permission errors".to_string(),
                ],
                narrative_text: vec![
                    "Once upon a time, in a distant kingdom, there lived a young scholar.".to_string(),
                    "This scholar spent her days studying ancient texts in the royal library.".to_string(),
                    "One day, she discovered a mysterious manuscript that would change everything.".to_string(),
                    "The manuscript contained strange symbols that seemed to shift before her eyes.".to_string(),
                    "As she studied the symbols, she began to understand their meaning.".to_string(),
                    "The symbols told the story of an ancient civilization with advanced knowledge.".to_string(),
                    "This knowledge had been hidden for centuries, waiting for someone to rediscover it.".to_string(),
                    "The scholar realized that this discovery could revolutionize their understanding of history.".to_string(),
                ],
                fragmented_text: vec![
                    "Random thought one.".to_string(),
                    "Completely unrelated second paragraph about weather.".to_string(),
                    "Technical jargon without context: API endpoints and database schemas.".to_string(),
                    "Back to weather: it's raining today.".to_string(),
                    "Philosophical musing about the nature of existence.".to_string(),
                ],
                empty_text: vec![],
                single_paragraph: vec![
                    "This is the only paragraph in this text, but it contains multiple sentences. Each sentence builds upon the previous one. The coherence should be measurable even in this simple case.".to_string(),
                ],
                hierarchical_structure: vec![
                    "# Chapter 1: Introduction".to_string(),
                    "This chapter introduces the main concepts.".to_string(),
                    "## Section 1.1: Basic Principles".to_string(),
                    "These are the fundamental principles we will explore.".to_string(),
                    "### Subsection 1.1.1: First Principle".to_string(),
                    "The first principle states that structure matters.".to_string(),
                    "### Subsection 1.1.2: Second Principle".to_string(),
                    "The second principle emphasizes coherence.".to_string(),
                    "## Section 1.2: Advanced Concepts".to_string(),
                    "Now we turn to more advanced topics.".to_string(),
                    "# Chapter 2: Implementation".to_string(),
                    "This chapter focuses on practical implementation.".to_string(),
                ],
                discourse_heavy: vec![
                    "The problem with current text analysis methods is their limited scope.".to_string(),
                    "To solve this problem, we propose a multi-layered approach.".to_string(),
                    "Our solution combines several techniques: hierarchical analysis, pattern detection, and coherence calculation.".to_string(),
                    "For example, hierarchical analysis examines document structure.".to_string(),
                    "In contrast, pattern detection focuses on discourse relationships.".to_string(),
                    "Similarly, coherence calculation provides quantitative measures.".to_string(),
                    "As a result of these combined techniques, we achieve better text understanding.".to_string(),
                    "Therefore, our approach offers significant improvements over existing methods.".to_string(),
                ],
                marker_rich_text: vec![
                    "Introduction: This paper presents a comprehensive analysis.".to_string(),
                    "Furthermore, we examine three main aspects of the problem.".to_string(),
                    "First, we consider the theoretical foundations.".to_string(),
                    "Second, we analyze practical implementations.".to_string(),
                    "Third, we evaluate experimental results.".to_string(),
                    "For instance, our experiments show significant improvements.".to_string(),
                    "Moreover, the results are consistent across different domains.".to_string(),
                    "In addition, we provide detailed statistical analysis.".to_string(),
                    "Conclusion: Our findings demonstrate the effectiveness of the proposed approach.".to_string(),
                ],
            }
        }
    }

    /// Configuration variants for comprehensive testing
    struct ConfigTestSuite {
        minimal: StructuralCoherenceConfig,
        standard: StructuralCoherenceConfig,
        comprehensive: StructuralCoherenceConfig,
        academic_focused: StructuralCoherenceConfig,
        technical_focused: StructuralCoherenceConfig,
        custom_weights: StructuralCoherenceConfig,
    }

    impl ConfigTestSuite {
        fn new() -> Self {
            Self {
                minimal: StructuralCoherenceConfig {
                    general: GeneralAnalysisConfig {
                        min_paragraph_length: 1,
                        max_analysis_depth: 1,
                        enable_caching: false,
                        parallel_processing: false,
                        detailed_metrics: false,
                    },
                    hierarchical: HierarchicalAnalysisConfig::minimal(),
                    discourse: DiscoursePatternConfig::minimal(),
                    markers: StructuralMarkerConfig::minimal(),
                    boundaries: BoundaryDetectionConfig::minimal(),
                    coherence: CoherenceCalculationConfig::minimal(),
                    advanced: AdvancedAnalysisConfig::minimal(),
                },
                standard: StructuralCoherenceConfig::default(),
                comprehensive: StructuralCoherenceConfig {
                    general: GeneralAnalysisConfig {
                        min_paragraph_length: 1,
                        max_analysis_depth: 10,
                        enable_caching: true,
                        parallel_processing: true,
                        detailed_metrics: true,
                    },
                    hierarchical: HierarchicalAnalysisConfig::comprehensive(),
                    discourse: DiscoursePatternConfig::comprehensive(),
                    markers: StructuralMarkerConfig::comprehensive(),
                    boundaries: BoundaryDetectionConfig::comprehensive(),
                    coherence: CoherenceCalculationConfig::comprehensive(),
                    advanced: AdvancedAnalysisConfig::comprehensive(),
                },
                academic_focused: StructuralCoherenceConfig::for_academic_papers(),
                technical_focused: StructuralCoherenceConfig::for_technical_documentation(),
                custom_weights: StructuralCoherenceConfig {
                    general: GeneralAnalysisConfig::default(),
                    hierarchical: HierarchicalAnalysisConfig {
                        enable_analysis: true,
                        max_depth: 5,
                        level_weights: {
                            let mut weights = HashMap::new();
                            weights.insert(HierarchicalLevel::Document, 0.5);
                            weights.insert(HierarchicalLevel::Chapter, 0.3);
                            weights.insert(HierarchicalLevel::Section, 0.2);
                            weights
                        },
                        balance_threshold: 0.8,
                        transition_penalties: HashMap::new(),
                    },
                    discourse: DiscoursePatternConfig::default(),
                    markers: StructuralMarkerConfig::default(),
                    boundaries: BoundaryDetectionConfig::default(),
                    coherence: CoherenceCalculationConfig::default(),
                    advanced: AdvancedAnalysisConfig::default(),
                },
            }
        }
    }

    #[test]
    fn test_analyzer_initialization_compatibility() {
        let configs = ConfigTestSuite::new();

        // Test all configuration variants can initialize analyzer
        let analyzers = vec![
            StructuralCoherenceAnalyzer::new(configs.minimal.clone()),
            StructuralCoherenceAnalyzer::new(configs.standard.clone()),
            StructuralCoherenceAnalyzer::new(configs.comprehensive.clone()),
            StructuralCoherenceAnalyzer::new(configs.academic_focused.clone()),
            StructuralCoherenceAnalyzer::new(configs.technical_focused.clone()),
            StructuralCoherenceAnalyzer::new(configs.custom_weights.clone()),
        ];

        // Verify all analyzers initialized successfully
        assert_eq!(analyzers.len(), 6);

        // Test with default configuration
        let default_analyzer = StructuralCoherenceAnalyzer::default();
        assert!(!format!("{:?}", default_analyzer).is_empty());
    }

    #[test]
    fn test_basic_analysis_compatibility() {
        let test_data = TestDataSuite::new();
        let analyzer = StructuralCoherenceAnalyzer::default();

        // Test basic analysis on all test cases
        let results = vec![
            analyzer.analyze_structural_coherence(&test_data.simple_text),
            analyzer.analyze_structural_coherence(&test_data.complex_academic_paper),
            analyzer.analyze_structural_coherence(&test_data.technical_documentation),
            analyzer.analyze_structural_coherence(&test_data.narrative_text),
            analyzer.analyze_structural_coherence(&test_data.single_paragraph),
            analyzer.analyze_structural_coherence(&test_data.hierarchical_structure),
            analyzer.analyze_structural_coherence(&test_data.discourse_heavy),
            analyzer.analyze_structural_coherence(&test_data.marker_rich_text),
        ];

        // All analyses should succeed
        for result in &results {
            assert!(result.is_ok(), "Analysis failed: {:?}", result);
        }

        // Verify result structure
        for result in results {
            let analysis = result.expect("operation should succeed");
            assert!(analysis.overall_coherence_score >= 0.0);
            assert!(analysis.overall_coherence_score <= 1.0);
            assert!(!analysis.detailed_metrics.is_none());
        }
    }

    #[test]
    fn test_empty_and_edge_cases_compatibility() {
        let test_data = TestDataSuite::new();
        let analyzer = StructuralCoherenceAnalyzer::default();

        // Test empty text
        let empty_result = analyzer.analyze_structural_coherence(&test_data.empty_text);
        assert!(empty_result.is_ok());
        let empty_analysis = empty_result.expect("operation should succeed");
        assert_relative_eq!(empty_analysis.overall_coherence_score, 0.0, epsilon = 1e-6);

        // Test fragmented text
        let fragmented_result = analyzer.analyze_structural_coherence(&test_data.fragmented_text);
        assert!(fragmented_result.is_ok());
        let fragmented_analysis = fragmented_result.expect("operation should succeed");
        assert!(fragmented_analysis.overall_coherence_score < 0.5); // Should be low coherence

        // Test single paragraph
        let single_result = analyzer.analyze_structural_coherence(&test_data.single_paragraph);
        assert!(single_result.is_ok());
        let single_analysis = single_result.expect("operation should succeed");
        assert!(single_analysis.overall_coherence_score > 0.0);

        // Test very long strings
        let long_paragraphs: Vec<String> = (0..100)
            .map(|i| {
                format!(
                    "This is paragraph number {} with consistent content and structure.",
                    i
                )
            })
            .collect();
        let long_result = analyzer.analyze_structural_coherence(&long_paragraphs);
        assert!(long_result.is_ok());
    }

    #[test]
    fn test_hierarchical_analysis_compatibility() {
        let test_data = TestDataSuite::new();
        let analyzer = StructuralCoherenceAnalyzer::default();

        // Test hierarchical analysis on structured text
        let result = analyzer.analyze_hierarchical_structure(&test_data.hierarchical_structure);
        assert!(result.is_ok());

        let analysis = result.expect("operation should succeed");
        assert!(analysis.tree_structure.len() > 0);
        assert!(analysis
            .level_distribution
            .contains_key(&HierarchicalLevel::Chapter));
        assert!(analysis
            .level_distribution
            .contains_key(&HierarchicalLevel::Section));
        assert!(analysis.balance_score >= 0.0);
        assert!(analysis.balance_score <= 1.0);

        // Test on non-hierarchical text
        let simple_result = analyzer.analyze_hierarchical_structure(&test_data.simple_text);
        assert!(simple_result.is_ok());

        // Test level detection
        for paragraph in &test_data.hierarchical_structure {
            let level = analyzer.detect_hierarchical_level(&paragraph);
            assert!(matches!(
                level,
                HierarchicalLevel::Document
                    | HierarchicalLevel::Chapter
                    | HierarchicalLevel::Section
                    | HierarchicalLevel::Subsection
                    | HierarchicalLevel::Paragraph
            ));
        }
    }

    #[test]
    fn test_discourse_pattern_analysis_compatibility() {
        let test_data = TestDataSuite::new();
        let analyzer = StructuralCoherenceAnalyzer::default();

        // Test discourse pattern analysis
        let result = analyzer.analyze_discourse_patterns(&test_data.discourse_heavy);
        assert!(result.is_ok());

        let analysis = result.expect("operation should succeed");
        assert!(analysis.identified_patterns.len() > 0);
        assert!(analysis.overall_pattern_coherence >= 0.0);
        assert!(analysis.overall_pattern_coherence <= 1.0);

        // Should identify problem-solution pattern
        let has_problem_solution = analysis
            .identified_patterns
            .iter()
            .any(|p| matches!(p.pattern_type, DiscoursePattern::ProblemSolution));
        assert!(has_problem_solution);

        // Should identify compare-contrast pattern
        let has_compare_contrast = analysis
            .identified_patterns
            .iter()
            .any(|p| matches!(p.pattern_type, DiscoursePattern::CompareContrast));
        assert!(has_compare_contrast);

        // Test pattern detection on individual segments
        for paragraph in &test_data.discourse_heavy {
            let patterns = analyzer.detect_discourse_patterns(&paragraph);
            assert!(patterns.len() >= 0); // May be empty for some paragraphs
        }
    }

    #[test]
    fn test_structural_marker_analysis_compatibility() {
        let test_data = TestDataSuite::new();
        let analyzer = StructuralCoherenceAnalyzer::default();

        // Test structural marker analysis
        let result = analyzer.analyze_structural_markers(&test_data.marker_rich_text);
        assert!(result.is_ok());

        let analysis = result.expect("operation should succeed");
        assert!(analysis.identified_markers.len() > 0);
        assert!(analysis.marker_density >= 0.0);
        assert!(analysis.effectiveness_score >= 0.0);
        assert!(analysis.effectiveness_score <= 1.0);

        // Should identify introduction markers
        let has_intro_markers = analysis
            .identified_markers
            .iter()
            .any(|m| matches!(m.marker_type, StructuralMarker::Introduction));
        assert!(has_intro_markers);

        // Should identify enumeration markers
        let has_enum_markers = analysis
            .identified_markers
            .iter()
            .any(|m| matches!(m.marker_type, StructuralMarker::Enumeration));
        assert!(has_enum_markers);

        // Should identify conclusion markers
        let has_conclusion_markers = analysis
            .identified_markers
            .iter()
            .any(|m| matches!(m.marker_type, StructuralMarker::Conclusion));
        assert!(has_conclusion_markers);

        // Test marker detection on individual paragraphs
        for paragraph in &test_data.marker_rich_text {
            let markers = analyzer.detect_structural_markers(&paragraph);
            // Some paragraphs may not have markers
        }
    }

    #[test]
    fn test_boundary_detection_compatibility() {
        let test_data = TestDataSuite::new();
        let analyzer = StructuralCoherenceAnalyzer::default();

        // Test boundary detection
        let result = analyzer.detect_structural_boundaries(&test_data.complex_academic_paper);
        assert!(result.is_ok());

        let boundaries = result.expect("operation should succeed");
        assert!(boundaries.section_boundaries.len() > 0);
        assert!(boundaries.topic_boundaries.len() > 0);
        assert!(boundaries.confidence_scores.len() == boundaries.section_boundaries.len());

        // All confidence scores should be between 0 and 1
        for score in &boundaries.confidence_scores {
            assert!(*score >= 0.0);
            assert!(*score <= 1.0);
        }

        // Test on simple text
        let simple_result = analyzer.detect_structural_boundaries(&test_data.simple_text);
        assert!(simple_result.is_ok());
    }

    #[test]
    fn test_coherence_calculation_compatibility() {
        let test_data = TestDataSuite::new();
        let analyzer = StructuralCoherenceAnalyzer::default();

        // Test coherence calculation with different components
        let hierarchical = analyzer
            .analyze_hierarchical_structure(&test_data.hierarchical_structure)
            .expect("operation should succeed");
        let discourse = analyzer
            .analyze_discourse_patterns(&test_data.discourse_heavy)
            .expect("operation should succeed");
        let markers = analyzer
            .analyze_structural_markers(&test_data.marker_rich_text)
            .expect("operation should succeed");

        let result = analyzer.calculate_overall_coherence(&hierarchical, &discourse, &markers);
        assert!(result.is_ok());

        let calculation = result.expect("operation should succeed");
        assert!(calculation.overall_score >= 0.0);
        assert!(calculation.overall_score <= 1.0);
        assert!(calculation.hierarchical_contribution >= 0.0);
        assert!(calculation.discourse_contribution >= 0.0);
        assert!(calculation.marker_contribution >= 0.0);

        // Component weights should sum to approximately 1.0
        let weight_sum = calculation.hierarchical_weight
            + calculation.discourse_weight
            + calculation.marker_weight;
        assert_relative_eq!(weight_sum, 1.0, epsilon = 1e-6);
    }

    #[test]
    fn test_advanced_analysis_compatibility() {
        let test_data = TestDataSuite::new();
        let analyzer = StructuralCoherenceAnalyzer::default();

        // Test advanced analysis features
        let result = analyzer.perform_advanced_analysis(&test_data.complex_academic_paper);
        assert!(result.is_ok());

        let analysis = result.expect("operation should succeed");
        assert!(analysis.rhetorical_structure.is_some());
        assert!(analysis.reader_experience_metrics.is_some());
        assert!(analysis.complexity_analysis.is_some());

        if let Some(rhetorical) = &analysis.rhetorical_structure {
            assert!(rhetorical.argument_structure.len() > 0);
            assert!(rhetorical.evidence_distribution.len() > 0);
            assert!(rhetorical.persuasive_elements.len() >= 0);
        }

        if let Some(reader_exp) = &analysis.reader_experience_metrics {
            assert!(reader_exp.cognitive_load_score >= 0.0);
            assert!(reader_exp.cognitive_load_score <= 1.0);
            assert!(reader_exp.navigation_ease_score >= 0.0);
            assert!(reader_exp.navigation_ease_score <= 1.0);
        }

        if let Some(complexity) = &analysis.complexity_analysis {
            assert!(complexity.structural_complexity >= 0.0);
            assert!(complexity.information_density >= 0.0);
            assert!(complexity.conceptual_depth >= 0);
        }
    }

    #[test]
    fn test_configuration_compatibility() {
        // Test all configuration creation methods
        let configs = vec![
            StructuralCoherenceConfig::default(),
            StructuralCoherenceConfig::minimal(),
            StructuralCoherenceConfig::comprehensive(),
            StructuralCoherenceConfig::for_academic_papers(),
            StructuralCoherenceConfig::for_technical_documentation(),
            StructuralCoherenceConfig::for_creative_writing(),
        ];

        for config in configs {
            let analyzer = StructuralCoherenceAnalyzer::new(config);
            // Should be able to create analyzer with any config
            assert!(!format!("{:?}", analyzer).is_empty());
        }

        // Test configuration builder pattern
        let builder_config = StructuralCoherenceConfig::builder()
            .general_config(GeneralAnalysisConfig {
                min_paragraph_length: 5,
                max_analysis_depth: 3,
                enable_caching: true,
                parallel_processing: false,
                detailed_metrics: true,
            })
            .hierarchical_config(HierarchicalAnalysisConfig::default())
            .build();

        let builder_analyzer = StructuralCoherenceAnalyzer::new(builder_config);
        assert!(!format!("{:?}", builder_analyzer).is_empty());
    }

    #[test]
    fn test_result_serialization_compatibility() {
        let test_data = TestDataSuite::new();
        let analyzer = StructuralCoherenceAnalyzer::default();

        // Test serialization of main result
        let result = analyzer
            .analyze_structural_coherence(&test_data.complex_academic_paper)
            .expect("operation should succeed");

        let serialized = serde_json::to_string(&result);
        assert!(serialized.is_ok());

        let deserialized: Result<StructuralCoherenceResult, _> =
            serde_json::from_str(&serialized.expect("operation should succeed"));
        assert!(deserialized.is_ok());

        let recovered_result = deserialized.expect("operation should succeed");
        assert_relative_eq!(
            recovered_result.overall_coherence_score,
            result.overall_coherence_score,
            epsilon = 1e-10
        );

        // Test serialization of individual analysis components
        let hierarchical = analyzer
            .analyze_hierarchical_structure(&test_data.hierarchical_structure)
            .expect("operation should succeed");
        let hier_serialized = serde_json::to_string(&hierarchical);
        assert!(hier_serialized.is_ok());

        let discourse = analyzer
            .analyze_discourse_patterns(&test_data.discourse_heavy)
            .expect("operation should succeed");
        let disc_serialized = serde_json::to_string(&discourse);
        assert!(disc_serialized.is_ok());

        let markers = analyzer
            .analyze_structural_markers(&test_data.marker_rich_text)
            .expect("operation should succeed");
        let mark_serialized = serde_json::to_string(&markers);
        assert!(mark_serialized.is_ok());
    }

    #[test]
    fn test_error_handling_compatibility() {
        let analyzer = StructuralCoherenceAnalyzer::default();

        // Test with invalid configurations
        let invalid_config = StructuralCoherenceConfig {
            general: GeneralAnalysisConfig {
                min_paragraph_length: 0,
                max_analysis_depth: 0,
                enable_caching: false,
                parallel_processing: false,
                detailed_metrics: false,
            },
            hierarchical: HierarchicalAnalysisConfig::minimal(),
            discourse: DiscoursePatternConfig::minimal(),
            markers: StructuralMarkerConfig::minimal(),
            boundaries: BoundaryDetectionConfig::minimal(),
            coherence: CoherenceCalculationConfig::minimal(),
            advanced: AdvancedAnalysisConfig::minimal(),
        };

        let invalid_analyzer = StructuralCoherenceAnalyzer::new(invalid_config);
        // Should still create analyzer but might produce different results

        // Test error enum compatibility
        let error_variants = vec![
            StructuralCoherenceError::InvalidInput("test".to_string()),
            StructuralCoherenceError::ConfigurationError("test".to_string()),
            StructuralCoherenceError::AnalysisError("test".to_string()),
            StructuralCoherenceError::ComputationError("test".to_string()),
        ];

        for error in error_variants {
            assert!(!format!("{:?}", error).is_empty());
            assert!(!format!("{}", error).is_empty());
        }
    }

    #[test]
    fn test_performance_consistency() {
        let test_data = TestDataSuite::new();
        let analyzer = StructuralCoherenceAnalyzer::default();

        // Measure performance on different text sizes
        let performance_tests = vec![
            ("simple", &test_data.simple_text),
            ("single", &test_data.single_paragraph),
            ("complex", &test_data.complex_academic_paper),
        ];

        for (name, text) in performance_tests {
            let start_time = Instant::now();
            let result = analyzer.analyze_structural_coherence(text);
            let elapsed = start_time.elapsed();

            assert!(result.is_ok(), "Performance test failed for {}", name);
            assert!(
                elapsed.as_secs() < 10,
                "Analysis took too long for {}: {:?}",
                name,
                elapsed
            );

            // Performance should be roughly consistent across runs
            let start_time_2 = Instant::now();
            let result_2 = analyzer.analyze_structural_coherence(text);
            let elapsed_2 = start_time_2.elapsed();

            assert!(result_2.is_ok());
            let time_diff = if elapsed > elapsed_2 {
                elapsed - elapsed_2
            } else {
                elapsed_2 - elapsed
            };
            assert!(
                time_diff.as_millis() < 1000,
                "Performance inconsistency for {}",
                name
            );
        }
    }

    #[test]
    fn test_thread_safety_compatibility() {
        use std::sync::Arc;
        use std::thread;

        let test_data = Arc::new(TestDataSuite::new());
        let analyzer = Arc::new(StructuralCoherenceAnalyzer::default());

        let mut handles = vec![];

        // Test concurrent access
        for i in 0..4 {
            let test_data_clone = Arc::clone(&test_data);
            let analyzer_clone = Arc::clone(&analyzer);

            let handle = thread::spawn(move || {
                let text = match i % 4 {
                    0 => &test_data_clone.simple_text,
                    1 => &test_data_clone.complex_academic_paper,
                    2 => &test_data_clone.technical_documentation,
                    _ => &test_data_clone.narrative_text,
                };

                let result = analyzer_clone.analyze_structural_coherence(text);
                assert!(
                    result.is_ok(),
                    "Concurrent analysis failed for thread {}",
                    i
                );
                result.expect("operation should succeed").overall_coherence_score
            });

            handles.push(handle);
        }

        let scores: Vec<f64> = handles.into_iter().map(|h| h.join().expect("map operation should succeed")).collect();
        assert_eq!(scores.len(), 4);

        // All scores should be valid
        for score in scores {
            assert!(score >= 0.0);
            assert!(score <= 1.0);
        }
    }

    #[test]
    fn test_memory_usage_compatibility() {
        let test_data = TestDataSuite::new();
        let analyzer = StructuralCoherenceAnalyzer::default();

        // Test memory usage doesn't grow excessively
        let initial_usage = get_memory_usage();

        for _ in 0..10 {
            let _ = analyzer.analyze_structural_coherence(&test_data.complex_academic_paper);
        }

        let final_usage = get_memory_usage();
        let memory_growth = final_usage - initial_usage;

        // Memory growth should be reasonable (less than 100MB)
        assert!(
            memory_growth < 100_000_000,
            "Excessive memory growth: {} bytes",
            memory_growth
        );
    }

    #[test]
    fn test_integration_compatibility() {
        // Test integration with torsh-text ecosystem
        let test_data = TestDataSuite::new();
        let analyzer = StructuralCoherenceAnalyzer::default();

        // Test that results can be used in typical workflows
        let result = analyzer
            .analyze_structural_coherence(&test_data.complex_academic_paper)
            .expect("operation should succeed");

        // Should be able to extract useful metrics
        assert!(result.overall_coherence_score > 0.0);

        if let Some(detailed) = &result.detailed_metrics {
            if let Some(doc_structure) = &detailed.document_structure_analysis {
                assert!(doc_structure.total_sections > 0);
                assert!(!doc_structure.section_hierarchy.is_empty());
            }

            if let Some(advanced) = &detailed.advanced_structural_analysis {
                if let Some(rhetorical) = &advanced.rhetorical_structure {
                    // Should have identified some rhetorical elements
                    assert!(rhetorical.argument_structure.len() >= 0);
                }
            }
        }

        // Test that individual analysis components can be used independently
        let hierarchical = analyzer
            .analyze_hierarchical_structure(&test_data.hierarchical_structure)
            .expect("operation should succeed");
        let discourse = analyzer
            .analyze_discourse_patterns(&test_data.discourse_heavy)
            .expect("operation should succeed");
        let markers = analyzer
            .analyze_structural_markers(&test_data.marker_rich_text)
            .expect("operation should succeed");

        // Should be able to combine results manually
        let coherence_calc = analyzer
            .calculate_overall_coherence(&hierarchical, &discourse, &markers)
            .expect("operation should succeed");
        assert!(coherence_calc.overall_score >= 0.0);
        assert!(coherence_calc.overall_score <= 1.0);
    }

    /// Helper function to get current memory usage (approximate)
    fn get_memory_usage() -> usize {
        // This is a simplified memory usage estimation
        // In a real implementation, you might use a more sophisticated approach
        std::mem::size_of::<StructuralCoherenceAnalyzer>() * 1000 // Rough estimate
    }

    #[test]
    fn test_comprehensive_api_surface_compatibility() {
        let test_data = TestDataSuite::new();
        let configs = ConfigTestSuite::new();

        // Test every public method exists and works
        for config in [configs.minimal, configs.standard, configs.comprehensive] {
            let analyzer = StructuralCoherenceAnalyzer::new(config);

            // Core analysis methods
            assert!(analyzer
                .analyze_structural_coherence(&test_data.simple_text)
                .is_ok());
            assert!(analyzer
                .analyze_hierarchical_structure(&test_data.hierarchical_structure)
                .is_ok());
            assert!(analyzer
                .analyze_discourse_patterns(&test_data.discourse_heavy)
                .is_ok());
            assert!(analyzer
                .analyze_structural_markers(&test_data.marker_rich_text)
                .is_ok());
            assert!(analyzer
                .detect_structural_boundaries(&test_data.complex_academic_paper)
                .is_ok());
            assert!(analyzer
                .perform_advanced_analysis(&test_data.technical_documentation)
                .is_ok());

            // Individual detection methods
            for paragraph in &test_data.complex_academic_paper[..3] {
                // Test subset to avoid long test times
                let _ = analyzer.detect_hierarchical_level(paragraph);
                let _ = analyzer.detect_discourse_patterns(paragraph);
                let _ = analyzer.detect_structural_markers(paragraph);
            }

            // Configuration access
            let _ = analyzer.get_configuration();

            // Utility methods
            assert!(analyzer.validate_input(&test_data.simple_text).is_ok());
        }
    }
}

#[cfg(test)]
mod integration_compatibility_tests {
    use super::*;
    use std::path::PathBuf;
    use tempfile::TempDir;

    #[test]
    fn test_file_based_analysis_compatibility() {
        let temp_dir = TempDir::new().expect("Temp Dir should succeed");
        let test_file_path = temp_dir.path().join("test_document.txt");

        let test_content = vec![
            "# Document Title".to_string(),
            "This is the introduction paragraph.".to_string(),
            "## First Section".to_string(),
            "This section contains the main content.".to_string(),
            "### Subsection".to_string(),
            "More detailed information here.".to_string(),
            "## Second Section".to_string(),
            "Additional content and analysis.".to_string(),
            "## Conclusion".to_string(),
            "Summary and final thoughts.".to_string(),
        ];

        // Write test content to file
        std::fs::write(&test_file_path, test_content.join("\n")).expect("operation should succeed");

        // Test file-based analysis if supported
        let analyzer = StructuralCoherenceAnalyzer::default();

        // Read file and analyze (simulating file-based workflow)
        let file_content = std::fs::read_to_string(&test_file_path).expect("fs should succeed");
        let paragraphs: Vec<String> = file_content.lines().map(|s| s.to_string()).collect();

        let result = analyzer.analyze_structural_coherence(&paragraphs);
        assert!(result.is_ok());

        let analysis = result.expect("operation should succeed");
        assert!(analysis.overall_coherence_score > 0.0);
    }

    #[test]
    fn test_large_document_compatibility() {
        // Test with a larger document to ensure scalability
        let large_document: Vec<String> = (0..1000)
            .map(|i| match i % 10 {
                0 => format!("# Chapter {}", i / 10 + 1),
                1 => format!("## Section {}.1", i / 10 + 1),
                2 => format!("### Subsection {}.1.1", i / 10 + 1),
                3 => "This is an introduction paragraph.".to_string(),
                4 => "For example, this paragraph contains an example.".to_string(),
                5 => "Furthermore, this paragraph provides additional details.".to_string(),
                6 => "In contrast, this paragraph presents a different viewpoint.".to_string(),
                7 => "As a result, we can draw certain conclusions.".to_string(),
                8 => "Therefore, this analysis is important.".to_string(),
                _ => "This is a concluding paragraph.".to_string(),
            })
            .collect();

        let analyzer = StructuralCoherenceAnalyzer::default();
        let start_time = std::time::Instant::now();

        let result = analyzer.analyze_structural_coherence(&large_document);
        let elapsed = start_time.elapsed();

        assert!(result.is_ok());
        assert!(elapsed.as_secs() < 60); // Should complete within reasonable time

        let analysis = result.expect("operation should succeed");
        assert!(analysis.overall_coherence_score >= 0.0);
        assert!(analysis.overall_coherence_score <= 1.0);
    }
}