anno-eval 0.10.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
//! NER evaluation modes following SemEval-2013 Task 9.1.
//!
//! # The Core Problem
//!
//! Suppose your model predicted "New York City" but the gold label was "New York":
//!
//! ```text
//! Text:       "I visited New York City yesterday"
//!                        ▼▼▼▼▼▼▼▼▼▼▼▼▼
//! Gold:       [====New York====]
//!                  0         8
//!
//! Predicted:  [=====New York City=====]
//!                  0              13
//!
//! Is this prediction correct? It depends on what you're measuring.
//! ```
//!
//! # Visual Guide to Each Mode
//!
//! ## Strict Mode (CoNLL Standard)
//!
//! "Did you get EXACTLY the right span AND the right type?"
//!
//! ```text
//! Case 1: Perfect match
//!   Gold:  [John]  type=PER
//!   Pred:  [John]  type=PER
//!   Result: ✓ TRUE POSITIVE
//!
//! Case 2: Wrong boundary
//!   Gold:  [New York]      type=LOC
//!   Pred:  [New York City] type=LOC
//!   Result: ✗ Both boundaries must match exactly
//!
//! Case 3: Wrong type
//!   Gold:  [Apple] type=ORG
//!   Pred:  [Apple] type=LOC
//!   Result: ✗ Type must match exactly
//! ```
//!
//! ## Partial Mode (Lenient Boundaries)
//!
//! "Did you find something that OVERLAPS the gold span with the RIGHT type?"
//!
//! ```text
//! Gold:     [====New York====]
//!                0         8
//!
//! Pred:     [=====New York City=====]
//!                0              13
//!
//!           |◄──overlap──►|
//!           Chars 0-8 are shared
//!
//! Overlap?  ✓ Yes (8 chars)
//! Type?     ✓ Both LOC
//! Result:   ✓ TRUE POSITIVE in Partial mode
//! ```
//!
//! ## Exact Mode (Boundary Detection)
//!
//! "Did you find the EXACT span, regardless of type?"
//!
//! ```text
//! Gold:  [Apple]  type=ORG
//! Pred:  [Apple]  type=LOC   (wrong type!)
//!
//! Boundaries match? ✓ Yes (both 0-5)
//! Result: ✓ TRUE POSITIVE in Exact mode
//!
//! Use case: "Can my model find entity boundaries at all?"
//! ```
//!
//! ## Type Mode (Classification Focus)
//!
//! "Did you identify the RIGHT TYPE somewhere in the overlapping region?"
//!
//! ```text
//! Gold:  [The Apple Company]  type=ORG
//! Pred:  [Apple]              type=ORG
//!
//! Overlap?  ✓ Yes ("Apple" is inside)
//! Type?     ✓ Both ORG
//! Result:   ✓ TRUE POSITIVE in Type mode
//!
//! Use case: "Can my model classify entity types correctly?"
//! ```
//!
//! # Diagnostic Patterns
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────────┐
//! │                    What Your Scores Tell You                    │
//! ├─────────────────────────────────────────────────────────────────┤
//! │                                                                 │
//! │  High Strict, Low Partial                                       │
//! │  ─────────────────────────                                      │
//! │  → Model finds exact spans but confuses types                   │
//! │  → Fix: Better type classification                              │
//! │                                                                 │
//! │  Low Strict, High Partial                                       │
//! │  ─────────────────────────                                      │
//! │  → Model finds general area but not exact boundaries            │
//! │  → Fix: Better boundary detection (tokenization? BIO tags?)     │
//! │                                                                 │
//! │  Low Strict, Low Partial                                        │
//! │  ─────────────────────────                                      │
//! │  → Model is missing entities entirely                           │
//! │  → Fix: More training data, lower threshold                     │
//! │                                                                 │
//! │  High Strict, High Partial (ideal!)                             │
//! │  ───────────────────────────────────                            │
//! │  → Model is working well                                        │
//! │                                                                 │
//! └─────────────────────────────────────────────────────────────────┘
//! ```
//!
//! # Summary Table
//!
//! | Mode | Boundary | Type | Use Case |
//! |------|----------|------|----------|
//! | **Strict** | Exact | Exact | Production benchmarks (CoNLL standard) |
//! | **Exact** | Exact | Any | Boundary detection evaluation |
//! | **Partial** | Overlap | Exact | Lenient type evaluation |
//! | **Type** | Any | Exact | Type classification evaluation |
//!
//! # Example
//!
//! ```rust
//! use anno_eval::eval::modes::{EvalMode, evaluate_with_mode, MultiModeResults};
//! use anno_eval::eval::GoldEntity;
//! use anno::{Entity, EntityType};
//!
//! let predicted = vec![
//!     Entity::new("New York City", EntityType::Location, 0, 13, 0.9),
//! ];
//! let gold = vec![
//!     GoldEntity::new("New York", EntityType::Location, 0),
//! ];
//!
//! // Strict mode (default) - requires exact boundary + type
//! let strict = evaluate_with_mode(&predicted, &gold, EvalMode::Strict);
//! println!("Strict F1: {:.1}%", strict.f1 * 100.0);
//!
//! // Partial mode - allows boundary overlap
//! let partial = evaluate_with_mode(&predicted, &gold, EvalMode::Partial);
//! println!("Partial F1: {:.1}%", partial.f1 * 100.0);
//!
//! // Get all modes at once
//! let all = MultiModeResults::compute(&predicted, &gold);
//! println!("Strict: {:.1}%, Partial: {:.1}%",
//!     all.strict.f1 * 100.0, all.partial.f1 * 100.0);
//! ```

