YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
fix(search): use query-side expansion instead of data normalization
Louis Beaumont8bf61e1ee469
diff --git a/screenpipe-db/src/db.rs b/screenpipe-db/src/db.rsindex 28a8e9dc0..26656fd4b 100644--- a/screenpipe-db/src/db.rs+++ b/screenpipe-db/src/db.rs@@ -20,10 +20,10 @@ use zerocopy::AsBytes;use futures::future::try_join_all;use crate::{- text_normalizer::normalize_text, AudioChunksResponse, AudioDevice, AudioEntry, AudioResult,- AudioResultRaw, ContentType, DeviceType, FrameData, FrameRow, OCREntry, OCRResult,- OCRResultRaw, OcrEngine, OcrTextBlock, Order, SearchMatch, SearchResult, Speaker,- TagContentType, TextBounds, TextPosition, TimeSeriesChunk, UiContent, VideoMetadata,+ AudioChunksResponse, AudioDevice, AudioEntry, AudioResult, AudioResultRaw, ContentType,+ DeviceType, FrameData, FrameRow, OCREntry, OCRResult, OCRResultRaw, OcrEngine, OcrTextBlock,+ Order, SearchMatch, SearchResult, Speaker, TagContentType, TextBounds, TextPosition,+ TimeSeriesChunk, UiContent, VideoMetadata,};pub struct DatabaseManager {@@ -370,16 +370,12 @@ impl DatabaseManager {text_json: &str,ocr_engine: Arc<OcrEngine>,) -> Result<(), sqlx::Error> {- // Normalize text for better FTS indexing (splits camelCase, number boundaries)- // This improves search recall without requiring database migrations- let normalized_text = normalize_text(text);- let text_length = text.len() as i64; // Use original length for consistency-+ let text_length = text.len() as i64;let mut tx = self.pool.begin().await?;sqlx::query("INSERT INTO ocr_text (frame_id, text, text_json, ocr_engine, text_length) VALUES (?1, ?2, ?3, ?4, ?5)").bind(frame_id)- .bind(&normalized_text)- .bind(text_json) // Keep original JSON for display with bounding boxes+ .bind(text)+ .bind(text_json).bind(format!("{:?}", *ocr_engine)).bind(text_length).execute(&mut *tx)@@ -2300,11 +2296,8 @@ impl DatabaseManager {// Create an indexed subquery for FTS matchinglet search_condition = if !query.is_empty() {let fts_match = if fuzzy_match {- query- .split_whitespace()- .map(|word| format!("{}*", word))- .collect::<Vec<_>>()- .join(" OR ")+ // Use intelligent query expansion for compound words+ crate::text_normalizer::expand_search_query(query)} else {query.to_string()};diff --git a/screenpipe-db/src/lib.rs b/screenpipe-db/src/lib.rsindex a5a38d70d..b63b4c355 100644--- a/screenpipe-db/src/lib.rs+++ b/screenpipe-db/src/lib.rs@@ -5,7 +5,7 @@ mod types;mod video_db;pub use db::{parse_all_text_positions, DatabaseManager};-pub use text_normalizer::normalize_text;+pub use text_normalizer::expand_search_query;pub use migration_worker::{create_migration_worker, MigrationCommand, MigrationConfig, MigrationResponse, MigrationStatus,MigrationWorker,diff --git a/screenpipe-db/src/text_normalizer.rs b/screenpipe-db/src/text_normalizer.rsindex 32aced3d7..ce3425ef8 100644--- a/screenpipe-db/src/text_normalizer.rs+++ b/screenpipe-db/src/text_normalizer.rs@@ -1,7 +1,8 @@-//! Text normalization for improved FTS indexing.+//! Text normalization and query expansion for improved FTS search.//!-//! This module handles splitting compound words (camelCase, number boundaries)-//! to improve full-text search recall without requiring database migrations.+//! This module provides query-side improvements to full-text search recall+//! without requiring database migrations. It expands search queries to catch+//! compound words that OCR may have concatenated.use once_cell::sync::Lazy;use regex::Regex;@@ -11,25 +12,11 @@ static CAMEL_CASE: Lazy<Regex> = Lazy::new(|| Regex::new(r"([a-z])([A-Z])").unwrstatic NUM_TO_LETTER: Lazy<Regex> = Lazy::new(|| Regex::new(r"([0-9])([a-zA-Z])").unwrap());static LETTER_TO_NUM: Lazy<Regex> = Lazy::new(|| Regex::new(r"([a-zA-Z])([0-9])").unwrap());-/// Normalize text for better FTS indexing by splitting compound words.+/// Split compound words on camelCase and number boundaries.///-/// Splits on:-/// - camelCase boundaries: "ActivityPerformance" → "Activity Performance"-/// - number-to-letter: "123abc" → "123 abc"-/// - letter-to-number: "abc123" → "abc 123"-///-/// This is intentionally lightweight - just regex replacements with pre-compiled patterns.-///-/// # Example-/// ```-/// use screenpipe_db::text_normalizer::normalize_text;-///-/// assert_eq!(normalize_text("camelCase"), "camel Case");-/// assert_eq!(normalize_text("test123word"), "test 123 word");-/// assert_eq!(normalize_text("ActivityPerformance"), "Activity Performance");-/// ```+/// Used internally for query expansion.#[inline]-pub fn normalize_text(text: &str) -> String {+fn split_compound(text: &str) -> String {// Fast path: if no uppercase letters or digits, skip processingif !text.bytes().any(|b| b.is_ascii_uppercase() || b.is_ascii_digit()) {return text.to_string();@@ -41,63 +28,135 @@ pub fn normalize_text(text: &str) -> String {result.into_owned()}+/// Expand a search query to improve recall on compound words.+///+/// Takes a user query and returns an expanded FTS5 query that searches for:+/// 1. The original term (with prefix matching)+/// 2. Split parts of compound words (with prefix matching)+///+/// This catches cases where OCR concatenated words like "ActivityPerformance"+/// when the user searches for "activity" or "performance".+///+/// # Example+/// ```+/// use screenpipe_db::text_normalizer::expand_search_query;+///+/// // Single word - just adds prefix matching+/// assert_eq!(expand_search_query("test"), "test*");+///+/// // Compound word - expands to catch parts+/// assert_eq!(expand_search_query("proStart"), "(proStart* OR pro* OR Start*)");+/// ```+pub fn expand_search_query(query: &str) -> String {+ let query = query.trim();+ if query.is_empty() {+ return String::new();+ }++ // Process each word in the query+ let expanded_terms: Vec<String> = query+ .split_whitespace()+ .flat_map(|word| {+ let split = split_compound(word);+ let parts: Vec<&str> = split.split_whitespace().collect();++ if parts.len() > 1 {+ // Word was split - include original and parts with prefix matching+ let mut terms = vec![format!("{}*", word)];+ for part in parts {+ if part.len() >= 2 {+ // Only add parts with 2+ chars to avoid noise+ terms.push(format!("{}*", part));+ }+ }+ terms+ } else {+ // No split needed - just add prefix matching+ vec![format!("{}*", word)]+ }+ })+ .collect();++ if expanded_terms.len() == 1 {+ expanded_terms[0].clone()+ } else {+ format!("({})", expanded_terms.join(" OR "))+ }+}+#[cfg(test)]mod tests {use super::*;#[test]- fn test_camel_case_split() {- assert_eq!(normalize_text("camelCase"), "camel Case");- assert_eq!(normalize_text("CamelCase"), "Camel Case");- assert_eq!(normalize_text("camelCaseWord"), "camel Case Word");- assert_eq!(normalize_text("ActivityPerformance"), "Activity Performance");- assert_eq!(normalize_text("iPhone"), "i Phone");+ fn test_split_compound_camel_case() {+ assert_eq!(split_compound("camelCase"), "camel Case");+ assert_eq!(split_compound("CamelCase"), "Camel Case");+ assert_eq!(split_compound("ActivityPerformance"), "Activity Performance");+ }++ #[test]+ fn test_split_compound_numbers() {+ assert_eq!(split_compound("test123"), "test 123");+ assert_eq!(split_compound("123test"), "123 test");+ assert_eq!(split_compound("test123word"), "test 123 word");+ }++ #[test]+ fn test_split_compound_no_change() {+ assert_eq!(split_compound("hello"), "hello");+ assert_eq!(split_compound("hello world"), "hello world");}#[test]- fn test_number_boundaries() {- assert_eq!(normalize_text("test123"), "test 123");- assert_eq!(normalize_text("123test"), "123 test");- assert_eq!(normalize_text("test123word"), "test 123 word");- assert_eq!(normalize_text("abc123def456"), "abc 123 def 456");+ fn test_expand_simple_query() {+ assert_eq!(expand_search_query("test"), "test*");+ assert_eq!(expand_search_query("hello"), "hello*");}#[test]- fn test_mixed() {- assert_eq!(normalize_text("myVar123Test"), "my Var 123 Test");- assert_eq!(normalize_text("0Activity20"), "0 Activity 20");+ fn test_expand_compound_query() {+ assert_eq!(+ expand_search_query("proStart"),+ "(proStart* OR pro* OR Start*)"+ );+ assert_eq!(+ expand_search_query("ActivityPerformance"),+ "(ActivityPerformance* OR Activity* OR Performance*)"+ );}#[test]- fn test_no_change_needed() {- assert_eq!(normalize_text("hello world"), "hello world");- assert_eq!(normalize_text("simple"), "simple");- assert_eq!(normalize_text(""), "");+ fn test_expand_number_boundary() {+ assert_eq!(+ expand_search_query("test123"),+ "(test123* OR test* OR 123*)"+ );}#[test]- fn test_already_spaced() {- assert_eq!(normalize_text("Hello World"), "Hello World");- assert_eq!(normalize_text("test 123"), "test 123");+ fn test_expand_multi_word_query() {+ // Each word gets expanded independently+ assert_eq!(+ expand_search_query("hello world"),+ "(hello* OR world*)"+ );}#[test]- fn test_special_characters_preserved() {- assert_eq!(normalize_text("[email protected]"), "[email protected]");- assert_eq!(normalize_text("path/to/file"), "path/to/file");- assert_eq!(normalize_text("user_name"), "user_name");+ fn test_expand_empty_query() {+ assert_eq!(expand_search_query(""), "");+ assert_eq!(expand_search_query(" "), "");}#[test]- fn test_unicode_preserved() {- assert_eq!(normalize_text("héllo"), "héllo");- assert_eq!(normalize_text("日本語"), "日本語");+ fn test_expand_filters_short_parts() {+ // Single char parts should be filtered out+ assert_eq!(expand_search_query("iPhone"), "(iPhone* OR Phone*)");}#[test]- fn test_fast_path_lowercase_only() {- // These should hit the fast path (no uppercase or digits)- let text = "this is all lowercase text with no numbers";- assert_eq!(normalize_text(text), text);+ fn test_expand_preserves_lowercase() {+ assert_eq!(expand_search_query("simple"), "simple*");}}