Skip to main content
While N-gram context checking is fast and effective, it has limitations (short context window, data sparsity). Semantic Validation uses deep learning (Transformers) to understand the meaning of the full sentence. Semantic Validation uses a Masked Language Model to detect deep context errors by masking each word and comparing model predictions against the original. This is the AI-powered strategy at priority 70 in the validation pipeline.

How It Works

mySpellChecker uses a Masked Language Model (MLM) approach, similar to BERT or RoBERTa.
  1. Masking: The system takes a sentence and hides the suspicious word.
    • Sentence: “မောင်မောင်က အောင်အောင်ကို လှမ်းပျော်လိုက်သည်။” (Maung Maung [ပျော် = happy, should be ပြော = speak] called out to Aung Aung).
    • Masked: “မောင်မောင်က အောင်အောင်ကို [MASK]လိုက်သည်။”
  2. Prediction: The AI model predicts the most likely words to fill the hole based on the entire sentence context.
    • Predictions: “ပြော” (Say - 99%), “ကြည့်” (Look - 0.5%)…
  3. Comparison:
    • The original word “ပျော်” (Happy) is contextually nonsense here (very low probability).
    • A phonetically similar neighbor “ပြော” (Say) has high probability.
    • The system flags this as a semantic error and suggests “ပြော”.

Architecture

  • Model Format: ONNX (Open Neural Network Exchange) for high-performance inference on CPU.
  • Tokenizer: HFTokenizerWrapper (adapts HuggingFace tokenizers like XLM-RoBERTa, mBERT) or custom tokenizer.json (via tokenizers library via RawTokenizersWrapper).
  • Optimization: The model is quantized (int8) to reduce size and increase speed.

Word-Aligned Multi-Token Masking

Myanmar words are frequently split into multiple BPE subword tokens. Standard BERT-style masking (one token at a time) fails for these words. The semantic checker implements word-aligned masking:

The Problem

Alignment Algorithm

  1. Tokenize the full sentence
  2. Map each token to its character offset range using the tokenizer’s offset mapping
  3. Find all tokens whose character offsets overlap with the target word’s span
  4. Mask all those tokens simultaneously
The alignment results are cached (LRU 256 entries) since the same sentence/word combinations are often checked repeatedly.

Beam Search for Multi-Token Prediction

When multiple tokens are masked, the checker uses beam search to find the most likely complete word:
This avoids the “diagonal selection” bug where independently picking the best token at each masked position produces invalid word combinations.

Confidence Calibration

Different transformer architectures produce logits at different scales. The checker auto-detects the model family and applies appropriate scaling: The logit scale converts raw model logits to a [0, 1] confidence score. Override with logit_scale in SemanticConfig if needed.

Inference Backends

The semantic checker supports two backends through the inference_backends.py adapter:

ONNX Runtime (Default)

PyTorch Fallback

Note: GPU device selection is configured via SemanticConfig.device (e.g., "cuda:0"), not on the SemanticChecker constructor directly.

Tokenizer Wrappers

Two tokenizer adapters provide a unified interface:
  • HFTokenizerWrapper: Wraps HuggingFace AutoTokenizer (for models like XLM-RoBERTa, mBERT)
  • RawTokenizersWrapper: Wraps the tokenizers library format (for custom tokenizer.json files)
Both expose: encode(text), decode(ids), token_to_id(token), get_offsets(text)

Training Your Own Model

Since generic models may not cover your specific domain (e.g., medical, legal), mySpellChecker provides a built-in training pipeline. You can train a custom model on your own text corpus without needing a GPU cluster or cloud API.
1

Install Training Tools

2

Prepare Data

Create a simple text file (corpus.txt) with one sentence per line.
3

Train

Use the train-model CLI command. This handles tokenization, training (RoBERTa), and ONNX export automatically.
4

Result

The ./my_semantic_model folder will contain:
  • model.onnx: The optimized AI model.
  • tokenizer.json: The custom vocabulary.

Usage

Prerequisites

Configuration

You can load the model using file paths or pass pre-loaded objects. Option A: File Paths (Simple)
Option B: Pre-loaded Objects (Advanced)

SemanticChecker Methods

Performance Considerations

  • Latency: Neural network inference is slower than N-gram lookup (~50ms - 150ms on CPU).
  • Strategy: Use Semantic Validation when accuracy is paramount (e.g., final proofreading, offline batch processing).

See Also