Persona reads: a search's normal form is its content words, stemmed and sorted; a number loses its leading zeros

Soul's R8 item 4. ReadRequest::normal lowercases a search's words, drops the
stopwords, strips a light plural ("pages" is "page", "queries" is "query",
"class" stays) and sorts them, so "Merge pages and notices" and "the notice,
page merge" are one search. `#089` normalises to `#89`. Paths keep their case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-21 03:18:08 +01:00
commit a4c9f07243
2 changed files with 35 additions and 4 deletions

View file

@ -74,17 +74,27 @@ pub enum ReadRequest {
}
impl ReadRequest {
/// One normal form per thing read (Soul's R7): the org and repo case-blind, a search's words lowercased with their
/// spaces collapsed and their order kept, a recall's and a campaign's lowercased. Two requests with the same form are
/// the same read.
/// One normal form per thing read (Soul's R7, R8): the org and repo case-blind; a search's words lowercased, its
/// stopwords dropped, each lightly stemmed of a plural and the rest sorted, so "Merge pages" and "the page merge" are
/// one search; a number without its leading zeros (`#089` is `#89`); a recall's and a campaign's lowercased. Two
/// requests with the same form are the same read.
pub(crate) fn normal(&self) -> String {
let collapse = |text: &str| text.split_whitespace().collect::<Vec<_>>().join(" ").to_lowercase();
let searched = |words: &str| {
let mut words: Vec<String> = word_tokens(words).into_iter().filter(|word| content(word)).map(|word| stem(&word)).collect();
words.sort();
words.dedup();
words.join(" ")
};
match self {
Self::Repo(target) => {
let rest = target.split_once('/').map_or(target.as_str(), |(_, rest)| rest);
let end = rest.find(['/', '#', '@', '?']).unwrap_or(rest.len());
let (name, suffix) = rest.split_at(end);
let suffix = suffix.strip_prefix("?q=").map_or_else(|| suffix.to_owned(), |words| format!("?q={}", collapse(words)));
let suffix = match suffix.strip_prefix("?q=") {
Some(words) => format!("?q={}", searched(words)),
None => suffix.strip_prefix('#').map_or_else(|| suffix.to_owned(), |number| format!("#{}", Some(number.trim_start_matches('0')).filter(|digits| !digits.is_empty()).unwrap_or("0"))),
};
format!("repo:gamecult/{}{suffix}", name.to_lowercase())
}
Self::Campaign(slug) => format!("campaign:{}", slug.to_lowercase()),
@ -235,6 +245,15 @@ pub(crate) fn word_tokens(text: &str) -> Vec<String> {
text.split(|c: char| !c.is_alphanumeric()).filter(|word| !word.is_empty()).map(str::to_lowercase).collect()
}
/// A word without a plural ending, lightly: "pages" is "page", "queries" is "query", "class" stays.
fn stem(word: &str) -> String {
match word.strip_suffix("ies") {
Some(base) if word.len() > 4 => format!("{base}y"),
_ if word.len() > 3 && word.ends_with('s') && !word.ends_with("ss") => word[..word.len() - 1].to_owned(),
_ => word.to_owned(),
}
}
/// Whether a word carries content: not one of `STOPWORDS`.
fn content(word: &str) -> bool {
!STOPWORDS.contains(&word)

View file

@ -145,3 +145,15 @@ fn r7_a_live_search_keeps_only_results_that_hold_her_words() {
assert_eq!(repo_read(&Recorded, "GameCult/Bifrost?q=consent signature"), Err(ReadFailure::NotFound));
assert_eq!(repo_read(&Recorded, "GameCult/Bifrost?q=sealion"), Err(ReadFailure::NotFound));
}
/// Soul's R8: a search's normal form is its content words, lowercased, lightly stemmed and sorted; a number loses its
/// leading zeros.
#[test]
fn r8_a_search_and_a_number_have_one_normal_form() {
let normal = |target: &str| ReadRequest::Repo(target.into()).normal();
assert_eq!(normal("GameCult/Bifrost?q=Merge pages and notices"), "repo:gamecult/bifrost?q=merge notice page");
assert_eq!(normal("GameCult/Bifrost?q=the notice, page merge"), normal("GameCult/Bifrost?q=Merge pages and notices"));
assert_eq!(normal("GameCult/Bifrost#089"), normal("gamecult/bifrost#89"));
assert_eq!(normal("GameCult/Bifrost#0"), "repo:gamecult/bifrost#0");
assert_eq!(normal("GameCult/Bifrost?q=queries class"), "repo:gamecult/bifrost?q=class query");
}