UNPKG

urdu-text-utils

Version:

Comprehensive Urdu NLP & text processing toolkit: normalization, Roman Urdu transliteration, stop words, script detection, digits, diacritics (aerab), sorting, search, and statistics. Zero dependencies, ESM + CJS.

43 lines (42 loc) • 2.08 kB
export interface SearchOptions<T> { /** Read the searchable text out of each item, for searching objects. */ getText?: (item: T) => string; /** * Allow small spelling differences via edit distance on individual words. * Costs O(query x item) per candidate word, so it runs only after the exact * substring pass fails. Default `false`. */ fuzzy?: boolean; /** Maximum edit distance per word when `fuzzy` is on. Default `1`. */ maxDistance?: number; /** Cap the number of results. */ limit?: number; /** Return matches ordered by score (best first) instead of input order. Default `true`. */ sortByScore?: boolean; } export interface SearchResult<T> { item: T; /** 1 = exact fold match, 0.9 = prefix, 0.8 = substring, lower = fuzzy word match. */ score: number; } /** Levenshtein distance with early exit once the limit is exceeded. */ export declare function editDistance(a: string, b: string, limit?: number): number; /** * Search a list of Urdu strings, ignoring diacritics and Unicode variant spellings. * * Both sides are folded with {@link foldUrdu} first, so `محمد` matches `مُحَمَّد` * and Arabic-keyboard `محمد` (with ه/ي) matches Urdu-keyboard `محمد`. * * @example * searchUrdu("محمد", ["مُحَمَّد علی", "احمد", "محمد خان"]) * // ["مُحَمَّد علی", "محمد خان"] */ export declare function searchUrdu<T = string>(query: string, items: readonly T[], options?: SearchOptions<T>): T[]; /** Same as {@link searchUrdu} but keeps the match scores. */ export declare function searchUrduRanked<T = string>(query: string, items: readonly T[], options?: SearchOptions<T>): Array<SearchResult<T>>; /** * Highlight every occurrence of `query` in `text` by wrapping it. * Matching is diacritic-insensitive, but the returned string keeps the original * spelling and diacritics intact — offsets are mapped back to the source. */ export declare function highlightUrdu(text: string, query: string, wrap?: (match: string) => string): string;