anno-eval 0.8.0

Evaluation harnesses, datasets, and muxer-backed sampling for anno
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
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
//! Book-Scale Coreference Diagnostics
//!
//! This module provides enhanced diagnostics for evaluating coreference
//! resolution at book scale, incorporating insights from BOOKCOREF
//! (Martinelli et al., 2025) and "How to Evaluate Coreference in Literary
//! Texts?" (Duron-Tejedor et al., 2023; arXiv:2401.00238).
//!
//! # The Book-Scale Challenge
//!
//! Coreference systems optimized for short documents can show different behavior
//! at book scale. In particular, common metrics can diverge in what they “reward”
//! (e.g., favoring long chains vs penalizing boundary errors).
//!
//! # Key Insights
//!
//! 1. **Metric divergence**: different metrics can disagree substantially at scale.
//! 2. **Windowed vs full**: long-range dependencies can be masked by windowed evaluation.
//! 3. **Long chains dominate**: a few long chains can dominate aggregate scores.
//!
//! # This Module Provides
//!
//! - `BookScaleAnalysis`: Complete diagnostic report
//! - `PerBookBreakdown`: Individual book performance (like Table 3)
//! - `ChainLengthStratification`: Performance by chain length
//! - `WindowedVsFullComparison`: Diagnose long-range dependency issues
//! - `MetricReliabilityAssessment`: Which metrics to trust at this scale
//!
//! # Example
//!
//! ```rust,ignore
//! use anno::eval::book_scale::{BookScaleAnalyzer, BookScaleConfig};
//! use anno::eval::coref::CorefChain;
//!
//! let analyzer = BookScaleAnalyzer::new(BookScaleConfig::default());
//!
//! let analysis = analyzer.analyze(
//!     &predicted_chains,
//!     &gold_chains,
//!     document_length,
//! );
//!
//! if analysis.has_scale_issues() {
//!     println!("Book-scale issues detected:");
//!     println!("{}", analysis.diagnostic_report());
//! }
//! ```

use super::coref::{CorefChain, Mention};
use super::coref_metrics::{b_cubed_score, ceaf_e_score, ceaf_m_score, lea_score, muc_score};
use super::types::{CorefDocStats, DocumentScale, MetricDivergence};
use serde::{Deserialize, Serialize};

// =============================================================================
// Serializable Score Types (self-contained for book_scale module)
// =============================================================================

/// Precision/Recall/F1 scores (serializable).
#[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize)]
pub struct Scores {
    /// Precision
    pub precision: f64,
    /// Recall
    pub recall: f64,
    /// F1 score
    pub f1: f64,
}

impl Scores {
    /// Create from tuple (p, r, f1).
    pub fn from_tuple((precision, recall, f1): (f64, f64, f64)) -> Self {
        Self {
            precision,
            recall,
            f1,
        }
    }
}

// =============================================================================
// Configuration
// =============================================================================

/// Configuration for book-scale analysis.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BookScaleConfig {
    /// Window size for windowed evaluation (tokens)
    pub window_size: usize,
    /// Overlap between windows
    pub window_overlap: usize,
    /// Threshold for "long" chains (mentions)
    pub long_chain_threshold: usize,
    /// Threshold for "short" chains (mentions)
    pub short_chain_threshold: usize,
    /// MUC-CEAF divergence threshold for scale issues
    pub divergence_threshold: f64,
    /// Performance drop threshold (windowed → full) for concerno
    pub performance_drop_threshold: f64,
}

impl Default for BookScaleConfig {
    fn default() -> Self {
        Self {
            window_size: 1500,
            window_overlap: 200,
            long_chain_threshold: 10,         // >10 mentions = long chain
            short_chain_threshold: 2,         // 2-10 = short, 1 = singleton
            divergence_threshold: 0.30,       // 30 F1 point gap
            performance_drop_threshold: 0.15, // 15 F1 point drop
        }
    }
}

// =============================================================================
// Book-Scale Analysis
// =============================================================================

/// Coreference evaluation scores (serializable version).
///
/// This is a simplified, serializable version of the full CorefEvaluation.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CorefEvalScores {
    /// MUC scores
    pub muc: Scores,
    /// B³ scores
    pub b_cubed: Scores,
    /// CEAF-e scores
    pub ceaf_e: Scores,
    /// CEAF-m scores
    pub ceaf_m: Scores,
    /// LEA scores
    pub lea: Scores,
    /// CoNLL F1 (average of MUC, B³, CEAF-e)
    pub conll_f1: f64,
}

/// Complete book-scale analysis results.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BookScaleAnalysis {
    /// Full document evaluation
    pub full_doc_eval: CorefEvalScores,
    /// Windowed evaluation (average over windows)
    pub windowed_eval: Option<WindowedEvaluation>,
    /// Per-chain-length stratified evaluation
    pub stratified: StratifiedEvaluation,
    /// Document statistics
    pub doc_stats: CorefDocStats,
    /// Metric reliability assessment
    pub reliability: MetricReliability,
    /// Overall scale classification
    pub scale: DocumentScale,
    /// Diagnostic flags
    pub diagnostics: BookScaleDiagnostics,
}

