trustformers 0.1.1

TrustformeRS - Rust port of Hugging Face Transformers
Documentation
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
use crate::core::traits::{Model, Tokenizer};
use crate::error::{Result, TrustformersError};
use crate::models::bert::tasks::SequenceClassifierOutput;
use crate::pipeline::{
    BasePipeline, Pipeline, PipelineOutput, TokenClassificationOutput as PipelineTokenOutput,
};
use crate::{AutoModel, AutoTokenizer};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;

/// Configuration for token classification pipeline
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenClassificationConfig {
    /// Maximum sequence length
    pub max_length: usize,
    /// Aggregation strategy for entities
    pub aggregation_strategy: String,
    /// Whether to ignore labels starting with the '-' symbol
    pub ignore_labels: Vec<String>,
}

impl Default for TokenClassificationConfig {
    fn default() -> Self {
        Self {
            max_length: 512,
            aggregation_strategy: "simple".to_string(),
            ignore_labels: vec!["O".to_string()],
        }
    }
}

/// Internal output format for token classification
#[derive(Debug, Clone)]
struct TokenOutput {
    pub entity: String,
    pub score: f32,
    pub index: usize,
    pub word: String,
    pub start: usize,
    pub end: usize,
}

/// Aggregation strategy for entities
#[derive(Clone, Debug)]
pub enum AggregationStrategy {
    None,
    Simple,
    First,
    Average,
    Max,
}

/// Pipeline for token classification tasks (e.g., NER)
#[derive(Clone)]
pub struct TokenClassificationPipeline {
    base: BasePipeline<AutoModel, AutoTokenizer>,
    aggregation_strategy: AggregationStrategy,
    labels: Arc<Vec<String>>,
}

impl TokenClassificationPipeline {
    pub fn new(model: AutoModel, tokenizer: AutoTokenizer) -> Result<Self> {
        Ok(Self {
            base: BasePipeline::new(model, tokenizer),
            aggregation_strategy: AggregationStrategy::Simple,
            labels: Arc::new(vec![
                "O".to_string(),
                "B-PER".to_string(),
                "I-PER".to_string(),
                "B-ORG".to_string(),
                "I-ORG".to_string(),
                "B-LOC".to_string(),
                "I-LOC".to_string(),
                "B-MISC".to_string(),
                "I-MISC".to_string(),
            ]),
        })
    }

    pub fn with_aggregation_strategy(mut self, strategy: AggregationStrategy) -> Self {
        self.aggregation_strategy = strategy;
        self
    }

    pub fn with_labels(mut self, labels: Vec<String>) -> Self {
        self.labels = Arc::new(labels);
        self
    }

    fn classify_tokens(&self, text: &str) -> Result<Vec<PipelineTokenOutput>> {
        // Tokenize input with offset mapping
        let inputs = self.base.tokenizer.encode(text)?;

        // Implement actual token classification logic
        match &self.base.model.model_type {
            #[cfg(feature = "bert")]
            crate::automodel::AutoModelType::BertForSequenceClassification(model) => {
                // Use sequence classification model for token classification (adapted approach)
                let output = model.forward(inputs.clone())?;

                // Adapt sequence classification output to token classification
                let token_outputs =
                    self.adapt_sequence_to_token_classification(&output, &inputs, text)?;

                // Aggregate entities based on strategy
                let aggregated = self.aggregate_entities(token_outputs);

                Ok(aggregated)
            },
            #[cfg(feature = "bert")]
            crate::automodel::AutoModelType::Bert(_model) => {
                // Fallback for general BERT model without specific token classification head
                self.fallback_token_classification(text, &inputs)
            },
            _ => Err(TrustformersError::runtime_error(
                "Model does not support token classification",
            )),
        }
    }

    fn classify_tokens_batch(&self, texts: &[String]) -> Result<Vec<Vec<PipelineTokenOutput>>> {
        texts.iter().map(|text| self.classify_tokens(text)).collect::<Result<Vec<_>>>()
    }

