use ahash::HashMap;
use super::utils::{extract_punctuation, shannon_entropy, split_sentences, split_words};
#[must_use]
pub fn compute_bigram_repetition_rate(text: &str) -> f32 {
let words = split_words(text);
if words.len() < 2 {
return 0.0;
}
let bigrams: Vec<String> = words
.windows(2)
.map(|window| format!("{} {}", window[0], window[1]))
.collect();
if bigrams.is_empty() {
return 0.0;
}
let mut bigram_counts: HashMap<String, usize> = HashMap::default();
for bigram in bigrams {
*bigram_counts.entry(bigram).or_insert(0) += 1;
}
let repeated_bigrams = bigram_counts.values().filter(|&&count| count > 1).count();
repeated_bigrams as f32 / bigram_counts.len() as f32
}
#[must_use]
pub fn compute_punctuation_entropy(text: &str) -> f32 {
let punctuation = extract_punctuation(text);
if punctuation.is_empty() {
return 0.0;
}
let mut punct_counts: HashMap<char, usize> = HashMap::default();
for c in punctuation {
*punct_counts.entry(c).or_insert(0) += 1;
}
shannon_entropy(&punct_counts)
}
#[must_use]
pub fn compute_lexical_diversity(text: &str) -> f32 {
let words = split_words(text);
if words.is_empty() {
return 0.0;
}
let unique_words: std::collections::HashSet<_> = words.iter().collect();
unique_words.len() as f32 / words.len() as f32
}
#[must_use]
pub fn compute_vocab_richness(text: &str) -> f32 {
let words = split_words(text);
if words.is_empty() {
return 0.0;
}
let unique_words: std::collections::HashSet<_> = words.iter().collect();
(unique_words.len() as f32).sqrt() / words.len() as f32
}
#[must_use]
pub fn compute_word_repetition_rate(text: &str) -> f32 {
let words = split_words(text);
if words.is_empty() {
return 0.0;
}
let mut word_counts: HashMap<String, usize> = HashMap::default();
for word in words {
*word_counts.entry(word).or_insert(0) += 1;
}
let repeated_words = word_counts.values().filter(|&&count| count > 1).count();
repeated_words as f32 / word_counts.len() as f32
}
#[must_use]
pub fn compute_sentence_length_cv(text: &str) -> f32 {
let sentences = split_sentences(text);
if sentences.len() < 2 {
return 0.0;
}
let lengths: Vec<f32> = sentences
.iter()
.map(|s| s.split_whitespace().count() as f32)
.collect();
let mean = lengths.iter().sum::<f32>() / lengths.len() as f32;
if mean < f32::EPSILON {
return 0.0;
}
let variance =
lengths.iter().map(|&len| (len - mean).powi(2)).sum::<f32>() / lengths.len() as f32;
let std = variance.sqrt();
std / mean
}
#[must_use]
pub fn compute_avg_word_length(text: &str) -> f32 {
let words: Vec<&str> = text.split_whitespace().collect();
if words.is_empty() {
return 0.0;
}
let total_chars: usize = words.iter().map(|w| w.len()).sum();
total_chars as f32 / words.len() as f32
}
#[must_use]
pub fn compute_chunk_punctuation_entropy(text: &str) -> f32 {
compute_punctuation_entropy(text)
}
#[must_use]
pub fn compute_word_frequency_entropy(text: &str) -> f32 {
let words = split_words(text);
if words.is_empty() {
return 0.0;
}
let mut word_counts: HashMap<String, usize> = HashMap::default();
for word in words {
*word_counts.entry(word).or_insert(0) += 1;
}
shannon_entropy(&word_counts)
}
#[cfg(test)]
#[allow(clippy::float_cmp)]
mod tests {
use super::*;
#[test]
fn test_bigram_repetition_rate() {
let text = "the cat sat. the dog ran.";
let rate = compute_bigram_repetition_rate(text);
assert!((0.0..=1.0).contains(&rate));
let text2 = "the cat the cat the dog";
let rate2 = compute_bigram_repetition_rate(text2);
assert!(rate2 > 0.0);
}
#[test]
fn test_punctuation_entropy() {
let text = "Hello! How are you? I'm fine.";
let entropy = compute_punctuation_entropy(text);
assert!(entropy > 0.0);
let text2 = "Hello world";
let entropy2 = compute_punctuation_entropy(text2);
assert_eq!(entropy2, 0.0);
}
#[test]
fn test_lexical_diversity() {
let text = "the cat sat on the mat";
let diversity = compute_lexical_diversity(text);
assert!((diversity - 0.833).abs() < 0.01);
}
#[test]
fn test_vocab_richness() {
let text = "the cat sat on the mat";
let richness = compute_vocab_richness(text);
assert!((richness - 0.373).abs() < 0.01);
}
#[test]
fn test_word_repetition_rate() {
let text = "the cat sat on the mat";
let rate = compute_word_repetition_rate(text);
assert!((rate - 0.2).abs() < 0.01);
}
#[test]
fn test_sentence_length_cv() {
let text = "Short. This is a longer sentence. Very long sentence here.";
let cv = compute_sentence_length_cv(text);
assert!(cv > 0.0);
let text2 = "Only one sentence";
let cv2 = compute_sentence_length_cv(text2);
assert_eq!(cv2, 0.0);
}
#[test]
fn test_avg_word_length() {
let text = "cat dog";
let avg = compute_avg_word_length(text);
assert_eq!(avg, 3.0); }
#[test]
fn test_word_frequency_entropy() {
let text = "the cat sat on the mat";
let entropy = compute_word_frequency_entropy(text);
assert!(entropy > 0.0);
}
#[test]
fn test_empty_inputs() {
let empty = "";
assert_eq!(compute_bigram_repetition_rate(empty), 0.0);
assert_eq!(compute_punctuation_entropy(empty), 0.0);
assert_eq!(compute_lexical_diversity(empty), 0.0);
assert_eq!(compute_vocab_richness(empty), 0.0);
assert_eq!(compute_word_repetition_rate(empty), 0.0);
assert_eq!(compute_sentence_length_cv(empty), 0.0);
assert_eq!(compute_avg_word_length(empty), 0.0);
assert_eq!(compute_word_frequency_entropy(empty), 0.0);
}
}