impl BookScaleAnalysis {
    /// Check if document has book-scale issues.
    pub fn has_scale_issues(&self) -> bool {
        self.diagnostics.has_issues()
    }

    /// Generate a diagnostic report.
    pub fn diagnostic_report(&self) -> String {
        let mut report = String::new();

        report.push_str("=== Book-Scale Coreference Analysis ===\n\n");
        report.push_str(&format!("Document Scale: {}\n", self.scale));
        report.push_str(&format!(
            "Document Length: {} chars ({} mentions in {} chains)\n\n",
            self.doc_stats.doc_length, self.doc_stats.mention_count, self.doc_stats.chain_count
        ));

        // Metrics summary
        report.push_str("Full-Document Metrics:\n");
        report.push_str(&format!(
            "  MUC:    {:.1}%\n",
            self.full_doc_eval.muc.f1 * 100.0
        ));
        report.push_str(&format!(
            "  B³:     {:.1}%\n",
            self.full_doc_eval.b_cubed.f1 * 100.0
        ));
        report.push_str(&format!(
            "  CEAF-e: {:.1}%\n",
            self.full_doc_eval.ceaf_e.f1 * 100.0
        ));
        report.push_str(&format!(
            "  CoNLL:  {:.1}%\n\n",
            self.full_doc_eval.conll_f1 * 100.0
        ));

        // Windowed comparison
        if let Some(ref windowed) = self.windowed_eval {
            report.push_str("Windowed vs Full-Document Comparison:\n");
            report.push_str(&format!(
                "  Windowed CoNLL:  {:.1}%\n",
                windowed.avg_conll_f1 * 100.0
            ));
            report.push_str(&format!(
                "  Full-Doc CoNLL:  {:.1}%\n",
                self.full_doc_eval.conll_f1 * 100.0
            ));
            report.push_str(&format!(
                "  Performance Drop: {:.1} F1 points\n\n",
                windowed.performance_drop * 100.0
            ));
        }

        // Stratified evaluation
        report.push_str("Chain-Length Stratified Evaluation:\n");
        report.push_str(&format!(
            "  Long chains (>10):  {:.1}% F1 ({} chains)\n",
            self.stratified.long_chains.f1 * 100.0,
            self.stratified.long_chain_count
        ));
        report.push_str(&format!(
            "  Short chains (2-10): {:.1}% F1 ({} chains)\n",
            self.stratified.short_chains.f1 * 100.0,
            self.stratified.short_chain_count
        ));
        report.push_str(&format!(
            "  Singletons (1):     {:.1}% F1 ({} chains)\n\n",
            self.stratified.singletons.f1 * 100.0,
            self.stratified.singleton_count
        ));

        // Reliability assessment
        report.push_str("Metric Reliability:\n");
        report.push_str(&format!(
            "  MUC:    {} ({})\n",
            self.reliability.muc_reliability, self.reliability.muc_note
        ));
        report.push_str(&format!(
            "  B³:     {} ({})\n",
            self.reliability.b_cubed_reliability, self.reliability.b_cubed_note
        ));
        report.push_str(&format!(
            "  CEAF-e: {} ({})\n",
            self.reliability.ceaf_e_reliability, self.reliability.ceaf_e_note
        ));
        report.push_str(&format!(
            "  LEA:    {} ({})\n\n",
            self.reliability.lea_reliability, self.reliability.lea_note
        ));

        // Diagnostics
        if self.has_scale_issues() {
            report.push_str("⚠️ ISSUES DETECTED:\n");
            if self.diagnostics.high_metric_divergence {
                report
                    .push_str("  • High metric divergence - MUC and CEAF disagree significantly\n");
            }
            if self.diagnostics.large_performance_drop {
                report.push_str(
                    "  • Large windowed→full performance drop - long-range dependencies failing\n",
                );
            }
            if self.diagnostics.long_chain_dominance {
                report.push_str("  • Long chains dominate - main characters skewing metrics\n");
            }
            if self.diagnostics.singleton_neglect {
                report.push_str("  • Singleton neglect - minor entities being ignored\n");
            }
            report.push_str("\nRECOMMENDATIONS:\n");
            for rec in &self.diagnostics.recommendations {
                report.push_str(&format!("{}\n", rec));
            }
        } else {
            report.push_str("✓ No significant scale issues detected.\n");
        }

        report
    }
}

/// Windowed evaluation results.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WindowedEvaluation {
    /// Number of windows
    pub num_windows: usize,
    /// Window size used
    pub window_size: usize,
    /// Average CoNLL F1 across windows
    pub avg_conll_f1: f64,
    /// Standard deviation of CoNLL F1
    pub std_conll_f1: f64,
    /// Performance drop (windowed - full_doc)
    pub performance_drop: f64,
    /// Per-window evaluations
    pub window_evals: Vec<CorefEvalScores>,
}

