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
894
895
896
897
898
899
//! Large-scale benchmark datasets for NER evaluation.
//!
//! Unlike the synthetic module which provides curated examples, this module
//! generates large datasets for stress testing and statistical significance.
//!
//! # Philosophy (burntsushi-style)
//!
//! - **Real** edge cases, not toy examples
//! - Models should perform **just okay** on these, not perfectly
//! - Tests statistical significance, not cherry-picked success cases
//!
//! # Usage
//!
//! ```rust,no_run
//! use anno_eval::eval::benchmark::{generate_large_dataset, EdgeCaseType};
//!
//! // Generate 1000 hard examples
//! let dataset = generate_large_dataset(1000, EdgeCaseType::All);
//! assert!(dataset.len() >= 1000);
//! ```

use super::datasets::GoldEntity;
use super::synthetic::{AnnotatedExample, Difficulty, Domain};
use anno::{EntityCategory, EntityType};

/// Types of edge cases to generate
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EdgeCaseType {
    /// All edge case types
    All,
    /// Ambiguous entities (words that could be multiple types)
    Ambiguous,
    /// Unicode edge cases (RTL, combining characters, emoji)
    Unicode,
    /// Dense text (many entities close together)
    Dense,
    /// Sparse text (single entity in long text)
    Sparse,
    /// Nested or overlapping entity candidates
    Nested,
    /// Unusual capitalization or casing
    Casing,
    /// Boundary cases (entities at start/end, with punctuation)
    Boundary,
    /// Multi-word entities (proper name + title, company + suffix)
    MultiWord,
    /// Numeric pattern edge cases
    NumericEdge,
    /// Domain-specific jargon
    Jargon,
}

/// Generate a large dataset of challenging examples.
///
/// Returns at least `min_count` examples, potentially more due to template expansion.
#[must_use]
pub fn generate_large_dataset(
    min_count: usize,
    edge_case_type: EdgeCaseType,
) -> Vec<AnnotatedExample> {
    let mut examples = Vec::with_capacity(min_count);

    match edge_case_type {
        EdgeCaseType::All => {
            let per_type = min_count / 10;
            examples.extend(generate_ambiguous_examples(per_type));
            examples.extend(generate_unicode_examples(per_type));
            examples.extend(generate_dense_examples(per_type));
            examples.extend(generate_sparse_examples(per_type));
            examples.extend(generate_nested_examples(per_type));
            examples.extend(generate_casing_examples(per_type));
            examples.extend(generate_boundary_examples(per_type));
            examples.extend(generate_multiword_examples(per_type));
            examples.extend(generate_numeric_edge_examples(per_type));
            examples.extend(generate_jargon_examples(per_type));
        }
        EdgeCaseType::Ambiguous => examples.extend(generate_ambiguous_examples(min_count)),
        EdgeCaseType::Unicode => examples.extend(generate_unicode_examples(min_count)),
        EdgeCaseType::Dense => examples.extend(generate_dense_examples(min_count)),
        EdgeCaseType::Sparse => examples.extend(generate_sparse_examples(min_count)),
        EdgeCaseType::Nested => examples.extend(generate_nested_examples(min_count)),
        EdgeCaseType::Casing => examples.extend(generate_casing_examples(min_count)),
        EdgeCaseType::Boundary => examples.extend(generate_boundary_examples(min_count)),
        EdgeCaseType::MultiWord => examples.extend(generate_multiword_examples(min_count)),
        EdgeCaseType::NumericEdge => examples.extend(generate_numeric_edge_examples(min_count)),
        EdgeCaseType::Jargon => examples.extend(generate_jargon_examples(min_count)),
    }

    // Ensure we have at least min_count by duplicating with variations
    while examples.len() < min_count {
        let idx = examples.len() % examples.len().max(1);
        if let Some(ex) = examples.get(idx).cloned() {
            examples.push(ex);
        } else {
            break;
        }
    }

    examples
}

// ============================================================================
// Ambiguous Examples - Words that could be multiple types
// ============================================================================

