mod adaptive;
mod context;
pub(crate) mod furniture;
mod queue;
mod weights;
pub(crate) use adaptive::{Adaptive, MIN_RELEVANCE};
pub(crate) use context::link_contexts;
use context::prose_words;
pub(crate) use queue::{BestFirst, Offer, cluster_key};
use pagesift_extract::Query;
use pagesift_net::Url;
use weights::TermWeights;
const W_ANCHOR: f32 = 2.0;
const W_LOCATOR: f32 = 1.5;
const W_CONTEXT: f32 = 1.0;
const W_PARENT: f32 = 0.5;
const PHRASE_BONUS: f32 = 0.5;
const DEPTH_DECAY: f32 = 0.9;
const LANGUAGE_PORTAL_SCORE: f32 = 0.6;
const SECTION_PRIOR: f32 = furniture::FURNITURE_FACTOR * (W_ANCHOR * 0.5 + PHRASE_BONUS);
pub(crate) const LIST_FREE: usize = 8;
pub(crate) struct Focus {
pub query: Query,
weights: TermWeights,
}
impl Focus {
pub(crate) fn new(query: &str) -> Self {
let query = Query::new(query);
let weights = TermWeights::new(query.len());
Self { query, weights }
}
pub(crate) fn observe_page(&mut self, markdown: &str) {
self.weights.observe(self.query.hit_mask(markdown));
}
fn coverage(&self, text: &str) -> f32 {
self.weights.coverage(self.query.hit_mask(text))
}
pub(crate) fn page_relevance(
&self,
url: &Url,
title: Option<&str>,
markdown: &str,
links: usize,
) -> f32 {
let mut r = 0.5 * self.coverage(title.unwrap_or_default()) + 0.5 * self.coverage(markdown);
if furniture::is_hub(links, prose_words(markdown)) {
r *= furniture::HUB_FACTOR;
}
if furniture::is_furniture_url(url, &self.wanted()) {
r *= furniture::FURNITURE_FACTOR;
}
r
}
fn wanted(&self) -> impl Fn(&str) -> bool {
let query = &self.query;
move |w: &str| query.hit_mask(w) != 0
}
pub(crate) fn link_score(
&self,
url: &Url,
anchor: Option<&str>,
context: Option<&str>,
parent: f32,
depth: usize,
) -> f32 {
let anchor_mask = self.query.hit_mask(anchor.unwrap_or_default());
let mut raw = W_ANCHOR * self.weights.coverage(anchor_mask)
+ W_LOCATOR * self.coverage(&locator(url))
+ W_CONTEXT * self.coverage(context.unwrap_or_default())
+ W_PARENT * parent;
if anchor_mask.count_ones() >= 2 {
raw += PHRASE_BONUS;
}
let wanted = self.wanted();
let furniture = furniture::is_furniture_url(url, &wanted)
|| anchor.is_some_and(|a| furniture::is_furniture_anchor(a, &wanted));
if furniture::is_dated_url(url) {
raw *= furniture::DATED_FACTOR;
} else if furniture {
raw *= furniture::FURNITURE_FACTOR;
} else if self.english() && language_portal(url, anchor) {
raw = raw.max(LANGUAGE_PORTAL_SCORE);
} else if is_section(url, anchor) {
raw = raw.max(SECTION_PRIOR);
}
raw * DEPTH_DECAY.powi(depth.min(64) as i32)
}
fn english(&self) -> bool {
self.query.terms().iter().all(|t| t.is_ascii())
}
}
fn is_section(url: &Url, anchor: Option<&str>) -> bool {
let segments = url.path().split('/').filter(|s| !s.is_empty()).count();
segments <= 1
&& url.query().is_none()
&& anchor.is_some_and(|a| {
let words = a.split_whitespace().count();
(1..=4).contains(&words)
})
}
fn language_portal(url: &Url, anchor: Option<&str>) -> bool {
let anchor = anchor.unwrap_or_default().trim().to_ascii_lowercase();
if matches!(
anchor.as_str(),
"english"
| "en"
| "eng"
| "in english"
| "english site"
| "english version"
| "english page"
| "english top"
| "english home"
) {
return true;
}
url.path()
.split('/')
.rfind(|seg| !seg.is_empty())
.is_some_and(|seg| {
matches!(
seg.to_ascii_lowercase().as_str(),
"en" | "english" | "en-us" | "en-gb"
)
})
}
fn locator(url: &Url) -> String {
let mut s = String::with_capacity(url.as_str().len());
if let Some(host) = url.host_str() {
for label in host.split('.') {
if label != "www" {
s.push_str(label);
s.push(' ');
}
}
}
s.push_str(url.path());
if let Some(q) = url.query() {
s.push(' ');
s.push_str(q);
}
s
}
#[cfg(test)]
mod tests {
use super::*;
fn u(s: &str) -> Url {
Url::parse(s).unwrap()
}
#[test]
fn relevant_links_score_higher_and_depth_decays() {
let f = Focus::new("refund policy");
let good = f.link_score(
&u("https://e.com/help/refund-policy"),
Some("Refund policy"),
None,
0.0,
1,
);
let ctx = f.link_score(
&u("https://e.com/help/x"),
Some("here"),
Some("Read our refund policy here"),
0.0,
1,
);
let bad = f.link_score(&u("https://e.com/team/x"), Some("Our team"), None, 0.0, 1);
assert!(good > ctx && ctx > bad, "{good} {ctx} {bad}");
assert_eq!(bad, 0.0);
let section = f.link_score(&u("https://e.com/docs/"), Some("Docs"), None, 0.0, 1);
let dated = f.link_score(
&u("https://e.com/blog/2026/04/refund-story/"),
Some("A refund story"),
None,
0.0,
1,
);
assert!(section > dated && dated > 0.0, "{section} {dated}");
assert!(section < ctx, "{section} {ctx}");
let api = Focus::new("api authentication");
let reference = api.link_score(
&u("https://e.com/docs/api/"),
Some("API reference"),
None,
0.0,
2,
);
let blog = api.link_score(
&u("https://e.com/blog/api-news/"),
Some("API news"),
None,
0.0,
2,
);
assert!(reference > 2.0 * blog, "{reference} {blog}");
let deep = f.link_score(
&u("https://e.com/help/refund-policy"),
Some("Refund policy"),
None,
0.0,
3,
);
assert!(deep < good);
let parent = f.link_score(&u("https://e.com/team/x"), Some("Our team"), None, 1.0, 1);
assert!(parent > bad);
let home = u("https://e.com/");
assert!(
f.page_relevance(&home, Some("Refund policy"), "refund", 3)
> f.page_relevance(&home, None, "refund", 3)
);
}
#[test]
fn furniture_and_hubs_are_demoted() {
let f = Focus::new("international students admission");
let content = f.link_score(
&u("https://e.edu/admissions/international/"),
Some("International students"),
None,
0.2,
2,
);
let news = f.link_score(
&u("https://e.edu/news/2026/05/story-1/"),
Some("International students celebrate the festival"),
None,
0.2,
1,
);
assert!(content > news, "{content} {news}");
let about = f.link_score(&u("https://e.edu/about/"), Some("About"), None, 0.9, 1);
let study = f.link_score(&u("https://e.edu/study/"), Some("Study"), None, 0.9, 1);
assert!(about < study);
let home = u("https://e.edu/");
let text = "international students admission";
let article = f.page_relevance(&home, None, text, 3);
let hub = f.page_relevance(&home, None, text, 40);
let list = "- [International students admission](https://e.edu/a)\n- [International students admission](https://e.edu/b)\n- [International students admission](https://e.edu/c)\n- [International students admission](https://e.edu/d)\n- [International students admission](https://e.edu/e)\n- [International students admission](https://e.edu/f)\n- [International students admission](https://e.edu/g)\n- [International students admission](https://e.edu/h)\n";
assert!(f.page_relevance(&home, None, list, 8) < article);
let dated = f.page_relevance(&u("https://e.edu/news/2026/"), None, text, 3);
assert!(hub < article && dated < article, "{article} {hub} {dated}");
}
#[test]
fn host_labels_count_and_common_terms_fade() {
let mut f = Focus::new("international students visa");
let intl = f.link_score(&u("https://international.e.edu/"), None, None, 0.0, 1);
let plain = f.link_score(&u("https://www.e.edu/"), None, None, 0.0, 1);
assert!(intl > plain && plain == 0.0, "{intl} {plain}");
for i in 0..6 {
f.observe_page(if i == 0 {
"students visa"
} else {
"students and more students"
});
}
let students = f.link_score(&u("https://e.edu/x"), Some("students"), None, 0.0, 1);
let visa = f.link_score(&u("https://e.edu/y"), Some("visa"), None, 0.0, 1);
assert!(visa > students, "{visa} {students}");
}
}