/// Stratified evaluation by chain length.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct StratifiedEvaluation {
    /// Evaluation on long chains (>threshold mentions)
    pub long_chains: Scores,
    /// Evaluation on short chains (2-threshold mentions)
    pub short_chains: Scores,
    /// Evaluation on singletons
    pub singletons: Scores,
    /// Count of long chains
    pub long_chain_count: usize,
    /// Count of short chains
    pub short_chain_count: usize,
    /// Count of singletons
    pub singleton_count: usize,
}

/// Reliability assessment for each coreference metric at book scale.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricReliability {
    /// MUC metric reliability level
    pub muc_reliability: ReliabilityLevel,
    /// Notes about MUC reliability
    pub muc_note: String,
    /// B-cubed metric reliability level
    pub b_cubed_reliability: ReliabilityLevel,
    /// Notes about B-cubed reliability
    pub b_cubed_note: String,
    /// CEAF-e metric reliability level
    pub ceaf_e_reliability: ReliabilityLevel,
    /// Notes about CEAF-e reliability
    pub ceaf_e_note: String,
    /// LEA metric reliability level
    pub lea_reliability: ReliabilityLevel,
    /// Notes about LEA reliability
    pub lea_note: String,
}

impl Default for MetricReliability {
    fn default() -> Self {
        Self {
            muc_reliability: ReliabilityLevel::Medium,
            muc_note: "May be inflated at scale".to_string(),
            b_cubed_reliability: ReliabilityLevel::Medium,
            b_cubed_note: "Moderate reliability".to_string(),
            ceaf_e_reliability: ReliabilityLevel::Medium,
            ceaf_e_note: "May collapse at scale".to_string(),
            lea_reliability: ReliabilityLevel::High,
            lea_note: "Most stable across scales".to_string(),
        }
    }
}

/// Reliability level for a metric at book scale.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ReliabilityLevel {
    /// High reliability - metric is stable at scale
    High,
    /// Medium reliability - some degradation expected
    Medium,
    /// Low reliability - significant variance at scale
    Low,
    /// Unreliable - metric not recommended for book-length texts
    Unreliable,
}

impl std::fmt::Display for ReliabilityLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ReliabilityLevel::High => write!(f, "HIGH"),
            ReliabilityLevel::Medium => write!(f, "MEDIUM"),
            ReliabilityLevel::Low => write!(f, "LOW"),
            ReliabilityLevel::Unreliable => write!(f, "UNRELIABLE"),
        }
    }
}

/// Diagnostic flags for book-scale issues.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct BookScaleDiagnostics {
    /// MUC-CEAF divergence exceeds threshold
    pub high_metric_divergence: bool,
    /// Windowed→Full performance drop exceeds threshold
    pub large_performance_drop: bool,
    /// Long chains dominate the evaluation
    pub long_chain_dominance: bool,
    /// Singletons have very low performance
    pub singleton_neglect: bool,
    /// Recommendations for improvement
    pub recommendations: Vec<String>,
}

impl BookScaleDiagnostics {
    /// Check if any issues were detected.
    pub fn has_issues(&self) -> bool {
        self.high_metric_divergence
            || self.large_performance_drop
            || self.long_chain_dominance
            || self.singleton_neglect
    }
}

// =============================================================================
// Analyzer
// =============================================================================

/// Book-scale coreference analyzer.
pub struct BookScaleAnalyzer {
    config: BookScaleConfig,
}

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

impl BookScaleAnalyzer {
    /// Create a new analyzer with configuration.
    pub fn new(config: BookScaleConfig) -> Self {
        Self { config }
    }

    /// Perform complete book-scale analysis.
    pub fn analyze(
        &self,
        predicted: &[CorefChain],
        gold: &[CorefChain],
        doc_length: usize,
    ) -> BookScaleAnalysis {
        // Compute full-document evaluation
        let full_doc_eval = self.evaluate_chains(predicted, gold);

        // Compute document statistics
        let mut doc_stats = CorefDocStats::from_chains(gold);
        doc_stats.doc_length = doc_length;

        // Determine scale
        let scale = doc_stats.scale_classification();

        // Compute windowed evaluation (if document is long enough)
        let windowed_eval = if doc_length > self.config.window_size * 2 {
            Some(self.compute_windowed_eval(predicted, gold, doc_length))
        } else {
            None
        };

        // Compute stratified evaluation
        let stratified = self.compute_stratified_eval(predicted, gold);

        // Assess metric reliability
        let reliability = self.assess_reliability(&full_doc_eval, &stratified, scale);

        // Generate diagnostics
        let diagnostics =
            self.generate_diagnostics(&full_doc_eval, windowed_eval.as_ref(), &stratified, scale);

        BookScaleAnalysis {
            full_doc_eval,
            windowed_eval,
            stratified,
            doc_stats,
            reliability,
            scale,
            diagnostics,
        }
    }

