scirs2-metrics 0.3.0

Machine Learning evaluation metrics module for SciRS2 (scirs2-metrics)
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
//! METEOR (Metric for Evaluation of Translation with Explicit ORdering)
//!
//! Computes the METEOR score for evaluating machine translation and text generation.
//! METEOR combines unigram precision and recall with a penalty for fragmentation.
//!
//! Unlike BLEU, METEOR:
//! - Is based on the harmonic mean of precision and recall (recall-weighted)
//! - Accounts for stemming and synonyms (simplified here to exact match + stem matching)
//! - Applies a fragmentation penalty for non-contiguous matches
//!
//! # References
//!
//! Banerjee, S. & Lavie, A. (2005).
//! "METEOR: An Automatic Metric for MT Evaluation with Improved Correlation with Human Judgments."

use crate::error::{MetricsError, Result};
use std::collections::HashSet;

use super::tokenize;

/// Computes the METEOR score between a reference and candidate text.
///
/// This implementation uses:
/// - Exact unigram matching
/// - Simplified stemming (common English suffix stripping)
/// - A fragmentation penalty based on the number of matching chunks
///
/// The formula is:
///   METEOR = F_mean * (1 - penalty)
/// where F_mean = (10 * P * R) / (9 * P + R)  [harmonic mean biased toward recall]
/// and penalty = 0.5 * (chunks / matches)^3
///
/// # Arguments
///
/// * `reference` - The reference text.
/// * `candidate` - The candidate text.
///
/// # Returns
///
/// The METEOR score in [0, 1].
///
/// # Examples
///
/// ```
/// use scirs2_metrics::text_metrics::meteor::meteor_score;
///
/// let score = meteor_score("the cat sat on the mat", "the cat sat on the mat")
///     .expect("Failed");
/// assert!((score - 1.0).abs() < 1e-6);
/// ```
pub fn meteor_score(reference: &str, candidate: &str) -> Result<f64> {
    let ref_tokens = tokenize(reference);
    let cand_tokens = tokenize(candidate);

    meteor_from_tokens(&ref_tokens, &cand_tokens)
}

/// Computes METEOR score from pre-tokenized sequences.
pub fn meteor_from_tokens(ref_tokens: &[String], cand_tokens: &[String]) -> Result<f64> {
    if ref_tokens.is_empty() && cand_tokens.is_empty() {
        return Ok(1.0);
    }
    if ref_tokens.is_empty() || cand_tokens.is_empty() {
        return Ok(0.0);
    }

    // Stage 1: Exact matching
    let (exact_matches, ref_matched_exact, cand_matched_exact) =
        exact_match(ref_tokens, cand_tokens);

    // Stage 2: Stem matching (for unmatched tokens)
    let (stem_matches, ref_matched_stem, cand_matched_stem) = stem_match(
        ref_tokens,
        cand_tokens,
        &ref_matched_exact,
        &cand_matched_exact,
    );

    let total_matches = exact_matches + stem_matches;

    if total_matches == 0 {
        return Ok(0.0);
    }

    // Compute precision and recall
    let precision = total_matches as f64 / cand_tokens.len() as f64;
    let recall = total_matches as f64 / ref_tokens.len() as f64;

    // Harmonic mean with recall weight (alpha=0.9, meaning recall is 9x as important)
    // F = (10 * P * R) / (9 * P + R)  -- standard METEOR parameterization
    let f_mean = if precision + recall > 0.0 {
        10.0 * precision * recall / (9.0 * precision + recall)
    } else {
        return Ok(0.0);
    };

    // Compute fragmentation penalty
    // Combine matched indices from both stages
    let mut all_ref_matched: Vec<bool> = ref_matched_exact.clone();
    let mut all_cand_matched: Vec<bool> = cand_matched_exact.clone();
    for i in 0..ref_tokens.len() {
        if ref_matched_stem[i] {
            all_ref_matched[i] = true;
        }
    }
    for i in 0..cand_tokens.len() {
        if cand_matched_stem[i] {
            all_cand_matched[i] = true;
        }
    }

    let chunks = count_chunks(&all_cand_matched);
    let penalty = if total_matches > 0 {
        0.5 * (chunks as f64 / total_matches as f64).powi(3)
    } else {
        0.0
    };

    Ok(f_mean * (1.0 - penalty))
}

