-
Notifications
You must be signed in to change notification settings - Fork 0
/
bite185.py
29 lines (21 loc) · 879 Bytes
/
bite185.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from difflib import SequenceMatcher
from os import path
from urllib.request import urlretrieve
DICTIONARY = path.join('temp', 'dictionary.txt')
if not path.isfile(DICTIONARY):
urlretrieve('http://bit.ly/2iQ3dlZ', DICTIONARY)
def load_words():
"""Return a set of words from DICTIONARY"""
with open(DICTIONARY) as f:
return {word.strip().lower() for word in f.readlines()}
def suggest_word(misspelled_word: str, words: set = None) -> str:
"""Return a valid alternative word that best matches
the entered misspelled word"""
if words is None:
words = load_words()
matches_dict = {}
for word in words:
matches_dict[word] = SequenceMatcher(None, word, misspelled_word).ratio()
sorted_by_value = sorted(matches_dict.items(), key=lambda kv: kv[1])
return sorted_by_value[-1][0]
print(suggest_word('abberration'))