    /// Evaluate chains using standard metrics.
    fn evaluate_chains(&self, predicted: &[CorefChain], gold: &[CorefChain]) -> CorefEvalScores {
        let muc = Scores::from_tuple(muc_score(predicted, gold));
        let b_cubed = Scores::from_tuple(b_cubed_score(predicted, gold));
        let ceaf_e = Scores::from_tuple(ceaf_e_score(predicted, gold));
        let ceaf_m = Scores::from_tuple(ceaf_m_score(predicted, gold));
        let lea = Scores::from_tuple(lea_score(predicted, gold));

        let conll_f1 = (muc.f1 + b_cubed.f1 + ceaf_e.f1) / 3.0;

        CorefEvalScores {
            muc,
            b_cubed,
            ceaf_e,
            ceaf_m,
            lea,
            conll_f1,
        }
    }

    /// Compute windowed evaluation.
    fn compute_windowed_eval(
        &self,
        predicted: &[CorefChain],
        gold: &[CorefChain],
        doc_length: usize,
    ) -> WindowedEvaluation {
        let step = self
            .config
            .window_size
            .saturating_sub(self.config.window_overlap);
        let mut window_evals = Vec::new();

        let mut offset = 0;
        while offset < doc_length {
            let window_end = (offset + self.config.window_size).min(doc_length);

            // Filter chains to this window
            let pred_window = self.filter_to_window(predicted, offset, window_end);
            let gold_window = self.filter_to_window(gold, offset, window_end);

            if !pred_window.is_empty() || !gold_window.is_empty() {
                let eval = self.evaluate_chains(&pred_window, &gold_window);
                window_evals.push(eval);
            }

            if window_end >= doc_length {
                break;
            }
            offset += step.max(1);
        }

        // Compute statistics
        let conll_scores: Vec<f64> = window_evals.iter().map(|e| e.conll_f1).collect();
        let avg_conll_f1 = if !conll_scores.is_empty() {
            conll_scores.iter().sum::<f64>() / conll_scores.len() as f64
        } else {
            0.0
        };

        let std_conll_f1 = if conll_scores.len() > 1 {
            let variance = conll_scores
                .iter()
                .map(|x| (x - avg_conll_f1).powi(2))
                .sum::<f64>()
                / (conll_scores.len() - 1) as f64;
            variance.sqrt()
        } else {
            0.0
        };

        // Performance drop (positive = windowed is better)
        let full_doc_eval = self.evaluate_chains(predicted, gold);
        let performance_drop = avg_conll_f1 - full_doc_eval.conll_f1;

        WindowedEvaluation {
            num_windows: window_evals.len(),
            window_size: self.config.window_size,
            avg_conll_f1,
            std_conll_f1,
            performance_drop,
            window_evals,
        }
    }

    /// Filter chains to a specific window.
    fn filter_to_window(&self, chains: &[CorefChain], start: usize, end: usize) -> Vec<CorefChain> {
        chains
            .iter()
            .filter_map(|chain| {
                let filtered_mentions: Vec<Mention> = chain
                    .mentions
                    .iter()
                    .filter(|m| m.start >= start && m.end <= end)
                    .cloned()
                    .collect();

                if filtered_mentions.is_empty() {
                    None
                } else {
                    let mut new_chain = CorefChain::new(filtered_mentions);
                    new_chain.cluster_id = chain.cluster_id;
                    new_chain.entity_type = chain.entity_type.clone();
                    Some(new_chain)
                }
            })
            .collect()
    }

    /// Compute stratified evaluation by chain length.
    fn compute_stratified_eval(
        &self,
        predicted: &[CorefChain],
        gold: &[CorefChain],
    ) -> StratifiedEvaluation {
        let (pred_long, pred_short, pred_singleton) = self.stratify_chains(predicted);
        let (gold_long, gold_short, gold_singleton) = self.stratify_chains(gold);

        let long_chains = if !pred_long.is_empty() || !gold_long.is_empty() {
            Scores::from_tuple(muc_score(&pred_long, &gold_long))
        } else {
            Scores::default()
        };

        let short_chains = if !pred_short.is_empty() || !gold_short.is_empty() {
            Scores::from_tuple(muc_score(&pred_short, &gold_short))
        } else {
            Scores::default()
        };

        let singletons = if !pred_singleton.is_empty() || !gold_singleton.is_empty() {
            // Singletons need different handling - use B³ or exact match
            Scores::from_tuple(b_cubed_score(&pred_singleton, &gold_singleton))
        } else {
            Scores::default()
        };

        StratifiedEvaluation {
            long_chains,
            short_chains,
            singletons,
            long_chain_count: gold_long.len(),
            short_chain_count: gold_short.len(),
            singleton_count: gold_singleton.len(),
        }
    }