fn generate_ambiguous_examples(count: usize) -> Vec<AnnotatedExample> {
    // Words that are genuinely ambiguous: "Apple" (company or fruit),
    // "Washington" (person, city, or state), "Amazon" (company or river)
    let templates = [
        // Apple - company vs fruit (this is genuinely hard)
        ("I love Apple products but prefer android.", vec![]), // Apple = company, but no clear entity marker
        ("The Apple fell from the tree.", vec![]),             // Apple = fruit, not an entity
        (
            "Apple Inc. reported earnings.",
            vec![("Apple Inc.", EntityType::Organization, 0)],
        ),
        (
            "Steve Jobs founded Apple.",
            vec![
                ("Steve Jobs", EntityType::Person, 0),
                ("Apple", EntityType::Organization, 19),
            ],
        ),
        // Washington - person, city, state, building
        (
            "Washington was the first president.",
            vec![("Washington", EntityType::Person, 0)],
        ),
        (
            "I visited Washington D.C. last week.",
            vec![("Washington D.C.", EntityType::Location, 10)],
        ),
        ("The meeting is at Washington Hotel.", vec![]), // Ambiguous: Location or Organization?
        (
            "George Washington crossed the Delaware.",
            vec![
                ("George Washington", EntityType::Person, 0),
                ("Delaware", EntityType::Location, 30),
            ],
        ),
        // Amazon - company vs river
        (
            "Amazon ships globally.",
            vec![("Amazon", EntityType::Organization, 0)],
        ),
        (
            "The Amazon river is massive.",
            vec![
                ("Amazon", EntityType::Location, 4), // River name
            ],
        ),
        (
            "Amazon Prime is popular.",
            vec![
                ("Amazon Prime", EntityType::Organization, 0), // Product/service
            ],
        ),
        // Jordan - person vs country
        (
            "Michael Jordan played basketball.",
            vec![("Michael Jordan", EntityType::Person, 0)],
        ),
        (
            "Jordan borders Israel.",
            vec![
                ("Jordan", EntityType::Location, 0),
                ("Israel", EntityType::Location, 15),
            ],
        ),
        (
            "Jordan Peele directed the film.",
            vec![("Jordan Peele", EntityType::Person, 0)],
        ),
        // Paris - city vs person (Paris Hilton)
        (
            "Paris is beautiful in spring.",
            vec![("Paris", EntityType::Location, 0)],
        ),
        (
            "Paris Hilton attended the event.",
            vec![("Paris Hilton", EntityType::Person, 0)],
        ),
        // China - country vs dinnerware (lowercase = tableware)
        ("Made in China.", vec![("China", EntityType::Location, 8)]),
        ("The china cabinet is antique.", vec![]), // lowercase china = porcelain
        // May - month vs name vs verb
        (
            "May is a lovely month.",
            vec![
                ("May", EntityType::Date, 0), // Month
            ],
        ),
        (
            "Theresa May resigned as PM.",
            vec![("Theresa May", EntityType::Person, 0)],
        ),
        ("You may proceed.", vec![]), // verb, not entity
        // Bill - name vs legislation vs invoice
        (
            "Bill Gates founded Microsoft.",
            vec![
                ("Bill Gates", EntityType::Person, 0),
                ("Microsoft", EntityType::Organization, 19),
            ],
        ),
        ("The bill passed the Senate.", vec![]), // legislation, not entity
        ("Please pay the bill.", vec![]),        // invoice, not entity
    ];

    generate_from_templates(&templates, count, Domain::News, Difficulty::Hard)
}

// ============================================================================
// Unicode Examples - RTL, combining characters, emoji
// ============================================================================

