N-gram models capture the likelihood of word sequences, enabling detection of “real-word errors” where a correctly spelled word is used incorrectly in context.Documentation Index
Fetch the complete documentation index at: https://docs.myspellchecker.com/llms.txt
Use this file to discover all available pages before exploring further.
Overview
N-gram models capture the likelihood of word sequences, enabling detection of “real-word errors”: correctly spelled words used incorrectly in context.How It Works
Bigram Model (2-gram)
Calculates the probability of word pairs:Trigram Model (3-gram)
Extends to word triplets for richer context:4-gram and 5-gram Models
Higher-order N-grams provide deeper context for detecting errors in longer phrases:fourgrams and fivegrams database tables and configured via fourgram_threshold and fivegram_threshold in NgramContextConfig (both default to 0.0001). The context checker uses backoff: if a 5-gram probability is available it takes precedence, falling back to 4-gram, trigram, then bigram.
Smoothing Strategies
The library supports multiple smoothing strategies for handling unseen N-grams:Stupid Backoff (Default)
Fast and effective for most use cases:Add-K (Laplace) Smoothing
Adds constant k to all counts:No Smoothing
Returns raw probabilities (for pre-smoothed data):Configuration
When using
SpellCheckerConfig (recommended), NgramContextConfig is created automatically from your config and passed to the checker. The values shown above are the defaults. Individual threshold parameters are not accepted as constructor kwargs; they must go through NgramContextConfig.Error Detection
The checker uses a two-path detection strategy based on raw trigram availability:-
Trigram path: When a raw trigram probability exists in the corpus (
P_raw(w3|w1,w2) > 0), the checker uses the trigram-specific threshold (trigram_threshold, default0.0001) to determine if the word is an error. This avoids false positives from smoothed backoff values. - Bigram fallback path: When no raw trigram is found, the checker falls back to bigram probabilities with bidirectional context checking, unigram backoff for common words, and typo neighbor detection via SymSpell.
Suggestion Generation
Suggestions are generated by:- Finding words with higher conditional probability
- Filtering by edit distance (max 2)
- Ranking by combined probability and distance score
Performance
| Operation | Complexity | Typical Time |
|---|---|---|
| Bigram lookup | O(1) | <1ms |
| Trigram lookup | O(1) | <1ms |
| Context analysis | O(n) | ~100ms for avg text |
| Suggestion generation | O(k) | ~50ms |
Database Schema
N-gram data is stored in SQLite using INTEGER foreign keys referencing thewords table (not TEXT columns):
NgramContextChecker Methods
| Method | Description |
|---|---|
get_smoothed_bigram_probability(w1, w2) | Smoothed P(w2 | w1) |
get_smoothed_trigram_probability(w1, w2, w3) | Smoothed P(w3 | w1, w2) |
get_smoothed_fourgram_probability(w1, w2, w3, w4) | Smoothed P(w4 | w1, w2, w3) |
get_smoothed_fivegram_probability(w1, w2, w3, w4, w5) | Smoothed P(w5 | w1, w2, w3, w4) |
get_best_left_probability(prev_words, candidate) | Best left-context probability across available N-gram orders |
get_best_right_probability(candidate, next_words) | Best right-context probability |
is_contextual_error(prev_word, current_word, ...) | Check if word is contextually unlikely |
check_word_in_context(word, prev_words, next_words) | Full context check with suggestions |
compare_contextual_probability(w1, w2, context) | Compare probabilities between two candidates |
suggest(prev_word, current_word, ...) | Generate context-aware suggestions |
clear_context_cache() | Clear all internal probability caches |
See Also
- Context Checking - Feature documentation
- SymSpell Algorithm - Word-level suggestions
- Semantic Checking - AI-powered context analysis
- Performance Tuning - Optimization