Skip to main content

synaptic_eval/
exact_match.rs

1use async_trait::async_trait;
2use synaptic_core::SynapticError;
3
4use crate::evaluator::{EvalResult, Evaluator};
5
6/// Evaluator that checks for exact string match between prediction and reference.
7pub struct ExactMatchEvaluator {
8    ignore_case: bool,
9}
10
11impl ExactMatchEvaluator {
12    /// Create a case-sensitive exact match evaluator.
13    pub fn new() -> Self {
14        Self { ignore_case: false }
15    }
16
17    /// Create a case-insensitive exact match evaluator.
18    pub fn case_insensitive() -> Self {
19        Self { ignore_case: true }
20    }
21}
22
23impl Default for ExactMatchEvaluator {
24    fn default() -> Self {
25        Self::new()
26    }
27}
28
29#[async_trait]
30impl Evaluator for ExactMatchEvaluator {
31    async fn evaluate(
32        &self,
33        prediction: &str,
34        reference: &str,
35        _input: &str,
36    ) -> Result<EvalResult, SynapticError> {
37        let matches = if self.ignore_case {
38            prediction.to_lowercase() == reference.to_lowercase()
39        } else {
40            prediction == reference
41        };
42
43        if matches {
44            Ok(EvalResult::pass())
45        } else {
46            Ok(EvalResult::fail()
47                .with_reasoning(format!("Expected {:?}, got {:?}", reference, prediction)))
48        }
49    }
50}