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
//! Entity length bias evaluation for Named Entity Recognition.
//!
//! Measures performance differences based on entity text length.
//! Models often exhibit bias toward entity lengths common in training data,
//! performing worse on very short or very long entities.
//!
//! # Research Background
//!
//! - Jeong & Kang (2021): "Regularization for Long Named Entity Recognition"
//!   - Pre-trained language models tend to be biased toward dataset patterns
//!   - Length statistics of training data directly influence performance
//!
//! # Key Metrics
//!
//! - **Length Bucket Recognition Rate**: Performance by character/word length
//! - **Length Parity Gap**: Max difference across length buckets
//! - **Short Entity Bias**: Performance on 1-2 word entities vs longer
//!
//! # Example
//!
//! ```rust
//! use anno_eval::eval::length_bias::{EntityLengthEvaluator, create_length_varied_dataset};
//!
//! let examples = create_length_varied_dataset();
//! let evaluator = EntityLengthEvaluator::default();
//! // let results = evaluator.evaluate(&model, &examples);
//! ```

use crate::{EntityType, Model};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// =============================================================================
// Length Categories
// =============================================================================

/// Length bucket for entity classification.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum LengthBucket {
    /// Very short: 1-5 characters (e.g., "NYC", "IBM")
    VeryShort,
    /// Short: 6-15 characters (e.g., "John Smith")
    Short,
    /// Medium: 16-30 characters (e.g., "University of California")
    Medium,
    /// Long: 31-50 characters (e.g., "Massachusetts Institute of Technology")
    Long,
    /// Very long: 51+ characters (e.g., compound organization names)
    VeryLong,
}

impl LengthBucket {
    /// Classify a string by its character length.
    pub fn from_char_length(len: usize) -> Self {
        match len {
            0..=5 => LengthBucket::VeryShort,
            6..=15 => LengthBucket::Short,
            16..=30 => LengthBucket::Medium,
            31..=50 => LengthBucket::Long,
            _ => LengthBucket::VeryLong,
        }
    }

    /// Classify a string by its word count.
    pub fn from_word_count(words: usize) -> Self {
        match words {
            0..=1 => LengthBucket::VeryShort,
            2 => LengthBucket::Short,
            3..=4 => LengthBucket::Medium,
            5..=7 => LengthBucket::Long,
            _ => LengthBucket::VeryLong,
        }
    }
}

/// Word count bucket for finer-grained analysis.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum WordCountBucket {
    /// Single word (e.g., "Microsoft")
    SingleWord,
    /// Two words (e.g., "John Smith")
    TwoWords,
    /// Three words (e.g., "New York City")
    ThreeWords,
    /// Four or more words
    FourPlusWords,
}

impl WordCountBucket {
    /// Classify by word count.
    pub fn from_count(count: usize) -> Self {
        match count {
            0..=1 => WordCountBucket::SingleWord,
            2 => WordCountBucket::TwoWords,
            3 => WordCountBucket::ThreeWords,
            _ => WordCountBucket::FourPlusWords,
        }
    }
}

// =============================================================================
// Length Test Example
// =============================================================================

/// An entity example with length metadata.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LengthTestExample {
    /// The entity text
    pub entity_text: String,
    /// Full sentence containing the entity
    pub sentence: String,
    /// Expected entity type
    pub entity_type: EntityType,
    /// Character length of entity
    pub char_length: usize,
    /// Word count of entity
    pub word_count: usize,
    /// Character length bucket
    pub char_bucket: LengthBucket,
    /// Word count bucket
    pub word_bucket: WordCountBucket,
}

impl LengthTestExample {
    /// Create a new length test example.
    pub fn new(entity: &str, entity_type: EntityType) -> Self {
        let sentence = format!("The entity {} was mentioned.", entity);
        let char_length = entity.chars().count();
        let word_count = entity.split_whitespace().count();

        Self {
            entity_text: entity.to_string(),
            sentence,
            entity_type,
            char_length,
            word_count,
            char_bucket: LengthBucket::from_char_length(char_length),
            word_bucket: WordCountBucket::from_count(word_count),
        }
    }

    /// Create with a custom sentence.
    pub fn with_sentence(entity: &str, sentence: &str, entity_type: EntityType) -> Self {
        let char_length = entity.chars().count();
        let word_count = entity.split_whitespace().count();

        Self {
            entity_text: entity.to_string(),
            sentence: sentence.to_string(),
            entity_type,
            char_length,
            word_count,
            char_bucket: LengthBucket::from_char_length(char_length),
            word_bucket: WordCountBucket::from_count(word_count),
        }
    }
}

