rustam 0.1.0

Full Persian NLP pipeline for Rust — normalization, tokenization, stemming, lemmatization, conjugation, spell correction, and more
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! Informal-to-formal Persian normalization.
//!
//! [`InformalNormalizer`] converts colloquial Persian text (e.g. chat messages)
//! to standard written Persian.  It maps informal word forms and verb
//! conjugations to their formal equivalents, returning all valid candidates
//! for each token so the caller can choose the best one.
//!
//! [`InformalLemmatizer`] extends [`Lemmatizer`] with an informal-verb
//! conjugation table and an informal-word map.

use std::collections::{HashMap, HashSet};

use crate::{
    conjugation::Conjugation,
    data::{parse_verbs, INFORMAL_VERBS_DAT, INFORMAL_WORDS_DAT, VERBS_DAT},
    lemmatizer::Lemmatizer,
    normalizer::Normalizer,
    sentence_tokenizer::SentenceTokenizer,
    types::WordEntry,
    word_tokenizer::WordTokenizer,
};

// ---------------------------------------------------------------------------
// Data parsing helpers
// ---------------------------------------------------------------------------

/// Parses `iwords.dat`: each line is `informal_word formal_word`.
fn parse_iwords(source: &str) -> HashMap<String, String> {
    source
        .lines()
        .filter_map(|line| {
            let line = line.trim();
            if line.is_empty() { return None; }
            let mut parts = line.splitn(2, ' ');
            let informal = parts.next()?.to_string();
            let formal = parts.next()?.to_string();
            Some((informal, formal))
        })
        .collect()
}

/// Parses `iverbs.dat`: each line is `formal_past#formal_present informal_present flag`.
fn parse_iverbs(source: &str) -> Vec<InformalVerbEntry> {
    source
        .lines()
        .filter_map(|line| {
            let line = line.trim();
            if line.is_empty() { return None; }
            let mut parts = line.splitn(3, ' ');
            let formal = parts.next()?.to_string();
            let informal_present = parts.next()?.to_string();
            let flag: bool = parts.next()?.trim() == "1";
            let mut roots = formal.splitn(2, '#');
            let past = roots.next()?.to_string();
            let present = roots.next()?.to_string();
            Some(InformalVerbEntry { formal, past, present: present.clone(), informal_present, flag })
        })
        .collect()
}

struct InformalVerbEntry {
    formal: String,    // "past#present"
    past: String,
    present: String,
    informal_present: String,
    flag: bool,        // true → bā-prefix verbs (برمی...)
}

// ---------------------------------------------------------------------------
// InformalLemmatizer
// ---------------------------------------------------------------------------

/// Extends [`Lemmatizer`] to handle informal (colloquial) Persian verb and word forms.
///
/// Adds all informal conjugation forms to the verb lookup table, and all
/// informal word surface forms to the word set.
pub struct InformalLemmatizer {
    inner: Lemmatizer,
    /// extra informal word set (surface forms only, no lemma — return as-is)
    extra_words: HashSet<String>,
}

