Skip to main content
This layer uses N-gram language models (bigram and trigram probabilities) to catch errors that pass both syllable and word validation, like writing ထမင်းသွား (“rice go”) instead of ထမင်းစား (“rice eat”).

Understanding Real-Word Errors

The Problem

Consider this Myanmar sentence:
Both “ထမင်း” (rice) and “သွား” (go) are valid words individually. But together, they don’t form a natural phrase. The user likely meant:
Standard spell checkers miss these errors because each word is spelled correctly.

The Solution: N-gram Analysis

mySpellChecker uses N-gram probabilities to detect statistically unlikely word combinations:

How It Works

Bigram Model

Analyzes pairs of adjacent words:

Trigram Model

Analyzes triplets for more context:

Higher-Order N-grams

The checker also supports 4-gram and 5-gram models for deeper context. When available, higher-order N-grams take precedence via a hierarchical backoff strategy:
  1. If 5-gram probability exists → use fivegram_threshold
  2. Else if 4-gram exists → use fourgram_threshold
  3. Else if trigram exists → use trigram_threshold
  4. Else fallback to bigram path

Probability Threshold

Words below a probability threshold are flagged. Each N-gram order has its own threshold:

Configuration

Enable Context Checking

Context Settings

NgramContextChecker Configuration

Smoothing Strategies

The library supports multiple smoothing strategies for handling unseen N-grams:

Skipped Context Words

The context validator skips 33 high-frequency Myanmar particles that appear in virtually all contexts and have low discriminative power. These particles are never flagged as context errors:
  • Subject/object markers: က, ကို, သည်, တယ်
  • Locative particles: မှာ, မှ, တွင်
  • Comitative/conjunctive: နဲ့, နှင့်, နှင်
  • Genitive/possessive: ရဲ့,
  • Emphasis/interjection: ကွာ, ဗျာ, နော်, ဟေ့, ကွ, လေ, ပါ, ပဲ, ပေါ့
  • Other common particles: များ, လည်း, တော့, ပြီး, ဖို့, အတွက်
  • Question/ending particles: လား, လဲ, လို့
  • Verb complements: ကျ, ပြ, ချ (too short for meaningful N-gram validation when split from compound verbs)
The full set is defined in core/constants/myanmar_constants.py:SKIPPED_CONTEXT_WORDS.

Context Error Types

Word Substitution

Correct word, wrong context:

N-gram Probability Calculation

Bigram Probability

Trigram Probability

Smoothing

For unseen N-grams, smoothing prevents zero probabilities:
Configure smoothing via SmoothingStrategy enum:

Performance Characteristics

Context checking is slower than syllable/word validation because it performs N-gram lookups for each word pair. Memory usage and latency depend on corpus size, database backend, and caching configuration.

API Reference

Using SpellChecker for Context Validation

Note: Direct instantiation of ContextValidator requires a DI container setup with registered validation strategies. For most use cases, use SpellChecker.check() with use_context_checker=True in the config.

NgramContextChecker

Common Patterns

Context-Aware Autocomplete

Detect Uncommon Phrases

Context-Only Validation

Limitations

Rare Valid Phrases

Unusual but correct phrases may be flagged:
Solution: Adjust threshold or use domain-specific corpus.

Corpus Bias

N-gram probabilities reflect corpus biases:
Solution: Use balanced corpus or multiple domain corpora.

Short Context

Limited context may reduce accuracy:
Solution: Process longer text segments when possible.

Troubleshooting

Issue: Too many false positives

Cause: Threshold too high or corpus too narrow Solution:

Issue: Missing context errors

Cause: Threshold too low or missing N-grams Solution:

Issue: Slow context checking

Cause: Large N-gram database or many lookups Solution: Enable caching via AlgorithmCacheConfig to speed up repeated lookups:

Next Steps