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
//! Table-driven dataset metadata.
//!
//! This module provides a single source of truth for all dataset metadata,
//! replacing the 15+ separate `match` statements that previously existed.
//!
//! # Design
//!
//! Instead of:
//! ```rust,ignore
//! fn domain(&self) -> &'static str {
//!     match self {
//!         DatasetId::WikiGold => "news",
//!         DatasetId::BC5CDR => "biomedical",
//!         // ... 450+ more cases
//!     }
//! }
//! ```
//!
//! We use:
//! ```rust,ignore
//! fn domain(&self) -> &'static str {
//!     METADATA[self.index()].domain
//! }
//! ```
//!
//! This reduces ~18K lines to ~3K lines and makes adding datasets O(1) instead of O(n).

use bitflags::bitflags;

bitflags! {
    /// Dataset capability/category flags.
    ///
    /// Using bitflags allows O(1) category checks and easy combination queries.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub struct DatasetFlags: u32 {
        /// Named Entity Recognition dataset
        const NER = 1 << 0;
        /// Coreference resolution dataset
        const COREFERENCE = 1 << 1;
        /// Intra-document coreference
        const INTRA_DOC_COREF = 1 << 2;
        /// Cross-document / inter-document coreference
        const INTER_DOC_COREF = 1 << 3;
        /// Temporal NER (dates, durations, temporal expressions)
        const TEMPORAL_NER = 1 << 4;
        /// Biomedical/clinical domain
        const BIOMEDICAL = 1 << 5;
        /// Social media domain (Twitter, Reddit, etc.)
        const SOCIAL_MEDIA = 1 << 6;
        /// Specialized domain (not general news)
        const SPECIALIZED_DOMAIN = 1 << 7;
        /// Relation extraction dataset
        const RELATION_EXTRACTION = 1 << 8;
        /// Historical texts
        const HISTORICAL = 1 << 9;
        /// Bias evaluation dataset
        const BIAS_EVALUATION = 1 << 10;
        /// Dialogue/conversational coreference
        const DIALOGUE_COREF = 1 << 11;
        /// Joint NER + Relation Extraction
        const JOINT_NER_RE = 1 << 12;
        /// Discontinuous/nested NER
        const DISCONTINUOUS_NER = 1 << 13;
        /// Few-shot learning dataset
        const FEW_SHOT = 1 << 14;
        /// Multilingual dataset
        const MULTILINGUAL = 1 << 15;
        /// Constructed/artificial language
        const CONSTRUCTED_LANGUAGE = 1 << 16;
        /// Code-switching dataset
        const CODE_SWITCHING = 1 << 17;
        /// African language dataset
        const AFRICAN_LANGUAGE = 1 << 18;
        /// Entity linking dataset
        const ENTITY_LINKING = 1 << 19;
        /// Event extraction dataset
        const EVENT_EXTRACTION = 1 << 20;
        /// Legal domain
        const LEGAL = 1 << 21;
        /// Financial domain
        const FINANCIAL = 1 << 22;
        /// Scientific domain
        const SCIENTIFIC = 1 << 23;
        /// Literary/fiction domain
        const LITERARY = 1 << 24;
        /// News domain
        const NEWS = 1 << 25;
        /// Low-resource language
        const LOW_RESOURCE = 1 << 26;
    }
}

impl Default for DatasetFlags {
    fn default() -> Self {
        Self::NER
    }
}

/// Complete metadata for a single dataset.
///
/// All fields are static references for zero-cost access.
#[derive(Debug, Clone, Copy)]
pub struct DatasetMetadata {
    /// Human-readable name
    pub name: &'static str,
    /// Short description (1-2 sentences)
    pub description: &'static str,
    /// Download URL (HuggingFace, GitHub, etc.)
    pub download_url: &'static str,
    /// Primary domain (news, biomedical, social-media, etc.)
    pub domain: &'static str,
    /// ISO 639-1/3 language code (en, de, zh, multilingual, etc.)
    pub language: &'static str,
    /// Entity types in this dataset
    pub entity_types: &'static [&'static str],
    /// Capability flags (replaces all is_* methods)
    pub flags: DatasetFlags,
    /// Academic citation (if available)
    pub citation: Option<&'static str>,
    /// License (MIT, CC-BY, etc.)
    pub license: Option<&'static str>,
    /// Publication year
    pub year: Option<u16>,
    /// Paper URL (arXiv, ACL Anthology, etc.)
    pub paper_url: Option<&'static str>,
}