use super::datasets::GoldEntity;
use anno::{Entity, EntityType};
use serde::{Deserialize, Serialize};

// =============================================================================
// Evaluation Configuration
// =============================================================================

/// Configuration for NER evaluation.
///
/// Partial matching modes (Partial, Type) accept any overlap by default.
/// In practice, you may want to require a minimum overlap ratio to avoid
/// counting barely-touching spans as matches.
///
/// # Example
///
/// ```rust
/// use anno_eval::eval::modes::EvalConfig;
///
/// // Require at least 50% overlap for partial matches
/// let config = EvalConfig::new().with_min_overlap(0.5);
///
/// // Strict config (default behavior)
/// let strict = EvalConfig::strict();
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvalConfig {
    /// Minimum overlap ratio (IoU) required for partial/type matches.
    ///
    /// Range: 0.0 to 1.0
    /// - 0.0: Any overlap counts (default)
    /// - 0.5: At least 50% overlap required
    /// - 1.0: Effectively requires exact boundaries
    pub min_overlap: f64,
}

impl Default for EvalConfig {
    fn default() -> Self {
        Self { min_overlap: 0.0 }
    }
}

impl EvalConfig {
    /// Create a new configuration with default settings.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a strict configuration (default overlap behavior).
    #[must_use]
    pub fn strict() -> Self {
        Self::default()
    }

    /// Set the minimum overlap threshold for partial matches.
    ///
    /// # Arguments
    ///
    /// * `threshold` - Minimum IoU (0.0-1.0) for partial matches
    #[must_use]
    pub fn with_min_overlap(mut self, threshold: f64) -> Self {
        self.min_overlap = threshold.clamp(0.0, 1.0);
        self
    }
}

// =============================================================================
// Evaluation Modes
// =============================================================================

/// Evaluation mode following SemEval-2013 Task 9.1.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
pub enum EvalMode {
    /// Strict: Exact boundary AND exact type (CoNLL standard).
    /// This is the default and most commonly reported metric.
    #[default]
    Strict,

    /// Exact boundary match only (type can differ).
    /// Useful for evaluating span detection separately from classification.
    Exact,

    /// Partial boundary overlap with exact type.
    /// More lenient than strict; gives credit for overlapping predictions.
    Partial,

    /// Any overlap with exact type.
    /// Most lenient; only requires some overlap and correct type.
    Type,
}

