pub fn normalize_transcript(text: &str) -> Vec<String> {
text.split_whitespace()
.map(|word| {
word.chars()
.filter(|c| c.is_alphanumeric() || *c == '\'')
.flat_map(char::to_lowercase)
.collect::<String>()
})
.filter(|word| !word.is_empty())
.collect()
}
pub fn word_error_rate(
hypothesis: &[String],
reference: &[String],
) -> f64 {
if reference.is_empty() {
return if hypothesis.is_empty() { 0.0 } else { 1.0 };
}
let mut prev: Vec<usize> = (0..=hypothesis.len()).collect();
let mut cur = vec![0usize; hypothesis.len() + 1];
for (r, refr) in reference.iter().enumerate() {
cur[0] = r + 1;
for (h, hyp) in hypothesis.iter().enumerate() {
let cost = usize::from(refr != hyp);
cur[h + 1] = (prev[h] + cost).min(prev[h + 1] + 1).min(cur[h] + 1);
}
std::mem::swap(&mut prev, &mut cur);
}
prev[hypothesis.len()] as f64 / reference.len() as f64
}
pub fn text_error_rate(
hypothesis: &str,
reference: &str,
) -> f64 {
word_error_rate(
&normalize_transcript(hypothesis),
&normalize_transcript(reference),
)
}
#[cfg(test)]
mod tests {
use super::*;
fn words(s: &str) -> Vec<String> {
normalize_transcript(s)
}
#[test]
fn test_normalize_strips_case_and_punctuation() {
assert_eq!(
words(" The Moon?? Why -- choose this, as our goal! "),
["the", "moon", "why", "choose", "this", "as", "our", "goal"],
);
assert_eq!(words("we're"), ["we're"]);
}
#[test]
fn test_wer_counts_each_edit_once() {
let refr = words("we choose to go to the moon");
assert_eq!(word_error_rate(&refr, &refr), 0.0);
let sub = words("we choose to go to the sun");
assert!((word_error_rate(&sub, &refr) - 1.0 / 7.0).abs() < 1e-12);
let del = words("we choose to go to moon");
assert!((word_error_rate(&del, &refr) - 1.0 / 7.0).abs() < 1e-12);
let ins = words("we choose to go to the big moon");
assert!((word_error_rate(&ins, &refr) - 1.0 / 7.0).abs() < 1e-12);
}
#[test]
fn test_wer_edge_cases() {
assert_eq!(word_error_rate(&[], &[]), 0.0);
assert_eq!(word_error_rate(&words("anything"), &[]), 1.0);
assert_eq!(word_error_rate(&[], &words("we choose to go")), 1.0);
}
}