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
//! # Question Answering Metrics for TrustformeRS
//!
//! This module provides evaluation metrics for question answering tasks, including
//! extractive QA, reading comprehension, and answer generation systems.
//!
//! ## Overview
//!
//! The `QuestionAnsweringMetric` implementation provides specialized evaluation for
//! question answering models using Exact Match (EM) and token-level F1 scores,
//! which are the standard metrics for QA evaluation.
//!
//! ## Features
//!
//! - **Exact Match (EM)**: Measures percentage of predictions that match exactly
//! - **Token-level F1**: Measures overlap between predicted and reference answer tokens
//! - **Answer normalization**: Standardizes text for fair comparison
//! - **Combined scoring**: Averages EM and F1 for comprehensive evaluation
//!
//! ## Usage Examples
//!
//! ### Basic QA Evaluation
//!
//! ```rust,ignore
//! use trustformers::auto::metrics::{QuestionAnsweringMetric, MetricInput, Metric};
//!
//! let mut metric = QuestionAnsweringMetric::new();
//!
//! // QA predictions and references
//! let predictions = MetricInput::Text(vec![
//!     "Barack Obama".to_string(),
//!     "July 20, 1969".to_string(),
//! ]);
//! let references = MetricInput::Text(vec![
//!     "Barack Hussein Obama".to_string(),
//!     "July 20, 1969".to_string(),
//! ]);
//!
//! metric.add_batch(&predictions, &references)?;
//!
//! // Compute results
//! let result = metric.compute()?;
//! println!("QA Score: {:.3}", result.value);
//! println!("Exact Match: {:.3}", result.details.get("exact_match").unwrap());
//! println!("F1: {:.3}", result.details.get("f1").unwrap());
//! ```
//!
//! ### Reading Comprehension Evaluation
//!
//! ```rust,ignore
//! use trustformers::auto::metrics::{QuestionAnsweringMetric, MetricInput, Metric};
//!
//! let mut metric = QuestionAnsweringMetric::new();
//!
//! // Model answers vs gold standard answers
//! let predictions = MetricInput::Text(vec![
//!     "The capital of France".to_string(),
//!     "Machine learning algorithm".to_string(),
//! ]);
//! let references = MetricInput::Text(vec![
//!     "Paris is the capital of France".to_string(),
//!     "A machine learning algorithm".to_string(),
//! ]);
//!
//! metric.add_batch(&predictions, &references)?;
//! let result = metric.compute()?;
//! ```
//!
//! ## Implementation Details
//!
//! ### Evaluation Metrics
//!
//! 1. **Exact Match (EM)**:
//!    - Percentage of predictions that match references exactly after normalization
//!    - Binary: either 0 (no match) or 1 (exact match) for each sample
//!    - Final score is the average across all samples
//!
//! 2. **Token-level F1**:
//!    - Precision: `common_tokens / predicted_tokens`
//!    - Recall: `common_tokens / reference_tokens`
//!    - F1: `2 * precision * recall / (precision + recall)`
//!    - Averaged across all samples
//!
//! ### Answer Normalization
//!
//! Before comparison, answers are normalized by:
//! - Converting to lowercase
//! - Removing non-alphanumeric characters (except spaces)
//! - Collapsing multiple spaces to single spaces
//! - Trimming leading/trailing whitespace
//!
//! This ensures fair comparison despite formatting differences.
//!
//! ### Performance Characteristics
//!
//! - **Time complexity**: O(n*m) where n and m are token counts in answers
//! - **Space complexity**: O(n) for storing answer pairs
//! - **Normalization cost**: Minimal preprocessing overhead

use super::{Metric, MetricInput, MetricResult};
use crate::error::{Result, TrustformersError};
use crate::evaluation::bridge::NlpAdapter;
use std::collections::HashMap;