impl DatasetMetadata {
    /// Create metadata with required fields only.
    #[must_use]
    pub const fn new(
        name: &'static str,
        description: &'static str,
        download_url: &'static str,
    ) -> Self {
        Self {
            name,
            description,
            download_url,
            domain: "general",
            language: "en",
            entity_types: &[],
            flags: DatasetFlags::NER,
            citation: None,
            license: None,
            year: None,
            paper_url: None,
        }
    }

    /// Builder: set domain.
    #[must_use]
    pub const fn domain(mut self, domain: &'static str) -> Self {
        self.domain = domain;
        self
    }

    /// Builder: set language.
    #[must_use]
    pub const fn language(mut self, language: &'static str) -> Self {
        self.language = language;
        self
    }

    /// Builder: set entity types.
    #[must_use]
    pub const fn entity_types(mut self, types: &'static [&'static str]) -> Self {
        self.entity_types = types;
        self
    }

    /// Builder: set flags.
    #[must_use]
    pub const fn flags(mut self, flags: DatasetFlags) -> Self {
        self.flags = flags;
        self
    }

    /// Builder: set citation.
    #[must_use]
    pub const fn citation(mut self, citation: &'static str) -> Self {
        self.citation = Some(citation);
        self
    }

    /// Builder: set license.
    #[must_use]
    pub const fn license(mut self, license: &'static str) -> Self {
        self.license = Some(license);
        self
    }

    /// Builder: set year.
    #[must_use]
    pub const fn year(mut self, year: u16) -> Self {
        self.year = Some(year);
        self
    }

    /// Builder: set paper URL.
    #[must_use]
    pub const fn paper_url(mut self, url: &'static str) -> Self {
        self.paper_url = Some(url);
        self
    }

    // Flag query methods (all O(1))

    /// Returns `true` if this dataset supports named entity recognition.
    ///
    /// NER datasets provide entity span annotations with type labels.
    #[inline]
    pub const fn is_ner(&self) -> bool {
        self.flags.contains(DatasetFlags::NER)
    }

    /// Returns `true` if this dataset contains coreference annotations.
    ///
    /// Coreference datasets provide mention clusters or entity chains
    /// indicating which mentions refer to the same entity.
    #[inline]
    pub const fn is_coreference(&self) -> bool {
        self.flags.contains(DatasetFlags::COREFERENCE)
    }

    /// Returns `true` if this dataset contains within-document coreference.
    ///
    /// Intra-document coreference datasets link mentions within a single
    /// document, forming coreference chains or clusters.
    #[inline]
    pub const fn is_intra_doc_coref(&self) -> bool {
        self.flags.contains(DatasetFlags::INTRA_DOC_COREF)
    }

    /// Returns `true` if this dataset contains cross-document coreference.
    ///
    /// Inter-document coreference datasets link entities across multiple
    /// documents, enabling cross-document entity resolution.
    #[inline]
    pub const fn is_inter_doc_coref(&self) -> bool {
        self.flags.contains(DatasetFlags::INTER_DOC_COREF)
    }

    /// Returns `true` if this dataset contains temporal entity annotations.
    ///
    /// Temporal NER datasets include time expressions, events with temporal
    /// anchors, or entities with temporal attributes (e.g., historical entities).
    #[inline]
    pub const fn is_temporal_ner(&self) -> bool {
        self.flags.contains(DatasetFlags::TEMPORAL_NER)
    }

    /// Returns `true` if this dataset is from the biomedical domain.
    ///
    /// Biomedical datasets include medical, clinical, or life science text
    /// with specialized entity types (diseases, genes, proteins, chemicals).
    #[inline]
    pub const fn is_biomedical(&self) -> bool {
        self.flags.contains(DatasetFlags::BIOMEDICAL)
    }

    /// Returns `true` if this dataset contains social media text.
    ///
    /// Social media datasets include Twitter, Reddit, or other informal text
    /// with non-standard capitalization, abbreviations, and informal language.
    #[inline]
    pub const fn is_social_media(&self) -> bool {
        self.flags.contains(DatasetFlags::SOCIAL_MEDIA)
    }

    /// Returns `true` if this dataset is from a specialized domain.
    ///
    /// Specialized domain datasets include technical, legal, financial, or
    /// other domain-specific text requiring specialized entity recognition.
    #[inline]
    pub const fn is_specialized_domain(&self) -> bool {
        self.flags.contains(DatasetFlags::SPECIALIZED_DOMAIN)
    }

    /// Returns `true` if this dataset supports relation extraction.
    ///
    /// Relation extraction datasets provide entity-relation-entity triples,
    /// enabling evaluation of models that extract structured relationships.
    #[inline]
    pub const fn is_relation_extraction(&self) -> bool {
        self.flags.contains(DatasetFlags::RELATION_EXTRACTION)
    }

    /// Returns `true` if this dataset contains historical text.
    ///
    /// Historical datasets include ancient texts, historical documents, or
    /// diachronic corpora that test model robustness to language evolution.
    #[inline]
    pub const fn is_historical(&self) -> bool {
        self.flags.contains(DatasetFlags::HISTORICAL)
    }

    /// Returns `true` if this dataset is designed for bias evaluation.
    ///
    /// Bias evaluation datasets test for gender, demographic, or other biases
    /// in entity recognition and coreference resolution.
    #[inline]
    pub const fn is_bias_evaluation(&self) -> bool {
        self.flags.contains(DatasetFlags::BIAS_EVALUATION)
    }

    /// Returns `true` if this dataset contains dialogue coreference annotations.
    ///
    /// Dialogue coreference datasets include multi-party conversations, meetings,
    /// or interviews where coreference resolution must handle speaker turns and
    /// dialogue-specific phenomena (e.g., prosody, gestures).
    #[inline]
    pub const fn is_dialogue_coref(&self) -> bool {
        self.flags.contains(DatasetFlags::DIALOGUE_COREF)
    }

    /// Returns `true` if this dataset supports joint NER and relation extraction.
    ///
    /// Joint datasets provide both entity annotations and relation triples,
    /// enabling evaluation of models that perform both tasks simultaneously.
    #[inline]
    pub const fn is_joint_ner_re(&self) -> bool {
        self.flags.contains(DatasetFlags::JOINT_NER_RE)
    }

    /// Returns `true` if this dataset contains discontinuous entity annotations.
    ///
    /// Discontinuous entities span non-contiguous tokens (e.g., "left and right
    /// ventricle" where "ventricle" is split). Requires specialized evaluation
    /// metrics beyond standard span-based NER.
    #[inline]
    pub const fn is_discontinuous_ner(&self) -> bool {
        self.flags.contains(DatasetFlags::DISCONTINUOUS_NER)
    }

    /// Returns `true` if this dataset is designed for few-shot learning evaluation.
    ///
    /// Few-shot datasets typically have small training sets or are used to test
    /// zero-shot transfer from related domains.
    #[inline]
    pub const fn is_few_shot(&self) -> bool {
        self.flags.contains(DatasetFlags::FEW_SHOT)
    }

    /// Returns `true` if this dataset covers multiple languages.
    ///
    /// Multilingual datasets enable cross-lingual evaluation and testing of
    /// zero-shot transfer between languages.
    #[inline]
    pub const fn is_multilingual(&self) -> bool {
        self.flags.contains(DatasetFlags::MULTILINGUAL)
    }

    /// Returns `true` if this dataset contains constructed/artificial languages.
    ///
    /// Constructed languages (e.g., Esperanto, Klingon) test model generalization
    /// to languages with different structural properties than natural languages.
    #[inline]
    pub const fn is_constructed_language(&self) -> bool {
        self.flags.contains(DatasetFlags::CONSTRUCTED_LANGUAGE)
    }

    /// Returns `true` if this dataset contains code-switched text.
    ///
    /// Code-switching datasets include text where speakers mix multiple languages
    /// within the same utterance, requiring models to handle language boundaries.
    #[inline]
    pub const fn is_code_switching(&self) -> bool {
        self.flags.contains(DatasetFlags::CODE_SWITCHING)
    }

    /// Returns `true` if this dataset contains African languages.
    ///
    /// African language datasets are important for evaluating model performance
    /// on under-resourced languages and diverse linguistic structures.
    #[inline]
    pub const fn is_african_language(&self) -> bool {
        self.flags.contains(DatasetFlags::AFRICAN_LANGUAGE)
    }
}