    /// Stratify chains by length.
    fn stratify_chains(
        &self,
        chains: &[CorefChain],
    ) -> (Vec<CorefChain>, Vec<CorefChain>, Vec<CorefChain>) {
        let mut long = Vec::new();
        let mut short = Vec::new();
        let mut singleton = Vec::new();

        for chain in chains {
            let len = chain.len();
            if len > self.config.long_chain_threshold {
                long.push(chain.clone());
            } else if len >= self.config.short_chain_threshold {
                short.push(chain.clone());
            } else {
                singleton.push(chain.clone());
            }
        }

        (long, short, singleton)
    }

    /// Assess metric reliability based on document characteristics.
    fn assess_reliability(
        &self,
        eval: &CorefEvalScores,
        _stratified: &StratifiedEvaluation,
        scale: DocumentScale,
    ) -> MetricReliability {
        let divergence =
            MetricDivergence::from_scores(eval.muc.f1, eval.b_cubed.f1, eval.ceaf_e.f1);

        // MUC reliability
        let (muc_rel, muc_note) = if divergence.muc_ceaf_divergence > 0.40 {
            (
                ReliabilityLevel::Low,
                "Severely inflated due to long chains".to_string(),
            )
        } else if divergence.muc_ceaf_divergence > 0.25 {
            (
                ReliabilityLevel::Medium,
                "May be inflated at this scale".to_string(),
            )
        } else {
            (ReliabilityLevel::High, "Reliable at this scale".to_string())
        };

        // CEAF-e reliability
        let (ceaf_rel, ceaf_note) = match scale {
            DocumentScale::BookScale => (
                ReliabilityLevel::Low,
                "Known to collapse at book scale".to_string(),
            ),
            DocumentScale::Long => (
                ReliabilityLevel::Medium,
                "May underestimate at this length".to_string(),
            ),
            _ => (ReliabilityLevel::High, "Reliable at this scale".to_string()),
        };

        // B³ reliability
        let (b3_rel, b3_note) = if divergence.muc_b3_divergence > 0.30 {
            (
                ReliabilityLevel::Medium,
                "Moderate divergence from MUC".to_string(),
            )
        } else {
            (ReliabilityLevel::High, "Stable metric".to_string())
        };

        // LEA is generally most stable
        let (lea_rel, lea_note) = (
            ReliabilityLevel::High,
            "Most stable across document scales".to_string(),
        );

        MetricReliability {
            muc_reliability: muc_rel,
            muc_note,
            b_cubed_reliability: b3_rel,
            b_cubed_note: b3_note,
            ceaf_e_reliability: ceaf_rel,
            ceaf_e_note: ceaf_note,
            lea_reliability: lea_rel,
            lea_note,
        }
    }

    /// Generate diagnostic flags and recommendations.
    fn generate_diagnostics(
        &self,
        eval: &CorefEvalScores,
        windowed: Option<&WindowedEvaluation>,
        stratified: &StratifiedEvaluation,
        scale: DocumentScale,
    ) -> BookScaleDiagnostics {
        let mut diagnostics = BookScaleDiagnostics::default();

        // Check metric divergence
        let divergence = (eval.muc.f1 - eval.ceaf_e.f1).abs();
        if divergence > self.config.divergence_threshold {
            diagnostics.high_metric_divergence = true;
            diagnostics
                .recommendations
                .push("Use LEA or stratified metrics instead of CoNLL F1".to_string());
        }

        // Check performance drop
        if let Some(w) = windowed {
            if w.performance_drop > self.config.performance_drop_threshold {
                diagnostics.large_performance_drop = true;
                diagnostics.recommendations.push(
                    "Consider incremental/streaming coref approach (Longdoc-style)".to_string(),
                );
            }
        }

        // Check long chain dominance
        let total_chains =
            stratified.long_chain_count + stratified.short_chain_count + stratified.singleton_count;
        if total_chains > 0 {
            let _long_chain_ratio = stratified.long_chain_count as f64 / total_chains as f64;
            // Even if few long chains, they might dominate mentions
            if stratified.long_chains.f1 > stratified.short_chains.f1 + 0.20 {
                diagnostics.long_chain_dominance = true;
                diagnostics
                    .recommendations
                    .push("Report per-chain-length metrics separately".to_string());
            }
        }

        // Check singleton neglect
        if stratified.singleton_count > 0 && stratified.singletons.f1 < 0.50 {
            diagnostics.singleton_neglect = true;
            diagnostics
                .recommendations
                .push("System may be ignoring minor entities".to_string());
        }

        // Scale-specific recommendations
        match scale {
            DocumentScale::BookScale => {
                diagnostics
                    .recommendations
                    .push("Consider BOOKCOREF-style windowed+grouped evaluation".to_string());
            }
            DocumentScale::Long => {
                diagnostics
                    .recommendations
                    .push("Monitor for metric divergence as length increases".to_string());
            }
            _ => {}
        }

        diagnostics
    }
}

// =============================================================================
// Per-Book Breakdown (like BOOKCOREF Table 3)
// =============================================================================

