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.

93 lines (92 loc) • 3.66 kB
/** * Canonical Urdu prefixes (سابقے) ordered by descending length. */ export declare const URDU_PREFIXES: readonly ["غیر", "خود", "اہل", "بے", "نا", "لا", "بد", "کم", "ان", "ہم", "با", "پُر"]; /** * Canonical Urdu suffixes (لاحقے) ordered by descending length. */ export declare const URDU_SUFFIXES: readonly ["یںگے", "ینگے", "ےگا", "ےگی", "ینگی", "داری", "گاری", "کاری", "سازی", "بازی", "مندی", "ترین", "ستان", "خانہ", "نامہ", "جات", "گان", "دار", "گار", "کار", "ساز", "باز", "مند", "ناک", "وار", "دان", "زار", "یت", "پن", "ائی", "تر", "یاں", "ئیں", "ؤں", "یوں", "وں", "یں", "ات", "ہا", "ین", "تیں", "تا", "تی", "تے", "نا"]; /** * Irreducible base words that must not have prefixes or suffixes stripped falsely. * Exported so the PHP port can consume the identical list via tables.json. */ export declare const PROTECTED_WORDS: Set<string>; export interface StemmerOptions { /** * Whether to strip canonical Urdu prefixes (e.g. بے-, نا-, غیر-, لا-). * @default true */ stripPrefixes?: boolean; /** * Whether to strip canonical Urdu suffixes (e.g. -وں, -یں, -یاں, -دار, -تے). * @default true */ stripSuffixes?: boolean; /** * Minimum character length of the remaining root word. * Prevents over-stemming of short roots. * @default 2 */ minStemLength?: number; /** * Custom list of prefixes to strip in addition to or in place of defaults. */ customPrefixes?: string[]; /** * Custom list of suffixes to strip in addition to or in place of defaults. */ customSuffixes?: string[]; /** * Map of exact exception words to their canonical stems. */ exceptions?: Record<string, string>; } export interface AffixBreakdown { /** The stripped prefix, if any */ prefix?: string; /** The stemmed base root */ stem: string; /** The stripped suffix, if any */ suffix?: string; } /** * Analyzes an Urdu word and extracts its prefix, stem, and suffix. * * @example * getAffixes("بےوقوف") * // { prefix: "بے", stem: "وقوف" } * * getAffixes("کتابیں") * // { stem: "کتاب", suffix: "یں" } * * getAffixes("غیرملکیوں") * // { prefix: "غیر", stem: "ملکی", suffix: "یوں" } * * getAffixes("نااہلی") * // { prefix: "نا", stem: "اہلی" } */ export declare function getAffixes(input: string, options?: StemmerOptions): AffixBreakdown; /** * Reduces an Urdu word to its morphological root/stem by stripping common prefixes, * plurals, tense inflections, and adjectival/nominal suffixes. * * @example * stemUrdu("کتابیں") // "کتاب" * stemUrdu("لڑکیاں") // "لڑکی" * stemUrdu("دعاؤں") // "دعا" * stemUrdu("بےوقوف") // "وقوف" * stemUrdu("نااہل") // "اہل" * stemUrdu("خوبصورت ترین") // "خوبصورت" */ export declare function stemUrdu(word: string, options?: StemmerOptions): string; /** * Stems all words within a block of Urdu text while preserving punctuation, * whitespaces, and document formatting. * * Ideal for search engine indexing, TF-IDF scoring, and AI/LLM embeddings preprocessing. * * @example * stemUrduText("طلباء کتابیں پڑھتے ہیں اور کہانیاں سنتے ہیں۔") * // "طلباء کتاب پڑھ ہیں اور کہانی سن ہیں۔" */ export declare function stemUrduText(text: string, options?: StemmerOptions): string;