Skip to content

A fast, helpful, and open-source document parser

add searchItems back to the API

Logan Markewich889a6f115afe
diff --git a/crates/liteparse-napi/src/lib.rs b/crates/liteparse-napi/src/lib.rs
index d21ba4d..4ea7f4d 100644
--- a/crates/liteparse-napi/src/lib.rs
+++ b/crates/liteparse-napi/src/lib.rs
@@ -3,7 +3,7 @@ use napi_derive::napi;
mod types;
-use types::{JsLiteParseConfig, JsParseResult, JsScreenshotResult};
+use types::{JsLiteParseConfig, JsParseResult, JsScreenshotResult, JsTextItem};
/// Main LiteParse parser class.
#[napi]
@@ -107,3 +107,21 @@ impl LiteParse {
JsLiteParseConfig::from_rust(&self.config)
}
}
+
+/// Search text items for phrase matches, returning merged items with combined bounding boxes.
+#[napi]
+pub fn search_items(
+ items: Vec<JsTextItem>,
+ phrase: String,
+ case_sensitive: Option<bool>,
+) -> Vec<JsTextItem> {
+ let rust_items: Vec<_> = items.iter().map(|i| i.to_rust()).collect();
+ let options = liteparse::search::SearchOptions {
+ phrase,
+ case_sensitive: case_sensitive.unwrap_or(false),
+ };
+ liteparse::search::search_items(&rust_items, &options)
+ .iter()
+ .map(JsTextItem::from_rust)
+ .collect()
+}
diff --git a/crates/liteparse-napi/src/types.rs b/crates/liteparse-napi/src/types.rs
index 9955dd6..c38e1e8 100644
--- a/crates/liteparse-napi/src/types.rs
+++ b/crates/liteparse-napi/src/types.rs
@@ -121,6 +121,20 @@ pub struct JsTextItem {
}
impl JsTextItem {
+ pub fn to_rust(&self) -> TextItem {
+ TextItem {
+ text: self.text.clone(),
+ x: self.x as f32,
+ y: self.y as f32,
+ width: self.width as f32,
+ height: self.height as f32,
+ font_name: self.font_name.clone(),
+ font_size: self.font_size.map(|v| v as f32),
+ confidence: self.confidence.map(|v| v as f32),
+ ..Default::default()
+ }
+ }
+
pub fn from_rust(item: &TextItem) -> Self {
Self {
text: item.text.clone(),
diff --git a/crates/liteparse-python/src/lib.rs b/crates/liteparse-python/src/lib.rs
index 873134a..43dd255 100644
--- a/crates/liteparse-python/src/lib.rs
+++ b/crates/liteparse-python/src/lib.rs
@@ -42,6 +42,20 @@ impl PyTextItem {
}
impl PyTextItem {
+ fn to_rust(&self) -> liteparse::types::TextItem {
+ liteparse::types::TextItem {
+ text: self.text.clone(),
+ x: self.x as f32,
+ y: self.y as f32,
+ width: self.width as f32,
+ height: self.height as f32,
+ font_name: self.font_name.clone(),
+ font_size: self.font_size.map(|v| v as f32),
+ confidence: self.confidence.map(|v| v as f32),
+ ..Default::default()
+ }
+ }
+
fn from_rust(item: &liteparse::types::TextItem) -> Self {
Self {
text: item.text.clone(),
@@ -347,6 +361,21 @@ impl LiteParse {
// Module
// ---------------------------------------------------------------------------
+/// Search text items for phrase matches, returning merged items with combined bounding boxes.
+#[pyfunction]
+#[pyo3(signature = (items, phrase, *, case_sensitive = false))]
+fn search_items(items: Vec<PyTextItem>, phrase: String, case_sensitive: bool) -> Vec<PyTextItem> {
+ let rust_items: Vec<_> = items.iter().map(|i| i.to_rust()).collect();
+ let options = liteparse::search::SearchOptions {
+ phrase,
+ case_sensitive,
+ };
+ liteparse::search::search_items(&rust_items, &options)
+ .iter()
+ .map(PyTextItem::from_rust)
+ .collect()
+}
+
/// Run the `lit` CLI with the given arguments.
#[pyfunction]
fn run_cli(args: Vec<String>) -> PyResult<()> {
@@ -361,5 +390,6 @@ fn _liteparse(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<PyTextItem>()?;
m.add_class::<PyScreenshotResult>()?;
m.add_function(wrap_pyfunction!(run_cli, m)?)?;
+ m.add_function(wrap_pyfunction!(search_items, m)?)?;
Ok(())
}
diff --git a/crates/liteparse-wasm/src/lib.rs b/crates/liteparse-wasm/src/lib.rs
index 76f3478..6c82a51 100644
--- a/crates/liteparse-wasm/src/lib.rs
+++ b/crates/liteparse-wasm/src/lib.rs
@@ -17,6 +17,7 @@ use wasm_bindgen_futures::JsFuture;
use liteparse::config::{LiteParseConfig, OutputFormat};
use liteparse::ocr::{OcrEngine, OcrOptions, OcrResult};
use liteparse::parser::LiteParse as CoreLiteParse;
+use liteparse::search;
use liteparse::types::PdfInput;
// ---------------------------------------------------------------------------
@@ -338,3 +339,86 @@ impl LiteParse {
.map_err(|e| JsError::new(&format!("serialize result failed: {}", e)))
}
}
+
+// ---------------------------------------------------------------------------
+// searchItems (standalone function)
+// ---------------------------------------------------------------------------
+
+#[derive(Deserialize)]
+#[serde(rename_all = "camelCase")]
+struct JsSearchTextItem {
+ text: String,
+ x: f32,
+ y: f32,
+ width: f32,
+ height: f32,
+ #[serde(default)]
+ font_name: Option<String>,
+ #[serde(default)]
+ font_size: Option<f32>,
+ #[serde(default)]
+ confidence: Option<f32>,
+}
+
+#[derive(Deserialize)]
+#[serde(rename_all = "camelCase", default)]
+struct JsSearchOptions {
+ phrase: String,
+ case_sensitive: bool,
+}
+
+impl Default for JsSearchOptions {
+ fn default() -> Self {
+ Self {
+ phrase: String::new(),
+ case_sensitive: false,
+ }
+ }
+}
+
+/// Search text items for phrase matches, returning merged items with combined bounding boxes.
+#[wasm_bindgen(js_name = "searchItems")]
+pub fn search_items(items: JsValue, options: JsValue) -> Result<JsValue, JsError> {
+ let js_items: Vec<JsSearchTextItem> = serde_wasm_bindgen::from_value(items)
+ .map_err(|e| JsError::new(&format!("invalid items: {}", e)))?;
+ let js_opts: JsSearchOptions = serde_wasm_bindgen::from_value(options)
+ .map_err(|e| JsError::new(&format!("invalid options: {}", e)))?;
+
+ let rust_items: Vec<liteparse::types::TextItem> = js_items
+ .into_iter()
+ .map(|i| liteparse::types::TextItem {
+ text: i.text,
+ x: i.x,
+ y: i.y,
+ width: i.width,
+ height: i.height,
+ font_name: i.font_name,
+ font_size: i.font_size,
+ confidence: i.confidence,
+ ..Default::default()
+ })
+ .collect();
+
+ let options = search::SearchOptions {
+ phrase: js_opts.phrase,
+ case_sensitive: js_opts.case_sensitive,
+ };
+
+ let results = search::search_items(&rust_items, &options);
+ let js_results: Vec<JsTextItem<'_>> = results
+ .iter()
+ .map(|i| JsTextItem {
+ text: &i.text,
+ x: i.x,
+ y: i.y,
+ width: i.width,
+ height: i.height,
+ font_name: i.font_name.as_deref(),
+ font_size: i.font_size,
+ confidence: i.confidence,
+ })
+ .collect();
+
+ serde_wasm_bindgen::to_value(&js_results)
+ .map_err(|e| JsError::new(&format!("serialize results failed: {}", e)))
+}
diff --git a/crates/liteparse/src/lib.rs b/crates/liteparse/src/lib.rs
index b376d8d..9888934 100644
--- a/crates/liteparse/src/lib.rs
+++ b/crates/liteparse/src/lib.rs
@@ -10,4 +10,5 @@ pub mod parser;
pub mod projection;
#[cfg(not(target_arch = "wasm32"))]
pub mod render;
+pub mod search;
pub mod types;
diff --git a/crates/liteparse/src/search.rs b/crates/liteparse/src/search.rs
new file mode 100644
index 0000000..af84a77
--- /dev/null
+++ b/crates/liteparse/src/search.rs
@@ -0,0 +1,203 @@
+use crate::types::TextItem;
+
+/// Options for searching text items.
+pub struct SearchOptions {
+ pub phrase: String,
+ pub case_sensitive: bool,
+}
+
+/// Search text items for phrase matches, returning synthetic merged items.
+///
+/// Consecutive text items are concatenated and searched. When a phrase spans
+/// multiple items, the result is a single merged item with a combined bounding
+/// box and the matched text. Font metadata is taken from the first matched item.
+pub fn search_items(items: &[TextItem], options: &SearchOptions) -> Vec<TextItem> {
+ let mut results = Vec::new();
+ let normalize = |s: &str| -> String {
+ if options.case_sensitive {
+ s.to_string()
+ } else {
+ s.to_lowercase()
+ }
+ };
+ let q = normalize(&options.phrase);
+
+ // Pre-compute separator between each pair of adjacent items.
+ // If two items are on the same line and spatially adjacent, join without a space.
+ let mut seps: Vec<&str> = vec![""; items.len()];
+ for i in 1..items.len() {
+ let prev = &items[i - 1];
+ let cur = &items[i];
+ let font_size = prev.font_size.or(cur.font_size).unwrap_or(12.0);
+ let same_line = (cur.y - prev.y).abs() < font_size * 0.5;
+ let gap = cur.x - (prev.x + prev.width);
+ seps[i] = if same_line && gap <= font_size * 0.3 {
+ ""
+ } else {
+ " "
+ };
+ }
+
+ let mut start = 0;
+ while start < items.len() {
+ let mut combined = String::new();
+ let mut found = false;
+
+ for end in start..items.len() {
+ if end > start {
+ combined.push_str(seps[end]);
+ }
+ combined.push_str(&items[end].text);
+
+ if normalize(&combined).contains(&q) {
+ // Narrow from the left: drop leading items that aren't part of the match
+ let mut narrowed = combined.clone();
+ let mut s = start;
+ while s < end {
+ let skip_len = items[s].text.len() + seps[s + 1].len();
+ let without = &narrowed[skip_len..];
+ if normalize(without).contains(&q) {
+ narrowed = without.to_string();
+ s += 1;
+ } else {
+ break;
+ }
+ }
+
+ // Merge bounding boxes of matched items
+ let matched = &items[s..=end];
+ let x = matched.iter().map(|m| m.x).fold(f32::INFINITY, f32::min);
+ let y = matched.iter().map(|m| m.y).fold(f32::INFINITY, f32::min);
+ let x2 = matched
+ .iter()
+ .map(|m| m.x + m.width)
+ .fold(f32::NEG_INFINITY, f32::max);
+ let y2 = matched
+ .iter()
+ .map(|m| m.y + m.height)
+ .fold(f32::NEG_INFINITY, f32::max);
+
+ results.push(TextItem {
+ text: options.phrase.clone(),
+ x,
+ y,
+ width: x2 - x,
+ height: y2 - y,
+ font_name: matched[0].font_name.clone(),
+ font_size: matched[0].font_size,
+ ..Default::default()
+ });
+
+ // Advance past the match to avoid duplicates
+ start = end + 1;
+ found = true;
+ break;
+ }
+
+ // Stop expanding if combined text is already much longer than the query
+ if combined.len() > q.len() * 2 {
+ break;
+ }
+ }
+
+ if !found {
+ start += 1;
+ }
+ }
+
+ results
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn make_item(text: &str, x: f32, y: f32, width: f32) -> TextItem {
+ TextItem {
+ text: text.into(),
+ x,
+ y,
+ width,
+ height: 12.0,
+ font_name: Some("Arial".into()),
+ font_size: Some(12.0),
+ ..Default::default()
+ }
+ }
+
+ #[test]
+ fn single_item_match() {
+ let items = vec![make_item("hello world", 0.0, 0.0, 80.0)];
+ let opts = SearchOptions {
+ phrase: "hello world".into(),
+ case_sensitive: false,
+ };
+ let results = search_items(&items, &opts);
+ assert_eq!(results.len(), 1);
+ assert_eq!(results[0].text, "hello world");
+ }
+
+ #[test]
+ fn multi_item_phrase() {
+ // Three items on the same line with gaps large enough to insert spaces
+ let items = vec![
+ make_item("0°C", 0.0, 0.0, 20.0),
+ make_item("to", 30.0, 0.0, 12.0),
+ make_item("70°C", 52.0, 0.0, 24.0),
+ ];
+ let opts = SearchOptions {
+ phrase: "0°C to 70°C".into(),
+ case_sensitive: false,
+ };
+ let results = search_items(&items, &opts);
+ assert_eq!(results.len(), 1);
+ assert_eq!(results[0].text, "0°C to 70°C");
+ assert_eq!(results[0].x, 0.0);
+ assert_eq!(results[0].width, 76.0); // 52 + 24
+ }
+
+ #[test]
+ fn case_insensitive() {
+ let items = vec![make_item("Hello World", 0.0, 0.0, 80.0)];
+ let opts = SearchOptions {
+ phrase: "hello world".into(),
+ case_sensitive: false,
+ };
+ assert_eq!(search_items(&items, &opts).len(), 1);
+ }
+
+ #[test]
+ fn case_sensitive_no_match() {
+ let items = vec![make_item("Hello World", 0.0, 0.0, 80.0)];
+ let opts = SearchOptions {
+ phrase: "hello world".into(),
+ case_sensitive: true,
+ };
+ assert_eq!(search_items(&items, &opts).len(), 0);
+ }
+
+ #[test]
+ fn no_match() {
+ let items = vec![make_item("foo bar", 0.0, 0.0, 40.0)];
+ let opts = SearchOptions {
+ phrase: "baz".into(),
+ case_sensitive: false,
+ };
+ assert_eq!(search_items(&items, &opts).len(), 0);
+ }
+
+ #[test]
+ fn multiple_matches() {
+ let items = vec![
+ make_item("hello", 0.0, 0.0, 30.0),
+ make_item("world", 0.0, 20.0, 30.0),
+ make_item("hello", 0.0, 40.0, 30.0),
+ ];
+ let opts = SearchOptions {
+ phrase: "hello".into(),
+ case_sensitive: false,
+ };
+ let results = search_items(&items, &opts);
+ assert_eq!(results.len(), 2);
+ }
+}
diff --git a/packages/node/src/lib.ts b/packages/node/src/lib.ts
index eb4acc9..d51ab55 100644
--- a/packages/node/src/lib.ts
+++ b/packages/node/src/lib.ts
@@ -155,4 +155,25 @@ function toTextItem(item: NativeTextItem): TextItem {
};
}
+// ---------------------------------------------------------------------------
+// searchItems — standalone utility
+// ---------------------------------------------------------------------------
+
+export interface SearchItemsOptions {
+ phrase: string;
+ caseSensitive?: boolean;
+}
+
+export function searchItems(
+ items: TextItem[],
+ options: SearchItemsOptions,
+): TextItem[] {
+ const nativeResults = native.searchItems(
+ items,
+ options.phrase,
+ options.caseSensitive ?? false,
+ );
+ return nativeResults.map(toTextItem);
+}
+
export default LiteParse;
diff --git a/packages/node/src/native.ts b/packages/node/src/native.ts
index 698b1bd..cb4c786 100644
--- a/packages/node/src/native.ts
+++ b/packages/node/src/native.ts
@@ -12,6 +12,11 @@ const __dirname = dirname(fileURLToPath(import.meta.url));
interface NativeBindings {
LiteParse: new (config?: LiteParseNativeConfig) => LiteParseNative;
+ searchItems(
+ items: NativeTextItem[],
+ phrase: string,
+ caseSensitive?: boolean | null,
+ ): NativeTextItem[];
}
export interface LiteParseNativeConfig {
diff --git a/packages/python/liteparse/__init__.py b/packages/python/liteparse/__init__.py
index 79b502c..659c0d4 100644
--- a/packages/python/liteparse/__init__.py
+++ b/packages/python/liteparse/__init__.py
@@ -1,4 +1,4 @@
-from .parser import LiteParse
+from .parser import LiteParse, search_items
from .types import (
ParseResult,
ParsedPage,
@@ -15,4 +15,5 @@ __all__ = [
"TextItem",
"ScreenshotResult",
"ParseError",
+ "search_items",
]
diff --git a/packages/python/liteparse/parser.py b/packages/python/liteparse/parser.py
index 91edd9f..5e46e99 100644
--- a/packages/python/liteparse/parser.py
+++ b/packages/python/liteparse/parser.py
@@ -4,6 +4,7 @@ from pathlib import Path
from typing import Any, List, Optional, Union
from liteparse._liteparse import LiteParse as _NativeLiteParse
+from liteparse._liteparse import search_items as _native_search_items
from .types import (
ParsedPage,
@@ -192,3 +193,40 @@ class LiteParse:
def __repr__(self) -> str:
return "LiteParse()"
+
+
+def search_items(
+ items: List[TextItem],
+ phrase: str,
+ *,
+ case_sensitive: bool = False,
+) -> List[TextItem]:
+ """
+ Search text items for phrase matches, returning merged items with combined bounding boxes.
+
+ A phrase may span multiple adjacent text items. This function concatenates
+ consecutive items, finds matches, and returns synthetic merged TextItem
+ objects with combined bounding boxes.
+
+ Args:
+ items: List of TextItem objects to search through.
+ phrase: The phrase to search for.
+ case_sensitive: Whether the search should be case-sensitive (default: False).
+
+ Returns:
+ List of TextItem objects representing the matched regions.
+ """
+ native_results = _native_search_items(items, phrase, case_sensitive=case_sensitive)
+ return [
+ TextItem(
+ text=item.text,
+ x=item.x,
+ y=item.y,
+ width=item.width,
+ height=item.height,
+ font_name=item.font_name,
+ font_size=item.font_size,
+ confidence=item.confidence,
+ )
+ for item in native_results
+ ]