impl EvalMode {
    /// All available modes.
    pub fn all() -> &'static [EvalMode] {
        &[
            EvalMode::Strict,
            EvalMode::Exact,
            EvalMode::Partial,
            EvalMode::Type,
        ]
    }

    /// Human-readable name.
    #[must_use]
    pub fn name(&self) -> &'static str {
        match self {
            EvalMode::Strict => "Strict",
            EvalMode::Exact => "Exact",
            EvalMode::Partial => "Partial",
            EvalMode::Type => "Type",
        }
    }

    /// Description of what this mode evaluates.
    #[must_use]
    pub fn description(&self) -> &'static str {
        match self {
            EvalMode::Strict => "Exact boundary + exact type (CoNLL standard)",
            EvalMode::Exact => "Exact boundary only (type can differ)",
            EvalMode::Partial => "Partial boundary overlap + exact type",
            EvalMode::Type => "Any overlap + exact type",
        }
    }
}

// =============================================================================
// Mode-specific Results
// =============================================================================

/// Results for a single evaluation mode.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ModeResults {
    /// Evaluation mode used
    pub mode: EvalMode,
    /// Precision (0.0-1.0)
    pub precision: f64,
    /// Recall (0.0-1.0)
    pub recall: f64,
    /// F1 score (0.0-1.0)
    pub f1: f64,
    /// True positives (matches)
    pub true_positives: usize,
    /// False positives (spurious predictions)
    pub false_positives: usize,
    /// False negatives (missed entities)
    pub false_negatives: usize,
}

impl ModeResults {
    /// Compute results for a specific mode.
    ///
    /// Formulas:
    /// - `Precision = TP / (TP + FP)`
    /// - `Recall = TP / (TP + FN)`
    /// - `F1 = 2 × (P × R) / (P + R)` (harmonic mean)
    ///
    /// Reference: Manning et al. (2008) [Introduction to Information Retrieval](https://nlp.stanford.edu/IR-book/html/htmledition/evaluation-in-information-retrieval-1.html)
    #[must_use]
    pub fn compute(predicted: &[Entity], gold: &[GoldEntity], mode: EvalMode) -> Self {
        let (tp, fp, fn_count) = count_matches(predicted, gold, mode);

        let precision = if tp + fp > 0 {
            tp as f64 / (tp + fp) as f64
        } else {
            0.0
        };

        let recall = if tp + fn_count > 0 {
            tp as f64 / (tp + fn_count) as f64
        } else {
            0.0
        };

        let f1 = if precision + recall > 0.0 {
            2.0 * precision * recall / (precision + recall)
        } else {
            0.0
        };

        Self {
            mode,
            precision,
            recall,
            f1,
            true_positives: tp,
            false_positives: fp,
            false_negatives: fn_count,
        }
    }
}

/// Results across all evaluation modes.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct MultiModeResults {
    /// Strict mode (exact boundary + exact type)
    pub strict: ModeResults,
    /// Exact mode (exact boundary only)
    pub exact: ModeResults,
    /// Partial mode (partial boundary + exact type)
    pub partial: ModeResults,
    /// Type mode (any overlap + exact type)
    pub type_mode: ModeResults,
}

impl MultiModeResults {
    /// Compute all modes at once.
    #[must_use]
    pub fn compute(predicted: &[Entity], gold: &[GoldEntity]) -> Self {
        Self {
            strict: ModeResults::compute(predicted, gold, EvalMode::Strict),
            exact: ModeResults::compute(predicted, gold, EvalMode::Exact),
            partial: ModeResults::compute(predicted, gold, EvalMode::Partial),
            type_mode: ModeResults::compute(predicted, gold, EvalMode::Type),
        }
    }

    /// Get results for a specific mode.
    #[must_use]
    pub fn get(&self, mode: EvalMode) -> &ModeResults {
        match mode {
            EvalMode::Strict => &self.strict,
            EvalMode::Exact => &self.exact,
            EvalMode::Partial => &self.partial,
            EvalMode::Type => &self.type_mode,
        }
    }

