booky 0.8.0

A tool to analyze English text
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
use deunicode::deunicode_char;
use std::fmt;

/// Word class
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq, Ord, PartialOrd)]
pub enum WordClass {
    /// `A`: Adjective
    Adjective,
    /// `Av`: Adverb
    Adverb,
    /// `C`: Conjunction
    Conjunction,
    /// `D`: Determiner
    Determiner,
    /// `I`: Interjection
    Interjection,
    /// `N`: Noun
    #[default]
    Noun,
    /// `P`: Preposition
    Preposition,
    /// `Pn`: Pronoun
    Pronoun,
    /// `V`: Verb
    Verb,
}

/// Word attributes
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Ord, PartialOrd)]
pub enum WordAttr {
    /// `a`: Auxiliary verb (e.g. "cannot")
    Auxiliary,
    /// `c` Comparative / superlative inflected forms
    Comparative,
    /// `n`: Proper (name) noun
    Proper,
    /// `p`: Plurale Tantum (e.g. "pants" or "scissors")
    PluraleTantum,
    /// `s`: Singulare Tantum (e.g. "dust" or "information")
    SingulareTantum,
    /// `t` Transitive verb or preposition
    Transitive,
    /// `z` Alternate `z => s` spelling
    AlternateZ,
}

/// Word Lexeme
#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Lexeme {
    /// Lemma word form
    lemma: String,
    /// Word class
    word_class: WordClass,
    /// Attributes
    attr: String,
    /// Irregular forms (encoded)
    irregular_forms: Vec<String>,
    /// All forms
    forms: Vec<String>,
}

impl TryFrom<&str> for WordClass {
    type Error = ();

    fn try_from(cl: &str) -> Result<Self, Self::Error> {
        match cl {
            "N" => Ok(WordClass::Noun),
            "V" => Ok(WordClass::Verb),
            "A" => Ok(WordClass::Adjective),
            "Av" => Ok(WordClass::Adverb),
            "P" => Ok(WordClass::Preposition),
            "Pn" => Ok(WordClass::Pronoun),
            "C" => Ok(WordClass::Conjunction),
            "D" => Ok(WordClass::Determiner),
            "I" => Ok(WordClass::Interjection),
            _ => Err(()),
        }
    }
}

impl fmt::Display for WordClass {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        let wc = match self {
            WordClass::Noun => "N",
            WordClass::Verb => "V",
            WordClass::Adjective => "A",
            WordClass::Adverb => "Av",
            WordClass::Preposition => "P",
            WordClass::Pronoun => "Pn",
            WordClass::Conjunction => "C",
            WordClass::Determiner => "D",
            WordClass::Interjection => "I",
        };
        write!(fmt, "{wc}")
    }
}

impl WordClass {
    /// Build regular inflected forms
    fn build_regular_forms(self, lex: &Lexeme, lemma: &str) -> Vec<String> {
        let mut forms = Vec::new();
        match self {
            WordClass::Adjective => {
                forms.push(adjective_comparative(lemma));
                forms.push(adjective_superlative(lemma));
            }
            WordClass::Noun if lex.has_plural() => {
                forms.push(noun_plural(lemma));
            }
            WordClass::Verb => {
                forms.push(verb_present(lemma));
                forms.push(verb_present_participle(lemma));
                forms.push(verb_past(lemma));
            }
            _ => (),
        }
        forms
    }
}

impl TryFrom<char> for WordAttr {
    type Error = ();

    fn try_from(val: char) -> Result<Self, Self::Error> {
        match val {
            'a' => Ok(Self::Auxiliary),
            'c' => Ok(Self::Comparative),
            'n' => Ok(Self::Proper),
            'p' => Ok(Self::PluraleTantum),
            's' => Ok(Self::SingulareTantum),
            't' => Ok(Self::Transitive),
            'z' => Ok(Self::AlternateZ),
            _ => Err(()),
        }
    }
}

impl TryFrom<&str> for Lexeme {
    type Error = ();