/// Per-book evaluation breakdown.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerBookEvaluation {
    /// Book identifier
    pub book_id: String,
    /// Book title (optional)
    pub title: Option<String>,
    /// Author (optional)
    pub author: Option<String>,
    /// Token count
    pub token_count: usize,
    /// Full-document evaluation
    pub full_doc: CorefEvalScores,
    /// Windowed evaluation
    pub windowed: Option<WindowedEvaluation>,
    /// Scale classification
    pub scale: DocumentScale,
}

/// Multi-book evaluation report.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultiBookReport {
    /// Individual book evaluations
    pub books: Vec<PerBookEvaluation>,
    /// Aggregate statistics
    pub aggregate: AggregateStats,
}

/// Aggregate statistics across multiple books.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AggregateStats {
    /// Total books
    pub total_books: usize,
    /// Mean CoNLL F1 (full doc)
    pub mean_conll_f1: f64,
    /// Std dev of CoNLL F1
    pub std_conll_f1: f64,
    /// Mean performance drop (windowed → full)
    pub mean_performance_drop: f64,
    /// Books with scale issues
    pub books_with_issues: usize,
}

impl MultiBookReport {
    /// Generate from per-book evaluations.
    pub fn from_books(books: Vec<PerBookEvaluation>) -> Self {
        let total_books = books.len();

        let conll_scores: Vec<f64> = books.iter().map(|b| b.full_doc.conll_f1).collect();

        let mean_conll_f1 = if !conll_scores.is_empty() {
            conll_scores.iter().sum::<f64>() / conll_scores.len() as f64
        } else {
            0.0
        };

        let std_conll_f1 = if conll_scores.len() > 1 {
            let variance = conll_scores
                .iter()
                .map(|x| (x - mean_conll_f1).powi(2))
                .sum::<f64>()
                / (conll_scores.len() - 1) as f64;
            variance.sqrt()
        } else {
            0.0
        };

        let performance_drops: Vec<f64> = books
            .iter()
            .filter_map(|b| b.windowed.as_ref().map(|w| w.performance_drop))
            .collect();

        let mean_performance_drop = if !performance_drops.is_empty() {
            performance_drops.iter().sum::<f64>() / performance_drops.len() as f64
        } else {
            0.0
        };

        let books_with_issues = books
            .iter()
            .filter(|b| {
                let divergence = (b.full_doc.muc.f1 - b.full_doc.ceaf_e.f1).abs();
                divergence > 0.30
                    || b.windowed
                        .as_ref()
                        .map(|w| w.performance_drop > 0.15)
                        .unwrap_or(false)
            })
            .count();

        let aggregate = AggregateStats {
            total_books,
            mean_conll_f1,
            std_conll_f1,
            mean_performance_drop,
            books_with_issues,
        };

        Self { books, aggregate }
    }