/// Computes METEOR score with configurable parameters.
///
/// # Arguments
///
/// * `reference` - The reference text.
/// * `candidate` - The candidate text.
/// * `alpha` - Recall weight parameter (default 0.9). Higher values favor recall more.
/// * `beta` - Fragmentation penalty weight (default 3.0).
/// * `gamma` - Fragmentation penalty multiplier (default 0.5).
///
/// # Returns
///
/// The METEOR score in [0, 1].
pub fn meteor_score_parametric(
    reference: &str,
    candidate: &str,
    alpha: f64,
    beta: f64,
    gamma: f64,
) -> Result<f64> {
    if alpha <= 0.0 || alpha >= 1.0 {
        return Err(MetricsError::InvalidInput(
            "alpha must be in (0, 1)".to_string(),
        ));
    }
    if beta < 0.0 {
        return Err(MetricsError::InvalidInput("beta must be >= 0".to_string()));
    }
    if gamma < 0.0 {
        return Err(MetricsError::InvalidInput("gamma must be >= 0".to_string()));
    }

    let ref_tokens = tokenize(reference);
    let cand_tokens = tokenize(candidate);

    if ref_tokens.is_empty() && cand_tokens.is_empty() {
        return Ok(1.0);
    }
    if ref_tokens.is_empty() || cand_tokens.is_empty() {
        return Ok(0.0);
    }

    let (exact_matches, ref_matched_exact, cand_matched_exact) =
        exact_match(&ref_tokens, &cand_tokens);
    let (stem_matches, _ref_matched_stem, cand_matched_stem) = stem_match(
        &ref_tokens,
        &cand_tokens,
        &ref_matched_exact,
        &cand_matched_exact,
    );

    let total_matches = exact_matches + stem_matches;
    if total_matches == 0 {
        return Ok(0.0);
    }

    let precision = total_matches as f64 / cand_tokens.len() as f64;
    let recall = total_matches as f64 / ref_tokens.len() as f64;

    // Parameterized harmonic mean: F = P * R / (alpha * P + (1-alpha) * R)
    let f_mean = if precision + recall > 0.0 {
        precision * recall / (alpha * precision + (1.0 - alpha) * recall)
    } else {
        return Ok(0.0);
    };

    let mut all_cand_matched = cand_matched_exact;
    for i in 0..cand_tokens.len() {
        if cand_matched_stem[i] {
            all_cand_matched[i] = true;
        }
    }

    let chunks = count_chunks(&all_cand_matched);
    let frag = if total_matches > 0 {
        chunks as f64 / total_matches as f64
    } else {
        0.0
    };
    let penalty = gamma * frag.powf(beta);

    Ok(f_mean * (1.0 - penalty))
}

/// Computes corpus-level METEOR, averaged across sentence pairs.
///
/// # Arguments
///
/// * `references` - Slice of reference texts.
/// * `candidates` - Slice of candidate texts.
///
/// # Returns
///
/// The mean METEOR score in [0, 1].
pub fn corpus_meteor(references: &[&str], candidates: &[&str]) -> Result<f64> {
    if references.len() != candidates.len() {
        return Err(MetricsError::InvalidInput(
            "references and candidates must have the same length".to_string(),
        ));
    }
    if references.is_empty() {
        return Err(MetricsError::InvalidInput(
            "inputs must not be empty".to_string(),
        ));
    }

    let mut score_sum = 0.0;
    for (r, c) in references.iter().zip(candidates.iter()) {
        score_sum += meteor_score(r, c)?;
    }

    Ok(score_sum / references.len() as f64)
}

