lang-check 0.5.1

Multilingual prose linter with tree-sitter extraction and pluggable checking engines
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
//! Morphological acceptance: recognising a word built on material already known.
//!
//! Adding `algebra` to a dictionary implies `subalgebra`, `quasi-algebra` and
//! `algebraicity` too. Materialising that closure is impossible — prefixes are freely
//! composable, so the set is unbounded — so it is *recognised* at check time instead:
//! peel affixes off the flagged token and ask whether what is left is known.
//!
//! # Why derivation is analysed but inflection is generated
//!
//! Stripping is the wrong tool for inflection. `occur` + `-ed` is `occurred`, not
//! `occured`, and a stripper cannot see that: it removes `ed`, finds `occur`, and
//! accepts a real misspelling. The same shape accepts `childs` and `mouses`, which are
//! errors a checker exists to catch. Inflection is therefore *generated* — see
//! [`crate::dictionary`] — where the orthographic conditions still apply.
//!
//! Derivation is the opposite case. `-ness`, `-ity`, `-able` and the prefixes attach
//! without conditions, and no table can say which of twenty derivational suffixes a
//! given root licenses, so generation would either miss most real words or invent
//! nonsense. Here the residue constraint does the work: `subadditivity` is accepted
//! only because `sub-` peels, `-ivity` restores `-ive`, and `additive` is a real word.
//!
//! # What stops this from accepting typos
//!
//! The guards here — a minimum root length, one prefix at most, a bounded number of
//! suffix steps — are weak on their own. `untill` decomposes as `un` + `till`, and
//! `till` really is a noun and a verb.
//!
//! The guard that carries the decision lives in [`crate::suppression`]: a token one
//! edit away from a word the engine itself proposed is a typo, not a coinage. Measured
//! over single-edit misspellings of common English words, decomposition alone accepts
//! about 1%; with the suggestion test it accepts none. Nothing here should be read as
//! safe without that check.

use std::sync::{Arc, LazyLock};

use harper_core::spell::{Dictionary as HarperDictionary, FstDictionary};

use crate::dictionary::Dictionary;

/// Hyphen characters treated as compound joiners.
///
/// The ASCII hyphen plus the two Unicode hyphens that survive a copy-paste from typeset
/// text. En and em dashes are punctuation, not joiners, and are excluded.
pub const HYPHENS: [char; 3] = ['-', '\u{2010}', '\u{2011}'];

/// Shortest residue that may be treated as a root.
///
/// Three, not four, because `set`, `map` and `ring` are the stems this vocabulary is
/// built from, and `subset`, `submap` and `coset` are words worth accepting. Measured
/// against a typo corpus, three and four leak identically, so the shorter bound is free.
const MIN_ROOT_CHARS: usize = 3;

/// Shortest root when a derivational suffix was removed to reach it.
///
/// One more than [`MIN_ROOT_CHARS`], because a two-letter suffix landing on a
/// three-letter word is a coincidence rather than a derivation: `noticable` peels to
/// `notic`, then `-ic` peels to `not`, and the misspelling is silently accepted. Only
/// prefixation reaches genuinely short roots — `subset`, `coset`, `submap`.
const MIN_DERIVED_ROOT_CHARS: usize = 4;

/// How many derivational suffixes may be peeled from one token.
///
/// Two reaches `subadditivity` (`sub-` then `-ivity`) and `equisatisfiability`
/// (`-ability` then `-y`); a third step buys no attested word and widens the search.
const MAX_SUFFIX_STEPS: usize = 2;

/// The productive prefix list, embedded at build time.
static PREFIX_LIST: &str = include_str!("../dictionaries/affixes/prefixes.txt");

/// Prefixes ordered longest first, so `counter` is tried before `co`.
static PREFIXES: LazyLock<Vec<&'static str>> = LazyLock::new(|| {
    let mut prefixes: Vec<&'static str> = PREFIX_LIST
        .lines()
        .map(str::trim)
        .filter(|line| !line.is_empty() && !line.starts_with('#'))
        .collect();
    prefixes.sort_unstable_by(|a, b| b.len().cmp(&a.len()).then_with(|| a.cmp(b)));
    prefixes
});

/// Harper's curated dictionary, consulted as a root oracle and for part of speech.
///
/// Already built unconditionally by the harper engine, so this is usually free; when
/// harper is switched off it costs one lazy FST deserialisation.
static CURATED: LazyLock<Arc<FstDictionary>> = LazyLock::new(FstDictionary::curated);

/// A derivational suffix, with the stem endings it may put back.
///
/// `-ivity` restores `-ive` so `additivity` reaches `additive`; `-ity` restores a bare
/// `e` so `activity` reaches `active`. An empty restoration is plain concatenation.
struct SuffixRule {
    suffix: &'static str,
    restores: &'static [&'static str],
}