/// Question answering metric implementation
///
/// Provides evaluation metrics for question answering tasks using Exact Match
/// and token-level F1 scores, following standard QA evaluation practices.
///
/// ## Design Principles
///
/// - **Standard metrics**: Implements widely-accepted QA evaluation measures
/// - **Fair comparison**: Normalizes answers to handle formatting differences
/// - **Comprehensive**: Combines exact matching with fuzzy token-level scoring
/// - **Accumulative**: Supports batch-wise evaluation for large datasets
///
/// ## Supported Input Types
///
/// - `Text`: Predicted answers and reference answers as strings
///
/// ## Metric Interpretation
///
/// - **Exact Match**: Higher values indicate more precise predictions
/// - **F1 Score**: Higher values indicate better token-level overlap
/// - **Combined Score**: Average of EM and F1 for balanced evaluation
#[derive(Debug, Clone)]
pub struct QuestionAnsweringMetric {
    /// Accumulated predicted answers
    predictions: Vec<String>,
    /// Accumulated reference answers
    references: Vec<String>,
}

impl QuestionAnsweringMetric {
    /// Create a new question answering metric instance
    ///
    /// Initializes an empty metric ready to accumulate answer predictions and references.
    ///
    /// # Returns
    ///
    /// New `QuestionAnsweringMetric` instance with empty state.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use trustformers::auto::metrics::QuestionAnsweringMetric;
    ///
    /// let metric = QuestionAnsweringMetric::new();
    /// assert_eq!(metric.name(), "question_answering");
    /// ```
    pub fn new() -> Self {
        Self {
            predictions: Vec::new(),
            references: Vec::new(),
        }
    }

    /// Normalize an answer string for comparison
    ///
    /// Applies standard normalization to ensure fair comparison between
    /// predicted and reference answers, handling common formatting variations.
    ///
    /// # Arguments
    ///
    /// * `s` - The answer string to normalize
    ///
    /// # Returns
    ///
    /// Normalized string ready for comparison
    ///
    /// # Normalization Steps
    ///
    /// 1. Convert to lowercase
    /// 2. Keep only alphanumeric characters and spaces
    /// 3. Collapse multiple spaces to single spaces
    /// 4. Trim leading and trailing whitespace
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use trustformers::auto::metrics::QuestionAnsweringMetric;
    ///
    /// let metric = QuestionAnsweringMetric::new();
    /// let normalized = metric.normalize_answer("  Barack H. Obama!  ");
    /// assert_eq!(normalized, "barack h obama");
    /// ```
    fn normalize_answer(&self, s: &str) -> String {
        s.to_lowercase()
            .chars()
            .filter(|c| c.is_alphanumeric() || c.is_whitespace())
            .collect::<String>()
            .split_whitespace()
            .collect::<Vec<&str>>()
            .join(" ")
    }
}

impl Metric for QuestionAnsweringMetric {
    /// Add a batch of answer predictions and references
    ///
    /// Accumulates question answering data for later metric computation.
    /// Both predictions and references must be text strings representing answers.
    ///
    /// # Arguments
    ///
    /// * `predictions` - Predicted answers from the QA model (Text)
    /// * `references` - Ground truth reference answers (Text)
    ///
    /// # Input Format Requirements
    ///
    /// - **Text**: Vector of strings containing predicted and reference answers
    /// - Both predictions and references should have the same length
    ///
    /// # Returns
    ///
    /// `Ok(())` on success, error if input formats are incompatible.
    ///
    /// # Errors
    ///
    /// - `InvalidInput`: If input types are not both Text variants
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    ///    /// use trustformers::auto::metrics::{QuestionAnsweringMetric, MetricInput, Metric};
    ///
    /// let mut metric = QuestionAnsweringMetric::new();
    ///
    /// let predictions = MetricInput::Text(vec![
    ///     "Paris".to_string(),
    ///     "Albert Einstein".to_string(),
    /// ]);
    /// let references = MetricInput::Text(vec![
    ///     "Paris, France".to_string(),
    ///     "Einstein".to_string(),
    /// ]);
    ///
    /// metric.add_batch(&predictions, &references)?;

