/** * Lexical search helpers: trigram query sanitization, Levenshtein fuzzy * correction, and proximity/title re-ranking. * * These are pure functions (no I/O, no DB coupling) so they can be unit-tested * in isolation and shared across the PostgreSQL + PG keyword stores and the fusion * layer in contextual-search-rlm.ts. * * Algorithm references (rewritten fresh in TS, not copied): * - RRF multi-stream fusion + proximity rerank: Cormack et al. 2009; the * minSpan/adjacent-pair window approach layers a positional signal on top * of rank-based RRF. * - Levenshtein fuzzy correction over a per-store vocabulary table, * length-bounded by maxEditDistance and LRU-cached at the call site. */ import type { SearchResult } from "@massa-th0th/shared"; /** * Common words that match everywhere and dilute BM25/proximity scoring. * Filtered from proximity/title/fuzzy paths; kept as a fallback when every * query term is a stopword so a query never produces an empty term list. */ export declare const STOPWORDS: Set; /** * Build an FTS5 trigram MATCH expression from a raw query. * * Trigram tokenizers match 3-char substrings, so very short tokens (<3 chars) * are dropped (they cannot form a trigram and would yield zero matches). The * result is an OR-joined quoted-token list suitable for `WHERE tbl MATCH ?`. * Returns "" when no usable tokens remain — callers should treat "" as * "skip the trigram stream". * * mode "OR" broadens recall (any token matches); "AND" narrows it. We default * to OR so the trigram stream supplements (not replaces) the porter keyword * stream in RRF fusion. */ export declare function sanitizeTrigramQuery(query: string, mode?: "AND" | "OR"): string; /** * Classic two-row Levenshtein edit distance. O(a.length * b.length) time, * O(b.length) space. */ export declare function levenshtein(a: string, b: string): number; /** * Length-tolerant max edit distance. Longer words tolerate more edits because * a single typo/inflection is proportionally smaller. Short words (≤4 chars) * get distance 1 to avoid spurious corrections of "is"→"if" style noise. */ export declare function maxEditDistance(wordLength: number): number; /** Find all character positions of `term` in `text` (ascending). */ export declare function findAllPositions(text: string, term: string): number[]; /** * Minimum window span covering at least one position from each list. * Sweep-line: repeatedly advance the pointer at the current minimum until any * list is exhausted. Returns Infinity for empty input, 0 for a single list. */ export declare function findMinSpan(positionLists: number[][]): number; /** * Count matched adjacent pairs across consecutive query terms within `gap` * chars. Each right position is consumed by at most one left position so a * repeated token like "foo foo bar" counts 1 pair, not 2. Layers a saturating * phrase-frequency signal on top of minSpan proximity. */ export declare function countAdjacentPairs(positionLists: number[][], terms: string[], gap?: number): number; /** * Extract query terms for proximity/title scoring: lowercase, split on * whitespace, drop very short tokens, then filter stopwords (falling back to * all terms when every term is a stopword so the term list is never empty). */ export declare function extractQueryTerms(query: string): string[]; /** * Proximity + title re-ranking pass. * * Boosts results whose title contains query terms and whose body positions the * query terms close together. Code chunks get a stronger title boost * (function/class names are high signal); prose chunks get a moderate one. * * This is a STABLE re-rank on top of RRF: results are sorted by boost (desc) * with the original rank as tiebreaker, so equally-boosted results keep their * RRF order. Applied after fusion, before minScore/file-chunk limiting. * * Pure over inputs — safe to unit-test without a DB. */ export declare function applyProximityRerank(results: SearchResult[], query: string): SearchResult[]; //# sourceMappingURL=lexical-search.d.ts.map