// =============================================================================
// Evaluation Results
// =============================================================================

/// Results of entity length bias evaluation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LengthBiasResults {
    /// Overall recognition rate
    pub overall_recognition_rate: f64,
    /// Recognition rate by character length bucket
    pub by_char_bucket: HashMap<String, f64>,
    /// Recognition rate by word count bucket
    pub by_word_bucket: HashMap<String, f64>,
    /// Recognition rate by entity type
    pub by_entity_type: HashMap<String, f64>,
    /// Maximum gap across character length buckets
    pub char_length_parity_gap: f64,
    /// Maximum gap across word count buckets
    pub word_count_parity_gap: f64,
    /// Short (1-2 words) vs long (4+ words) gap
    pub short_vs_long_gap: f64,
    /// Average character length of correctly recognized entities
    pub avg_recognized_char_length: f64,
    /// Average character length of missed entities
    pub avg_missed_char_length: f64,
    /// Total examples tested
    pub total_tested: usize,
}

// =============================================================================
// Evaluator
// =============================================================================

/// Evaluator for entity length bias.
#[derive(Debug, Clone, Default)]
pub struct EntityLengthEvaluator {
    /// Include detailed per-example results
    pub detailed: bool,
}

impl EntityLengthEvaluator {
    /// Create a new evaluator.
    pub fn new(detailed: bool) -> Self {
        Self { detailed }
    }

    /// Evaluate NER model for length bias.
    pub fn evaluate(&self, model: &dyn Model, examples: &[LengthTestExample]) -> LengthBiasResults {
        let mut by_char_bucket: HashMap<String, (usize, usize)> = HashMap::new();
        let mut by_word_bucket: HashMap<String, (usize, usize)> = HashMap::new();
        let mut by_entity_type: HashMap<String, (usize, usize)> = HashMap::new();
        let mut total_recognized = 0;
        let mut recognized_char_lengths: Vec<usize> = Vec::new();
        let mut missed_char_lengths: Vec<usize> = Vec::new();

        for example in examples {
            // Extract entities
            let entities = model
                .extract_entities(&example.sentence, None)
                .unwrap_or_default();

            // Check if entity was recognized with correct type
            let recognized = entities.iter().any(|e| {
                e.entity_type == example.entity_type
                    && example
                        .sentence
                        .get(
                            anno::offset::TextSpan::from_chars(
                                &example.sentence,
                                e.start(),
                                e.end(),
                            )
                            .byte_range(),
                        )
                        .map(|s| s.contains(&example.entity_text))
                        .unwrap_or(false)
            });

            if recognized {
                total_recognized += 1;
                recognized_char_lengths.push(example.char_length);
            } else {
                missed_char_lengths.push(example.char_length);
            }

            // Update char bucket stats
            let char_key = format!("{:?}", example.char_bucket);
            let char_entry = by_char_bucket.entry(char_key).or_insert((0, 0));
            char_entry.1 += 1;
            if recognized {
                char_entry.0 += 1;
            }

            // Update word bucket stats
            let word_key = format!("{:?}", example.word_bucket);
            let word_entry = by_word_bucket.entry(word_key).or_insert((0, 0));
            word_entry.1 += 1;
            if recognized {
                word_entry.0 += 1;
            }

            // Update entity type stats
            let type_key = format!("{:?}", example.entity_type);
            let type_entry = by_entity_type.entry(type_key).or_insert((0, 0));
            type_entry.1 += 1;
            if recognized {
                type_entry.0 += 1;
            }
        }

        // Convert counts to rates
        let to_rate = |counts: &HashMap<String, (usize, usize)>| -> HashMap<String, f64> {
            counts
                .iter()
                .map(|(k, (correct, total))| {
                    let rate = if *total > 0 {
                        *correct as f64 / *total as f64
                    } else {
                        0.0
                    };
                    (k.clone(), rate)
                })
                .collect()
        };

        let char_rates = to_rate(&by_char_bucket);
        let word_rates = to_rate(&by_word_bucket);
        let type_rates = to_rate(&by_entity_type);

        // Compute parity gaps
        let char_length_parity_gap = compute_max_gap(&char_rates);
        let word_count_parity_gap = compute_max_gap(&word_rates);

        // Short vs long gap
        let short_rate = word_rates
            .iter()
            .filter(|(k, _)| k.contains("SingleWord") || k.contains("TwoWords"))
            .map(|(_, v)| *v)
            .sum::<f64>()
            / 2.0;
        let long_rate = word_rates
            .get("FourPlusWords")
            .copied()
            .unwrap_or(short_rate);
        let short_vs_long_gap = (short_rate - long_rate).abs();

        // Average lengths
        let avg_recognized = if recognized_char_lengths.is_empty() {
            0.0
        } else {
            recognized_char_lengths.iter().sum::<usize>() as f64
                / recognized_char_lengths.len() as f64
        };
        let avg_missed = if missed_char_lengths.is_empty() {
            0.0
        } else {
            missed_char_lengths.iter().sum::<usize>() as f64 / missed_char_lengths.len() as f64
        };

        LengthBiasResults {
            overall_recognition_rate: if examples.is_empty() {
                0.0
            } else {
                total_recognized as f64 / examples.len() as f64
            },
            by_char_bucket: char_rates,
            by_word_bucket: word_rates,
            by_entity_type: type_rates,
            char_length_parity_gap,
            word_count_parity_gap,
            short_vs_long_gap,
            avg_recognized_char_length: avg_recognized,
            avg_missed_char_length: avg_missed,
            total_tested: examples.len(),
        }
    }
}