/// The derivational suffixes, longest first so `-ability` is tried before `-ity`.
///
/// Only *derivational* suffixes belong here — ones that build a new lexeme. Plural,
/// past and progressive are inflectional and are generated instead, because stripping
/// them accepts `childs` and `occured`.
///
/// Two deliberate omissions, both learned from measurement:
/// * `-ly` does **not** restore a bare `e`. It would reach `immediate` from the
///   misspelling `immediatly`, and no real word needs it — `simply` is served by the
///   `le` restoration instead.
/// * there is no `-ing`/`-ed`/`-s` rule, for the reason in the module docs.
const SUFFIX_RULES: &[SuffixRule] = &[
    SuffixRule {
        suffix: "ization",
        restores: &["ize", "izes", ""],
    },
    SuffixRule {
        suffix: "isation",
        restores: &["ise", "ises", ""],
    },
    SuffixRule {
        suffix: "ability",
        restores: &["able", ""],
    },
    SuffixRule {
        suffix: "ibility",
        restores: &["ible"],
    },
    SuffixRule {
        suffix: "izable",
        restores: &["ize", ""],
    },
    SuffixRule {
        suffix: "ivity",
        restores: &["ive"],
    },
    SuffixRule {
        suffix: "ical",
        restores: &["y", "ic", ""],
    },
    SuffixRule {
        suffix: "ally",
        restores: &["", "al", "ic"],
    },
    SuffixRule {
        suffix: "ness",
        restores: &["", "e"],
    },
    SuffixRule {
        suffix: "less",
        restores: &[""],
    },
    SuffixRule {
        suffix: "ship",
        restores: &[""],
    },
    SuffixRule {
        suffix: "hood",
        restores: &[""],
    },
    SuffixRule {
        suffix: "wise",
        restores: &[""],
    },
    SuffixRule {
        suffix: "able",
        restores: &["", "e"],
    },
    SuffixRule {
        suffix: "ible",
        restores: &[""],
    },
    SuffixRule {
        suffix: "ity",
        restores: &["", "e"],
    },
    SuffixRule {
        suffix: "ism",
        restores: &["", "e"],
    },
    SuffixRule {
        suffix: "ist",
        restores: &["", "e"],
    },
    SuffixRule {
        suffix: "ful",
        restores: &[""],
    },
    SuffixRule {
        suffix: "oid",
        restores: &["", "e"],
    },
    SuffixRule {
        suffix: "ify",
        restores: &["", "y"],
    },
    SuffixRule {
        suffix: "ize",
        restores: &["", "e"],
    },
    SuffixRule {
        suffix: "ise",
        restores: &["", "e"],
    },
    // No `e` restoration for `-ic`: it reaches `note` from `notic`, which is how the
    // misspelling `noticable` slips through as `notic`+`able` and then `not`+`ic`. The
    // `y` restoration is the one that earns its place -- `historic` from `history`.
    SuffixRule {
        suffix: "ic",
        restores: &["", "y"],
    },
    SuffixRule {
        suffix: "al",
        restores: &["", "e"],
    },
    SuffixRule {
        suffix: "ly",
        restores: &["", "le"],
    },
    SuffixRule {
        suffix: "or",
        restores: &["", "e"],
    },
    SuffixRule {
        suffix: "er",
        restores: &["", "e"],
    },
];

/// One affix removed on the way to a known root.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AffixStep {
    /// A productive prefix, as written in the prefix list.
    Prefix(&'static str),
    /// A derivational suffix, as written in [`SUFFIX_RULES`].
    Suffix(&'static str),
}

/// A successful reading of a token as affixed known material.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Analysis {
    /// The known word the token was built on.
    pub root: String,
    /// The affixes removed to reach it, outermost first.
    pub steps: Vec<AffixStep>,
}

impl Analysis {
    /// The decomposition as `sub-+algebra`, for logs and the inspector.
    #[must_use]
    pub fn describe(&self) -> String {
        let mut out = String::new();
        for step in &self.steps {
            match step {
                AffixStep::Prefix(prefix) => {
                    out.push_str(prefix);
                    out.push_str("-+");
                }
                AffixStep::Suffix(suffix) => {
                    out.push_str("+-");
                    out.push_str(suffix);
                    out.push('/');
                }
            }
        }
        out.push_str(&self.root);
        out
    }
}

/// Decides whether a flagged token is a well-formed derivation of known material.
///
/// Holds no dictionary: the servers keep theirs behind an `Arc<Mutex<..>>` and lock it
/// per check, so the lexicon is passed to [`Self::analyze`] instead of borrowed here.
#[derive(Debug, Clone)]
pub struct AffixAnalyzer {
    english: bool,
}

impl AffixAnalyzer {
    /// Build an analyzer for a BCP-47 language tag.
    ///
    /// Only English is analysed. German `un-`/`über-` prefixation composes with
    /// noun-noun compounding, which is a segmentation problem this cannot express, and
    /// guessing there would suppress real misspellings.
    #[must_use]
    pub fn new(language: &str) -> Self {
        Self {
            english: language.to_lowercase().starts_with("en"),
        }
    }