fn generate_unicode_examples(count: usize) -> Vec<AnnotatedExample> {
    let templates = [
        // CJK characters
        (
            "株式会社トヨタ自動車 reported earnings.",
            vec![("株式会社トヨタ自動車", EntityType::Organization, 0)],
        ),
        (
            "Contact 田中太郎 for details.",
            vec![("田中太郎", EntityType::Person, 8)],
        ),
        // Arabic (RTL)
        (
            "محمد is a common name.",
            vec![("محمد", EntityType::Person, 0)],
        ),
        // Cyrillic
        (
            "Владимир Путин addressed the nation.",
            vec![("Владимир Путин", EntityType::Person, 0)],
        ),
        (
            "Visit Москва this summer.",
            vec![("Москва", EntityType::Location, 6)],
        ),
        // Greek
        (
            "Αθήνα is the capital of Greece.",
            vec![
                ("Αθήνα", EntityType::Location, 0),
                ("Greece", EntityType::Location, 24),
            ],
        ),
        // Mixed scripts
        (
            "Sony (ソニー株式会社) announced new products.",
            vec![
                ("Sony", EntityType::Organization, 0),
                ("ソニー株式会社", EntityType::Organization, 6),
            ],
        ),
        // Emoji with entities
        (
            "Tim Berners-Lee invented the Web.",
            vec![
                ("Tim Berners-Lee", EntityType::Person, 0),
                (
                    "Web",
                    EntityType::custom("Technology", EntityCategory::Misc),
                    29,
                ),
            ],
        ),
        // Accented characters
        (
            "François Hollande was president.",
            vec![("François Hollande", EntityType::Person, 0)],
        ),
        (
            "Zürich is in Switzerland.",
            vec![
                ("Zürich", EntityType::Location, 0),
                ("Switzerland", EntityType::Location, 13),
            ],
        ),
        (
            "São Paulo is Brazil's largest city.",
            vec![
                ("São Paulo", EntityType::Location, 0),
                ("Brazil", EntityType::Location, 13),
            ],
        ),
        // Combining characters
        (
            "José García works at Google.",
            vec![
                ("José García", EntityType::Person, 0),
                ("Google", EntityType::Organization, 21),
            ],
        ),
        // Long multi-byte
        (
            "北京大学 and 清华大学 are top universities.",
            vec![
                ("北京大学", EntityType::Organization, 0),
                ("清华大学", EntityType::Organization, 9),
            ],
        ),
    ];

    generate_from_templates(&templates, count, Domain::News, Difficulty::Hard)
}

// ============================================================================
// Dense Examples - Many entities close together
// ============================================================================

fn generate_dense_examples(count: usize) -> Vec<AnnotatedExample> {
    let templates = [
        // Multiple entities per sentence
        (
            "Apple, Google, Microsoft, Amazon, and Meta all reported earnings.",
            vec![
                ("Apple", EntityType::Organization, 0),
                ("Google", EntityType::Organization, 7),
                ("Microsoft", EntityType::Organization, 15),
                ("Amazon", EntityType::Organization, 26),
                ("Meta", EntityType::Organization, 38),
            ],
        ),
        (
            "John Smith, Jane Doe, and Bob Wilson attended.",
            vec![
                ("John Smith", EntityType::Person, 0),
                ("Jane Doe", EntityType::Person, 12),
                ("Bob Wilson", EntityType::Person, 26),
            ],
        ),
        (
            "Contact support@company.com or sales@company.com.",
            vec![
                ("support@company.com", EntityType::Email, 8),
                ("sales@company.com", EntityType::Email, 31),
            ],
        ),
        (
            "Visit https://example.com and https://test.org.",
            vec![
                ("https://example.com", EntityType::Url, 6),
                ("https://test.org", EntityType::Url, 30),
            ],
        ),
        (
            "$100, $200, $300, and $400 are the prices.",
            vec![
                ("$100", EntityType::Money, 0),
                ("$200", EntityType::Money, 6),
                ("$300", EntityType::Money, 12),
                ("$400", EntityType::Money, 22),
            ],
        ),
        // Very dense entity sequence
        (
            "Tokyo, Beijing, Seoul, Bangkok, Singapore.",
            vec![
                ("Tokyo", EntityType::Location, 0),
                ("Beijing", EntityType::Location, 7),
                ("Seoul", EntityType::Location, 16),
                ("Bangkok", EntityType::Location, 23),
                ("Singapore", EntityType::Location, 32),
            ],
        ),
        // Dates and numbers
        (
            "January 1, February 2, March 3, April 4, May 5.",
            vec![
                ("January 1", EntityType::Date, 0),
                ("February 2", EntityType::Date, 11),
                ("March 3", EntityType::Date, 23),
                ("April 4", EntityType::Date, 32),
                ("May 5", EntityType::Date, 41),
            ],
        ),
        // Mixed entity types, dense
        (
            "Tim Cook (Apple), Sundar Pichai (Google), Satya Nadella (Microsoft).",
            vec![
                ("Tim Cook", EntityType::Person, 0),
                ("Apple", EntityType::Organization, 10),
                ("Sundar Pichai", EntityType::Person, 18),
                ("Google", EntityType::Organization, 33),
                ("Satya Nadella", EntityType::Person, 42),
                ("Microsoft", EntityType::Organization, 57),
            ],
        ),
    ];

    generate_from_templates(&templates, count, Domain::News, Difficulty::Hard)
}