/// Compute maximum gap between any two rates.
fn compute_max_gap(rates: &HashMap<String, f64>) -> f64 {
    if rates.len() < 2 {
        return 0.0;
    }

    let values: Vec<f64> = rates.values().copied().collect();
    let min = values.iter().copied().fold(f64::INFINITY, f64::min);
    let max = values.iter().copied().fold(f64::NEG_INFINITY, f64::max);

    max - min
}

// =============================================================================
// Length-Varied Dataset
// =============================================================================

/// Create a dataset with entities of varying lengths.
pub fn create_length_varied_dataset() -> Vec<LengthTestExample> {
    vec![
        // === PERSON entities by length ===
        // Very short (abbreviations, initials)
        LengthTestExample::with_sentence(
            "JFK",
            "JFK gave a famous speech in Berlin.",
            EntityType::Person,
        ),
        LengthTestExample::with_sentence(
            "FDR",
            "FDR led the country through World War II.",
            EntityType::Person,
        ),
        // Short (typical names)
        LengthTestExample::with_sentence(
            "John Smith",
            "John Smith attended the meeting.",
            EntityType::Person,
        ),
        LengthTestExample::with_sentence(
            "Mary Johnson",
            "Mary Johnson won the award.",
            EntityType::Person,
        ),
        // Medium (names with middle name or title)
        LengthTestExample::with_sentence(
            "Dr. Martin Luther King",
            "Dr. Martin Luther King delivered a powerful speech.",
            EntityType::Person,
        ),
        LengthTestExample::with_sentence(
            "William Jefferson Clinton",
            "William Jefferson Clinton served as president.",
            EntityType::Person,
        ),
        // Long (full names with titles/suffixes)
        LengthTestExample::with_sentence(
            "His Royal Highness Prince William",
            "His Royal Highness Prince William visited the hospital.",
            EntityType::Person,
        ),
        // === ORGANIZATION entities by length ===
        // Very short
        LengthTestExample::with_sentence(
            "IBM",
            "IBM announced new products.",
            EntityType::Organization,
        ),
        LengthTestExample::with_sentence(
            "MIT",
            "MIT published research findings.",
            EntityType::Organization,
        ),
        LengthTestExample::with_sentence(
            "NASA",
            "NASA launched a new satellite.",
            EntityType::Organization,
        ),
        // Short
        LengthTestExample::with_sentence(
            "Google Inc",
            "Google Inc acquired the startup.",
            EntityType::Organization,
        ),
        LengthTestExample::with_sentence(
            "Apple Computer",
            "Apple Computer revolutionized mobile phones.",
            EntityType::Organization,
        ),
        // Medium
        LengthTestExample::with_sentence(
            "University of California",
            "University of California released the study.",
            EntityType::Organization,
        ),
        LengthTestExample::with_sentence(
            "World Health Organization",
            "World Health Organization issued guidelines.",
            EntityType::Organization,
        ),
        // Long
        LengthTestExample::with_sentence(
            "Massachusetts Institute of Technology",
            "Massachusetts Institute of Technology won the competition.",
            EntityType::Organization,
        ),
        LengthTestExample::with_sentence(
            "International Business Machines Corporation",
            "International Business Machines Corporation reported earnings.",
            EntityType::Organization,
        ),
        // Very long
        LengthTestExample::with_sentence(
            "United States Department of Health and Human Services",
            "United States Department of Health and Human Services announced the policy.",
            EntityType::Organization,
        ),
        LengthTestExample::with_sentence(
            "European Organization for Nuclear Research",
            "European Organization for Nuclear Research discovered the particle.",
            EntityType::Organization,
        ),
        // === LOCATION entities by length ===
        // Very short
        LengthTestExample::with_sentence(
            "NYC",
            "NYC is known for its skyline.",
            EntityType::Location,
        ),
        LengthTestExample::with_sentence("LA", "LA has beautiful weather.", EntityType::Location),
        // Short
        LengthTestExample::with_sentence(
            "New York",
            "New York is a bustling city.",
            EntityType::Location,
        ),
        LengthTestExample::with_sentence(
            "London",
            "London has many museums.",
            EntityType::Location,
        ),
        // Medium
        LengthTestExample::with_sentence(
            "San Francisco Bay Area",
            "San Francisco Bay Area is a tech hub.",
            EntityType::Location,
        ),
        LengthTestExample::with_sentence(
            "United Arab Emirates",
            "United Arab Emirates hosted the conference.",
            EntityType::Location,
        ),
        // Long
        LengthTestExample::with_sentence(
            "Democratic Republic of the Congo",
            "Democratic Republic of the Congo has vast resources.",
            EntityType::Location,
        ),
        LengthTestExample::with_sentence(
            "Saint Vincent and the Grenadines",
            "Saint Vincent and the Grenadines is in the Caribbean.",
            EntityType::Location,
        ),
        // Very long
        LengthTestExample::with_sentence(
            "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
            "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch is a town in Wales.",
            EntityType::Location,
        ),
        // === Additional PERSON examples with titles and suffixes ===
        LengthTestExample::with_sentence(
            "Dr. Jane Smith",
            "Dr. Jane Smith diagnosed the patient.",
            EntityType::Person,
        ),
        LengthTestExample::with_sentence(
            "Prof. John Doe",
            "Prof. John Doe published the research.",
            EntityType::Person,
        ),
        LengthTestExample::with_sentence(
            "Mary-Jane Watson",
            "Mary-Jane Watson attended the event.",
            EntityType::Person,
        ),
        LengthTestExample::with_sentence(
            "José María García",
            "José María García spoke at the conference.",
            EntityType::Person,
        ),
        LengthTestExample::with_sentence(
            "Robert Williams Jr.",
            "Robert Williams Jr. inherited the business.",
            EntityType::Person,
        ),
        LengthTestExample::with_sentence(
            "Elizabeth Taylor III",
            "Elizabeth Taylor III was the third generation.",
            EntityType::Person,
        ),
        LengthTestExample::with_sentence(
            "Jean-Pierre Dubois",
            "Jean-Pierre Dubois visited from France.",
            EntityType::Person,
        ),
        LengthTestExample::with_sentence(
            "Mary Ann Johnson",
            "Mary Ann Johnson was the keynote speaker.",
            EntityType::Person,
        ),
        // === Additional ORGANIZATION examples ===
        LengthTestExample::with_sentence(
            "AT&T",
            "AT&T announced the merger.",
            EntityType::Organization,
        ),
        LengthTestExample::with_sentence(
            "3M",
            "3M developed new materials.",
            EntityType::Organization,
        ),
        LengthTestExample::with_sentence(
            "JPMorgan Chase",
            "JPMorgan Chase reported earnings.",
            EntityType::Organization,
        ),
        LengthTestExample::with_sentence(
            "Bank of America",
            "Bank of America opened new branches.",
            EntityType::Organization,
        ),
        LengthTestExample::with_sentence(
            "General Electric Company",
            "General Electric Company restructured operations.",
            EntityType::Organization,
        ),
        LengthTestExample::with_sentence(
            "The Coca-Cola Company",
            "The Coca-Cola Company launched a new product.",
            EntityType::Organization,
        ),
        LengthTestExample::with_sentence(
            "Procter & Gamble",
            "Procter & Gamble acquired the brand.",
            EntityType::Organization,
        ),
        LengthTestExample::with_sentence(
            "Johnson & Johnson",
            "Johnson & Johnson developed the vaccine.",
            EntityType::Organization,
        ),
        // === Additional LOCATION examples ===
        LengthTestExample::with_sentence("UK", "UK announced new policies.", EntityType::Location),
        LengthTestExample::with_sentence("USA", "USA hosted the summit.", EntityType::Location),
        LengthTestExample::with_sentence(
            "Los Angeles",
            "Los Angeles hosted the Olympics.",
            EntityType::Location,
        ),
        LengthTestExample::with_sentence(
            "San Diego",
            "San Diego is a coastal city.",
            EntityType::Location,
        ),
        LengthTestExample::with_sentence(
            "New York City",
            "New York City never sleeps.",
            EntityType::Location,
        ),
        LengthTestExample::with_sentence(
            "Greater London Area",
            "Greater London Area has millions of residents.",
            EntityType::Location,
        ),
        LengthTestExample::with_sentence(
            "Republic of South Africa",
            "Republic of South Africa celebrated independence.",
            EntityType::Location,
        ),
        LengthTestExample::with_sentence(
            "Federative Republic of Brazil",
            "Federative Republic of Brazil hosted the World Cup.",
            EntityType::Location,
        ),
        // === DATE examples (for completeness) ===
        LengthTestExample::with_sentence(
            "2024",
            "The year 2024 was significant.",
            EntityType::Date,
        ),
        LengthTestExample::with_sentence(
            "January 15, 2024",
            "The meeting was scheduled for January 15, 2024.",
            EntityType::Date,
        ),
        LengthTestExample::with_sentence(
            "Q1 2024",
            "Q1 2024 showed strong growth.",
            EntityType::Date,
        ),
        // === MONEY examples ===
        LengthTestExample::with_sentence("$5", "The item cost $5.", EntityType::Money),
        LengthTestExample::with_sentence(
            "$1,234.56",
            "The total was $1,234.56.",
            EntityType::Money,
        ),
        LengthTestExample::with_sentence(
            "€1,000,000",
            "The investment was €1,000,000.",
            EntityType::Money,
        ),
    ]
}

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

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

    #[test]
    fn test_length_bucket_classification() {
        assert_eq!(LengthBucket::from_char_length(3), LengthBucket::VeryShort);
        assert_eq!(LengthBucket::from_char_length(10), LengthBucket::Short);
        assert_eq!(LengthBucket::from_char_length(25), LengthBucket::Medium);
        assert_eq!(LengthBucket::from_char_length(40), LengthBucket::Long);
        assert_eq!(LengthBucket::from_char_length(60), LengthBucket::VeryLong);
    }

    #[test]
    fn test_word_count_bucket() {
        assert_eq!(WordCountBucket::from_count(1), WordCountBucket::SingleWord);
        assert_eq!(WordCountBucket::from_count(2), WordCountBucket::TwoWords);
        assert_eq!(WordCountBucket::from_count(3), WordCountBucket::ThreeWords);
        assert_eq!(
            WordCountBucket::from_count(5),
            WordCountBucket::FourPlusWords
        );
    }

    #[test]
    fn test_create_length_dataset() {
        let examples = create_length_varied_dataset();

        // Should have examples in all length buckets
        let char_buckets: std::collections::HashSet<_> = examples
            .iter()
            .map(|e| format!("{:?}", e.char_bucket))
            .collect();

        assert!(
            char_buckets.contains("VeryShort"),
            "Should have very short entities"
        );
        assert!(char_buckets.contains("Short"), "Should have short entities");
        assert!(
            char_buckets.contains("Medium"),
            "Should have medium entities"
        );
        assert!(char_buckets.contains("Long"), "Should have long entities");
    }

    #[test]
    fn test_entity_type_coverage() {
        let examples = create_length_varied_dataset();

        let types: std::collections::HashSet<_> = examples
            .iter()
            .map(|e| format!("{:?}", e.entity_type))
            .collect();

        assert!(types.contains("Person"), "Should have PERSON entities");
        assert!(
            types.contains("Organization"),
            "Should have ORGANIZATION entities"
        );
        assert!(types.contains("Location"), "Should have LOCATION entities");
    }

    #[test]
    fn test_example_construction() {
        let example = LengthTestExample::new("John Smith", EntityType::Person);

        assert_eq!(example.entity_text, "John Smith");
        assert_eq!(example.char_length, 10);
        assert_eq!(example.word_count, 2);
        assert_eq!(example.char_bucket, LengthBucket::Short);
        assert_eq!(example.word_bucket, WordCountBucket::TwoWords);
    }
}