Skip to content

Typo Correction Example

from sinlib import TypoDetector
detector = TypoDetector.from_pretrained("Ransaka/sinlib")
result = detector("අපකරියට ගිය")
print(result)
# 'අපකීර්තියට ගිය'
suggestions = detector.suggest_correction("අඩිරාජ")
print(suggestions)
# ['අධිරාජ']
# A correctly spelled word passes through unchanged
result = detector("මගේ ගෙදර ලස්සනයි")
print(result)
# 'මගේ ගෙදර ලස්සනයි'
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 corrected
# Stricter: flag more words
strict = TypoDetector(threshold=1e-6)
# Lenient: only obvious typos
lenient = TypoDetector(threshold=1e-12)