    /// Print summary table.
    pub fn print_summary(&self) {
        println!("Evaluation Mode Results:");
        println!(
            "{:<10} {:>10} {:>10} {:>10}",
            "Mode", "Precision", "Recall", "F1"
        );
        println!("{:-<43}", "");
        for mode in EvalMode::all() {
            let r = self.get(*mode);
            println!(
                "{:<10} {:>9.1}% {:>9.1}% {:>9.1}%",
                mode.name(),
                r.precision * 100.0,
                r.recall * 100.0,
                r.f1 * 100.0
            );
        }
    }
}

// =============================================================================
// Matching Logic
// =============================================================================

/// Check if two entities match according to the given mode.
fn entities_match(pred: &Entity, gold: &GoldEntity, mode: EvalMode) -> bool {
    match mode {
        EvalMode::Strict => {
            // Exact boundary AND exact type
            pred.start() == gold.start
                && pred.end() == gold.end
                && types_match(&pred.entity_type, &gold.entity_type)
        }
        EvalMode::Exact => {
            // Exact boundary only (type can differ)
            pred.start() == gold.start && pred.end() == gold.end
        }
        EvalMode::Partial => {
            // Partial overlap AND exact type
            has_overlap(pred.start(), pred.end(), gold.start, gold.end)
                && types_match(&pred.entity_type, &gold.entity_type)
        }
        EvalMode::Type => {
            // Any overlap AND exact type
            has_overlap(pred.start(), pred.end(), gold.start, gold.end)
                && types_match(&pred.entity_type, &gold.entity_type)
        }
    }
}

/// Check if two entity types match.
fn types_match(a: &EntityType, b: &EntityType) -> bool {
    // Use the existing entity_type_matches logic
    super::entity_type_matches(a, b)
}

/// Check if two spans have any overlap.
fn has_overlap(start1: usize, end1: usize, start2: usize, end2: usize) -> bool {
    start1 < end2 && start2 < end1
}

/// Check if overlap meets minimum threshold.
///
/// This allows requiring a minimum overlap ratio for partial matches,
/// useful when barely-touching spans shouldn't count as matches.
/// Default threshold of 0.0 accepts any overlap.
fn has_sufficient_overlap(
    start1: usize,
    end1: usize,
    start2: usize,
    end2: usize,
    min_threshold: f64,
) -> bool {
    if !has_overlap(start1, end1, start2, end2) {
        return false;
    }
    if min_threshold <= 0.0 {
        return true;
    }
    overlap_ratio(start1, end1, start2, end2) >= min_threshold
}

/// Calculate overlap ratio (IoU) between two spans.
#[must_use]
pub fn overlap_ratio(start1: usize, end1: usize, start2: usize, end2: usize) -> f64 {
    let intersection_start = start1.max(start2);
    let intersection_end = end1.min(end2);

    if intersection_start >= intersection_end {
        return 0.0;
    }

    let intersection = (intersection_end - intersection_start) as f64;
    let union =
        ((end1 - start1) + (end2 - start2) - (intersection_end - intersection_start)) as f64;

    if union == 0.0 {
        1.0
    } else {
        intersection / union
    }
}

/// Count true positives, false positives, and false negatives.
fn count_matches(
    predicted: &[Entity],
    gold: &[GoldEntity],
    mode: EvalMode,
) -> (usize, usize, usize) {
    let mut gold_matched = vec![false; gold.len()];
    let mut tp = 0;
    let mut fp = 0;

    // For each prediction, try to find a matching gold entity
    for pred in predicted {
        let mut found_match = false;

        for (i, g) in gold.iter().enumerate() {
            if gold_matched[i] {
                continue;
            }

            if entities_match(pred, g, mode) {
                gold_matched[i] = true;
                found_match = true;
                tp += 1;
                break;
            }
        }

        if !found_match {
            fp += 1;
        }
    }

    let fn_count = gold_matched.iter().filter(|&&m| !m).count();

    (tp, fp, fn_count)
}