    fn try_from(line: &str) -> Result<Self, Self::Error> {
        let mut vals = line.split(',');
        let lemma = vals.next().filter(|v| !v.is_empty()).ok_or(())?;
        let (lemma, cla) = lemma.split_once(':').ok_or(())?;
        let lemma = lemma.to_string();
        let (wc, a) = cla.split_once('.').unwrap_or((cla, ""));
        let word_class = WordClass::try_from(wc)?;
        let attr = a.to_string();
        let mut irregular_forms = Vec::new();
        for form in vals {
            let form = decode_irregular(&lemma, form)?;
            let form = encode_irregular(&lemma, &form);
            irregular_forms.push(form);
        }
        let forms = Vec::new();
        let mut word = Lexeme {
            lemma,
            word_class,
            attr,
            irregular_forms,
            forms,
        };
        word.build_inflected_forms()?;
        Ok(word)
    }
}

/// Decode an irregular word form
fn decode_irregular(lemma: &str, form: &str) -> Result<String, ()> {
    if let Some(suffix) = form.strip_prefix('-')
        && let Some(ch) = suffix.chars().next()
    {
        if let Some((base, _ending)) = lemma.rsplit_once(ch) {
            let mut f = String::with_capacity(base.len() + suffix.len());
            f.push_str(base);
            f.push_str(suffix);
            return Ok(f);
        }
        // check for variant spelling of suffix joiner
        if let Some(alt) = deunicode_char(ch)
            && !alt.starts_with(ch)
            && let Some((base, _ending)) = lemma.rsplit_once(alt)
        {
            let mut f = String::with_capacity(base.len() + suffix.len());
            let mut suffix = suffix.chars();
            suffix.next(); // skip joiner character
            f.push_str(base);
            f.push_str(alt);
            f.push_str(suffix.as_str());
            return Ok(f);
        }
        return Err(());
    }
    Ok(form.into())
}

/// Encode an irregular word form
fn encode_irregular(lemma: &str, form: &str) -> String {
    let mut pos = None;
    for i in 3..lemma.len() {
        if let (Some((a0, a1)), Some((b0, b1))) =
            (lemma.split_at_checked(i), form.split_at_checked(i))
            && a0 == b0
        {
            let mut ch = a1.chars();
            if let Some(c) = ch.next()
                && b1.starts_with(c)
                && !ch.any(|x| x == c)
            {
                pos = Some(i);
            }
        }
    }
    if let Some(i) = pos {
        let suffix = &form[i..];
        let mut s = String::from('-');
        s.push_str(suffix);
        return s;
    }
    form.into()
}

impl fmt::Debug for Lexeme {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "{}:{}", self.lemma, self.word_class)?;
        if !self.attr.is_empty() {
            write!(fmt, ".{}", self.attr)?;
        }
        for form in &self.irregular_forms {
            write!(fmt, ",{form}")?;
        }
        Ok(())
    }
}

impl fmt::Display for Lexeme {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "{}:{}", self.lemma, self.word_class)?;
        if !self.attr.is_empty() {
            write!(fmt, ".{}", self.attr)?;
        }
        Ok(())
    }
}

impl Lexeme {
    /// Get lemma as a string slice
    pub fn lemma(&self) -> &str {
        &self.lemma
    }

    /// Get the word class
    pub fn word_class(&self) -> WordClass {
        self.word_class
    }

    /// Get all forms
    pub fn forms(&self) -> &[String] {
        &self.forms[..]
    }

    /// Check if a word has inflected forms
    fn has_inflected_forms(&self) -> bool {
        match self.word_class() {
            WordClass::Adjective => {
                for a in self.attr.chars() {
                    if let Ok(WordAttr::Comparative) = WordAttr::try_from(a) {
                        return true;
                    }
                }
                false
            }
            WordClass::Noun | WordClass::Verb => true,
            _ => false,
        }
    }

    /// Check if a word (noun) has plural form
    fn has_plural(&self) -> bool {
        for a in self.attr.chars() {
            if let Ok(WordAttr::SingulareTantum | WordAttr::PluraleTantum) =
                WordAttr::try_from(a)
            {
                return false;
            }
        }
        true
    }

