Skip to content

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

feat(search): add OCR text normalization for better FTS recall

Louis Beaumont3898e07351ab
diff --git a/Cargo.lock b/Cargo.lock
index 44321304e..c7cdfa5f4 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6323,7 +6323,7 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "screenpipe-audio"
-version = "0.2.77"
+version = "0.2.78"
dependencies = [
"anyhow",
"bytemuck",
@@ -6376,7 +6376,7 @@ dependencies = [
[[package]]
name = "screenpipe-core"
-version = "0.2.77"
+version = "0.2.78"
dependencies = [
"accessibility",
"accessibility-sys",
@@ -6419,7 +6419,7 @@ dependencies = [
[[package]]
name = "screenpipe-db"
-version = "0.2.77"
+version = "0.2.78"
dependencies = [
"anyhow",
"chrono",
@@ -6428,7 +6428,9 @@ dependencies = [
"image 0.25.5",
"libsqlite3-sys",
"oasgen",
+ "once_cell",
"rand 0.8.5",
+ "regex",
"serde",
"serde_json",
"sqlite-vec",
@@ -6441,7 +6443,7 @@ dependencies = [
[[package]]
name = "screenpipe-events"
-version = "0.2.77"
+version = "0.2.78"
dependencies = [
"anyhow",
"chrono",
@@ -6459,7 +6461,7 @@ dependencies = [
[[package]]
name = "screenpipe-integrations"
-version = "0.2.77"
+version = "0.2.78"
dependencies = [
"anyhow",
"image 0.25.5",
@@ -6474,7 +6476,7 @@ dependencies = [
[[package]]
name = "screenpipe-server"
-version = "0.2.77"
+version = "0.2.78"
dependencies = [
"anyhow",
"axum",
@@ -6537,7 +6539,7 @@ dependencies = [
[[package]]
name = "screenpipe-vision"
-version = "0.2.77"
+version = "0.2.78"
dependencies = [
"accessibility-sys",
"anyhow",
diff --git a/screenpipe-db/Cargo.toml b/screenpipe-db/Cargo.toml
index 2897d51ec..39c63805d 100644
--- a/screenpipe-db/Cargo.toml
+++ b/screenpipe-db/Cargo.toml
@@ -32,6 +32,8 @@ rand = "0.8.5"
criterion = { workspace = true }
oasgen = { workspace = true }
tracing-subscriber = { workspace = true }
+regex = "1.11"
+once_cell = "1.19"
[[bench]]
name = "db_benchmarks"
diff --git a/screenpipe-db/src/db.rs b/screenpipe-db/src/db.rs
index c165f1fdb..28a8e9dc0 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::{
- AudioChunksResponse, AudioDevice, AudioEntry, AudioResult, AudioResultRaw, ContentType,
- DeviceType, FrameData, FrameRow, OCREntry, OCRResult, OCRResultRaw, OcrEngine, OcrTextBlock,
- Order, SearchMatch, SearchResult, Speaker, TagContentType, TextBounds, TextPosition,
- TimeSeriesChunk, UiContent, VideoMetadata,
+ 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,
};
pub struct DatabaseManager {
@@ -370,12 +370,16 @@ impl DatabaseManager {
text_json: &str,
ocr_engine: Arc<OcrEngine>,
) -> Result<(), sqlx::Error> {
- let text_length = text.len() as i64;
+ // 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 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(text)
- .bind(text_json)
+ .bind(&normalized_text)
+ .bind(text_json) // Keep original JSON for display with bounding boxes
.bind(format!("{:?}", *ocr_engine))
.bind(text_length)
.execute(&mut *tx)
diff --git a/screenpipe-db/src/lib.rs b/screenpipe-db/src/lib.rs
index d2f85cdd9..a5a38d70d 100644
--- a/screenpipe-db/src/lib.rs
+++ b/screenpipe-db/src/lib.rs
@@ -1,9 +1,11 @@
mod db;
mod migration_worker;
+pub mod text_normalizer;
mod types;
mod video_db;
pub use db::{parse_all_text_positions, DatabaseManager};
+pub use text_normalizer::normalize_text;
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.rs
new file mode 100644
index 000000000..32aced3d7
--- /dev/null
+++ b/screenpipe-db/src/text_normalizer.rs
@@ -0,0 +1,103 @@
+//! Text normalization for improved FTS indexing.
+//!
+//! This module handles splitting compound words (camelCase, number boundaries)
+//! to improve full-text search recall without requiring database migrations.
+
+use once_cell::sync::Lazy;
+use regex::Regex;
+
+// Pre-compiled regexes for minimal overhead
+static CAMEL_CASE: Lazy<Regex> = Lazy::new(|| Regex::new(r"([a-z])([A-Z])").unwrap());
+static 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.
+///
+/// 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");
+/// ```
+#[inline]
+pub fn normalize_text(text: &str) -> String {
+ // Fast path: if no uppercase letters or digits, skip processing
+ if !text.bytes().any(|b| b.is_ascii_uppercase() || b.is_ascii_digit()) {
+ return text.to_string();
+ }
+
+ let result = CAMEL_CASE.replace_all(text, "$1 $2");
+ let result = NUM_TO_LETTER.replace_all(&result, "$1 $2");
+ let result = LETTER_TO_NUM.replace_all(&result, "$1 $2");
+ result.into_owned()
+}
+
+#[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");
+ }
+
+ #[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");
+ }
+
+ #[test]
+ fn test_mixed() {
+ assert_eq!(normalize_text("myVar123Test"), "my Var 123 Test");
+ assert_eq!(normalize_text("0Activity20"), "0 Activity 20");
+ }
+
+ #[test]
+ fn test_no_change_needed() {
+ assert_eq!(normalize_text("hello world"), "hello world");
+ assert_eq!(normalize_text("simple"), "simple");
+ assert_eq!(normalize_text(""), "");
+ }
+
+ #[test]
+ fn test_already_spaced() {
+ assert_eq!(normalize_text("Hello World"), "Hello World");
+ assert_eq!(normalize_text("test 123"), "test 123");
+ }
+
+ #[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");
+ }
+
+ #[test]
+ fn test_unicode_preserved() {
+ assert_eq!(normalize_text("héllo"), "héllo");
+ assert_eq!(normalize_text("日本語"), "日本語");
+ }
+
+ #[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);
+ }
+}