// ============================================================================
// Sparse Examples - Single entity in long text
// ============================================================================

fn generate_sparse_examples(count: usize) -> Vec<AnnotatedExample> {
    let templates = [
        (
            "The weather has been quite nice lately with temperatures hovering around the mid-seventies and clear skies throughout most of the week. According to the forecast, this trend is expected to continue for at least another few days before any significant changes occur in the atmospheric conditions. Many people have been taking advantage of the pleasant weather to spend time outdoors, enjoying activities such as hiking, biking, and picnicking in the parks. The local news station reported that John Smith was seen at the park yesterday.",
            vec![("John Smith", EntityType::Person, 493)],
        ),
        (
            "In a move that surprised absolutely no one, the quarterly financial results came in exactly as analysts had predicted, with revenues matching expectations and profit margins holding steady. The company's stock price barely moved on the news, reflecting the market's general sense that everything was proceeding according to plan. After hours of discussion and deliberation, the board decided to maintain the current dividend policy and continue with the existing strategic initiatives. CEO Maria Garcia addressed the shareholders.",
            vec![("Maria Garcia", EntityType::Person, 490)],
        ),
        (
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris. The meeting is at Google headquarters.",
            vec![("Google", EntityType::Organization, 210)],
        ),
    ];

    generate_from_templates(&templates, count, Domain::News, Difficulty::Medium)
}

// ============================================================================
// Nested Examples - Overlapping/nested entity candidates
// ============================================================================

fn generate_nested_examples(count: usize) -> Vec<AnnotatedExample> {
    let templates = [
        // Organization within location name
        (
            "University of California, Los Angeles is a great school.",
            vec![(
                "University of California, Los Angeles",
                EntityType::Organization,
                0,
            )],
        ),
        // Person name contains place name
        (
            "Jack London wrote adventure novels.",
            vec![("Jack London", EntityType::Person, 0)],
        ),
        // Location within organization name
        (
            "Bank of America reported earnings.",
            vec![("Bank of America", EntityType::Organization, 0)],
        ),
        // Multiple nested candidates
        (
            "New York Times published the story.",
            vec![("New York Times", EntityType::Organization, 0)],
        ),
        // Person name that is also a place
        (
            "Carolina Herrera designs fashion.",
            vec![("Carolina Herrera", EntityType::Person, 0)],
        ),
        // Compound proper nouns
        (
            "The Wall Street Journal reports on Wall Street.",
            vec![
                ("Wall Street Journal", EntityType::Organization, 4),
                ("Wall Street", EntityType::Location, 35),
            ],
        ),
        // University names (nested location)
        (
            "Harvard University is in Cambridge, Massachusetts.",
            vec![
                ("Harvard University", EntityType::Organization, 0),
                ("Cambridge", EntityType::Location, 25),
                ("Massachusetts", EntityType::Location, 36),
            ],
        ),
    ];

    generate_from_templates(&templates, count, Domain::Academic, Difficulty::Hard)
}

// ============================================================================
// Casing Examples - Unusual capitalization
// ============================================================================

fn generate_casing_examples(count: usize) -> Vec<AnnotatedExample> {
    let templates = [
        // All caps (headlines, tweets)
        (
            "APPLE ANNOUNCES NEW IPHONE",
            vec![("APPLE", EntityType::Organization, 0)],
        ),
        // lowercase (informal text, tweets)
        (
            "just saw tim cook at the apple store lol",
            vec![
                // These should ideally be detected but many models fail here
            ],
        ),
        // CamelCase (product names, hashtags)
        (
            "Download the iPhone app from Apple.",
            vec![("Apple", EntityType::Organization, 29)],
        ),
        // Mixed case (typos, stylistic)
        (
            "BREAKING: Amazon CEO resigns",
            vec![("Amazon", EntityType::Organization, 10)],
        ),
        // Title case vs sentence case
        (
            "The President Of The United States spoke today.",
            vec![("United States", EntityType::Location, 21)],
        ),
        // Acronyms with periods
        (
            "The U.S.A. is large.",
            vec![("U.S.A.", EntityType::Location, 4)],
        ),
        (
            "Contact the F.B.I. for assistance.",
            vec![("F.B.I.", EntityType::Organization, 12)],
        ),
    ];

    generate_from_templates(&templates, count, Domain::SocialMedia, Difficulty::Hard)
}