/// Evaluate with a specific mode.
#[must_use]
pub fn evaluate_with_mode(
    predicted: &[Entity],
    gold: &[GoldEntity],
    mode: EvalMode,
) -> ModeResults {
    ModeResults::compute(predicted, gold, mode)
}

/// Evaluate with a specific mode and configuration.
///
/// This allows customizing behavior like minimum overlap thresholds.
///
/// # Example
///
/// ```rust
/// use anno_eval::eval::modes::{EvalMode, EvalConfig, evaluate_with_config};
/// use anno_eval::eval::GoldEntity;
/// use anno::{Entity, EntityType};
///
/// let predicted = vec![Entity::new("New York", EntityType::Location, 0, 8, 0.9)];
/// let gold = vec![GoldEntity::new("New York City", EntityType::Location, 0)];
///
/// // Require 50% overlap for partial matches
/// let config = EvalConfig::new().with_min_overlap(0.5);
/// let results = evaluate_with_config(&predicted, &gold, EvalMode::Partial, &config);
/// ```
#[must_use]
pub fn evaluate_with_config(
    predicted: &[Entity],
    gold: &[GoldEntity],
    mode: EvalMode,
    config: &EvalConfig,
) -> ModeResults {
    let (tp, fp, fn_count) = count_matches_with_config(predicted, gold, mode, config);

    let precision = if tp + fp > 0 {
        tp as f64 / (tp + fp) as f64
    } else {
        0.0
    };

    let recall = if tp + fn_count > 0 {
        tp as f64 / (tp + fn_count) as f64
    } else {
        0.0
    };

    let f1 = if precision + recall > 0.0 {
        2.0 * precision * recall / (precision + recall)
    } else {
        0.0
    };

    ModeResults {
        mode,
        precision,
        recall,
        f1,
        true_positives: tp,
        false_positives: fp,
        false_negatives: fn_count,
    }
}

/// Count matches with configuration.
fn count_matches_with_config(
    predicted: &[Entity],
    gold: &[GoldEntity],
    mode: EvalMode,
    config: &EvalConfig,
) -> (usize, usize, usize) {
    let mut gold_matched = vec![false; gold.len()];
    let mut tp = 0;
    let mut fp = 0;

    for pred in predicted {
        let mut found_match = false;

        for (i, g) in gold.iter().enumerate() {
            if gold_matched[i] {
                continue;
            }

            if entities_match_with_config(pred, g, mode, config) {
                gold_matched[i] = true;
                found_match = true;
                tp += 1;
                break;
            }
        }

        if !found_match {
            fp += 1;
        }
    }

    let fn_count = gold_matched.iter().filter(|&&m| !m).count();

    (tp, fp, fn_count)
}

