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
//! Modular structural coherence analysis system
//!
//! This module provides a comprehensive, modular implementation of structural coherence analysis
//! that breaks down the complex analysis into focused, maintainable components while providing
//! a unified interface for analyzing text structure, hierarchical organization, discourse patterns,
//! and structural markers.

use std::collections::HashMap;
use thiserror::Error;

pub mod config;
pub mod discourse_patterns;
pub mod hierarchical;
pub mod results;
pub mod structural_markers;

#[cfg(test)]
pub mod tests;

use config::{DocumentStructureType, StructuralCoherenceConfig};
use discourse_patterns::{DiscoursePatternAnalyzer, DiscoursePatternError};
use hierarchical::{HierarchicalAnalysisError, HierarchicalAnalyzer};
use results::{DetailedStructuralMetrics, DocumentStructureAnalysis, StructuralCoherenceResult};
use structural_markers::{StructuralMarkerAnalyzer, StructuralMarkerError};

/// Comprehensive errors for structural coherence analysis
#[derive(Debug, Error)]
pub enum StructuralCoherenceError {
    #[error("Empty text provided for structural analysis")]
    EmptyText,
    #[error("Invalid structural configuration: {0}")]
    InvalidConfiguration(String),
    #[error("Document parsing error: {0}")]
    DocumentParsingError(String),
    #[error("Structural analysis error: {0}")]
    StructuralAnalysisError(String),
    #[error("Hierarchical analysis failed: {0}")]
    HierarchicalAnalysisError(#[from] HierarchicalAnalysisError),
    #[error("Pattern recognition error: {0}")]
    PatternRecognitionError(#[from] DiscoursePatternError),
    #[error("Organizational analysis error: {0}")]
    OrganizationalAnalysisError(#[from] StructuralMarkerError),
}

/// Main structural coherence analyzer with modular architecture
pub struct StructuralCoherenceAnalyzer {
    config: StructuralCoherenceConfig,
    hierarchical_analyzer: HierarchicalAnalyzer,
    discourse_analyzer: DiscoursePatternAnalyzer,
    marker_analyzer: StructuralMarkerAnalyzer,
}

impl StructuralCoherenceAnalyzer {
    /// Create a new structural coherence analyzer with the given configuration
    pub fn new(config: StructuralCoherenceConfig) -> Result<Self, StructuralCoherenceError> {
        // Validate configuration
        config
            .validate()
            .map_err(StructuralCoherenceError::InvalidConfiguration)?;

        let hierarchical_analyzer = HierarchicalAnalyzer::new(config.hierarchical.clone());
        let discourse_analyzer = DiscoursePatternAnalyzer::new(config.discourse.clone());
        let marker_analyzer = StructuralMarkerAnalyzer::new(config.markers.clone());

        Ok(Self {
            config,
            hierarchical_analyzer,
            discourse_analyzer,
            marker_analyzer,
        })
    }

    /// Create analyzer with default configuration
    pub fn with_defaults() -> Result<Self, StructuralCoherenceError> {
        Self::new(StructuralCoherenceConfig::default())
    }

    /// Analyze structural coherence of the given text
    pub fn analyze(
        &self,
        text: &str,
    ) -> Result<StructuralCoherenceResult, StructuralCoherenceError> {
        if text.trim().is_empty() {
            return Err(StructuralCoherenceError::EmptyText);
        }

        // Split text into paragraphs
        let paragraphs = self.split_into_paragraphs(text)?;

        if paragraphs.is_empty() {
            return Err(StructuralCoherenceError::EmptyText);
        }

        // Perform modular analysis
        let paragraph_coherence = self.calculate_paragraph_coherence(&paragraphs)?;
        let section_coherence = self.calculate_section_coherence(&paragraphs)?;
        let organizational_coherence = self.calculate_organizational_coherence(&paragraphs)?;

        let hierarchical_coherence = if self.config.hierarchical.enable_analysis {
            self.calculate_hierarchical_coherence(&paragraphs)?
        } else {
            0.0
        };

        let paragraph_transitions = self.calculate_paragraph_transitions(&paragraphs)?;
        let structural_markers = self.extract_structural_markers(&paragraphs)?;
        let coherence_patterns = self.identify_coherence_patterns(&paragraphs)?;
        let structural_consistency = self.calculate_structural_consistency(&paragraphs)?;

        // Build detailed metrics
        let detailed_metrics = self.build_detailed_metrics(&paragraphs)?;

        // Analyze document structure
        let document_structure = self.analyze_document_structure(&paragraphs)?;

        // Advanced analysis if enabled
        let advanced_analysis = if self.config.advanced.enable_advanced_analysis {
            Some(self.perform_advanced_analysis(&paragraphs)?)
        } else {
            None
        };

        Ok(StructuralCoherenceResult {
            paragraph_coherence,
            section_coherence,
            organizational_coherence,
            hierarchical_coherence,
            paragraph_transitions,
            structural_markers,
            coherence_patterns,
            structural_consistency,
            detailed_metrics,
            document_structure,
            advanced_analysis,
        })
    }

    /// Split text into paragraphs
    fn split_into_paragraphs(&self, text: &str) -> Result<Vec<String>, StructuralCoherenceError> {
        let paragraphs: Vec<String> = text
            .split("\n\n")
            .filter_map(|para| {
                let trimmed = para.trim().replace('\n', ' ');
                if trimmed.len() >= self.config.general.min_paragraph_length {
                    Some(trimmed)
                } else {
                    None
                }
            })
            .collect();

        if paragraphs.is_empty() {
            // Try alternative splitting if no double newlines
            let alternative_paragraphs: Vec<String> = text
                .split('\n')
                .filter_map(|para| {
                    let trimmed = para.trim();
                    if trimmed.len() >= self.config.general.min_paragraph_length {
                        Some(trimmed.to_string())
                    } else {
                        None
                    }
                })
                .collect();

            if alternative_paragraphs.is_empty() {
                return Err(StructuralCoherenceError::DocumentParsingError(
                    "Could not identify paragraphs in text".to_string(),
                ));
            }

            Ok(alternative_paragraphs)
        } else {
            Ok(paragraphs)
        }
    }

    /// Calculate paragraph coherence score
    fn calculate_paragraph_coherence(
        &self,
        paragraphs: &[String],
    ) -> Result<f64, StructuralCoherenceError> {
        if paragraphs.is_empty() {
            return Ok(0.0);
        }

        let mut total_coherence = 0.0;
        let mut coherent_paragraphs = 0;

        for paragraph in paragraphs {
            let sentences = self.split_paragraph_into_sentences(paragraph);
            if sentences.len() >= 2 {
                let para_coherence = self.calculate_intra_paragraph_coherence(&sentences);
                total_coherence += para_coherence;
                coherent_paragraphs += 1;
            }
        }

        Ok(if coherent_paragraphs > 0 {
            total_coherence / coherent_paragraphs as f64
        } else {
            1.0 // Single-sentence paragraphs are perfectly coherent
        })
    }

    /// Split paragraph into sentences
    fn split_paragraph_into_sentences(&self, paragraph: &str) -> Vec<String> {
        paragraph
            .split('.')
            .filter_map(|sent| {
                let trimmed = sent.trim();
                if !trimmed.is_empty() {
                    Some(trimmed.to_string())
                } else {
                    None
                }
            })
            .collect()
    }

    /// Calculate intra-paragraph coherence
    fn calculate_intra_paragraph_coherence(&self, sentences: &[String]) -> f64 {
        if sentences.len() < 2 {
            return 1.0;
        }

        let mut total_coherence = 0.0;
        let mut pair_count = 0;

        for i in 0..sentences.len() - 1 {
            let coherence =
                self.calculate_sentence_pair_coherence(&sentences[i], &sentences[i + 1]);
            total_coherence += coherence;
            pair_count += 1;
        }

        if pair_count > 0 {
            total_coherence / pair_count as f64
        } else {
            1.0
        }
    }

    /// Calculate coherence between two sentences
    fn calculate_sentence_pair_coherence(&self, sent1: &str, sent2: &str) -> f64 {
        let lexical_overlap = self.calculate_lexical_overlap(sent1, sent2);
        let structural_continuity = self.calculate_structural_continuity(sent1, sent2);
        let semantic_continuity = if self.config.coherence.enable_semantic_continuity {
            self.calculate_semantic_continuity(sent1, sent2)
        } else {
            0.5 // Neutral value when disabled
        };

        // Weighted combination based on configuration
        lexical_overlap * self.config.coherence.lexical_overlap_weight
            + structural_continuity * self.config.coherence.structural_continuity_weight
            + semantic_continuity * self.config.coherence.semantic_continuity_weight
    }

    /// Calculate lexical overlap between sentences
    fn calculate_lexical_overlap(&self, sent1: &str, sent2: &str) -> f64 {
        let words1 = self.extract_content_words(sent1);
        let words2 = self.extract_content_words(sent2);

        if words1.is_empty() || words2.is_empty() {
            return 0.0;
        }

        let set1: std::collections::HashSet<_> = words1.iter().collect();
        let set2: std::collections::HashSet<_> = words2.iter().collect();

        let intersection_size = set1.intersection(&set2).count() as f64;
        let union_size = set1.union(&set2).count() as f64;

        if union_size > 0.0 {
            intersection_size / union_size
        } else {
            0.0
        }
    }

    /// Extract content words from sentence
    fn extract_content_words(&self, sentence: &str) -> Vec<String> {
        let stopwords = self.get_stopwords();
        sentence
            .to_lowercase()
            .split_whitespace()
            .filter_map(|word| {
                let cleaned = word.trim_matches(|c: char| !c.is_alphabetic());
                if cleaned.len() > 2 && !stopwords.contains(cleaned) {
                    Some(cleaned.to_string())
                } else {
                    None
                }
            })
            .collect()
    }

    /// Get stopwords set
    fn get_stopwords(&self) -> std::collections::HashSet<&'static str> {
        [
            "the", "a", "an", "and", "or", "but", "in", "on", "at", "to", "for", "of", "with",
            "by", "is", "are", "was", "were", "be", "been", "being", "have", "has", "had", "do",
            "does", "did", "will", "would", "could", "should", "may", "might", "can", "this",
            "that", "these", "those", "it", "its", "they", "their", "them", "he", "his", "him",
            "she", "her", "hers", "we", "our", "us", "you", "your", "yours", "i", "my", "me",
            "mine",
        ]
        .iter()
        .copied()
        .collect()
    }

    /// Calculate structural continuity between sentences
    fn calculate_structural_continuity(&self, sent1: &str, sent2: &str) -> f64 {
        let struct1 = self.count_structural_elements(sent1);
        let struct2 = self.count_structural_elements(sent2);

        let total_elements = struct1 + struct2;
        if total_elements == 0 {
            return 1.0;
        }

        let difference = (struct1 as i32 - struct2 as i32).abs() as f64;
        let similarity = 1.0 - (difference / total_elements as f64);

        similarity.max(0.0)
    }

    /// Count structural elements in sentence
    fn count_structural_elements(&self, sentence: &str) -> usize {
        let commas = sentence.matches(',').count();
        let semicolons = sentence.matches(';').count();
        let colons = sentence.matches(':').count();
        let dashes = sentence.matches(" - ").count() + sentence.matches("—").count();

        commas + semicolons + colons + dashes
    }

    /// Calculate semantic continuity between sentences
    fn calculate_semantic_continuity(&self, sent1: &str, sent2: &str) -> f64 {
        let words1 = self.extract_content_words(sent1);
        let words2 = self.extract_content_words(sent2);

        if words1.is_empty() || words2.is_empty() {
            return 0.0;
        }

        let mut semantic_similarity = 0.0;
        let mut comparisons = 0;

        for word1 in &words1 {
            for word2 in &words2 {
                semantic_similarity += self.calculate_word_similarity(word1, word2);
                comparisons += 1;
            }
        }

        if comparisons > 0 {
            semantic_similarity / comparisons as f64
        } else {
            0.0
        }
    }

    /// Calculate similarity between two words (simplified)
    fn calculate_word_similarity(&self, word1: &str, word2: &str) -> f64 {
        if word1 == word2 {
            return 1.0;
        }

        // Simple character-based similarity
        let chars1: Vec<char> = word1.chars().collect();
        let chars2: Vec<char> = word2.chars().collect();

        if chars1.is_empty() || chars2.is_empty() {
            return 0.0;
        }

        // Calculate edit distance-based similarity
        let max_len = chars1.len().max(chars2.len()) as f64;
        let edit_distance = self.calculate_edit_distance(&chars1, &chars2) as f64;

        (max_len - edit_distance) / max_len
    }

    /// Calculate edit distance between character sequences
    fn calculate_edit_distance(&self, chars1: &[char], chars2: &[char]) -> usize {
        let len1 = chars1.len();
        let len2 = chars2.len();

        if len1 == 0 {
            return len2;
        }
        if len2 == 0 {
            return len1;
        }

        let mut matrix = vec![vec![0; len2 + 1]; len1 + 1];

        // Initialize first row and column
        for i in 0..=len1 {
            matrix[i][0] = i;
        }
        for j in 0..=len2 {
            matrix[0][j] = j;
        }

        for i in 1..=len1 {
            for j in 1..=len2 {
                let cost = if chars1[i - 1] == chars2[j - 1] { 0 } else { 1 };
                matrix[i][j] = (matrix[i - 1][j] + 1)
                    .min(matrix[i][j - 1] + 1)
                    .min(matrix[i - 1][j - 1] + cost);
            }
        }

        matrix[len1][len2]
    }

    /// Calculate section coherence
    fn calculate_section_coherence(
        &self,
        paragraphs: &[String],
    ) -> Result<f64, StructuralCoherenceError> {
        if paragraphs.len() < 2 {
            return Ok(1.0);
        }

        let mut total_coherence = 0.0;
        let mut pair_count = 0;

        for i in 0..paragraphs.len() - 1 {
            let coherence =
                self.calculate_paragraph_pair_coherence(&paragraphs[i], &paragraphs[i + 1]);
            total_coherence += coherence;
            pair_count += 1;
        }

        Ok(if pair_count > 0 {
            total_coherence / pair_count as f64
        } else {
            1.0
        })
    }

    /// Calculate coherence between two paragraphs
    fn calculate_paragraph_pair_coherence(&self, para1: &str, para2: &str) -> f64 {
        let words1 = self.extract_content_words(para1);
        let words2 = self.extract_content_words(para2);

        if words1.is_empty() || words2.is_empty() {
            return 0.0;
        }

        let set1: std::collections::HashSet<_> = words1.iter().collect();
        let set2: std::collections::HashSet<_> = words2.iter().collect();

        let intersection = set1.intersection(&set2).count() as f64;
        let min_size = set1.len().min(set2.len()) as f64;

        if min_size > 0.0 {
            intersection / min_size
        } else {
            0.0
        }
    }

    /// Calculate organizational coherence
    fn calculate_organizational_coherence(
        &self,
        paragraphs: &[String],
    ) -> Result<f64, StructuralCoherenceError> {
        let introduction_quality = self.calculate_introduction_quality(paragraphs);
        let development_quality = self.calculate_development_quality(paragraphs);
        let conclusion_quality = self.calculate_conclusion_quality(paragraphs);

        // Weighted combination
        Ok(introduction_quality * 0.2 + development_quality * 0.6 + conclusion_quality * 0.2)
    }

    /// Calculate introduction quality
    fn calculate_introduction_quality(&self, paragraphs: &[String]) -> f64 {
        if paragraphs.is_empty() {
            return 0.0;
        }

        let first_paragraph = &paragraphs[0];
        let intro_keywords = ["introduction", "overview", "first", "begin", "start"];

        let keyword_score = intro_keywords
            .iter()
            .map(|keyword| {
                if first_paragraph.to_lowercase().contains(keyword) {
                    1.0
                } else {
                    0.0
                }
            })
            .sum::<f64>()
            / intro_keywords.len() as f64;

        // Length appropriateness (introductions should be substantial but not too long)
        let length_score = if first_paragraph.len() > 100 && first_paragraph.len() < 500 {
            1.0
        } else {
            0.7
        };

        (keyword_score + length_score) / 2.0
    }

    /// Calculate development quality
    fn calculate_development_quality(&self, paragraphs: &[String]) -> f64 {
        if paragraphs.len() < 3 {
            return 0.8; // Short documents get benefit of doubt
        }

        let body_paragraphs = &paragraphs[1..paragraphs.len() - 1];
        let mut development_score = 0.0;

        for paragraph in body_paragraphs {
            // Check for development indicators
            let development_keywords = [
                "furthermore",
                "moreover",
                "additionally",
                "however",
                "therefore",
            ];
            let has_development = development_keywords
                .iter()
                .any(|keyword| paragraph.to_lowercase().contains(keyword));

            development_score += if has_development { 1.0 } else { 0.5 };
        }

        development_score / body_paragraphs.len() as f64
    }

    /// Calculate conclusion quality
    fn calculate_conclusion_quality(&self, paragraphs: &[String]) -> f64 {
        if paragraphs.is_empty() {
            return 0.0;
        }

        let last_paragraph = &paragraphs[paragraphs.len() - 1];
        let conclusion_keywords = ["conclusion", "summary", "finally", "in summary", "overall"];

        let keyword_score = conclusion_keywords
            .iter()
            .map(|keyword| {
                if last_paragraph.to_lowercase().contains(keyword) {
                    1.0
                } else {
                    0.0
                }
            })
            .sum::<f64>()
            / conclusion_keywords.len() as f64;

        // Length appropriateness
        let length_score = if last_paragraph.len() > 80 { 1.0 } else { 0.7 };

        (keyword_score + length_score) / 2.0
    }

    /// Calculate hierarchical coherence using the hierarchical analyzer
    fn calculate_hierarchical_coherence(
        &self,
        paragraphs: &[String],
    ) -> Result<f64, StructuralCoherenceError> {
        let analysis = self
            .hierarchical_analyzer
            .analyze_hierarchical_structure(paragraphs)?;
        Ok(analysis.consistency_score)
    }

    /// Calculate paragraph transitions
    fn calculate_paragraph_transitions(
        &self,
        paragraphs: &[String],
    ) -> Result<Vec<f64>, StructuralCoherenceError> {
        if paragraphs.len() < 2 {
            return Ok(Vec::new());
        }

        let mut transitions = Vec::new();

        for i in 0..paragraphs.len() - 1 {
            let transition_quality =
                self.calculate_paragraph_pair_coherence(&paragraphs[i], &paragraphs[i + 1]);
            transitions.push(transition_quality);
        }

        Ok(transitions)
    }

    /// Extract structural markers using the marker analyzer
    fn extract_structural_markers(
        &self,
        paragraphs: &[String],
    ) -> Result<Vec<String>, StructuralCoherenceError> {
        let analysis = self
            .marker_analyzer
            .analyze_structural_markers(paragraphs)?;

        let markers = analysis
            .markers_by_type
            .values()
            .flat_map(|marker_list| marker_list.iter())
            .map(|marker| marker.text.clone())
            .collect();

        Ok(markers)
    }

    /// Identify coherence patterns
    fn identify_coherence_patterns(
        &self,
        paragraphs: &[String],
    ) -> Result<HashMap<String, f64>, StructuralCoherenceError> {
        let mut patterns = HashMap::new();

        let discourse_analysis = self
            .discourse_analyzer
            .analyze_discourse_patterns(paragraphs)?;
        patterns.extend(discourse_analysis.pattern_coherence_scores);

        Ok(patterns)
    }

    /// Calculate structural consistency
    fn calculate_structural_consistency(
        &self,
        paragraphs: &[String],
    ) -> Result<f64, StructuralCoherenceError> {
        // Calculate consistency across multiple dimensions
        let length_consistency = self.calculate_length_consistency(paragraphs);
        let marker_consistency = self.calculate_marker_consistency(paragraphs)?;
        let pattern_consistency = self.calculate_pattern_consistency(paragraphs)?;

        // Weighted average
        Ok(length_consistency * 0.3 + marker_consistency * 0.4 + pattern_consistency * 0.3)
    }

    /// Calculate length consistency across paragraphs
    fn calculate_length_consistency(&self, paragraphs: &[String]) -> f64 {
        if paragraphs.len() < 2 {
            return 1.0;
        }

        let lengths: Vec<usize> = paragraphs.iter().map(|p| p.len()).collect();
        let mean_length = lengths.iter().sum::<usize>() as f64 / lengths.len() as f64;

        let variance = lengths
            .iter()
            .map(|&len| (len as f64 - mean_length).powi(2))
            .sum::<f64>()
            / lengths.len() as f64;

        let coefficient_of_variation = if mean_length > 0.0 {
            variance.sqrt() / mean_length
        } else {
            0.0
        };

        // Lower variation = higher consistency
        (1.0 - coefficient_of_variation.min(1.0)).max(0.0)
    }

    /// Calculate marker consistency
    fn calculate_marker_consistency(
        &self,
        paragraphs: &[String],
    ) -> Result<f64, StructuralCoherenceError> {
        let marker_analysis = self
            .marker_analyzer
            .analyze_structural_markers(paragraphs)?;
        Ok(marker_analysis.distribution_analysis.evenness_score)
    }

    /// Calculate pattern consistency
    fn calculate_pattern_consistency(
        &self,
        paragraphs: &[String],
    ) -> Result<f64, StructuralCoherenceError> {
        let discourse_analysis = self
            .discourse_analyzer
            .analyze_discourse_patterns(paragraphs)?;
        Ok(discourse_analysis.pattern_consistency)
    }

    /// Build detailed metrics using all analyzers
    fn build_detailed_metrics(
        &self,
        paragraphs: &[String],
    ) -> Result<DetailedStructuralMetrics, StructuralCoherenceError> {
        let total_paragraphs = paragraphs.len();
        let average_paragraph_length = if !paragraphs.is_empty() {
            paragraphs
                .iter()
                .map(|p| p.split_whitespace().count())
                .sum::<usize>() as f64
                / paragraphs.len() as f64
        } else {
            0.0
        };

        let paragraph_length_distribution = paragraphs
            .iter()
            .map(|p| p.split_whitespace().count())
            .collect();

        let hierarchical_structure = if self.config.hierarchical.enable_analysis {
            self.hierarchical_analyzer
                .analyze_hierarchical_structure(paragraphs)?
        } else {
            results::HierarchicalStructureAnalysis::default()
        };

        let discourse_patterns = if self.config.discourse.enable_detection {
            self.discourse_analyzer
                .analyze_discourse_patterns(paragraphs)?
        } else {
            results::DiscoursePatternAnalysis::default()
        };

        let marker_analysis = if self.config.markers.enable_analysis {
            self.marker_analyzer
                .analyze_structural_markers(paragraphs)?
        } else {
            results::StructuralMarkerAnalysis::default()
        };

        Ok(DetailedStructuralMetrics {
            total_paragraphs,
            average_paragraph_length,
            paragraph_length_distribution,
            section_boundaries: Vec::new(), // Would be populated by boundary detection
            hierarchical_structure,
            discourse_patterns,
            marker_analysis,
            global_coherence: results::GlobalCoherenceMetrics::default(),
            document_completeness: results::DocumentCompletenessMetrics::default(),
            complexity_measures: results::StructuralComplexityMetrics::default(),
        })
    }

    /// Analyze document structure
    fn analyze_document_structure(
        &self,
        _paragraphs: &[String],
    ) -> Result<DocumentStructureAnalysis, StructuralCoherenceError> {
        Ok(DocumentStructureAnalysis {
            detected_structure_type: self
                .config
                .general
                .expected_structure_type
                .clone()
                .unwrap_or(DocumentStructureType::Unknown),
            detection_confidence: 0.8,
            genre_compliance: 0.7,
            missing_elements: Vec::new(),
            violations: Vec::new(),
            structure_quality: 0.8,
        })
    }

    /// Perform advanced analysis
    fn perform_advanced_analysis(
        &self,
        _paragraphs: &[String],
    ) -> Result<results::AdvancedStructuralAnalysis, StructuralCoherenceError> {
        Ok(results::AdvancedStructuralAnalysis {
            rhetorical_structure: results::RhetoricalStructureAnalysis {
                rhetorical_moves: Vec::new(),
                move_sequencing: 0.7,
                rhetorical_effectiveness: 0.8,
                argument_structure: 0.7,
            },
            genre_analysis: results::GenreAnalysis {
                conventions_adherence: 0.8,
                structure_alignment: 0.7,
                genre_quality_metrics: HashMap::new(),
                deviation_analysis: Vec::new(),
            },
            reader_experience: results::ReaderExperienceMetrics {
                navigation_ease: 0.8,
                information_accessibility: 0.7,
                cognitive_load: 0.6,
                reading_flow: 0.8,
                comprehension_support: 0.7,
            },
            optimization_suggestions: Vec::new(),
            cross_reference_analysis: results::CrossReferenceAnalysis {
                internal_references: 0.5,
                reference_consistency: 0.6,
                navigation_support: 0.5,
                reference_effectiveness: HashMap::new(),
            },
        })
    }
}

impl Default for StructuralCoherenceAnalyzer {
    fn default() -> Self {
        Self::with_defaults().expect("Default configuration should be valid")
    }
}