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.
80 lines (79 loc) • 4.19 kB
TypeScript
/**
* Shared Unicode building blocks for the Arabic script as used by Urdu.
*
* Blocks worth remembering:
* U+0600-U+06FF Arabic (Urdu letters live here, plus the U+06Ax-U+06Dx extras)
* U+0750-U+077F Arabic Supplement
* U+08A0-U+08FF Arabic Extended-A
* U+FB50-U+FDFF Arabic Presentation Forms-A
* U+FE70-U+FEFC Arabic Presentation Forms-B
*
* Every class below is written with \u escapes on purpose: these characters are
* invisible or shape-shifting in an editor, and a silently wrong literal is the
* single easiest way to break an Urdu pipeline.
*/
/**
* Combining marks: harakat (ً-ٟ), quranic annotation (ۖ-ۭ),
* superscript alef (ٰ) and the ؐ-ؚ honorific marks.
* Deliberately excludes U+06D4 (Urdu full stop ۔), U+06D2 (ے) and U+06D3 (ۓ).
*/
export declare const DIACRITIC_RANGES = "\\u0610-\\u061A\\u064B-\\u065F\\u0670\\u06D6-\\u06ED";
export declare const DIACRITICS_RE: RegExp;
/** Tatweel / kashida U+0640 — display padding, never meaning. */
export declare const TATWEEL_RE: RegExp;
/** Bidi controls, joiners and BOM that leak in from copy-paste. ZWNJ is handled separately. */
export declare const INVISIBLE_RE: RegExp;
/** Zero-width non-joiner U+200C: occasionally meaningful, so stripped only on request. */
export declare const ZWNJ_RE: RegExp;
/** Any Arabic-script character: letters, marks, punctuation, digits, presentation forms. */
export declare const ARABIC_SCRIPT_RE: RegExp;
/**
* Whole-string match for a single Urdu word (dictionary keys and values):
* Arabic-script characters plus ZWNJ/ZWJ — the joiners are meaningful inside
* words such as جزاکاللہ — and nothing else. Rejects whitespace and
* foreign-script lookalikes (Devanagari, Gurmukhi, Latin) that would silently
* break lookups, so data-table integrity tests can share one definition.
*/
export declare const ARABIC_WORD_RE: RegExp;
/** Arabic-script letters only — no marks, no digits, no punctuation, no tatweel. */
export declare const ARABIC_LETTER_RE: RegExp;
export declare const ARABIC_LETTER_RE_G: RegExp;
/** Letters of any script — the denominator when measuring "how Urdu is this text". */
export declare const ANY_LETTER_RE: RegExp;
export declare const ANY_LETTER_RE_G: RegExp;
/** Urdu digits ۰-۹ (U+06F0) and Arabic-Indic digits ٠-٩ (U+0660) are two distinct blocks. */
export declare const URDU_DIGITS = "\u06F0\u06F1\u06F2\u06F3\u06F4\u06F5\u06F6\u06F7\u06F8\u06F9";
export declare const ARABIC_INDIC_DIGITS = "\u0660\u0661\u0662\u0663\u0664\u0665\u0666\u0667\u0668\u0669";
export declare const ASCII_DIGITS = "0123456789";
export declare const ANY_DIGIT_RE_G: RegExp;
/** Sentence terminators: Urdu full stop ۔, Arabic question mark ؟, ASCII . ! ? and ellipsis. */
export declare const SENTENCE_SPLIT_RE: RegExp;
/** Word separators: whitespace plus punctuation that cannot occur inside an Urdu word. */
export declare const WORD_SPLIT_RE: RegExp;
export interface UrduLetter {
/** The canonical Urdu letter. */
ch: string;
/** Default Roman Urdu value, used when no positional rule applies. */
roman: string;
/** The letter carries a vowel in Roman output (drives the schwa rule). */
vowel?: boolean;
/** Alternate spellings in the wild that normalizeUrdu folds to this letter. */
aliases?: string[];
/** Hamza-carrying letter: collates as its base letter with a secondary tiebreaker. */
variantOf?: string;
}
/**
* The canonical Urdu alphabet (حروف تہجی) in alphabetical order — the single
* source of truth for the letter inventory shared by normalization, collation
* and transliteration:
*
* - {@link URDU_LETTERS} order defines collation (primary letters only;
* hamza variants carry {@link UrduLetter.variantOf});
* - {@link UrduLetter.roman} and {@link UrduLetter.vowel} feed transliteration;
* - {@link UrduLetter.aliases} are the Arabic-keyboard and historical spellings
* that normalization folds onto the canonical letter.
*
* Keeping one table means adding a letter updates all three consumers at once
* instead of three hand-maintained maps drifting apart.
*/
export declare const URDU_LETTERS: readonly UrduLetter[];