    fn add_batch(&mut self, predictions: &MetricInput, references: &MetricInput) -> Result<()> {
        match (predictions, references) {
            (MetricInput::Text(pred), MetricInput::Text(ref_)) => {
                self.predictions.extend(pred.clone());
                self.references.extend(ref_.clone());
                Ok(())
            },
            _ => Err(TrustformersError::invalid_input_simple("Invalid input types for QA metric: expected Text for both predictions and references".to_string()
            )),
        }
    }

    /// Compute question answering metrics
    ///
    /// Calculates Exact Match and token-level F1 scores for question answering
    /// evaluation, following standard QA evaluation practices.
    ///
    /// # Returns
    ///
    /// `MetricResult` containing:
    /// - **Primary value**: Average of Exact Match and F1 scores
    /// - **Details**:
    ///   - `exact_match`: Percentage of predictions matching exactly
    ///   - `f1`: Average token-level F1 score across all samples
    ///
    /// # Errors
    ///
    /// - `InvalidInput`: If no data has been added to the metric
    /// - Note: Length mismatches are handled gracefully (shorter list determines pairs)
    ///
    /// # Algorithm Details
    ///
    /// For each (prediction, reference) pair:
    /// 1. **Normalization**: Apply answer normalization to both strings
    /// 2. **Exact Match**: Check if normalized strings are identical
    /// 3. **Token F1**:
    ///    - Tokenize by whitespace
    ///    - Count overlapping tokens
    ///    - Calculate precision, recall, and F1
    /// 4. **Aggregation**: Average EM and F1 across all pairs
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    ///    /// use trustformers::auto::metrics::{QuestionAnsweringMetric, MetricInput, Metric};
    ///
    /// let mut metric = QuestionAnsweringMetric::new();
    /// metric.add_batch(
    ///     &MetricInput::Text(vec!["Barack Obama".to_string()]),
    ///     &MetricInput::Text(vec!["Barack Hussein Obama".to_string()])
    /// )?;
    ///
    /// let result = metric.compute()?;
    /// assert_eq!(result.name, "question_answering");
    /// assert!(result.value >= 0.0 && result.value <= 1.0);
    /// assert!(result.details.contains_key("exact_match"));
    /// assert!(result.details.contains_key("f1"));

    fn compute(&self) -> Result<MetricResult> {
        if self.predictions.is_empty() {
            return Err(TrustformersError::invalid_input_simple(
                "No data available for metric computation".to_string(),
            ));
        }

        let num_pairs = self.predictions.len().min(self.references.len());

        // Exact match via local normalisation loop (must normalise before comparing).
        let mut exact_matches = 0;
        for (pred, ref_) in self.predictions.iter().zip(self.references.iter()) {
            let pred_norm = self.normalize_answer(pred);
            let ref_norm = self.normalize_answer(ref_);
            if pred_norm == ref_norm {
                exact_matches += 1;
            }
        }
        let exact_match_score =
            if num_pairs > 0 { exact_matches as f64 / num_pairs as f64 } else { 0.0 };

        // Token-level F1 — delegate to NlpAdapter::token_f1() on normalised strings.
        let norm_preds: Vec<String> =
            self.predictions.iter().map(|s| self.normalize_answer(s)).collect();
        let norm_refs: Vec<String> =
            self.references.iter().map(|s| self.normalize_answer(s)).collect();

        let preds_input = MetricInput::Text(norm_preds);
        let refs_input = MetricInput::Text(norm_refs);
        let mut token_f1_adapter = NlpAdapter::token_f1();
        token_f1_adapter.add_batch(&preds_input, &refs_input)?;
        let f1_result = token_f1_adapter.compute()?;
        let avg_f1 = f1_result.value;

        let mut details = HashMap::new();
        details.insert("exact_match".to_string(), exact_match_score);
        details.insert("f1".to_string(), avg_f1);

        Ok(MetricResult {
            name: "question_answering".to_string(),
            value: (exact_match_score + avg_f1) / 2.0,
            details,
            metadata: HashMap::new(),
        })
    }