impl InformalLemmatizer {
    /// Creates a new `InformalLemmatizer` from the embedded data files.
    pub fn new() -> Self {
        let mut inner = Lemmatizer::new();

        let iverbs = parse_iverbs(INFORMAL_VERBS_DAT);
        let iwords = parse_iwords(INFORMAL_WORDS_DAT);

        // Add informal word surfaces to words map (pointing to formal form).
        // Look up the formal entry first to avoid a double-borrow.
        let informal_inserts: Vec<(String, WordEntry)> = iwords
            .iter()
            .map(|(informal, formal)| {
                let entry = inner.words.get(formal.as_str()).cloned().unwrap_or(WordEntry {
                    frequency: 0,
                    pos_tags: vec![],
                });
                (informal.clone(), entry)
            })
            .collect();
        for (informal, entry) in informal_inserts {
            inner.words.entry(informal).or_insert(entry);
        }

        // For every ند-ending verb form, add a نه variant (informal spoken drop of د)
        let extra_verbs: Vec<(String, String)> = inner
            .verbs
            .iter()
            .filter(|(k, _)| k.ends_with('د'))
            .map(|(k, v)| {
                let n_form = format!("{}ن", &k[..k.len() - 'د'.len_utf8()]);
                (n_form, v.clone())
            })
            .collect();
        for (k, v) in extra_verbs {
            inner.verbs.entry(k).or_insert(v);
        }

        // Add informal conjugation forms
        for entry in &iverbs {
            for form in informal_conjugations(&entry.informal_present) {
                inner.verbs.entry(form).or_insert_with(|| entry.formal.clone());
            }
        }

        // Collect informal word surface set (for contains-check in all_in_lexicon)
        let mut extra_words: HashSet<String> = iwords.into_keys().collect();
        // Also add ً-stripped variants
        let stripped: Vec<String> = inner
            .words
            .keys()
            .filter(|w| w.ends_with('ً'))
            .map(|w| w[..w.len() - 'ً'.len_utf8()].to_string())
            .collect();
        extra_words.extend(stripped);

        Self { inner, extra_words }
    }

    /// Returns the lemma of `word` (informal or formal).
    pub fn lemmatize(&self, word: &str, pos: &str) -> String {
        self.inner.lemmatize(word, pos)
    }

    /// Returns `true` if `word` is in the lexicon (formal or informal surface).
    pub fn contains(&self, word: &str) -> bool {
        self.inner.words.contains_key(word)
            || self.inner.verbs.contains_key(word)
            || self.extra_words.contains(word)
    }
}

impl Default for InformalLemmatizer {
    fn default() -> Self {
        Self::new()
    }
}

// ---------------------------------------------------------------------------
// InformalNormalizer
// ---------------------------------------------------------------------------

/// Normalizes informal (colloquial) Persian text to standard written Persian.
///
/// The output is a `Vec<Vec<Vec<String>>>` — sentences of tokens of candidates —
/// because each informal word may have multiple valid formal readings.
///
/// # Examples
///
/// ```
/// use rustam::InformalNormalizer;
///
/// let n = InformalNormalizer::new();
/// let result = n.normalize("بابا یه شغل مناسب واسه بچه هام پیدا کرده");
/// // Each inner Vec<String> is the list of formal candidates for that token.
/// ```
pub struct InformalNormalizer {
    normalizer: Normalizer,
    sent_tokenizer: SentenceTokenizer,
    word_tokenizer: WordTokenizer,
    lemmatizer: Lemmatizer,
    ilemmatizer: InformalLemmatizer,
    iword_map: HashMap<String, String>,
    /// Maps informal past stem → canonical formal past stem
    past_verbs: HashMap<String, String>,
    /// Maps informal present stem → canonical formal present stem
    present_verbs: HashMap<String, String>,
    /// Maps every informal conjugated form → the matching formal conjugated form
    iverb_map: HashMap<String, String>,
}