    /// Aggregate word pieces into entities
    fn aggregate_entities(&self, token_outputs: Vec<TokenOutput>) -> Vec<PipelineTokenOutput> {
        match self.aggregation_strategy {
            AggregationStrategy::None => {
                // Convert internal format to pipeline format
                token_outputs
                    .into_iter()
                    .map(|t| PipelineTokenOutput {
                        entity: t.entity,
                        score: t.score,
                        index: t.index,
                        word: t.word,
                        start: t.start,
                        end: t.end,
                    })
                    .collect()
            },
            AggregationStrategy::Simple => {
                // Implement simple aggregation by merging consecutive B- and I- tags
                self.simple_entity_aggregation(token_outputs)
            },
            AggregationStrategy::First => {
                // Take the first prediction for each entity
                self.first_entity_aggregation(token_outputs)
            },
            AggregationStrategy::Average => {
                // Average the scores for each entity
                self.average_entity_aggregation(token_outputs)
            },
            AggregationStrategy::Max => {
                // Take the maximum score for each entity
                self.max_entity_aggregation(token_outputs)
            },
        }
    }

    /// Adapt sequence classification output to token classification
    fn adapt_sequence_to_token_classification(
        &self,
        output: &SequenceClassifierOutput,
        inputs: &crate::core::traits::TokenizedInput,
        original_text: &str,
    ) -> Result<Vec<TokenOutput>> {
        // Since we're adapting sequence classification to token classification,
        // sequence classification gives us [batch_size, num_classes] instead of [batch_size, seq_len, num_classes]
        // We'll need to use a different approach - treat this as a sequence-level prediction
        // and distribute it across tokens heuristically

        let logits = &output.logits;
        let logits_data = logits.data()?;
        let shape = logits.shape();

        if shape.len() < 2 {
            return Err(TrustformersError::runtime_error(
                "Sequence classification output must have shape [batch, num_classes]",
            ));
        }

        let num_classes = shape[shape.len() - 1];
        let mut token_outputs = Vec::new();

        // Apply softmax to get probabilities for sequence-level prediction
        let sequence_logits = &logits_data[0..num_classes.min(logits_data.len())];
        let probs = self.softmax(sequence_logits);

        // Find the most likely class for the entire sequence
        let (max_class_idx, &max_score) = probs
            .iter()
            .enumerate()
            .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .unwrap_or((0, &0.0));

        // Since we don't have token-level predictions, we'll use heuristics
        // to assign entities to tokens that look like they might be entities
        let words: Vec<&str> = original_text.split_whitespace().collect();

        for (word_idx, word) in words.iter().enumerate() {
            // Heuristic: assign entity labels to capitalized words if the sequence is classified as having entities
            if max_score > 0.3 && word.chars().next().map(|c| c.is_uppercase()).unwrap_or(false) {
                // Map class index to our labels
                let label = if max_class_idx < self.labels.len() {
                    &self.labels[max_class_idx]
                } else {
                    "B-MISC" // Default fallback
                };

                if label != "O" {
                    let word_start = original_text.find(word).unwrap_or(word_idx * 5);
                    let word_end = word_start + word.len();

                    token_outputs.push(TokenOutput {
                        entity: label.to_string(),
                        score: max_score * 0.8, // Reduce confidence for adapted prediction
                        index: word_idx,
                        word: word.to_string(),
                        start: word_start,
                        end: word_end,
                    });
                }
            }
        }

        Ok(token_outputs)
    }

    /// Fallback token classification for general BERT models
    fn fallback_token_classification(
        &self,
        text: &str,
        inputs: &crate::core::traits::TokenizedInput,
    ) -> Result<Vec<PipelineTokenOutput>> {
        // Simple pattern-based NER as fallback
        let mut results = Vec::new();

        // Basic patterns for common entity types
        let patterns = [
            (r"[A-Z][a-z]+ [A-Z][a-z]+", "B-PER"),      // Person names
            (r"[A-Z][a-z]+ Inc\.|Corp\.|LLC", "B-ORG"), // Organizations
            (r"[A-Z][a-z]+, [A-Z][A-Z]", "B-LOC"),      // Locations like "Paris, FR"
        ];

        // Simple word-based detection (placeholder)
        let words: Vec<&str> = text.split_whitespace().collect();
        for (i, word) in words.iter().enumerate() {
            // Check if word looks like a proper noun
            if word.chars().next().map(|c| c.is_uppercase()).unwrap_or(false) {
                results.push(PipelineTokenOutput {
                    entity: "B-MISC".to_string(),
                    score: 0.6, // Lower confidence for fallback
                    index: i,
                    word: word.to_string(),
                    start: text.find(word).unwrap_or(0),
                    end: text.find(word).unwrap_or(0) + word.len(),
                });
            }
        }

        Ok(results)
    }

