TypoDetector
Sinhala spell checker using a hybrid pipeline: Phonological Trie search, Seq2Seq BiGRU neural corrector, and a Stupid Backoff news-corpus language model for context-aware ranking.
TypoDetector
Section titled “TypoDetector”Main spell checking and correction class. Detects typos using Akshara Trigram scoring + BiGRU sequence labeling and corrects them using a hybrid retrieval-generative pipeline.
Constructor
Section titled “Constructor”Parameters
Section titled “Parameters”| Argument | Type | Description |
|---|---|---|
| cache_size | intOptional | Size of LRU caches for predictions. Default is 1000. |
| threshold | floatOptional | Probability threshold under which a word is marked as a typo. Default is 1e-8. |
| lazy_loading | boolOptional | If True, defers downloading/loading weights until first call. |
Methods
Section titled “Methods”from_pretrained
Section titled “from_pretrained”Load a pre-trained spell checker from the default repository.
suggest_correction
Section titled “suggest_correction”Return top spelling corrections for a misspelled word, ranked using Phonological Trie search, BiGRU Seq2Seq generation, and Stupid Backoff context scoring.
| Argument | Type | Description |
|---|---|---|
| word | strRequired | Single Sinhala word to correct. |
| n | intOptional | Number of suggestions to return. Default is 3. |
| prev_word | strOptional | Preceding word in context for bigram reranking. |
| next_word | strOptional | Following word in context for bigram reranking. |
is_word_suspicious
Section titled “is_word_suspicious”Check if a word is likely a typo using Akshara Trigram + BiGRU labeler.
word_ngram_probability
Section titled “word_ngram_probability”Retrieve Akshara n-gram log-likelihood for a word or phrase.
get_context_neg_log_prob
Section titled “get_context_neg_log_prob”Return the Stupid Backoff negative log probability of a candidate word in context.
Code Examples
Section titled “Code Examples”Import and Load
Section titled “Import and Load”from sinlib import TypoDetector
detector = TypoDetector.from_pretrained()Sentence Correction (Call)
Section titled “Sentence Correction (Call)”# Correct a full sentencecorrected = detector("ගුරුවරයා අපට උගන්වය්")# 'ගුරුවරයා අපට උගන්වයි'
corrected = detector("සිංහල බාෂාව ලස්සනයි")# 'සිංහල භාෂාව ලස්සනයි'Context-Aware Suggestions
Section titled “Context-Aware Suggestions”# Without contextdetector.suggest_correction("පසලට")# ['පාසලට', 'පාසැලට', 'පවුලට']
# With surrounding contextdetector.suggest_correction( "ලසන", prev_word="ලංකාව", next_word="රටක්")# ['ලස්සන', 'ලේඛන', ...]Detection Only
Section titled “Detection Only”detector.is_word_suspicious("සිංහල")# False — valid word
detector.is_word_suspicious("සින්හල")# True — likely typoCheck Probabilities
Section titled “Check Probabilities”prob = detector.word_ngram_probability("සිංහල")# ~3.2e-05 (higher is more common)