    /// Format as table (similar to BOOKCOREF Table 3).
    pub fn format_table(&self) -> String {
        let mut table = String::new();

        // Header
        table.push_str(&format!(
            "{:<30} {:>8} {:>8} {:>8} {:>8} {:>8}\n",
            "Book", "Tokens", "MUC", "", "CEAF", "CoNLL"
        ));
        table.push_str(&format!("{}\n", "-".repeat(78)));

        // Per-book rows
        for book in &self.books {
            let title = book
                .title
                .as_deref()
                .unwrap_or(&book.book_id)
                .chars()
                .take(28)
                .collect::<String>();

            table.push_str(&format!(
                "{:<30} {:>8} {:>7.1}% {:>7.1}% {:>7.1}% {:>7.1}%\n",
                title,
                book.token_count,
                book.full_doc.muc.f1 * 100.0,
                book.full_doc.b_cubed.f1 * 100.0,
                book.full_doc.ceaf_e.f1 * 100.0,
                book.full_doc.conll_f1 * 100.0,
            ));
        }

        // Separator
        table.push_str(&format!("{}\n", "-".repeat(78)));

        // Aggregate
        table.push_str(&format!(
            "{:<30} {:>8} {:>7.1}% ±{:.1}\n",
            "MEAN",
            "",
            self.aggregate.mean_conll_f1 * 100.0,
            self.aggregate.std_conll_f1 * 100.0
        ));

        table
    }
}

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

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

    fn make_chain(mentions: Vec<(&str, usize, usize)>) -> CorefChain {
        let m: Vec<Mention> = mentions
            .into_iter()
            .map(|(text, start, end)| Mention::new(text, start, end))
            .collect();
        CorefChain::new(m)
    }

    #[test]
    fn test_stratify_chains() {
        let config = BookScaleConfig::default();
        let analyzer = BookScaleAnalyzer::new(config);

        let chains = vec![
            make_chain(vec![("a", 0, 1)]),                           // singleton
            make_chain(vec![("b", 0, 1), ("c", 2, 3), ("d", 4, 5)]), // short
            make_chain((0..15).map(|i| ("x", i * 10, i * 10 + 1)).collect()), // long
        ];

        let (long, short, single) = analyzer.stratify_chains(&chains);
        assert_eq!(single.len(), 1);
        assert_eq!(short.len(), 1);
        assert_eq!(long.len(), 1);
    }

    #[test]
    fn test_reliability_assessment() {
        let config = BookScaleConfig::default();
        let analyzer = BookScaleAnalyzer::new(config);

        let eval = CorefEvalScores {
            muc: Scores {
                precision: 0.9,
                recall: 0.9,
                f1: 0.9,
            },
            b_cubed: Scores {
                precision: 0.7,
                recall: 0.7,
                f1: 0.7,
            },
            ceaf_e: Scores {
                precision: 0.4,
                recall: 0.4,
                f1: 0.4,
            },
            ceaf_m: Scores::default(),
            lea: Scores::default(),
            conll_f1: 0.67,
        };

        let stratified = StratifiedEvaluation::default();
        let reliability = analyzer.assess_reliability(&eval, &stratified, DocumentScale::BookScale);

        // MUC should be low reliability (large divergence)
        assert!(matches!(
            reliability.muc_reliability,
            ReliabilityLevel::Low | ReliabilityLevel::Medium
        ));
    }

    #[test]
    fn test_diagnostics_generation() {
        let config = BookScaleConfig::default();
        let analyzer = BookScaleAnalyzer::new(config);

        let eval = CorefEvalScores {
            muc: Scores {
                precision: 0.93,
                recall: 0.93,
                f1: 0.93,
            },
            b_cubed: Scores {
                precision: 0.62,
                recall: 0.62,
                f1: 0.62,
            },
            ceaf_e: Scores {
                precision: 0.33,
                recall: 0.33,
                f1: 0.33,
            },
            ceaf_m: Scores::default(),
            lea: Scores::default(),
            conll_f1: 0.63,
        };

        let windowed = WindowedEvaluation {
            num_windows: 10,
            window_size: 1500,
            avg_conll_f1: 0.78,
            std_conll_f1: 0.05,
            performance_drop: 0.15,
            window_evals: vec![],
        };

        let stratified = StratifiedEvaluation::default();

        let diagnostics = analyzer.generate_diagnostics(
            &eval,
            Some(&windowed),
            &stratified,
            DocumentScale::BookScale,
        );

        assert!(diagnostics.high_metric_divergence);
        assert!(diagnostics.has_issues());
    }

    #[test]
    fn test_multi_book_report() {
        let books = vec![
            PerBookEvaluation {
                book_id: "animal_farm".to_string(),
                title: Some("Animal Farm".to_string()),
                author: Some("George Orwell".to_string()),
                token_count: 29853,
                full_doc: CorefEvalScores {
                    muc: Scores {
                        precision: 0.9,
                        recall: 0.9,
                        f1: 0.9,
                    },
                    b_cubed: Scores {
                        precision: 0.6,
                        recall: 0.6,
                        f1: 0.6,
                    },
                    ceaf_e: Scores {
                        precision: 0.5,
                        recall: 0.5,
                        f1: 0.5,
                    },
                    ceaf_m: Scores::default(),
                    lea: Scores::default(),
                    conll_f1: 0.67,
                },
                windowed: None,
                scale: DocumentScale::Long,
            },
            PerBookEvaluation {
                book_id: "pride_prejudice".to_string(),
                title: Some("Pride and Prejudice".to_string()),
                author: Some("Jane Austen".to_string()),
                token_count: 121869,
                full_doc: CorefEvalScores {
                    muc: Scores {
                        precision: 0.85,
                        recall: 0.85,
                        f1: 0.85,
                    },
                    b_cubed: Scores {
                        precision: 0.55,
                        recall: 0.55,
                        f1: 0.55,
                    },
                    ceaf_e: Scores {
                        precision: 0.35,
                        recall: 0.35,
                        f1: 0.35,
                    },
                    ceaf_m: Scores::default(),
                    lea: Scores::default(),
                    conll_f1: 0.58,
                },
                windowed: None,
                scale: DocumentScale::BookScale,
            },
        ];

        let report = MultiBookReport::from_books(books);
        assert_eq!(report.aggregate.total_books, 2);
        assert!(report.aggregate.mean_conll_f1 > 0.5);

        let table = report.format_table();
        assert!(table.contains("Animal Farm"));
        assert!(table.contains("Pride and Prejudice"));
    }

    // =========================================================================
    // Additional Edge Case Tests
    // =========================================================================

    #[test]
    fn test_document_scale_classification() {
        // Short: 0-2000 tokens
        assert_eq!(DocumentScale::from_tokens(100), DocumentScale::Short);
        assert_eq!(DocumentScale::from_tokens(2000), DocumentScale::Short);
        // Medium: 2001-10000 tokens
        assert_eq!(DocumentScale::from_tokens(5000), DocumentScale::Medium);
        // Long: 10001-50000 tokens
        assert_eq!(DocumentScale::from_tokens(30000), DocumentScale::Long);
        // BookScale: >50000 tokens
        assert_eq!(DocumentScale::from_tokens(100000), DocumentScale::BookScale);
    }

    #[test]
    fn test_empty_chains_stratification() {
        let config = BookScaleConfig::default();
        let analyzer = BookScaleAnalyzer::new(config);

        let chains: Vec<CorefChain> = vec![];
        let (long, short, single) = analyzer.stratify_chains(&chains);

        assert!(long.is_empty());
        assert!(short.is_empty());
        assert!(single.is_empty());
    }

    #[test]
    fn test_all_singletons() {
        let config = BookScaleConfig::default();
        let analyzer = BookScaleAnalyzer::new(config);

        let chains = vec![
            make_chain(vec![("a", 0, 1)]),
            make_chain(vec![("b", 10, 11)]),
            make_chain(vec![("c", 20, 21)]),
        ];

        let (long, short, single) = analyzer.stratify_chains(&chains);

        assert!(long.is_empty());
        assert!(short.is_empty());
        assert_eq!(single.len(), 3);
    }

    #[test]
    fn test_all_long_chains() {
        let config = BookScaleConfig::default();
        let analyzer = BookScaleAnalyzer::new(config);

        // Create chains with 15+ mentions (long threshold)
        let chains = vec![
            make_chain((0..20).map(|i| ("x", i * 10, i * 10 + 1)).collect()),
            make_chain((0..25).map(|i| ("y", i * 10 + 5, i * 10 + 6)).collect()),
        ];

        let (long, short, single) = analyzer.stratify_chains(&chains);

        assert_eq!(long.len(), 2);
        assert!(short.is_empty());
        assert!(single.is_empty());
    }

    #[test]
    fn test_scores_default() {
        let scores = Scores::default();
        assert!((scores.precision - 0.0).abs() < 0.001);
        assert!((scores.recall - 0.0).abs() < 0.001);
        assert!((scores.f1 - 0.0).abs() < 0.001);
    }

    #[test]
    fn test_coref_eval_scores_conll_average() {
        let eval = CorefEvalScores {
            muc: Scores {
                precision: 0.8,
                recall: 0.8,
                f1: 0.8,
            },
            b_cubed: Scores {
                precision: 0.7,
                recall: 0.7,
                f1: 0.7,
            },
            ceaf_e: Scores {
                precision: 0.6,
                recall: 0.6,
                f1: 0.6,
            },
            ceaf_m: Scores::default(),
            lea: Scores::default(),
            conll_f1: 0.7, // (0.8 + 0.7 + 0.6) / 3 = 0.7
        };

        // CoNLL F1 is typically average of MUC, B³, CEAF-e
        let expected_conll = (0.8 + 0.7 + 0.6) / 3.0;
        assert!((eval.conll_f1 - expected_conll).abs() < 0.001);
    }

    #[test]
    fn test_windowed_evaluation_performance_drop() {
        let windowed = WindowedEvaluation {
            num_windows: 5,
            window_size: 1000,
            avg_conll_f1: 0.80,
            std_conll_f1: 0.03,
            performance_drop: 0.15,
            window_evals: vec![],
        };

        // Performance drop should be positive when full-doc is worse
        assert!(windowed.performance_drop > 0.0);
        assert_eq!(windowed.num_windows, 5);
    }

    #[test]
    fn test_diagnostics_no_issues_for_short_doc() {
        let config = BookScaleConfig::default();
        let analyzer = BookScaleAnalyzer::new(config);

        // Perfect scores - no issues expected
        let eval = CorefEvalScores {
            muc: Scores {
                precision: 0.85,
                recall: 0.85,
                f1: 0.85,
            },
            b_cubed: Scores {
                precision: 0.82,
                recall: 0.82,
                f1: 0.82,
            },
            ceaf_e: Scores {
                precision: 0.80,
                recall: 0.80,
                f1: 0.80,
            },
            ceaf_m: Scores::default(),
            lea: Scores::default(),
            conll_f1: 0.82,
        };

        let stratified = StratifiedEvaluation::default();
        let diagnostics = analyzer.generate_diagnostics(
            &eval,
            None, // No windowed evaluation
            &stratified,
            DocumentScale::Short,
        );

        // Small divergence for short docs - may or may not have issues
        // Just verify it doesn't panic and has expected fields
        let _ = diagnostics.high_metric_divergence;
        let _ = diagnostics.has_issues();
    }

    #[test]
    fn test_multi_book_report_empty() {
        let books: Vec<PerBookEvaluation> = vec![];
        let report = MultiBookReport::from_books(books);

        assert_eq!(report.aggregate.total_books, 0);
        assert!(report.books.is_empty());
    }

    #[test]
    fn test_per_book_evaluation_scale() {
        let book = PerBookEvaluation {
            book_id: "test".to_string(),
            title: Some("Test Book".to_string()),
            author: None,
            token_count: 200000,
            full_doc: CorefEvalScores::default(),
            windowed: None,
            scale: DocumentScale::BookScale,
        };

        assert!(book.token_count > 100000);
        assert_eq!(book.scale, DocumentScale::BookScale);
    }
}