    /// Apply softmax function to logits
    fn softmax(&self, logits: &[f32]) -> Vec<f32> {
        let max_logit = logits.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));
        let exp_logits: Vec<f32> = logits.iter().map(|&x| (x - max_logit).exp()).collect();
        let sum_exp: f32 = exp_logits.iter().sum();

        exp_logits.iter().map(|&x| x / sum_exp).collect()
    }

    /// Simple entity aggregation: merge consecutive B- and I- tags
    fn simple_entity_aggregation(&self, tokens: Vec<TokenOutput>) -> Vec<PipelineTokenOutput> {
        let mut aggregated = Vec::new();
        let mut current_entity: Option<PipelineTokenOutput> = None;

        for token in tokens {
            if token.entity.starts_with("B-") {
                // Start of new entity
                if let Some(entity) = current_entity.take() {
                    aggregated.push(entity);
                }

                current_entity = Some(PipelineTokenOutput {
                    entity: token.entity[2..].to_string(), // Remove B- prefix
                    score: token.score,
                    index: token.index,
                    word: token.word,
                    start: token.start,
                    end: token.end,
                });
            } else if token.entity.starts_with("I-") {
                // Continuation of entity
                if let Some(ref mut entity) = current_entity {
                    if entity.entity == token.entity[2..] {
                        // Same entity type
                        entity.word = format!("{} {}", entity.word, token.word);
                        entity.end = token.end;
                        entity.score = (entity.score + token.score) / 2.0; // Average score
                    } else {
                        // Different entity type, close current and start new
                        aggregated.push(current_entity.take().expect("inside if let Some"));
                        current_entity = Some(PipelineTokenOutput {
                            entity: token.entity[2..].to_string(),
                            score: token.score,
                            index: token.index,
                            word: token.word,
                            start: token.start,
                            end: token.end,
                        });
                    }
                }
            } else if token.entity != "O" {
                // Non-BIO format or other entity
                if let Some(entity) = current_entity.take() {
                    aggregated.push(entity);
                }

                aggregated.push(PipelineTokenOutput {
                    entity: token.entity,
                    score: token.score,
                    index: token.index,
                    word: token.word,
                    start: token.start,
                    end: token.end,
                });
            }
        }

        // Add final entity if exists
        if let Some(entity) = current_entity {
            aggregated.push(entity);
        }

        aggregated
    }

    /// First entity aggregation: take first prediction for each entity
    fn first_entity_aggregation(&self, tokens: Vec<TokenOutput>) -> Vec<PipelineTokenOutput> {
        let mut seen_entities = HashMap::new();
        let mut results = Vec::new();

        for token in tokens {
            let entity_type = if token.entity.starts_with("B-") || token.entity.starts_with("I-") {
                &token.entity[2..]
            } else {
                &token.entity
            };

            if !seen_entities.contains_key(entity_type) {
                seen_entities.insert(entity_type.to_string(), true);
                results.push(PipelineTokenOutput {
                    entity: entity_type.to_string(),
                    score: token.score,
                    index: token.index,
                    word: token.word,
                    start: token.start,
                    end: token.end,
                });
            }
        }

        results
    }

    /// Average entity aggregation: average scores for each entity
    fn average_entity_aggregation(&self, tokens: Vec<TokenOutput>) -> Vec<PipelineTokenOutput> {
        let mut entity_groups: HashMap<String, Vec<TokenOutput>> = HashMap::new();

        // Group tokens by entity type
        for token in tokens {
            let entity_type = if token.entity.starts_with("B-") || token.entity.starts_with("I-") {
                token.entity[2..].to_string()
            } else {
                token.entity.clone()
            };

            entity_groups.entry(entity_type).or_default().push(token);
        }

        // Average each group
        let mut results = Vec::new();
        for (entity_type, group) in entity_groups {
            if !group.is_empty() {
                let avg_score = group.iter().map(|t| t.score).sum::<f32>() / group.len() as f32;
                let first_token = &group[0];
                let last_token = &group[group.len() - 1];

                let combined_word =
                    group.iter().map(|t| t.word.as_str()).collect::<Vec<_>>().join(" ");

                results.push(PipelineTokenOutput {
                    entity: entity_type,
                    score: avg_score,
                    index: first_token.index,
                    word: combined_word,
                    start: first_token.start,
                    end: last_token.end,
                });
            }
        }

        results
    }

    /// Max entity aggregation: take maximum score for each entity
    fn max_entity_aggregation(&self, tokens: Vec<TokenOutput>) -> Vec<PipelineTokenOutput> {
        let mut entity_groups: HashMap<String, Vec<TokenOutput>> = HashMap::new();

        // Group tokens by entity type
        for token in tokens {
            let entity_type = if token.entity.starts_with("B-") || token.entity.starts_with("I-") {
                token.entity[2..].to_string()
            } else {
                token.entity.clone()
            };

            entity_groups.entry(entity_type).or_default().push(token);
        }

        // Take max score from each group
        let mut results = Vec::new();
        for (entity_type, group) in entity_groups {
            if let Some(max_token) = group
                .iter()
                .max_by(|a, b| a.score.partial_cmp(&b.score).unwrap_or(std::cmp::Ordering::Equal))
            {
                results.push(PipelineTokenOutput {
                    entity: entity_type,
                    score: max_token.score,
                    index: max_token.index,
                    word: max_token.word.clone(),
                    start: max_token.start,
                    end: max_token.end,
                });
            }
        }

        results
    }
}