/// Check if entities match with configuration.
fn entities_match_with_config(
    pred: &Entity,
    gold: &GoldEntity,
    mode: EvalMode,
    config: &EvalConfig,
) -> bool {
    match mode {
        EvalMode::Strict => {
            pred.start() == gold.start
                && pred.end() == gold.end
                && types_match(&pred.entity_type, &gold.entity_type)
        }
        EvalMode::Exact => pred.start() == gold.start && pred.end() == gold.end,
        EvalMode::Partial | EvalMode::Type => {
            has_sufficient_overlap(
                pred.start(),
                pred.end(),
                gold.start,
                gold.end,
                config.min_overlap,
            ) && types_match(&pred.entity_type, &gold.entity_type)
        }
    }
}

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

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

    fn pred(text: &str, ty: EntityType, start: usize, end: usize) -> Entity {
        Entity::new(text, ty, start, end, 0.9)
    }

    fn gold(text: &str, ty: EntityType, start: usize) -> GoldEntity {
        GoldEntity::new(text, ty, start)
    }

    #[test]
    fn test_strict_exact_match() {
        let predicted = vec![pred("John", EntityType::Person, 0, 4)];
        let gold_entities = vec![gold("John", EntityType::Person, 0)];

        let results = ModeResults::compute(&predicted, &gold_entities, EvalMode::Strict);
        assert!((results.f1 - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_strict_wrong_boundary() {
        let predicted = vec![pred("John Smith", EntityType::Person, 0, 10)];
        let gold_entities = vec![gold("John", EntityType::Person, 0)];

        let results = ModeResults::compute(&predicted, &gold_entities, EvalMode::Strict);
        assert_eq!(results.f1, 0.0); // Strict mode fails

        // But partial mode should match
        let partial = ModeResults::compute(&predicted, &gold_entities, EvalMode::Partial);
        assert!((partial.f1 - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_strict_wrong_type() {
        let predicted = vec![pred("Apple", EntityType::Organization, 0, 5)];
        let gold_entities = vec![gold("Apple", EntityType::Location, 0)];

        let results = ModeResults::compute(&predicted, &gold_entities, EvalMode::Strict);
        assert_eq!(results.f1, 0.0); // Wrong type

        // But exact mode (boundary only) should match
        let exact = ModeResults::compute(&predicted, &gold_entities, EvalMode::Exact);
        assert!((exact.f1 - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_partial_overlap() {
        // "New York City" vs "New York"
        let predicted = vec![pred("New York City", EntityType::Location, 0, 13)];
        let gold_entities = vec![gold("New York", EntityType::Location, 0)];

        // Strict: fail (different boundary)
        let strict = ModeResults::compute(&predicted, &gold_entities, EvalMode::Strict);
        assert_eq!(strict.f1, 0.0);

        // Partial: pass (overlap + same type)
        let partial = ModeResults::compute(&predicted, &gold_entities, EvalMode::Partial);
        assert!((partial.f1 - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_no_overlap() {
        let predicted = vec![pred("John", EntityType::Person, 0, 4)];
        let gold_entities = vec![gold("Mary", EntityType::Person, 10)];

        for mode in EvalMode::all() {
            let results = ModeResults::compute(&predicted, &gold_entities, *mode);
            assert_eq!(
                results.f1, 0.0,
                "Mode {:?} should fail with no overlap",
                mode
            );
        }
    }

    #[test]
    fn test_multi_mode_results() {
        let predicted = vec![
            pred("John", EntityType::Person, 0, 4),
            pred("New York City", EntityType::Location, 10, 23),
        ];
        let gold_entities = vec![
            gold("John", EntityType::Person, 0),
            gold("New York", EntityType::Location, 10),
        ];

        let all = MultiModeResults::compute(&predicted, &gold_entities);

        // Strict: 1/2 (John matches, NYC doesn't)
        assert!((all.strict.precision - 0.5).abs() < 0.001);

        // Partial: 2/2 (both overlap)
        assert!((all.partial.f1 - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_overlap_ratio() {
        // Complete overlap
        assert!((overlap_ratio(0, 10, 0, 10) - 1.0).abs() < 0.001);

        // No overlap
        assert!((overlap_ratio(0, 5, 10, 15) - 0.0).abs() < 0.001);

        // Partial overlap: [0,10] and [5,15]
        // Intersection: [5,10] = 5 chars
        // Union: 10 + 10 - 5 = 15 chars
        // IoU = 5/15 = 0.333...
        assert!(
            (overlap_ratio(0, 10, 5, 15) - (5.0 / 15.0)).abs() < 0.001,
            "Expected IoU of 5/15 = {}, got {}",
            5.0 / 15.0,
            overlap_ratio(0, 10, 5, 15)
        );
    }

    #[test]
    fn test_empty_inputs() {
        let empty_pred: Vec<Entity> = vec![];
        let empty_gold: Vec<GoldEntity> = vec![];

        let results = ModeResults::compute(&empty_pred, &empty_gold, EvalMode::Strict);
        assert_eq!(results.f1, 0.0);
        assert_eq!(results.true_positives, 0);
        assert_eq!(results.false_positives, 0);
        assert_eq!(results.false_negatives, 0);
    }

    // === EvalConfig tests ===

    #[test]
    fn test_config_default() {
        let config = EvalConfig::default();
        assert_eq!(config.min_overlap, 0.0);
    }

    #[test]
    fn test_config_with_overlap() {
        let config = EvalConfig::new().with_min_overlap(0.5);
        assert_eq!(config.min_overlap, 0.5);
    }

    #[test]
    fn test_config_clamp() {
        // Values outside 0-1 should be clamped
        let config = EvalConfig::new().with_min_overlap(1.5);
        assert_eq!(config.min_overlap, 1.0);

        let config = EvalConfig::new().with_min_overlap(-0.5);
        assert_eq!(config.min_overlap, 0.0);
    }

    #[test]
    fn test_partial_with_zero_threshold() {
        // Default: any overlap counts
        let predicted = vec![pred("New York City", EntityType::Location, 0, 13)];
        let gold_entities = vec![gold("New York", EntityType::Location, 0)];

        let config = EvalConfig::default();
        let results = evaluate_with_config(&predicted, &gold_entities, EvalMode::Partial, &config);

        // Should match (overlap exists)
        assert!((results.f1 - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_partial_with_high_threshold() {
        // "New York City" [0,13] vs "New York" [0,8]
        // Overlap: 8 chars, Union: 13 chars
        // IoU = 8/13 ≈ 0.615
        let predicted = vec![pred("New York City", EntityType::Location, 0, 13)];
        let gold_entities = vec![gold("New York", EntityType::Location, 0)];

        // 50% threshold - should pass (0.615 > 0.5)
        let config = EvalConfig::new().with_min_overlap(0.5);
        let results = evaluate_with_config(&predicted, &gold_entities, EvalMode::Partial, &config);
        assert!(
            (results.f1 - 1.0).abs() < 0.001,
            "0.5 threshold should pass"
        );

        // 70% threshold - should fail (0.615 < 0.7)
        let config = EvalConfig::new().with_min_overlap(0.7);
        let results = evaluate_with_config(&predicted, &gold_entities, EvalMode::Partial, &config);
        assert_eq!(results.f1, 0.0, "0.7 threshold should fail");
    }

    #[test]
    fn test_partial_barely_touching() {
        // Entities that barely touch: [0,5] and [4,10]
        // Overlap: 1 char, Union: 10 chars
        // IoU = 1/10 = 0.1
        let predicted = vec![pred("Apple", EntityType::Organization, 0, 5)];
        let gold_entities = vec![gold("Banana", EntityType::Organization, 4)];

        // Default (0%) should match
        let config = EvalConfig::default();
        let results = evaluate_with_config(&predicted, &gold_entities, EvalMode::Partial, &config);
        assert!((results.f1 - 1.0).abs() < 0.001);

        // 20% threshold should fail (0.1 < 0.2)
        let config = EvalConfig::new().with_min_overlap(0.2);
        let results = evaluate_with_config(&predicted, &gold_entities, EvalMode::Partial, &config);
        assert_eq!(results.f1, 0.0);
    }

    #[test]
    fn test_strict_mode_ignores_threshold() {
        // Strict mode requires exact boundaries, threshold shouldn't matter
        let predicted = vec![pred("John", EntityType::Person, 0, 4)];
        let gold_entities = vec![gold("John", EntityType::Person, 0)];

        let config = EvalConfig::new().with_min_overlap(0.99);
        let results = evaluate_with_config(&predicted, &gold_entities, EvalMode::Strict, &config);

        // Should still pass (exact match)
        assert!((results.f1 - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_has_sufficient_overlap() {
        // Any overlap with 0% threshold
        assert!(has_sufficient_overlap(0, 10, 5, 15, 0.0));

        // Needs at least 50% IoU
        // [0,10] and [5,15]: IoU = 5/15 ≈ 0.33
        assert!(!has_sufficient_overlap(0, 10, 5, 15, 0.5));

        // [0,10] and [2,12]: IoU = 8/12 ≈ 0.67
        assert!(has_sufficient_overlap(0, 10, 2, 12, 0.5));

        // No overlap at all
        assert!(!has_sufficient_overlap(0, 5, 10, 15, 0.0));
    }
}