/** * Fuzzy Matching Module for TONL Query API * * Provides fuzzy string matching capabilities including: * - Levenshtein distance * - Jaro-Winkler similarity * - Soundex/Metaphone phonetic matching * - Fuzzy contains/startsWith/endsWith * * @example * ```typescript * // In query filters: * doc.query("users[?(@.name ~= 'john')]") // fuzzy match * doc.query("users[?(@.name ~contains 'smi')]") // fuzzy contains * doc.query("users[?(@.name soundsLike 'Smith')]") // phonetic * ``` */ /** * Options for fuzzy matching operations */ export interface FuzzyOptions { /** * Similarity threshold (0.0 - 1.0) * Higher = stricter matching * @default 0.7 */ threshold?: number; /** * Case sensitive matching * @default false */ caseSensitive?: boolean; /** * Maximum Levenshtein distance allowed * Used for performance optimization * @default Infinity */ maxDistance?: number; /** * Algorithm to use for similarity * @default 'levenshtein' */ algorithm?: 'levenshtein' | 'jaro' | 'jaro-winkler' | 'dice'; /** * Timeout in milliseconds to prevent DoS * @default 100 */ timeout?: number; } /** * Calculate Levenshtein (edit) distance between two strings * * Uses Wagner-Fischer algorithm with O(min(m,n)) space optimization * * @param a - First string * @param b - Second string * @param maxDistance - Optional early termination if distance exceeds this * @returns Edit distance (number of insertions, deletions, substitutions) * * @example * ```typescript * levenshteinDistance("kitten", "sitting") // 3 * levenshteinDistance("hello", "hello") // 0 * levenshteinDistance("abc", "") // 3 * ``` */ export declare function levenshteinDistance(a: string, b: string, maxDistance?: number): number; /** * Calculate Levenshtein similarity (normalized 0-1) * * @param a - First string * @param b - Second string * @returns Similarity score (0 = completely different, 1 = identical) */ export declare function levenshteinSimilarity(a: string, b: string): number; /** * Calculate Jaro similarity between two strings * * @param a - First string * @param b - Second string * @returns Jaro similarity (0-1) */ export declare function jaroSimilarity(a: string, b: string): number; /** * Calculate Jaro-Winkler similarity * * Extends Jaro with prefix bonus for strings that match from the start * * @param a - First string * @param b - Second string * @param prefixScale - Prefix scale factor (default 0.1, max 0.25) * @returns Jaro-Winkler similarity (0-1) */ export declare function jaroWinklerSimilarity(a: string, b: string, prefixScale?: number): number; /** * Calculate Dice coefficient (bigram similarity) * * @param a - First string * @param b - Second string * @returns Dice coefficient (0-1) */ export declare function diceSimilarity(a: string, b: string): number; /** * Generate Soundex code for a string * * Classic American Soundex algorithm for phonetic matching * * @param str - Input string * @returns 4-character Soundex code * * @example * ```typescript * soundex("Robert") // "R163" * soundex("Rupert") // "R163" * soundex("Smith") // "S530" * soundex("Smythe") // "S530" * ``` */ export declare function soundex(str: string): string; /** * Generate Double Metaphone codes for a string * * More sophisticated phonetic algorithm that handles * many languages and produces two codes (primary and alternate) * * @param str - Input string * @returns Primary and alternate Metaphone codes */ export declare function metaphone(str: string): { primary: string; alternate: string; }; /** * Check if two strings sound alike using Soundex * * @param a - First string * @param b - Second string * @returns True if they have the same Soundex code */ export declare function soundsLike(a: string, b: string): boolean; /** * Check if two strings sound alike using Metaphone * * @param a - First string * @param b - Second string * @returns True if they have matching Metaphone codes */ export declare function soundsLikeMetaphone(a: string, b: string): boolean; /** * Check if two strings are a fuzzy match * * @param query - Query string * @param target - Target string to match against * @param options - Fuzzy matching options * @returns True if strings match within threshold * * @example * ```typescript * fuzzyMatch("john", "John") // true (case insensitive) * fuzzyMatch("john", "jon") // true (edit distance 1) * fuzzyMatch("john", "jane") // false * fuzzyMatch("john", "joohn", { threshold: 0.8 }) // true * ``` */ export declare function fuzzyMatch(query: string, target: string, options?: FuzzyOptions): boolean; /** * Calculate similarity score between two strings * * @param a - First string * @param b - Second string * @param options - Fuzzy options * @returns Similarity score (0-1) */ export declare function similarity(a: string, b: string, options?: FuzzyOptions): number; /** * Fuzzy string contains check * * @param haystack - String to search in * @param needle - String to search for * @param options - Fuzzy options * @returns True if haystack fuzzy-contains needle */ export declare function fuzzyContains(haystack: string, needle: string, options?: FuzzyOptions): boolean; /** * Fuzzy string starts with check * * @param str - String to check * @param prefix - Prefix to look for * @param options - Fuzzy options * @returns True if str fuzzy-starts with prefix */ export declare function fuzzyStartsWith(str: string, prefix: string, options?: FuzzyOptions): boolean; /** * Fuzzy string ends with check * * @param str - String to check * @param suffix - Suffix to look for * @param options - Fuzzy options * @returns True if str fuzzy-ends with suffix */ export declare function fuzzyEndsWith(str: string, suffix: string, options?: FuzzyOptions): boolean; /** * Find best matches for a query in a list of strings * * @param query - Query string * @param candidates - Array of candidate strings * @param options - Fuzzy options + limit * @returns Array of matches sorted by similarity */ export declare function fuzzySearch(query: string, candidates: string[], options?: FuzzyOptions & { limit?: number; }): Array<{ value: string; similarity: number; index: number; }>; /** * Evaluate fuzzy operator in filter expression * * Used internally by filter-evaluator.ts * * @param operator - Fuzzy operator type * @param left - Left operand (target string) * @param right - Right operand (query string or options) * @returns Boolean result */ export declare function evaluateFuzzyOperator(operator: string, left: any, right: any): boolean; /** * Check if an operator is a fuzzy operator */ export declare function isFuzzyOperator(operator: string): boolean; //# sourceMappingURL=fuzzy-matcher.d.ts.map