    /// Check if a word has alternate spelling form (`z => s`)
    fn has_alternate_z(&self) -> bool {
        for a in self.attr.chars() {
            if let Ok(WordAttr::AlternateZ) = WordAttr::try_from(a) {
                return true;
            }
        }
        false
    }

    /// Build inflected word forms
    fn build_inflected_forms(&mut self) -> Result<(), ()> {
        for variant in self.variant_spellings() {
            self.build_inflected(&variant)?;
        }
        Ok(())
    }

    /// Get all variant spellings of the lemma
    fn variant_spellings(&self) -> Vec<String> {
        let mut variants = Vec::new();
        variants.push(String::new());
        for ch in self.lemma.chars() {
            if let Some(alt) = deunicode_char(ch) {
                let mut more = Vec::new();
                if alt.chars().nth(0) != Some(ch) {
                    for variant in &variants {
                        let mut v = variant.to_string();
                        v.push_str(alt);
                        more.push(v);
                    }
                }
                if ch == 'æ' || ch == 'œ' {
                    for variant in &variants {
                        let mut v = variant.to_string();
                        v.push('e');
                        more.push(v);
                    }
                }
                for variant in variants.iter_mut() {
                    variant.push(ch);
                }
                variants.extend(more);
            }
        }
        if self.has_alternate_z() {
            let mut more = Vec::new();
            for variant in &variants {
                more.push(variant.replace('z', "s"));
            }
            variants.extend(more);
        }
        variants
    }

    /// Build inflected word forms
    fn build_inflected(&mut self, lemma: &str) -> Result<(), ()> {
        self.forms.push(lemma.to_string());
        if self.irregular_forms.is_empty() {
            if self.has_inflected_forms() {
                self.forms
                    .extend(self.word_class.build_regular_forms(self, lemma));
            }
        } else {
            for form in &self.irregular_forms {
                let form = decode_irregular(lemma, form)?;
                if form != lemma {
                    self.forms.push(form);
                }
            }
        }
        Ok(())
    }
}

/// Make a regular plural noun from the singular form
fn noun_plural(lemma: &str) -> String {
    if let Some(root) = lemma.strip_suffix("sis")
        && !root.is_empty()
    {
        return format!("{root}ses");
    }
    if ends_in_y(lemma) {
        let root = lemma.trim_end_matches('y');
        format!("{root}ies")
    } else if lemma.ends_with("s")
        || lemma.ends_with("sh")
        || lemma.ends_with("ch")
        || lemma.ends_with("x")
        || lemma.ends_with("z")
    {
        format!("{lemma}es")
    } else {
        format!("{lemma}s")
    }
}

/// Check if a character is a vowel
fn is_vowel(c: char) -> bool {
    matches!(c, 'a' | 'e' | 'i' | 'o' | 'u' | 'y')
}

/// Check if a word ends with a consonant which should repeat
fn consonant_end_repeat(s: &str) -> Option<char> {
    // consonant doubling rules (as far as I can tell):
    // 1. stress on final syllable
    // 2. always double an "l" final consonant (not "refuel", "parallel")
    let mut suffix = (' ', ' ', ' ');
    for c in s.chars() {
        if suffix.2 == 'q' && c == 'u' {
            // "qu" does not contain a vowel
            continue;
        }
        suffix = (suffix.1, suffix.2, c);
    }
    // check for exception suffixes
    if let 'w' | 'x' = suffix.2 {
        return None;
    }
    if let ('e', 'd') | ('e', 'n') | ('e', 'r') | ('o', 'n') =
        (suffix.1, suffix.2)
    {
        return None;
    }
    if !is_vowel(suffix.0) && is_vowel(suffix.1) && !is_vowel(suffix.2) {
        Some(suffix.2)
    } else {
        None
    }
}