    /// Reset the metric state
    ///
    /// Clears all accumulated predictions and references, preparing the metric
    /// for a new evaluation run. This is more efficient than creating a new
    /// metric instance.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    ///    /// use trustformers::auto::metrics::{QuestionAnsweringMetric, MetricInput, Metric};
    ///
    /// let mut metric = QuestionAnsweringMetric::new();
    /// metric.add_batch(
    ///     &MetricInput::Text(vec!["Paris".to_string()]),
    ///     &MetricInput::Text(vec!["Paris, France".to_string()])
    /// )?;
    ///
    /// metric.reset();
    /// // Metric is now ready for new answer pairs

    fn reset(&mut self) {
        self.predictions.clear();
        self.references.clear();
    }

    /// Get the metric name
    ///
    /// Returns the identifier for this metric type, used in logging and results.
    ///
    /// # Returns
    ///
    /// String slice "question_answering"
    fn name(&self) -> &str {
        "question_answering"
    }
}

impl Default for QuestionAnsweringMetric {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_qa_metric_basic() {
        let mut metric = QuestionAnsweringMetric::new();

        let predictions = MetricInput::Text(vec![
            "Barack Obama".to_string(),
            "July 20, 1969".to_string(),
        ]);
        let references = MetricInput::Text(vec![
            "Barack Hussein Obama".to_string(),
            "July 20, 1969".to_string(),
        ]);

        metric.add_batch(&predictions, &references).expect("add operation failed");

        let result = metric.compute().expect("operation failed in test");
        assert_eq!(result.name, "question_answering");
        assert!(result.value >= 0.0 && result.value <= 1.0);
        assert!(result.details.contains_key("exact_match"));
        assert!(result.details.contains_key("f1"));
    }

    #[test]
    fn test_qa_metric_perfect_match() {
        let mut metric = QuestionAnsweringMetric::new();

        let predictions = MetricInput::Text(vec!["Barack Obama".to_string()]);
        let references = MetricInput::Text(vec!["Barack Obama".to_string()]);

        metric.add_batch(&predictions, &references).expect("add operation failed");

        let result = metric.compute().expect("operation failed in test");
        assert_eq!(result.details.get("exact_match"), Some(&1.0));
        assert_eq!(result.details.get("f1"), Some(&1.0));
        assert_eq!(result.value, 1.0); // Average of 1.0 and 1.0
    }

    #[test]
    fn test_qa_metric_no_match() {
        let mut metric = QuestionAnsweringMetric::new();

        let predictions = MetricInput::Text(vec!["completely different".to_string()]);
        let references = MetricInput::Text(vec!["Barack Obama".to_string()]);

        metric.add_batch(&predictions, &references).expect("add operation failed");

        let result = metric.compute().expect("operation failed in test");
        assert_eq!(result.details.get("exact_match"), Some(&0.0));
        assert_eq!(result.details.get("f1"), Some(&0.0));
        assert_eq!(result.value, 0.0); // Average of 0.0 and 0.0
    }

    #[test]
    fn test_qa_metric_partial_match() {
        let mut metric = QuestionAnsweringMetric::new();

        let predictions = MetricInput::Text(vec!["Barack Obama".to_string()]);
        let references = MetricInput::Text(vec!["Barack Hussein Obama".to_string()]);

        metric.add_batch(&predictions, &references).expect("add operation failed");

        let result = metric.compute().expect("operation failed in test");
        assert_eq!(result.details.get("exact_match"), Some(&0.0)); // No exact match

        // F1 should be > 0 due to overlapping tokens
        let f1 = result.details.get("f1").expect("expected value not found");
        assert!(*f1 > 0.0 && *f1 < 1.0);
    }