// =============================================================================
// Standard entity type sets (reduces duplication)
// =============================================================================

/// CoNLL-style entity types (PER, LOC, ORG, MISC)
pub static CONLL_TYPES: &[&str] = &["PER", "LOC", "ORG", "MISC"];

/// OntoNotes entity types (18 types)
pub static ONTONOTES_TYPES: &[&str] = &[
    "PERSON",
    "NORP",
    "FAC",
    "ORG",
    "GPE",
    "LOC",
    "PRODUCT",
    "EVENT",
    "WORK_OF_ART",
    "LAW",
    "LANGUAGE",
    "DATE",
    "TIME",
    "PERCENT",
    "MONEY",
    "QUANTITY",
    "ORDINAL",
    "CARDINAL",
];

/// Biomedical entity types
pub static BIO_TYPES: &[&str] = &["Chemical", "Disease", "Gene", "Species"];

/// ACE entity types
pub static ACE_TYPES: &[&str] = &["PER", "ORG", "GPE", "LOC", "FAC", "VEH", "WEA"];

// =============================================================================
// Static metadata table (proof of concept - first 20 datasets)
// =============================================================================
//
// This table replaces the 15+ match statements in loader.rs.
// Full migration will happen incrementally.

/// Metadata for WikiGold dataset
pub static WIKIGOLD: DatasetMetadata = DatasetMetadata::new(
    "WikiGold",
    "Wikipedia-based NER (PER, LOC, ORG, MISC)",
    "https://huggingface.co/datasets/wikigold",
)
.domain("news")
.language("en")
.entity_types(CONLL_TYPES)
.flags(DatasetFlags::NER.union(DatasetFlags::NEWS))
.year(2009);