/// Make a regular present verb from the lemma form
fn verb_present(lemma: &str) -> String {
    if ends_in_y(lemma) {
        let root = lemma.trim_end_matches('y');
        format!("{root}ies")
    } else if lemma.ends_with("s") || lemma.ends_with("z") {
        match consonant_end_repeat(lemma) {
            Some(end) => format!("{lemma}{end}es"),
            None => format!("{lemma}es"),
        }
    } else if lemma.ends_with("sh")
        || lemma.ends_with("ch")
        || lemma.ends_with("x")
    {
        format!("{lemma}es")
    } else {
        format!("{lemma}s")
    }
}

/// Check if a lemma word ends in `y` (with no other vowel)
fn ends_in_y(lemma: &str) -> bool {
    lemma.ends_with("y")
        && !(lemma.ends_with("ay")
            || lemma.ends_with("ey")
            || lemma.ends_with("iy")
            || lemma.ends_with("oy")
            || lemma.ends_with("uy")
            || lemma.ends_with("yy"))
}

/// Check if a lemma word ends in `e`
fn ends_in_e(lemma: &str) -> bool {
    lemma.ends_with("e")
        && !(lemma.ends_with("ae")
            || lemma.ends_with("ee")
            || lemma.ends_with("ie")
            || lemma.ends_with("oe")
            || lemma.ends_with("ye"))
}

/// Make a regular present participle verb from the lemma form
fn verb_present_participle(lemma: &str) -> String {
    if let Some(end) = consonant_end_repeat(lemma) {
        return format!("{lemma}{end}ing");
    }
    if ends_in_e(lemma) {
        let root = lemma.trim_end_matches('e');
        format!("{root}ing")
    } else {
        format!("{lemma}ing")
    }
}

/// Make a regular past verb from the lemma form
fn verb_past(lemma: &str) -> String {
    if let Some(end) = consonant_end_repeat(lemma) {
        return format!("{lemma}{end}ed");
    }
    if lemma.ends_with("e") {
        format!("{lemma}d")
    } else if ends_in_y(lemma) {
        let root = lemma.trim_end_matches('y');
        format!("{root}ied")
    } else {
        format!("{lemma}ed")
    }
}

/// Make a regular comparative adjective from the lemma form
fn adjective_comparative(lemma: &str) -> String {
    if lemma.ends_with("e") {
        return format!("{lemma}r");
    } else if ends_in_y(lemma) {
        let root = lemma.trim_end_matches('y');
        return format!("{root}ier");
    }
    match consonant_end_repeat(lemma) {
        Some(end) => format!("{lemma}{end}er"),
        None => format!("{lemma}er"),
    }
}

/// Make a regular superlative adjective from the lemma form
fn adjective_superlative(lemma: &str) -> String {
    if lemma.ends_with("e") {
        return format!("{lemma}st");
    } else if ends_in_y(lemma) {
        let root = lemma.trim_end_matches('y');
        return format!("{root}iest");
    }
    match consonant_end_repeat(lemma) {
        Some(end) => format!("{lemma}{end}est"),
        None => format!("{lemma}est"),
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn variants() {
        let lex = Lexeme::try_from("café:N").unwrap();
        assert_eq!(lex.variant_spellings(), vec!["café", "cafe",]);
        let lex = Lexeme::try_from("façade:N").unwrap();
        assert_eq!(lex.variant_spellings(), vec!["façade", "facade",]);
        let lex = Lexeme::try_from("appliqué:V,-és,-éing,-éd").unwrap();
        assert_eq!(lex.variant_spellings(), vec!["appliqué", "applique"]);
        let lex = Lexeme::try_from("anæsthetize:V.z").unwrap();
        assert_eq!(
            lex.variant_spellings(),
            vec![
                "anæsthetize",
                "anaesthetize",
                "anesthetize",
                "anæsthetise",
                "anaesthetise",
                "anesthetise",
            ]
        );
    }

    #[test]
    fn irregular() {
        let a = decode_irregular("addendum", "-da").unwrap();
        let form = encode_irregular("addendum", &a);
        assert_eq!(form, "-da");
    }
}