pub fn shannon_entropy<T: std::hash::Hash + Eq, S: std::hash::BuildHasher>(
counts: &std::collections::HashMap<T, usize, S>,
) -> f32 {
if counts.is_empty() {
return 0.0;
}
let total: usize = counts.values().sum();
if total == 0 {
return 0.0;
}
let total_f = total as f32;
counts
.values()
.filter(|&&count| count > 0)
.map(|&count| {
let p = count as f32 / total_f;
-p * p.log2()
})
.sum()
}
pub fn split_words(text: &str) -> Vec<String> {
text.split_whitespace().map(str::to_lowercase).collect()
}
pub fn split_sentences(text: &str) -> Vec<&str> {
text.split(['.', '!', '?'])
.map(str::trim)
.filter(|s| !s.is_empty())
.collect()
}
#[must_use]
pub fn extract_punctuation(text: &str) -> Vec<char> {
const PUNCTUATION: &str = ".,!?;:'\"-()[]{}";
text.chars().filter(|c| PUNCTUATION.contains(*c)).collect()
}
#[cfg(test)]
#[allow(clippy::float_cmp)]
mod tests {
use std::collections::HashMap;
use super::*;
#[test]
fn test_shannon_entropy_empty() {
let counts: HashMap<char, usize> = HashMap::new();
let entropy = shannon_entropy(&counts);
assert_eq!(entropy, 0.0);
}
#[test]
fn test_shannon_entropy_single_symbol() {
let mut counts = HashMap::new();
counts.insert('a', 1);
let entropy = shannon_entropy(&counts);
assert_eq!(entropy, 0.0);
}
#[test]
fn test_shannon_entropy_uniform_symbol_repeated() {
let mut counts = HashMap::new();
counts.insert('a', 5);
let entropy = shannon_entropy(&counts);
assert_eq!(entropy, 0.0);
}
#[test]
fn test_shannon_entropy_two_equal() {
let mut counts = HashMap::new();
counts.insert('a', 1);
counts.insert('b', 1);
let entropy = shannon_entropy(&counts);
assert!((entropy - 1.0).abs() < 1e-6);
}
#[test]
fn test_shannon_entropy_aab() {
let mut counts = HashMap::new();
counts.insert('a', 2);
counts.insert('b', 1);
let entropy = shannon_entropy(&counts);
assert!((entropy - 0.9182958).abs() < 1e-6);
}
#[test]
fn test_shannon_entropy_four_equal() {
let mut counts = HashMap::new();
counts.insert('a', 1);
counts.insert('b', 1);
counts.insert('c', 1);
counts.insert('d', 1);
let entropy = shannon_entropy(&counts);
assert!((entropy - 2.0).abs() < 1e-6);
}
#[test]
fn test_shannon_entropy_skewed() {
let mut counts = HashMap::new();
counts.insert('a', 9);
counts.insert('b', 1);
let entropy = shannon_entropy(&counts);
assert!(entropy < 1.0);
assert!(entropy > 0.0);
assert!((entropy - 0.469).abs() < 0.01);
}
#[test]
fn test_shannon_entropy_zero_counts_filtered() {
let mut counts = HashMap::new();
counts.insert('a', 2);
counts.insert('b', 0); let entropy = shannon_entropy(&counts);
assert_eq!(entropy, 0.0); }
#[test]
fn test_split_words() {
let text = "Hello World Test";
let words = split_words(text);
assert_eq!(words, vec!["hello", "world", "test"]);
}
#[test]
fn test_split_sentences() {
let text = "First sentence. Second sentence! Third? Fourth.";
let sentences = split_sentences(text);
assert_eq!(sentences.len(), 4);
}
#[test]
fn test_extract_punctuation() {
let text = "Hello, world! How are you?";
let punct = extract_punctuation(text);
assert_eq!(punct, vec![',', '!', '?']);
}
}