    #[test]
    fn test_qa_metric_normalization() {
        let mut metric = QuestionAnsweringMetric::new();

        let predictions = MetricInput::Text(vec!["  Barack H. Obama!  ".to_string()]);
        let references = MetricInput::Text(vec!["Barack H Obama".to_string()]);

        metric.add_batch(&predictions, &references).expect("add operation failed");

        let result = metric.compute().expect("operation failed in test");
        assert_eq!(result.details.get("exact_match"), Some(&1.0)); // Should match after normalization
    }

    #[test]
    fn test_qa_metric_empty_answer() {
        let mut metric = QuestionAnsweringMetric::new();

        let predictions = MetricInput::Text(vec!["".to_string()]);
        let references = MetricInput::Text(vec!["Barack Obama".to_string()]);

        metric.add_batch(&predictions, &references).expect("add operation failed");

        let result = metric.compute().expect("operation failed in test");
        assert_eq!(result.details.get("exact_match"), Some(&0.0));
        assert_eq!(result.details.get("f1"), Some(&0.0)); // No tokens to match
    }

    #[test]
    fn test_qa_metric_multiple_batches() {
        let mut metric = QuestionAnsweringMetric::new();

        // First batch - perfect match
        metric
            .add_batch(
                &MetricInput::Text(vec!["Barack Obama".to_string()]),
                &MetricInput::Text(vec!["Barack Obama".to_string()]),
            )
            .expect("operation failed in test");

        // Second batch - no match
        metric
            .add_batch(
                &MetricInput::Text(vec!["different answer".to_string()]),
                &MetricInput::Text(vec!["Barack Obama".to_string()]),
            )
            .expect("operation failed in test");

        let result = metric.compute().expect("operation failed in test");
        // Should average: (1.0 + 0.0) / 2 = 0.5 for EM, (1.0 + 0.0) / 2 = 0.5 for F1
        assert_eq!(result.details.get("exact_match"), Some(&0.5));
        assert_eq!(result.details.get("f1"), Some(&0.5));
        assert_eq!(result.value, 0.5);
    }

    #[test]
    fn test_qa_metric_reset() {
        let mut metric = QuestionAnsweringMetric::new();

        metric
            .add_batch(
                &MetricInput::Text(vec!["Barack Obama".to_string()]),
                &MetricInput::Text(vec!["Barack Obama".to_string()]),
            )
            .expect("operation failed in test");

        metric.reset();

        // Should fail because no data after reset
        assert!(metric.compute().is_err());
    }

    #[test]
    fn test_qa_metric_invalid_input() {
        let mut metric = QuestionAnsweringMetric::new();

        let predictions = MetricInput::Classifications(vec![0, 1]);
        let references = MetricInput::Text(vec!["Barack Obama".to_string()]);

        let result = metric.add_batch(&predictions, &references);
        assert!(result.is_err());
    }

    #[test]
    fn test_answer_normalization() {
        let metric = QuestionAnsweringMetric::new();

        // Test various normalization cases
        assert_eq!(
            metric.normalize_answer("  Barack H. Obama!  "),
            "barack h obama"
        );
        assert_eq!(metric.normalize_answer("PARIS"), "paris");
        assert_eq!(metric.normalize_answer("New York City"), "new york city");
        // Hyphens are punctuation and get removed (standard SQuAD normalization)
        assert_eq!(metric.normalize_answer("123-456-7890"), "1234567890");
        assert_eq!(metric.normalize_answer(""), "");
        assert_eq!(metric.normalize_answer("   "), "");
    }

    #[test]
    fn test_qa_metric_case_sensitivity() {
        let mut metric = QuestionAnsweringMetric::new();

        // Different cases should match after normalization
        let predictions = MetricInput::Text(vec!["BARACK OBAMA".to_string()]);
        let references = MetricInput::Text(vec!["barack obama".to_string()]);

        metric.add_batch(&predictions, &references).expect("add operation failed");

        let result = metric.compute().expect("operation failed in test");
        assert_eq!(result.details.get("exact_match"), Some(&1.0));
        assert_eq!(result.details.get("f1"), Some(&1.0));
    }
}