// ============================================================================
// Boundary Examples - Entities at boundaries, with punctuation
// ============================================================================

fn generate_boundary_examples(count: usize) -> Vec<AnnotatedExample> {
    let templates = [
        // At start
        (
            "Apple is a company.",
            vec![("Apple", EntityType::Organization, 0)],
        ),
        // At end
        (
            "The company is Apple.",
            vec![("Apple", EntityType::Organization, 15)],
        ),
        // With punctuation
        (
            "Is Apple, the company, profitable?",
            vec![("Apple", EntityType::Organization, 3)],
        ),
        // With quotes
        (
            "\"Apple\" announced earnings.",
            vec![("Apple", EntityType::Organization, 1)],
        ),
        // With parentheses
        (
            "The company (Apple) is large.",
            vec![("Apple", EntityType::Organization, 13)],
        ),
        // With colon
        (
            "Company: Apple Inc.",
            vec![("Apple Inc.", EntityType::Organization, 9)],
        ),
        // Hyphenated
        (
            "The Apple-Google partnership.",
            vec![
                ("Apple", EntityType::Organization, 4),
                ("Google", EntityType::Organization, 10),
            ],
        ),
        // With apostrophe
        (
            "Apple's revenue increased.",
            vec![("Apple", EntityType::Organization, 0)],
        ),
        // Entity is the entire text
        ("John Smith", vec![("John Smith", EntityType::Person, 0)]),
        // Entity at very end with period
        (
            "The CEO is Tim Cook.",
            vec![("Tim Cook", EntityType::Person, 11)],
        ),
    ];

    generate_from_templates(&templates, count, Domain::News, Difficulty::Medium)
}

// ============================================================================
// Multi-Word Examples - Complex multi-token entities
// ============================================================================

fn generate_multiword_examples(count: usize) -> Vec<AnnotatedExample> {
    let templates = [
        // Titles with names
        (
            "Dr. John Smith presented the findings.",
            vec![("Dr. John Smith", EntityType::Person, 0)],
        ),
        (
            "Prof. Maria Garcia leads the department.",
            vec![("Prof. Maria Garcia", EntityType::Person, 0)],
        ),
        // Full company names
        (
            "International Business Machines Corporation announced layoffs.",
            vec![(
                "International Business Machines Corporation",
                EntityType::Organization,
                0,
            )],
        ),
        // Locations with qualifiers
        (
            "Greater Los Angeles Area is densely populated.",
            vec![("Greater Los Angeles Area", EntityType::Location, 0)],
        ),
        // Names with suffixes
        (
            "John Smith Jr. inherited the company.",
            vec![("John Smith Jr.", EntityType::Person, 0)],
        ),
        (
            "Robert Kennedy III is running.",
            vec![("Robert Kennedy III", EntityType::Person, 0)],
        ),
        // Organizations with "The"
        (
            "The New York Times reported.",
            vec![("The New York Times", EntityType::Organization, 0)],
        ),
        // Long place names
        (
            "The United Kingdom of Great Britain and Northern Ireland.",
            vec![(
                "United Kingdom of Great Britain and Northern Ireland",
                EntityType::Location,
                4,
            )],
        ),
    ];

    generate_from_templates(&templates, count, Domain::News, Difficulty::Hard)
}

// ============================================================================
// Numeric Edge Examples - Pattern edge cases
// ============================================================================

