Typo Correction Example
sinlib’s TypoDetector uses a three-stage hybrid pipeline to detect and correct Sinhala spelling errors:
- Phonological Trie — retrieves phonologically close dictionary candidates
- BiGRU Seq2Seq — generates correction candidates from a neural encoder-decoder
- Stupid Backoff LM — reranks candidates using a 151k-article news corpus bigram model
Basic sentence correction
Section titled “Basic sentence correction”from sinlib import TypoDetector
detector = TypoDetector.from_pretrained()
result = detector("ගුරුවරයා අපට උගන්වය්")print(result)# 'ගුරුවරයා අපට උගන්වයි'Diacritic errors
Section titled “Diacritic errors”result = detector("සිංහල බාෂාව ලස්සනයි")print(result)# 'සිංහල භාෂාව ලස්සනයි'School-related correction
Section titled “School-related correction”result = detector("මම පසලට ගියෙමි")print(result)# 'මම පාසලට ගියෙමි'Context-aware suggestions
Section titled “Context-aware suggestions”Passing prev_word and next_word enables bigram reranking to prefer
contextually appropriate corrections over phonologically close ones.
# Without context — returns closest phonological matchdetector.suggest_correction("ලසන")# ['වසන', 'ලේඛන', ...]
# With context — promotes the semantically correct candidatedetector.suggest_correction( "ලසන", prev_word="ලංකාව", next_word="රටක්")# ['ලස්සන', ...]Checking valid words
Section titled “Checking valid words”# A correctly spelled word passes through unchangedresult = detector("මගේ ගෙදර ලස්සනයි")print(result)# 'මගේ ගෙදර ලස්සනයි'Detection without correction
Section titled “Detection without correction”detector.is_word_suspicious("සිංහල") # False — validdetector.is_word_suspicious("සින්හල") # True — likely typoScoring words manually
Section titled “Scoring words manually”prob = detector.word_ngram_probability("සිංහල")print(prob)# ~3.2e-05 — plausible word
prob = detector.word_ngram_probability("xzqabc")print(prob)# ~1e-27 — implausible, would be correctedTuning threshold
Section titled “Tuning threshold”# Stricter: flag more words as suspiciousstrict = TypoDetector(threshold=1e-6)
# Lenient: only flag obvious typoslenient = TypoDetector(threshold=1e-12)