def autocorrect(word=""): """Call autocorrect using the best score function available.""" raw_word = word word = cats.lower(cats.remove_punctuation(raw_word)) if word in WORDS_SET or word == "": return raw_word # Heuristically choose candidate words to score. letters = set(word) candidates = [w for w, s in LETTER_SETS if similar(s, letters, SIMILARITY_LIMIT)] # Try various diff functions until one doesn't raise an exception. for fn in [cats.final_diff, cats.feline_fixes, cats.sphinx_swap]: try: guess = cats.autocorrect(word, candidates, fn, SIMILARITY_LIMIT) return reformat(guess, raw_word) except BaseException: pass return raw_word
with open(valid_words.strip(), 'r', encoding='utf8') as correct_file: correct_words = list(correct_file.read().split()) start_time = time.time() correct_words = list(test_dict.keys()) random.shuffle(correct_words) correctly_corrected, incorrectly_corrected, not_corrected, trial_counter = 0, 0, 0, 0 for correct in correct_words: elapsed_time = time.time() - start_time if elapsed_time > 45: break typos = test_dict[correct] for_print = f"{correct}\n" for typo in typos: guess = cats.autocorrect(typo, correct_words, cats.final_diff, cats.FINAL_DIFF_LIMIT) if guess == correct: for_print += f"\tCorrect: ({typo} -> {guess})\n" correctly_corrected += 1 elif guess != typo: for_print += f"\tIncorrect: ({typo} -> {guess})\n" incorrectly_corrected += 1 else: for_print += f"\tNo change: ({typo} -> {typo})\n" not_corrected += 1 trial_counter += 1 print(for_print) print(f"Correction Speed: {(trial_counter / elapsed_time) * 60} wpm") print(f"Correctly Corrected: {correctly_corrected} words") print(f"Incorrectly Corrected: {incorrectly_corrected} words")