#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ProseMetrics {
pub word_count: usize,
pub sentence_avg_words: f64,
pub link_density: f64,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct QualityGateConfig {
pub enabled: bool,
pub min_word_count: usize,
pub replacement_length_ratio: f64,
pub prose_min_sentence_words: f64,
pub prose_max_link_density: f64,
}
impl QualityGateConfig {
pub const fn disabled() -> Self {
Self {
enabled: false,
min_word_count: 0,
replacement_length_ratio: 0.0,
prose_min_sentence_words: 0.0,
prose_max_link_density: 1.0,
}
}
pub const fn active(
min_word_count: usize,
replacement_length_ratio: f64,
prose_min_sentence_words: f64,
prose_max_link_density: f64,
) -> Self {
Self {
enabled: true,
min_word_count,
replacement_length_ratio,
prose_min_sentence_words,
prose_max_link_density,
}
}
}
pub fn evaluate(text: &str, html: Option<&str>) -> ProseMetrics {
ProseMetrics {
word_count: word_count(text),
sentence_avg_words: sentence_avg_words(text),
link_density: link_density(html),
}
}
pub fn is_hard_fail(metrics: &ProseMetrics, cfg: &QualityGateConfig) -> bool {
metrics.sentence_avg_words < cfg.prose_min_sentence_words
|| metrics.link_density > cfg.prose_max_link_density
}
fn word_count(text: &str) -> usize {
text.split_whitespace().count()
}
fn sentence_avg_words(text: &str) -> f64 {
let mut sentences = 0usize;
let mut total_words = 0usize;
for piece in text.split(['。', '!', '?', '.', '!', '?']) {
let words = piece.split_whitespace().count();
if words > 0 {
sentences += 1;
total_words += words;
}
}
if sentences == 0 {
0.0
} else {
total_words as f64 / sentences as f64
}
}
fn link_density(html: Option<&str>) -> f64 {
let Some(html) = html else {
return 0.0;
};
let document = scraper::Html::parse_document(html);
let total = document
.root_element()
.text()
.map(|t| t.split_whitespace().count())
.sum::<usize>();
if total == 0 {
return 0.0;
}
let link_words = scraper::Selector::parse("a")
.ok()
.map(|sel| {
document
.select(&sel)
.map(|el| {
el.text()
.map(|t| t.split_whitespace().count())
.sum::<usize>()
})
.sum::<usize>()
})
.unwrap_or(0);
link_words as f64 / total as f64
}
#[cfg(test)]
mod tests {
use super::*;
fn words(n: usize) -> String {
vec!["word"; n].join(" ")
}
#[test]
fn evaluate_counts_whitespace_separated_words() {
let m = evaluate("one two three\nfour\tfive", None);
assert_eq!(m.word_count, 5);
}
#[test]
fn evaluate_ignores_repeated_whitespace() {
let m = evaluate(" a b ", None);
assert_eq!(m.word_count, 2);
}
#[test]
fn evaluate_empty_text_has_zero_word_count() {
let m = evaluate("", None);
assert_eq!(m.word_count, 0);
assert_eq!(m.sentence_avg_words, 0.0);
assert_eq!(m.link_density, 0.0);
}
#[test]
fn evaluate_sentence_avg_splits_on_terminators() {
let m = evaluate("one two. three four five! 六 七 八 九?", None);
assert!((m.sentence_avg_words - 3.0).abs() < f64::EPSILON);
}
#[test]
fn evaluate_text_without_terminators_is_one_sentence() {
let m = evaluate(&words(6), None);
assert!((m.sentence_avg_words - 6.0).abs() < f64::EPSILON);
assert_eq!(m.word_count, 6);
}
#[test]
fn evaluate_decimal_point_splits_sentence() {
let m = evaluate("version 3.5 here", None);
assert!((m.sentence_avg_words - 2.0).abs() < f64::EPSILON);
}
#[test]
fn evaluate_link_density_measures_link_text_ratio() {
let html = r#"<html><body>
<p>plain prose words here</p>
<a href="/x">link text words</a>
</body></html>"#;
let m = evaluate("ignored", Some(html));
assert!(
(m.link_density - 3.0 / 7.0).abs() < 1e-9,
"got {}",
m.link_density
);
}
#[test]
fn evaluate_link_density_zero_when_html_none() {
let m = evaluate(&words(10), None);
assert_eq!(m.link_density, 0.0);
}
#[test]
fn evaluate_pure_nav_page_has_high_link_density() {
let html = r#"<html><body>
<a href="/a">home link one</a>
<a href="/b">about link two</a>
<a href="/c">contact link three</a>
</body></html>"#;
let m = evaluate("ignored", Some(html));
assert!(m.link_density > 0.9, "got {}", m.link_density);
}
fn cfg() -> QualityGateConfig {
QualityGateConfig::active(150, 1.5, 7.0, 0.7)
}
#[test]
fn is_hard_fail_triggers_on_low_sentence_avg() {
let m = evaluate("buy now. click here", None); assert!(is_hard_fail(&m, &cfg()));
}
#[test]
fn is_hard_fail_passes_at_sentence_threshold_boundary() {
let m = evaluate(&words(7), None); assert!(!is_hard_fail(&m, &cfg()));
}
#[test]
fn is_hard_fail_triggers_on_high_link_density() {
let html = r#"<html><body>
<p>one</p>
<a href="/a">link words here</a>
</body></html>"#;
let m = evaluate(&words(50), Some(html)); assert!(is_hard_fail(&m, &cfg()));
}
#[test]
fn is_hard_fail_passes_at_link_density_boundary() {
let html = r#"<html><body>
<p>plain words</p>
<a href="/a">link text</a>
</body></html>"#; let m = evaluate(&words(50), Some(html));
assert!(!is_hard_fail(&m, &cfg()));
}
#[test]
fn is_hard_fail_false_for_prose_like_text() {
let m = evaluate(&words(200), None); assert!(!is_hard_fail(&m, &cfg()));
}
#[test]
fn disabled_config_has_enabled_false() {
let c = QualityGateConfig::disabled();
assert!(!c.enabled);
assert_eq!(c.min_word_count, 0);
}
#[test]
fn active_config_enables_gates_with_given_values() {
let c = QualityGateConfig::active(150, 1.5, 7.0, 0.7);
assert!(c.enabled);
assert_eq!(c.min_word_count, 150);
assert!((c.replacement_length_ratio - 1.5).abs() < f64::EPSILON);
assert!((c.prose_min_sentence_words - 7.0).abs() < f64::EPSILON);
assert!((c.prose_max_link_density - 0.7).abs() < f64::EPSILON);
}
#[test]
fn configs_compare_by_value() {
assert_ne!(
QualityGateConfig::disabled(),
QualityGateConfig::active(150, 1.5, 7.0, 0.7)
);
assert_eq!(
QualityGateConfig::active(150, 1.5, 7.0, 0.7),
QualityGateConfig::active(150, 1.5, 7.0, 0.7)
);
}
}