def test_automaton(): schema = fields.Schema(text=fields.TEXT) with TempIndex(schema, "automatonspell") as ix: with ix.writer() as w: w.add_document(text=u" ".join(_wordlist)) with ix.reader() as r: bterms = list(r.lexicon("text")) words = [bterm.decode("utf8") for bterm in bterms] assert words == _wordlist typo = "reoction" sugs = list(r.terms_within("text", typo, maxdist=2)) target = [w for w in _wordlist if levenshtein(typo, w) <= 2] assert sugs == target
def find_brute(target, k): for w in words: d = levenshtein(w, target, k) if d <= k and w[:prefixlen] == target[:prefixlen]: yield w
def find_brute(target, k): for w in words: if levenshtein(w, target, k) <= k: yield w
def test_list_corrector(): corr = spelling.ListCorrector(_wordlist) typo = "reoction" sugs = list(corr.suggest(typo, maxdist=2)) target = [w for w in _wordlist if levenshtein(typo, w) <= 2] assert sugs == target