How It Works
mySpellChecker uses a Masked Language Model (MLM) approach, similar to BERT or RoBERTa.- 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]လိုက်သည်။”
- 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%)…
- 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
- Tokenize the full sentence
- Map each token to its character offset range using the tokenizer’s offset mapping
- Find all tokens whose character offsets overlap with the target word’s span
- Mask all those tokens simultaneously
Beam Search for Multi-Token Prediction
When multiple tokens are masked, the checker uses beam search to find the most likely complete word: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 theinference_backends.py adapter:
ONNX Runtime (Default)
PyTorch Fallback
Note: GPU device selection is configured viaSemanticConfig.device(e.g.,"cuda:0"), not on theSemanticCheckerconstructor directly.
Tokenizer Wrappers
Two tokenizer adapters provide a unified interface:- HFTokenizerWrapper: Wraps HuggingFace
AutoTokenizer(for models like XLM-RoBERTa, mBERT) - RawTokenizersWrapper: Wraps the
tokenizerslibrary format (for customtokenizer.jsonfiles)
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)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
- Semantic Checking Feature: Feature-level documentation and configuration
- Training Guide: Training semantic MLM and neural reranker models