/// Metadata for WNUT-17 dataset
pub static WNUT17: DatasetMetadata = DatasetMetadata::new(
    "WNUT-17",
    "Social media NER (emerging entities)",
    "https://huggingface.co/datasets/wnut_17",
)
.domain("social-media")
.language("en")
.entity_types(&[
    "person",
    "location",
    "corporation",
    "product",
    "creative-work",
    "group",
])
.flags(DatasetFlags::NER.union(DatasetFlags::SOCIAL_MEDIA))
.year(2017);

/// Metadata for BC5CDR dataset
pub static BC5CDR: DatasetMetadata = DatasetMetadata::new(
    "BC5CDR",
    "Biomedical NER (chemicals, diseases)",
    "https://huggingface.co/datasets/bc5cdr",
)
.domain("biomedical")
.language("en")
.entity_types(&["Chemical", "Disease"])
.flags(
    DatasetFlags::NER
        .union(DatasetFlags::BIOMEDICAL)
        .union(DatasetFlags::SPECIALIZED_DOMAIN),
)
.year(2015);

/// Metadata for GAP coreference dataset
pub static GAP: DatasetMetadata = DatasetMetadata::new(
    "GAP",
    "Gendered Ambiguous Pronouns",
    "https://huggingface.co/datasets/gap",
)
.domain("coreference")
.language("en")
.entity_types(&["Pronoun", "Name"])
.flags(
    DatasetFlags::COREFERENCE
        .union(DatasetFlags::INTRA_DOC_COREF)
        .union(DatasetFlags::BIAS_EVALUATION),
)
.year(2018);

/// Metadata for OntoNotes 5.0 coreference dataset
pub static ONTONOTES_COREF: DatasetMetadata = DatasetMetadata::new(
    "OntoNotes 5.0 (Coreference)",
    "Standard coreference benchmark",
    "https://catalog.ldc.upenn.edu/LDC2013T19",
)
.domain("coreference")
.language("en")
.entity_types(ONTONOTES_TYPES)
.flags(
    DatasetFlags::COREFERENCE
        .union(DatasetFlags::INTRA_DOC_COREF)
        .union(DatasetFlags::NER),
)
.year(2013);

/// Metadata for ECB+ cross-document coreference dataset
pub static ECBPLUS: DatasetMetadata = DatasetMetadata::new(
    "ECB+",
    "Event Coreference Bank Plus",
    "http://www.newsreader-project.eu/results/data/the-ecb-corpus/",
)
.domain("coreference")
.language("en")
.entity_types(&["Event", "Entity"])
.flags(
    DatasetFlags::COREFERENCE
        .union(DatasetFlags::INTER_DOC_COREF)
        .union(DatasetFlags::EVENT_EXTRACTION),
)
.year(2014);

/// Metadata for MultiNERD multilingual dataset
pub static MULTINERD: DatasetMetadata = DatasetMetadata::new(
    "MultiNERD",
    "Multilingual NER (10 languages)",
    "https://huggingface.co/datasets/Babelscape/multinerd",
)
.domain("multilingual")
.language("multilingual")
.entity_types(&[
    "PER", "LOC", "ORG", "ANIM", "BIO", "CEL", "DIS", "EVE", "FOOD", "INST", "MEDIA", "MYTH",
    "PLANT", "TIME", "VEHI",
])
.flags(DatasetFlags::NER.union(DatasetFlags::MULTILINGUAL))
.year(2022);