impl InformalNormalizer {
    /// Creates an `InformalNormalizer` backed by the embedded data files.
    pub fn new() -> Self {
        let iverb_entries = parse_iverbs(INFORMAL_VERBS_DAT);
        let iword_map = parse_iwords(INFORMAL_WORDS_DAT);
        let default_verbs = parse_verbs(VERBS_DAT);

        let mut past_verbs: HashMap<String, String> = HashMap::new();
        let mut present_verbs: HashMap<String, String> = HashMap::new();
        let mut iverb_map: HashMap<String, String> = HashMap::new();

        // Populate from iverbs.dat
        for entry in &iverb_entries {
            present_verbs.insert(entry.informal_present.clone(), entry.present.clone());
            past_verbs.insert(entry.past.clone(), entry.past.clone());
        }
        // Populate from default verbs (formal present/past map to themselves)
        for vr in &default_verbs {
            present_verbs.insert(vr.present.clone(), vr.present.clone());
            past_verbs.insert(vr.past.clone(), vr.past.clone());
        }

        // Build informal conjugation → formal conjugation map
        let conj = Conjugation;
        for entry in &iverb_entries {
            let informal_forms = informal_conjugations(&entry.informal_present);
            let formal_root = format!("{}#{}", entry.past, entry.present);
            let formal_forms = conj.get_all(&formal_root);
            // flag == true → use forms starting at index 48 (imperfective)
            let (informal_range, formal_range) = if entry.flag {
                (0..informal_forms.len(), 48..formal_forms.len())
            } else {
                (8..informal_forms.len(), 56..formal_forms.len())
            };
            for (i, j) in informal_range.zip(formal_range) {
                if i < informal_forms.len() && j < formal_forms.len() {
                    let inf_form = &informal_forms[i];
                    let form_form = &formal_forms[j];
                    iverb_map.insert(inf_form.clone(), form_form.clone());
                    // Also insert ZWNJ-less and space variants
                    if inf_form.contains('\u{200C}') {
                        iverb_map.insert(
                            inf_form.replace('\u{200C}', ""),
                            form_form.clone(),
                        );
                        iverb_map.insert(
                            inf_form.replace('\u{200C}', " "),
                            form_form.clone(),
                        );
                    }
                    if inf_form.ends_with("ین") {
                        let alt = format!("{}د", &inf_form[..inf_form.len() - "ین".len()]);
                        iverb_map.insert(alt, form_form.clone());
                    }
                }
            }
        }

        Self {
            normalizer: Normalizer::new(),
            sent_tokenizer: SentenceTokenizer::new(),
            word_tokenizer: WordTokenizer::new(),
            lemmatizer: Lemmatizer::new(),
            ilemmatizer: InformalLemmatizer::new(),
            iword_map,
            past_verbs,
            present_verbs,
            iverb_map,
        }
    }

    // -----------------------------------------------------------------------
    // Accessors
    // -----------------------------------------------------------------------

    /// Returns a reference to the embedded `InformalLemmatizer`.
    pub fn lemmatizer(&self) -> &InformalLemmatizer {
        &self.ilemmatizer
    }

    /// Returns the map of informal past stem → canonical formal past stem.
    pub fn past_verb_map(&self) -> &HashMap<String, String> {
        &self.past_verbs
    }

    /// Returns the map of informal present stem → canonical formal present stem.
    pub fn present_verb_map(&self) -> &HashMap<String, String> {
        &self.present_verbs
    }

    // -----------------------------------------------------------------------
    // Public API
    // -----------------------------------------------------------------------

    /// Normalizes `text`, returning all formal-candidate lists per token.
    ///
    /// Structure: `result[sentence][token]` = `Vec<formal_candidates>`.
    pub fn normalize(&self, text: &str) -> Vec<Vec<Vec<String>>> {
        let text = self.normalizer.normalize(text);
        self.sent_tokenizer
            .tokenize(&text)
            .into_iter()
            .map(|sentence| {
                self.word_tokenizer
                    .tokenize(&sentence)
                    .into_iter()
                    .map(|word| self.normalized_word(&word))
                    .collect()
            })
            .collect()
    }

    /// Returns all formal candidates for a single informal `word`.
    pub fn normalized_word(&self, word: &str) -> Vec<String> {
        // Direct verb form lookup
        if let Some(formal) = self.iverb_map.get(word) {
            return vec![formal.clone()];
        }
        // Direct word map lookup
        if let Some(formal) = self.iword_map.get(word) {
            return vec![formal.clone(), word.to_string()];
        }
        // The word is already formal
        if self.lemmatizer.words.contains_key(word) {
            return vec![word.to_string()];
        }
        // Try suffix analysis
        let analyzed = self.analyze_word(word);
        if !analyzed.is_empty() {
            return analyzed;
        }
        vec![word.to_string()]
    }

    // -----------------------------------------------------------------------
    // Private helpers
    // -----------------------------------------------------------------------