    /// Read `token` as affixed known material, or return `None`.
    ///
    /// At least one affix must be removed: a token that is already a word in its own
    /// right yields `None`, not an empty analysis.
    ///
    /// The caller **must** still check the token against the engine's own suggestions
    /// before acting on a hit; see the module docs.
    #[must_use]
    pub fn analyze(&self, token: &str, dictionary: Option<&Dictionary>) -> Option<Analysis> {
        if !self.english {
            return None;
        }
        let lowered = token.to_lowercase();
        if !lowered
            .chars()
            .all(|c| c.is_ascii_alphabetic() || HYPHENS.contains(&c))
        {
            return None;
        }

        let mut steps = Vec::new();
        strip_prefix(&lowered, dictionary, &mut steps).or_else(|| {
            steps.clear();
            strip_suffixes(&lowered, dictionary, &mut steps)
        })
    }
}

/// Try one productive prefix, then let the suffix rules finish the job.
///
/// At most one prefix: allowing two lets `recomend` read as `re` + `co` + `mend`, and
/// no attested word needs a second.
fn strip_prefix(
    word: &str,
    dictionary: Option<&Dictionary>,
    steps: &mut Vec<AffixStep>,
) -> Option<Analysis> {
    for prefix in PREFIXES.iter() {
        let Some(rest) = word.strip_prefix(prefix) else {
            continue;
        };
        let residue = rest.strip_prefix(HYPHENS).unwrap_or(rest);
        if residue.len() < MIN_ROOT_CHARS || residue.starts_with(HYPHENS) {
            continue;
        }
        steps.push(AffixStep::Prefix(prefix));
        if let Some(analysis) = strip_suffixes(residue, dictionary, steps) {
            return Some(analysis);
        }
        steps.pop();
    }
    None
}

/// Peel derivational suffixes until the residue is a known word.
///
/// Recurses at most [`MAX_SUFFIX_STEPS`] deep and never reuses a rule, so it terminates
/// and cannot cycle between two spellings of the same ending.
fn strip_suffixes(
    word: &str,
    dictionary: Option<&Dictionary>,
    steps: &mut Vec<AffixStep>,
) -> Option<Analysis> {
    // Only a residue counts as a root. At the top of a suffix-only search `steps` is
    // empty and `word` is the token itself, and a token that is already a word is not
    // an affixed form — it is one engine flagging what another engine's dictionary
    // contains, which is a different feature.
    let stripped_a_suffix = steps.iter().any(|s| matches!(s, AffixStep::Suffix(_)));
    let floor = if stripped_a_suffix {
        MIN_DERIVED_ROOT_CHARS
    } else {
        MIN_ROOT_CHARS
    };
    if !steps.is_empty() && word.chars().count() >= floor && is_known(word, dictionary) {
        return Some(Analysis {
            root: word.to_string(),
            steps: steps.clone(),
        });
    }
    if steps
        .iter()
        .filter(|s| matches!(s, AffixStep::Suffix(_)))
        .count()
        >= MAX_SUFFIX_STEPS
    {
        return None;
    }

    for rule in SUFFIX_RULES {
        if steps.contains(&AffixStep::Suffix(rule.suffix)) {
            continue;
        }
        let Some(stem) = word.strip_suffix(rule.suffix) else {
            continue;
        };
        steps.push(AffixStep::Suffix(rule.suffix));
        for restore in rule.restores {
            let candidate = format!("{stem}{restore}");
            if candidate.len() < MIN_ROOT_CHARS
                || !restoration_is_wellformed(rule.suffix, &candidate)
            {
                continue;
            }
            if let Some(analysis) = strip_suffixes(&candidate, dictionary, steps) {
                return Some(analysis);
            }
        }
        steps.pop();
    }
    None
}

/// Whether restoring that ending is orthographically possible for this suffix.
///
/// English keeps a stem's final `e` before `-able` when it follows a soft `c` or `g`
/// (`noticeable`, `changeable`, `manageable`) and drops it otherwise (`movable`,
/// `provable`). So a restored `e` that lands on `ce` or `ge` says the word should have
/// kept it, which makes the token a misspelling — `noticable` — rather than a
/// derivation. Without this the analyzer silences it whenever the engine happens to
/// offer no correction.
fn restoration_is_wellformed(suffix: &str, candidate: &str) -> bool {
    suffix != "able" || !(candidate.ends_with("ce") || candidate.ends_with("ge"))
}

/// Whether `word` is a word either the workspace or harper already knows.
///
/// Harper's curated dictionary is consulted as well as the configured wordlists so that
/// `semicontinuity` resolves through `continuity` without anybody having to add a
/// general-English list to the project.
fn is_known(word: &str, dictionary: Option<&Dictionary>) -> bool {
    dictionary.is_some_and(|d| d.contains(word)) || CURATED.contains_word_str(word)
}

pub mod inflection;

#[cfg(test)]
mod tests;