Skip to content

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.

Class

Main spell checking and correction class. Detects typos using Akshara Trigram scoring + BiGRU sequence labeling and corrects them using a hybrid retrieval-generative pipeline.

TypoDetector(cache_size=1000, threshold=1e-8, lazy_loading=False)
ArgumentTypeDescription
cache_sizeintOptionalSize of LRU caches for predictions. Default is 1000.
thresholdfloatOptionalProbability threshold under which a word is marked as a typo. Default is 1e-8.
lazy_loadingboolOptionalIf True, defers downloading/loading weights until first call.

Load a pre-trained spell checker from the default repository.

TypoDetector.from_pretrained(pretrained_model_name_or_path="Ransaka/sinlib")

Return top spelling corrections for a misspelled word, ranked using Phonological Trie search, BiGRU Seq2Seq generation, and Stupid Backoff context scoring.

detector.suggest_correction(word, n=3, prev_word=None, next_word=None)
ArgumentTypeDescription
wordstrRequiredSingle Sinhala word to correct.
nintOptionalNumber of suggestions to return. Default is 3.
prev_wordstrOptionalPreceding word in context for bigram reranking.
next_wordstrOptionalFollowing word in context for bigram reranking.

Check if a word is likely a typo using Akshara Trigram + BiGRU labeler.

detector.is_word_suspicious(word)

Retrieve Akshara n-gram log-likelihood for a word or phrase.

detector.word_ngram_probability(word)

Return the Stupid Backoff negative log probability of a candidate word in context.

detector.get_context_neg_log_prob(prev_word, candidate, next_word)
from sinlib import TypoDetector
detector = TypoDetector.from_pretrained()
# Correct a full sentence
corrected = detector("ගුරුවරයා අපට උගන්වය්")
# 'ගුරුවරයා අපට උගන්වයි'
corrected = detector("සිංහල බාෂාව ලස්සනයි")
# 'සිංහල භාෂාව ලස්සනයි'
# Without context
detector.suggest_correction("පසලට")
# ['පාසලට', 'පාසැලට', 'පවුලට']
# With surrounding context
detector.suggest_correction(
"ලසන",
prev_word="ලංකාව",
next_word="රටක්"
)
# ['ලස්සන', 'ලේඛන', ...]
detector.is_word_suspicious("සිංහල")
# False — valid word
detector.is_word_suspicious("සින්හල")
# True — likely typo
prob = detector.word_ngram_probability("සිංහල")
# ~3.2e-05 (higher is more common)