/// Performs exact unigram matching between reference and candidate.
///
/// Returns (match_count, ref_matched_flags, cand_matched_flags).
fn exact_match(ref_tokens: &[String], cand_tokens: &[String]) -> (usize, Vec<bool>, Vec<bool>) {
    let mut ref_matched = vec![false; ref_tokens.len()];
    let mut cand_matched = vec![false; cand_tokens.len()];
    let mut matches = 0;

    for (ci, ct) in cand_tokens.iter().enumerate() {
        for (ri, rt) in ref_tokens.iter().enumerate() {
            if !ref_matched[ri] && !cand_matched[ci] && ct == rt {
                ref_matched[ri] = true;
                cand_matched[ci] = true;
                matches += 1;
                break;
            }
        }
    }

    (matches, ref_matched, cand_matched)
}

/// Performs stem matching for tokens not already matched exactly.
///
/// Uses a simplified English stemmer (suffix stripping).
fn stem_match(
    ref_tokens: &[String],
    cand_tokens: &[String],
    ref_exact_matched: &[bool],
    cand_exact_matched: &[bool],
) -> (usize, Vec<bool>, Vec<bool>) {
    let mut ref_matched = vec![false; ref_tokens.len()];
    let mut cand_matched = vec![false; cand_tokens.len()];
    let mut matches = 0;

    for (ci, ct) in cand_tokens.iter().enumerate() {
        if cand_exact_matched[ci] {
            continue;
        }
        let cand_stem = simple_stem(ct);
        for (ri, rt) in ref_tokens.iter().enumerate() {
            if ref_exact_matched[ri] || ref_matched[ri] || cand_matched[ci] {
                continue;
            }
            let ref_stem = simple_stem(rt);
            if cand_stem == ref_stem {
                ref_matched[ri] = true;
                cand_matched[ci] = true;
                matches += 1;
                break;
            }
        }
    }

    (matches, ref_matched, cand_matched)
}

/// Simple English suffix-stripping stemmer.
///
/// This is a greatly simplified Porter-like stemmer that handles common
/// English suffixes. It is sufficient for METEOR approximation.
fn simple_stem(word: &str) -> String {
    let w = word.to_lowercase();
    if w.len() <= 3 {
        return w;
    }

    // Remove common suffixes in order of length
    let suffixes = [
        "ational", "tional", "ation", "ment", "ness", "ence", "ance", "able", "ible", "ting",
        "ful", "less", "ous", "ive", "ing", "ies", "ied", "ion", "ity", "ism", "ist", "ize", "ise",
        "er", "ed", "ly", "al", "es", "en", "s",
    ];

    for suffix in &suffixes {
        if w.len() > suffix.len() + 2 && w.ends_with(suffix) {
            return w[..w.len() - suffix.len()].to_string();
        }
    }

    w
}