fn generate_numeric_edge_examples(count: usize) -> Vec<AnnotatedExample> {
    let templates = [
        // Money edge cases
        ("The price is $1.", vec![("$1", EntityType::Money, 13)]),
        ("It costs $0.01.", vec![("$0.01", EntityType::Money, 9)]),
        (
            "Worth $1,000,000.",
            vec![("$1,000,000", EntityType::Money, 6)],
        ),
        ("Price: $1.5M.", vec![("$1.5M", EntityType::Money, 7)]),
        (
            "Costs between $10-$20.",
            vec![
                ("$10", EntityType::Money, 14),
                ("$20", EntityType::Money, 18),
            ],
        ),
        // Percentage edge cases
        ("Increased 0.5%.", vec![("0.5%", EntityType::Percent, 10)]),
        ("Down 100%.", vec![("100%", EntityType::Percent, 5)]),
        ("About 33.333%.", vec![("33.333%", EntityType::Percent, 6)]),
        // Date edge cases
        ("On 1/1/2020.", vec![("1/1/2020", EntityType::Date, 3)]),
        (
            "Date: 2020-01-01.",
            vec![("2020-01-01", EntityType::Date, 6)],
        ),
        (
            "January 1st, 2020.",
            vec![("January 1st, 2020", EntityType::Date, 0)],
        ),
        ("The year 2020.", vec![]), // Year alone may not be a date entity
        // Time edge cases
        ("At 9:00 AM.", vec![("9:00 AM", EntityType::Time, 3)]),
        ("Meeting at 14:30.", vec![("14:30", EntityType::Time, 11)]),
        ("Around noon.", vec![]), // "noon" may or may not be time entity
        // Phone edge cases
        ("Call 555-1234.", vec![("555-1234", EntityType::Phone, 5)]),
        (
            "Phone: +1-800-555-1234.",
            vec![("+1-800-555-1234", EntityType::Phone, 7)],
        ),
        ("Ext. 1234.", vec![]), // Extension alone is not a phone
        // Email edge cases
        ("Email a@b.co.", vec![("a@b.co", EntityType::Email, 6)]),
        (
            "Contact test.user+tag@subdomain.example.com.",
            vec![("test.user+tag@subdomain.example.com", EntityType::Email, 8)],
        ),
        // URL edge cases
        (
            "Visit http://x.co.",
            vec![("http://x.co", EntityType::Url, 6)],
        ),
        ("Go to localhost:8080.", vec![]), // localhost is not typically a URL entity
    ];

    generate_from_templates(&templates, count, Domain::Technical, Difficulty::Hard)
}

// ============================================================================
// Jargon Examples - Domain-specific terminology
// ============================================================================

fn generate_jargon_examples(count: usize) -> Vec<AnnotatedExample> {
    let templates = [
        // Medical jargon - entities hard to distinguish from regular words
        ("The patient has COVID-19.", vec![]), // COVID-19 may be disease, not organization
        (
            "Pfizer developed the vaccine.",
            vec![("Pfizer", EntityType::Organization, 0)],
        ),
        // Legal jargon
        ("Per Brown v. Board of Education.", vec![]), // Case names are complex
        (
            "The defendant, John Smith, pleaded.",
            vec![("John Smith", EntityType::Person, 15)],
        ),
        // Tech jargon - product names vs companies
        ("Install Python 3.10.", vec![]), // Python is language, not entity
        (
            "Microsoft released Windows 11.",
            vec![("Microsoft", EntityType::Organization, 0)],
        ),
        ("The React library is popular.", vec![]), // React is library
        (
            "Facebook created React.",
            vec![("Facebook", EntityType::Organization, 0)],
        ),
        // Financial jargon
        ("S&P 500 rose 2%.", vec![("2%", EntityType::Percent, 13)]), // S&P 500 is index
        ("NASDAQ hit record highs.", vec![]),                        // NASDAQ is index
        (
            "Goldman Sachs upgraded the stock.",
            vec![("Goldman Sachs", EntityType::Organization, 0)],
        ),
        // Sports jargon
        (
            "The Lakers beat the Celtics 110-105.",
            vec![
                ("Lakers", EntityType::Organization, 4),
                ("Celtics", EntityType::Organization, 20),
            ],
        ),
        (
            "LeBron James scored 30 points.",
            vec![("LeBron James", EntityType::Person, 0)],
        ),
    ];

    generate_from_templates(&templates, count, Domain::Technical, Difficulty::Hard)
}

// ============================================================================
// Helper Functions
// ============================================================================