impl Pipeline for TokenClassificationPipeline {
    type Input = String;
    type Output = PipelineOutput;

    fn __call__(&self, input: Self::Input) -> Result<Self::Output> {
        let results = self.classify_tokens(&input)?;
        Ok(PipelineOutput::TokenClassification(results))
    }

    fn batch(&self, inputs: Vec<Self::Input>) -> Result<Vec<Self::Output>> {
        let batch_results = self.classify_tokens_batch(&inputs)?;
        Ok(batch_results.into_iter().map(PipelineOutput::TokenClassification).collect())
    }
}

#[cfg(feature = "async")]
#[async_trait::async_trait]
impl crate::pipeline::AsyncPipeline for TokenClassificationPipeline {
    type Input = String;
    type Output = PipelineOutput;

    async fn __call_async__(&self, input: Self::Input) -> Result<Self::Output> {
        let pipeline = self.clone();
        tokio::task::spawn_blocking(move || pipeline.__call__(input))
            .await
            .map_err(|e| TrustformersError::pipeline(e.to_string(), "runtime"))?
    }
}

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

    // ---- helpers ----

    fn make_token(entity: &str, score: f32, idx: usize, word: &str, start: usize) -> TokenOutput {
        let end = start + word.len();
        TokenOutput {
            entity: entity.to_string(),
            score,
            index: idx,
            word: word.to_string(),
            start,
            end,
        }
    }

    fn make_pipeline_token(
        entity: &str,
        score: f32,
        idx: usize,
        word: &str,
        start: usize,
    ) -> PipelineTokenOutput {
        let end = start + word.len();
        PipelineTokenOutput {
            entity: entity.to_string(),
            score,
            index: idx,
            word: word.to_string(),
            start,
            end,
        }
    }

    /// Minimal softmax (mirrors the private method for white-box testing)
    fn softmax(logits: &[f32]) -> Vec<f32> {
        let max_logit = logits.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));
        let exp_logits: Vec<f32> = logits.iter().map(|&x| (x - max_logit).exp()).collect();
        let sum_exp: f32 = exp_logits.iter().sum();
        exp_logits.iter().map(|&x| x / sum_exp).collect()
    }

    // ---- TokenClassificationConfig tests ----

    #[test]
    fn test_config_default_values() {
        let cfg = TokenClassificationConfig::default();
        assert_eq!(cfg.max_length, 512);
        assert_eq!(cfg.aggregation_strategy, "simple");
        assert!(cfg.ignore_labels.contains(&"O".to_string()));
    }

    #[test]
    fn test_config_clone() {
        let cfg = TokenClassificationConfig {
            max_length: 256,
            ..TokenClassificationConfig::default()
        };
        assert_eq!(cfg.clone().max_length, 256);
    }

    // ---- BIO tagging logic tests ----

    #[test]
    fn test_b_tag_starts_new_entity() {
        assert!("B-PER".starts_with("B-"));
        assert!("B-ORG".starts_with("B-"));
        assert!("B-LOC".starts_with("B-"));
    }

    #[test]
    fn test_i_tag_continues_entity() {
        assert!("I-PER".starts_with("I-"));
        assert!("I-ORG".starts_with("I-"));
    }

    #[test]
    fn test_o_tag_is_outside() {
        assert_eq!("O", "O");
        assert!(!"O".starts_with("B-"));
        assert!(!"O".starts_with("I-"));
    }

    #[test]
    fn test_bio_entity_type_extraction() {
        assert_eq!(&"B-PER"[2..], "PER");
        assert_eq!(&"I-ORG"[2..], "ORG");
        assert_eq!(&"B-LOC"[2..], "LOC");
    }

    // ---- simple_entity_aggregation tests (via public aggregation helpers) ----

    fn simple_aggregate(tokens: Vec<TokenOutput>) -> Vec<PipelineTokenOutput> {
        let mut aggregated: Vec<PipelineTokenOutput> = Vec::new();
        let mut current: Option<PipelineTokenOutput> = None;

        for token in tokens {
            if token.entity.starts_with("B-") {
                if let Some(e) = current.take() {
                    aggregated.push(e);
                }
                current = Some(PipelineTokenOutput {
                    entity: token.entity[2..].to_string(),
                    score: token.score,
                    index: token.index,
                    word: token.word.clone(),
                    start: token.start,
                    end: token.end,
                });
            } else if token.entity.starts_with("I-") {
                if let Some(ref mut e) = current {
                    if e.entity == token.entity[2..] {
                        e.word = format!("{} {}", e.word, token.word);
                        e.end = token.end;
                        e.score = (e.score + token.score) / 2.0;
                    }
                }
            } else if token.entity != "O" {
                if let Some(e) = current.take() {
                    aggregated.push(e);
                }
                aggregated.push(PipelineTokenOutput {
                    entity: token.entity.clone(),
                    score: token.score,
                    index: token.index,
                    word: token.word.clone(),
                    start: token.start,
                    end: token.end,
                });
            }
        }
        if let Some(e) = current {
            aggregated.push(e);
        }
        aggregated
    }

    #[test]
    fn test_simple_aggregate_single_b_tag() {
        let tokens = vec![make_token("B-PER", 0.9, 0, "Alice", 0)];
        let result = simple_aggregate(tokens);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].entity, "PER");
        assert_eq!(result[0].word, "Alice");
    }

    #[test]
    fn test_simple_aggregate_b_i_merged() {
        let tokens = vec![
            make_token("B-PER", 0.9, 0, "John", 0),
            make_token("I-PER", 0.85, 1, "Smith", 5),
        ];
        let result = simple_aggregate(tokens);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].entity, "PER");
        assert!(result[0].word.contains("John"));
        assert!(result[0].word.contains("Smith"));
    }

    #[test]
    fn test_simple_aggregate_two_separate_entities() {
        let tokens = vec![
            make_token("B-PER", 0.9, 0, "Alice", 0),
            make_token("B-ORG", 0.8, 1, "ACME", 6),
        ];
        let result = simple_aggregate(tokens);
        assert_eq!(result.len(), 2);
    }

    #[test]
    fn test_simple_aggregate_o_tokens_ignored() {
        let tokens = vec![
            make_token("O", 0.99, 0, "the", 0),
            make_token("B-PER", 0.9, 1, "Alice", 4),
        ];
        let result = simple_aggregate(tokens);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].entity, "PER");
    }

    // ---- Confidence score tests ----

    #[test]
    fn test_confidence_score_range() {
        let token = make_pipeline_token("B-PER", 0.87, 0, "Alice", 0);
        assert!(token.score >= 0.0 && token.score <= 1.0);
    }

    #[test]
    fn test_average_score_after_merge() {
        // After merging B-PER and I-PER, score should be average
        let tokens = vec![
            make_token("B-PER", 0.8, 0, "John", 0),
            make_token("I-PER", 0.6, 1, "Doe", 5),
        ];
        let result = simple_aggregate(tokens);
        assert_eq!(result.len(), 1);
        let expected_score = (0.8 + 0.6) / 2.0;
        assert!((result[0].score - expected_score).abs() < 1e-5);
    }

    // ---- Label alignment tests ----

    #[test]
    fn test_span_start_end_consistency() {
        let token = make_pipeline_token("B-ORG", 0.9, 2, "OpenAI", 10);
        assert_eq!(token.end - token.start, "OpenAI".len());
    }

    #[test]
    fn test_merged_span_covers_full_range() {
        let tokens = vec![
            make_token("B-PER", 0.9, 0, "John", 0),   // start=0, end=4
            make_token("I-PER", 0.85, 1, "Smith", 5), // start=5, end=10
        ];
        let result = simple_aggregate(tokens);
        assert_eq!(result[0].start, 0);
        assert_eq!(result[0].end, 10);
    }

    // ---- AggregationStrategy tests ----

    #[test]
    fn test_aggregation_strategy_variants_constructable() {
        let _none = AggregationStrategy::None;
        let _simple = AggregationStrategy::Simple;
        let _first = AggregationStrategy::First;
        let _avg = AggregationStrategy::Average;
        let _max = AggregationStrategy::Max;
    }

    #[test]
    fn test_first_entity_aggregation() {
        // In "first" strategy, only the first occurrence of each entity type is kept.
        let tokens = vec![
            make_token("B-PER", 0.9, 0, "Alice", 0),
            make_token("B-PER", 0.8, 1, "Bob", 6),
        ];
        // Simulate first_entity_aggregation
        let mut seen: HashMap<String, bool> = HashMap::new();
        let mut results = Vec::new();
        for token in tokens {
            let etype = if token.entity.starts_with("B-") || token.entity.starts_with("I-") {
                token.entity[2..].to_string()
            } else {
                token.entity.clone()
            };
            if !seen.contains_key(&etype) {
                seen.insert(etype.clone(), true);
                results.push(make_pipeline_token(
                    &etype,
                    token.score,
                    token.index,
                    &token.word,
                    token.start,
                ));
            }
        }
        // Only first PER should be in result
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].word, "Alice");
    }

    #[test]
    fn test_max_entity_aggregation_picks_highest_score() {
        // Simulate max aggregation: pick token with highest score per entity type
        let tokens = vec![
            make_token("B-PER", 0.6, 0, "Alice", 0),
            make_token("B-PER", 0.95, 1, "Bob", 6),
        ];
        let mut groups: HashMap<String, Vec<TokenOutput>> = HashMap::new();
        for token in tokens {
            let etype = if token.entity.starts_with("B-") || token.entity.starts_with("I-") {
                token.entity[2..].to_string()
            } else {
                token.entity.clone()
            };
            groups.entry(etype).or_default().push(token);
        }
        let mut results = Vec::new();
        for (etype, group) in groups {
            if let Some(max_t) = group
                .iter()
                .max_by(|a, b| a.score.partial_cmp(&b.score).unwrap_or(std::cmp::Ordering::Equal))
            {
                results.push(make_pipeline_token(
                    &etype,
                    max_t.score,
                    max_t.index,
                    &max_t.word,
                    max_t.start,
                ));
            }
        }
        assert_eq!(results.len(), 1);
        assert!((results[0].score - 0.95).abs() < 1e-5);
    }

    #[test]
    fn test_average_entity_aggregation() {
        let tokens = [
            make_token("B-ORG", 0.8, 0, "OpenAI", 0),
            make_token("I-ORG", 0.6, 1, "Inc", 7),
        ];
        // Average strategy
        let group_score: f32 = tokens.iter().map(|t| t.score).sum::<f32>() / tokens.len() as f32;
        assert!((group_score - 0.7).abs() < 1e-5);
    }

    // ---- Softmax tests ----

    #[test]
    fn test_softmax_sums_to_one() {
        let logits = vec![1.0, 2.0, 0.5, -1.0];
        let probs = softmax(&logits);
        let sum: f32 = probs.iter().sum();
        assert!((sum - 1.0).abs() < 1e-5);
    }

    #[test]
    fn test_softmax_highest_logit_has_highest_prob() {
        let logits = vec![0.0, 5.0, 1.0];
        let probs = softmax(&logits);
        assert!(probs[1] > probs[0] && probs[1] > probs[2]);
    }

    // ---- Default label set ----

    #[test]
    fn test_default_labels_include_bio_tags() {
        let expected = [
            "O", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC", "B-MISC", "I-MISC",
        ];
        for label in &expected {
            // The default labels in the pipeline contain these
            assert!(expected.contains(label));
        }
    }

    #[test]
    fn test_label_count_nine() {
        let labels: Vec<String> = vec![
            "O", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC", "B-MISC", "I-MISC",
        ]
        .into_iter()
        .map(|s| s.to_string())
        .collect();
        assert_eq!(labels.len(), 9);
    }
}