/// Counts the number of contiguous chunks of matched tokens.
///
/// A chunk is a maximal contiguous sequence of matched positions.
fn count_chunks(matched: &[bool]) -> usize {
    if matched.is_empty() {
        return 0;
    }

    let mut chunks = 0;
    let mut in_chunk = false;

    for &m in matched {
        if m {
            if !in_chunk {
                chunks += 1;
                in_chunk = true;
            }
        } else {
            in_chunk = false;
        }
    }

    chunks
}

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

    // ---- meteor_score tests ----

    #[test]
    fn test_meteor_perfect_match() {
        let val = meteor_score("the cat sat on the mat", "the cat sat on the mat")
            .expect("should succeed");
        assert!((val - 1.0).abs() < 0.01);
    }

    #[test]
    fn test_meteor_no_overlap() {
        let val = meteor_score("hello world", "goodbye moon").expect("should succeed");
        assert!((val - 0.0).abs() < 1e-6);
    }

    #[test]
    fn test_meteor_partial_overlap() {
        let val = meteor_score("the cat sat on the mat", "the cat sits on a mat")
            .expect("should succeed");
        assert!(val > 0.0 && val < 1.0);
    }

    #[test]
    fn test_meteor_empty_both() {
        let val = meteor_score("", "").expect("should succeed");
        assert!((val - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_meteor_empty_reference() {
        let val = meteor_score("", "hello world").expect("should succeed");
        assert!((val - 0.0).abs() < 1e-6);
    }

    #[test]
    fn test_meteor_empty_candidate() {
        let val = meteor_score("hello world", "").expect("should succeed");
        assert!((val - 0.0).abs() < 1e-6);
    }

    #[test]
    fn test_meteor_stem_matching() {
        // "running" and "runs" should stem-match to "run"
        let val =
            meteor_score("the cat is running fast", "the cat runs fast").expect("should succeed");
        assert!(val > 0.5); // Good overlap due to stem matching
    }

    // ---- meteor_score_parametric tests ----

    #[test]
    fn test_meteor_parametric_default() {
        let val = meteor_score_parametric("the cat sat", "the cat sat", 0.9, 3.0, 0.5)
            .expect("should succeed");
        assert!(val > 0.9);
    }

    #[test]
    fn test_meteor_parametric_invalid_alpha() {
        assert!(meteor_score_parametric("a", "a", 0.0, 3.0, 0.5).is_err());
        assert!(meteor_score_parametric("a", "a", 1.0, 3.0, 0.5).is_err());
    }

    #[test]
    fn test_meteor_parametric_invalid_beta() {
        assert!(meteor_score_parametric("a", "a", 0.5, -1.0, 0.5).is_err());
    }

    #[test]
    fn test_meteor_parametric_invalid_gamma() {
        assert!(meteor_score_parametric("a", "a", 0.5, 3.0, -1.0).is_err());
    }

    // ---- corpus_meteor tests ----

    #[test]
    fn test_corpus_meteor_perfect() {
        let refs = vec!["hello world", "the cat sat"];
        let cands = vec!["hello world", "the cat sat"];
        let val = corpus_meteor(&refs, &cands).expect("should succeed");
        assert!(val > 0.9);
    }

    #[test]
    fn test_corpus_meteor_mismatched() {
        let refs = vec!["a"];
        let cands = vec!["a", "b"];
        assert!(corpus_meteor(&refs, &cands).is_err());
    }

    #[test]
    fn test_corpus_meteor_empty() {
        let refs: Vec<&str> = vec![];
        let cands: Vec<&str> = vec![];
        assert!(corpus_meteor(&refs, &cands).is_err());
    }

    #[test]
    fn test_corpus_meteor_partial() {
        let refs = vec!["the quick brown fox", "a lazy dog"];
        let cands = vec!["the slow brown fox", "a lazy cat"];
        let val = corpus_meteor(&refs, &cands).expect("should succeed");
        assert!(val > 0.0 && val < 1.0);
    }

    // ---- Utility tests ----

    #[test]
    fn test_simple_stem() {
        assert_eq!(simple_stem("running"), "runn");
        assert_eq!(simple_stem("cats"), "cat");
        assert_eq!(simple_stem("played"), "play");
        assert_eq!(simple_stem("happiness"), "happi");
    }

    #[test]
    fn test_count_chunks_all_matched() {
        assert_eq!(count_chunks(&[true, true, true]), 1);
    }

    #[test]
    fn test_count_chunks_alternating() {
        assert_eq!(count_chunks(&[true, false, true, false, true]), 3);
    }

    #[test]
    fn test_count_chunks_none_matched() {
        assert_eq!(count_chunks(&[false, false, false]), 0);
    }

    #[test]
    fn test_count_chunks_empty() {
        assert_eq!(count_chunks(&[]), 0);
    }
}