#[allow(clippy::type_complexity)]
fn generate_from_templates(
    templates: &[(&str, Vec<(&str, EntityType, usize)>)],
    count: usize,
    domain: Domain,
    difficulty: Difficulty,
) -> Vec<AnnotatedExample> {
    let mut examples = Vec::with_capacity(count);

    for (text, entity_specs) in templates.iter().cycle().take(count.max(templates.len())) {
        let entities: Vec<GoldEntity> = entity_specs
            .iter()
            .map(|(txt, entity_type, start)| GoldEntity::new(*txt, entity_type.clone(), *start))
            .collect();

        examples.push(AnnotatedExample {
            text: (*text).to_string(),
            entities,
            domain,
            difficulty,
        });
    }

    examples
}

/// Statistics about a benchmark dataset.
#[derive(Debug, Clone)]
pub struct BenchmarkStats {
    /// Total number of examples in the dataset.
    pub total_examples: usize,
    /// Total number of entities across all examples.
    pub total_entities: usize,
    /// Average entities per example.
    pub avg_entities_per_example: f64,
    /// Number of examples with no entities (negative examples).
    pub examples_with_no_entities: usize,
    /// Distribution of edge case types (if tracked during generation).
    pub edge_case_distribution: Vec<(EdgeCaseType, usize)>,
}

impl BenchmarkStats {
    /// Calculate stats from a dataset
    pub fn from_dataset(examples: &[AnnotatedExample]) -> Self {
        let total_examples = examples.len();
        let total_entities: usize = examples.iter().map(|e| e.entities.len()).sum();
        let examples_with_no_entities = examples.iter().filter(|e| e.entities.is_empty()).count();

        Self {
            total_examples,
            total_entities,
            avg_entities_per_example: total_entities as f64 / total_examples.max(1) as f64,
            examples_with_no_entities,
            edge_case_distribution: vec![], // Would need tracking during generation
        }
    }
}

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

    #[test]
    fn test_generate_large_dataset() {
        let dataset = generate_large_dataset(100, EdgeCaseType::All);
        assert!(
            dataset.len() >= 100,
            "Should generate at least 100 examples"
        );
    }

    #[test]
    fn test_ambiguous_examples() {
        let examples = generate_ambiguous_examples(10);
        assert!(!examples.is_empty());
        // Verify some examples have no entities (genuinely ambiguous)
        let no_entity_count = examples.iter().filter(|e| e.entities.is_empty()).count();
        assert!(
            no_entity_count > 0,
            "Should have some ambiguous (no entity) cases"
        );
    }

    #[test]
    fn test_unicode_examples() {
        let examples = generate_unicode_examples(10);
        assert!(!examples.is_empty());
        // Verify we have non-ASCII text
        let has_unicode = examples.iter().any(|e| !e.text.is_ascii());
        assert!(has_unicode, "Should have non-ASCII characters");
    }

    #[test]
    fn test_entity_offsets_valid() {
        let dataset = generate_large_dataset(500, EdgeCaseType::All);
        for example in &dataset {
            for entity in &example.entities {
                assert!(
                    entity.start < example.text.len(),
                    "Entity '{}' start {} >= text length {} in: {}",
                    entity.text,
                    entity.start,
                    example.text.len(),
                    example.text
                );
                // Note: Using chars().count() for Unicode-safe comparison
                let char_count = example.text.chars().count();
                assert!(
                    entity.end <= char_count,
                    "Entity '{}' end {} > char count {} in: {}",
                    entity.text,
                    entity.end,
                    char_count,
                    example.text
                );
            }
        }
    }

    #[test]
    fn test_benchmark_stats() {
        let dataset = generate_large_dataset(100, EdgeCaseType::All);
        let stats = BenchmarkStats::from_dataset(&dataset);
        assert!(stats.total_examples >= 100);
        assert!(stats.total_entities > 0);
    }

    // Slow test - generates many examples
    #[test]
    #[ignore] // Run with: cargo test -- --ignored
    fn test_large_scale_benchmark() {
        let dataset = generate_large_dataset(10_000, EdgeCaseType::All);
        assert!(dataset.len() >= 10_000);
        let stats = BenchmarkStats::from_dataset(&dataset);
        println!("Large benchmark stats: {:?}", stats);
    }
}