    fn analyze_word(&self, word: &str) -> Vec<String> {
        const SUFFIXES: &[&str] = &[
            "هاست", "هایی", "هایم", "ترین", "ایی", "انی", "شان", "شون", "است",
            "تان", "تون", "مان", "مون", "هام", "هاش", "های", "طور", "ها", "تر",
            "ئی", "یی", "یم", "ام", "ای", "ان", "هم", "رو", "یت", "ه", "ی",
            "ش", "و", "ا", "ت", "م",
        ];

        let mut results: Vec<String> = Vec::new();

        for suffix in SUFFIXES {
            if word.ends_with(suffix) && word.len() > suffix.len() {
                let stem = &word[..word.len() - suffix.len()];
                let formal_stem = self
                    .iword_map
                    .get(stem)
                    .map(|s| s.as_str())
                    .or_else(|| self.lemmatizer.words.contains_key(stem).then_some(stem));

                if let Some(fs) = formal_stem {
                    let normalized = apply_suffix(fs, suffix);
                    for form in normalized {
                        if !results.contains(&form) {
                            results.push(form);
                        }
                    }
                }
            }
        }

        results
    }
}

impl Default for InformalNormalizer {
    fn default() -> Self {
        Self::new()
    }
}

// ---------------------------------------------------------------------------
// Informal conjugation generator
// ---------------------------------------------------------------------------

/// Generates the informal spoken conjugation forms for `verb` (present stem).
///
/// Produces 36 forms (6 × present_simple + negated + imperfective +
/// negated-imperfective + subjunctive + negated-subjunctive).
pub fn informal_conjugations(verb: &str) -> Vec<String> {
    let ends = ["م", "ی", "", "یم", "ین", "ن"];
    let mut present_simples: Vec<String> = ends.iter().map(|e| format!("{verb}{e}")).collect();
    // 3rd person singular: +ه unless verb ends in ا, then +د
    if verb.ends_with('ا') {
        present_simples[2] = format!("{verb}د");
    } else {
        present_simples[2] = format!("{verb}ه");
    }

    let negated: Vec<String> = present_simples.iter().map(|f| format!("ن{f}")).collect();
    let imperfective: Vec<String> =
        present_simples.iter().map(|f| format!("می\u{200C}{f}")).collect();
    let neg_imperfective: Vec<String> =
        present_simples.iter().map(|f| format!("نمی\u{200C}{f}")).collect();
    let subjunctive: Vec<String> = present_simples
        .iter()
        .map(|f| {
            if f.starts_with('ب') {
                f.clone()
            } else {
                format!("ب{f}")
            }
        })
        .collect();
    let neg_subjunctive: Vec<String> = present_simples.iter().map(|f| format!("ن{f}")).collect();

    [
        present_simples,
        negated,
        imperfective,
        neg_imperfective,
        subjunctive,
        neg_subjunctive,
    ]
    .concat()
}

// ---------------------------------------------------------------------------
// Suffix attachment helper
// ---------------------------------------------------------------------------

const ADHESIVE: &str = "بپتثجچحخسشصضعغفقکگلمنهی";

fn is_adhesive(ch: char) -> bool {
    ADHESIVE.contains(ch)
}

/// Attaches a morphological suffix to a stem, inserting ZWNJ where needed.
fn apply_suffix(stem: &str, suffix: &str) -> Vec<String> {
    let last = stem.chars().last().unwrap_or(' ');
    match suffix {
        "شون" => vec![format!("{stem}شان")],
        "تون" => vec![format!("{stem}تان")],
        "مون" => vec![format!("{stem}مان")],
        "هام" => {
            let sep = if is_adhesive(last) { "\u{200C}" } else { "" };
            vec![format!("{stem}{sep}هایم")]
        }
        "ها" | "ا" => {
            let sep = if is_adhesive(last) { "\u{200C}" } else { "" };
            vec![format!("{stem}{sep}ها")]
        }
        "رو" => vec![format!("{stem} را")],
        "و" => vec![format!("{stem} را"), format!("{stem} و")],
        "ه" => vec![
            format!("{stem}ه"),
            format!("{stem}ه است"),
            format!("{stem} است"),
        ],
        _ => vec![format!("{stem}{suffix}")],
    }
}