Skip to content

web search done right

don't treat 'site.com' etc. as a sentence boundary even though it contains a '.'

Mikkel Denkera46a24cde842
diff --git a/crates/core/src/snippet.rs b/crates/core/src/snippet.rs
index df1b4e44..b019292f 100644
--- a/crates/core/src/snippet.rs
+++ b/crates/core/src/snippet.rs
@@ -392,7 +392,7 @@ Survey in 2016, 2017, and 2018."#;
.expect("Search failed");
assert_eq!(result.webpages.len(), 1);
- assert_eq!(highlight(result.webpages[0].snippet.clone()), format!("{HIGHLIGHTEN_PREFIX}Rust{HIGHLIGHTEN_POSTFIX} is a systems programming {HIGHLIGHTEN_PREFIX}language{HIGHLIGHTEN_POSTFIX} sponsored by Mozilla which describes it as a \"safe, concurrent, practical {HIGHLIGHTEN_PREFIX}language{HIGHLIGHTEN_POSTFIX}\", supporting functional and imperative-procedural paradigms. {HIGHLIGHTEN_PREFIX}Rust{HIGHLIGHTEN_POSTFIX} is syntactically similar to C++[according to whom?"));
+ assert_eq!(highlight(result.webpages[0].snippet.clone()), format!("{HIGHLIGHTEN_PREFIX}Rust{HIGHLIGHTEN_POSTFIX} is a systems programming {HIGHLIGHTEN_PREFIX}language{HIGHLIGHTEN_POSTFIX} sponsored by Mozilla which describes it as a \"safe, concurrent, practical {HIGHLIGHTEN_PREFIX}language{HIGHLIGHTEN_POSTFIX}\", supporting functional and imperative-procedural paradigms. {HIGHLIGHTEN_PREFIX}Rust{HIGHLIGHTEN_POSTFIX} is syntactically similar to C++[according to whom?], but its designers intend it to provide better memory safety while still maintaining "));
}
#[test]
@@ -431,7 +431,7 @@ Survey in 2016, 2017, and 2018."#;
.expect("Search failed");
assert_eq!(result.webpages.len(), 1);
- assert_eq!(highlight(result.webpages[0].snippet.clone()), format!("Rust is a systems programming language sponsored by Mozilla which {HIGHLIGHTEN_PREFIX}describes{HIGHLIGHTEN_POSTFIX} it as a \"safe, concurrent, practical language\", supporting functional and imperative-procedural paradigms. Rust is syntactically similar to C++[according to whom?"));
+ assert_eq!(highlight(result.webpages[0].snippet.clone()), format!("Rust is a systems programming language sponsored by Mozilla which {HIGHLIGHTEN_PREFIX}describes{HIGHLIGHTEN_POSTFIX} it as a \"safe, concurrent, practical language\", supporting functional and imperative-procedural paradigms. Rust is syntactically similar to C++[according to whom?], but its designers intend it to provide better memory safety while still maintaining "));
}
#[test]
@@ -472,7 +472,7 @@ Survey in 2016, 2017, and 2018."#;
assert_eq!(result.webpages.len(), 1);
assert_eq!(
highlight(result.webpages[0].snippet.clone()),
- format!("Rust is a systems programming language sponsored by Mozilla which describes it as a \"safe, concurrent, practical language\", supporting functional and imperative-procedural {HIGHLIGHTEN_PREFIX}paradigms{HIGHLIGHTEN_POSTFIX}. Rust is syntactically similar to C++[according to whom?")
+ format!("Rust is a systems programming language sponsored by Mozilla which describes it as a \"safe, concurrent, practical language\", supporting functional and imperative-procedural {HIGHLIGHTEN_PREFIX}paradigms{HIGHLIGHTEN_POSTFIX}. Rust is syntactically similar to C++[according to whom?], but its designers intend it to provide better memory safety while still maintaining ")
);
}
diff --git a/crates/core/src/web_spell/mod.rs b/crates/core/src/web_spell/mod.rs
index 020b0435..fc26c9f2 100644
--- a/crates/core/src/web_spell/mod.rs
+++ b/crates/core/src/web_spell/mod.rs
@@ -34,6 +34,7 @@ pub use trainer::SecondTrainer;
use fst::Streamer;
use std::ops::Range;
+use crate::ceil_char_boundary;
use crate::floor_char_boundary;
use itertools::intersperse;
use serde::{Deserialize, Serialize};
@@ -106,19 +107,54 @@ impl Correction {
}
pub fn sentence_ranges(text: &str) -> Vec<Range<usize>> {
+ let skip = ["mr.", "ms.", "dr."];
+
let mut res = Vec::new();
let mut last_start = 0;
+ let text = text.to_ascii_lowercase();
+
// We should really do something more clever than this.
// Tried using `SRX`[https://docs.rs/srx/latest/srx/] but it was a bit too slow.
for (end, _) in text
.char_indices()
.filter(|(_, c)| matches!(c, '.' | '\n' | '?' | '!'))
{
- res.push(last_start..end + 1);
- last_start = floor_char_boundary(text, end + 2);
+ let end = ceil_char_boundary(&text, end + 1);
+
+ if skip.iter().any(|p| text[last_start..end].ends_with(p)) {
+ continue;
+ }
+
+ // skip 'site.com', '...', '!!!' etc.
+ if !text[end..].starts_with(|c: char| c.is_ascii_whitespace()) {
+ continue;
+ }
+
+ let mut start = last_start;
+
+ while start < end && text[start..].starts_with(|c: char| c.is_whitespace()) {
+ start = ceil_char_boundary(&text, start + 1);
+ }
+
+ // just a precaution
+ if start > end {
+ continue;
+ }
+
+ res.push(start..end);
+
+ last_start = end;
+ }
+
+ let mut start = last_start;
+
+ while start < text.len() && text[start..].starts_with(|c: char| c.is_whitespace()) {
+ start = floor_char_boundary(&text, start + 1);
}
+ res.push(start..text.len());
+
res
}
@@ -153,3 +189,41 @@ impl<'a> MergePointer<'a> {
!self.is_finished
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_sentence_ranges() {
+ let text = "This is a sentence. This is another sentence. This is a third sentence.";
+ let ranges = sentence_ranges(text);
+ assert_eq!(ranges.len(), 3);
+
+ assert_eq!(&text[ranges[0].clone()], "This is a sentence.");
+ assert_eq!(&text[ranges[1].clone()], "This is another sentence.");
+ assert_eq!(&text[ranges[2].clone()], "This is a third sentence.");
+
+ let text = "This is a sentence. This is another sentence. This is a third sentence";
+ let ranges = sentence_ranges(text);
+ assert_eq!(ranges.len(), 3);
+
+ assert_eq!(&text[ranges[0].clone()], "This is a sentence.");
+ assert_eq!(&text[ranges[1].clone()], "This is another sentence.");
+ assert_eq!(&text[ranges[2].clone()], "This is a third sentence");
+
+ let text = "mr. roberts";
+
+ let ranges = sentence_ranges(text);
+
+ assert_eq!(ranges.len(), 1);
+ assert_eq!(&text[ranges[0].clone()], "mr. roberts");
+
+ let text = "site.com is the best";
+
+ let ranges = sentence_ranges(text);
+
+ assert_eq!(ranges.len(), 1);
+ assert_eq!(&text[ranges[0].clone()], "site.com is the best");
+ }
+}