/// Metadata for FewNERD dataset
pub static FEWNERD: DatasetMetadata = DatasetMetadata::new(
    "FewNERD",
    "Few-shot NER with fine-grained types",
    "https://huggingface.co/datasets/DFKI-SLT/few-nerd",
)
.domain("general")
.language("en")
.entity_types(&[
    "person",
    "location",
    "organization",
    "building",
    "art",
    "product",
    "event",
    "other",
])
.flags(DatasetFlags::NER.union(DatasetFlags::FEW_SHOT))
.year(2021);

/// Metadata for MasakhaNER African languages dataset
pub static MASAKHANER: DatasetMetadata = DatasetMetadata::new(
    "MasakhaNER",
    "NER for African languages",
    "https://huggingface.co/datasets/masakhaner",
)
.domain("low-resource")
.language("multilingual")
.entity_types(CONLL_TYPES)
.flags(
    DatasetFlags::NER
        .union(DatasetFlags::MULTILINGUAL)
        .union(DatasetFlags::AFRICAN_LANGUAGE)
        .union(DatasetFlags::LOW_RESOURCE),
)
.year(2021);

/// Metadata for GENIA biomedical dataset
pub static GENIA: DatasetMetadata = DatasetMetadata::new(
    "GENIA",
    "Biomedical NER (genes, proteins)",
    "http://www.geniaproject.org/",
)
.domain("biomedical")
.language("en")
.entity_types(&["DNA", "RNA", "protein", "cell_line", "cell_type"])
.flags(
    DatasetFlags::NER
        .union(DatasetFlags::BIOMEDICAL)
        .union(DatasetFlags::SPECIALIZED_DOMAIN),
)
.year(2003);

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

    #[test]
    fn test_flags_operations() {
        let flags = DatasetFlags::NER | DatasetFlags::BIOMEDICAL | DatasetFlags::SPECIALIZED_DOMAIN;
        assert!(flags.contains(DatasetFlags::NER));
        assert!(flags.contains(DatasetFlags::BIOMEDICAL));
        assert!(!flags.contains(DatasetFlags::SOCIAL_MEDIA));
    }

    #[test]
    fn test_metadata_builder() {
        let meta = DatasetMetadata::new("Test", "A test dataset", "https://example.com")
            .domain("biomedical")
            .language("en")
            .entity_types(&["Disease", "Drug"])
            .flags(DatasetFlags::NER | DatasetFlags::BIOMEDICAL)
            .year(2023);

        assert_eq!(meta.name, "Test");
        assert_eq!(meta.domain, "biomedical");
        assert!(meta.is_biomedical());
        assert!(!meta.is_social_media());
        assert_eq!(meta.year, Some(2023));
    }

    #[test]
    fn test_const_construction() {
        // Verify const construction works (for static tables)
        const META: DatasetMetadata = DatasetMetadata::new("Const", "Desc", "url")
            .domain("test")
            .language("en");

        assert_eq!(META.name, "Const");
        assert_eq!(META.domain, "test");
    }

    #[test]
    fn test_static_wikigold() {
        assert_eq!(WIKIGOLD.name, "WikiGold");
        assert_eq!(WIKIGOLD.domain, "news");
        assert!(WIKIGOLD.is_ner());
        assert!(!WIKIGOLD.is_coreference());
    }

    #[test]
    fn test_static_bc5cdr() {
        assert!(BC5CDR.is_biomedical());
        assert!(BC5CDR.is_specialized_domain());
        assert_eq!(BC5CDR.entity_types.len(), 2);
    }

    #[test]
    fn test_static_gap() {
        assert!(GAP.is_coreference());
        assert!(GAP.is_intra_doc_coref());
        assert!(GAP.is_bias_evaluation());
        assert!(!GAP.is_ner());
    }

    #[test]
    fn test_static_ecbplus() {
        assert!(ECBPLUS.is_inter_doc_coref());
        assert!(!ECBPLUS.is_intra_doc_coref());
    }

    #[test]
    fn test_static_masakhaner() {
        assert!(MASAKHANER.is_african_language());
        assert!(MASAKHANER.is_multilingual());
    }
}