def spellcheck(word): flw = get_four_letter_words() s = Soundex() word_soundex = s.encode(word) possible_soundex_corrections = list() for w in flw: if word_soundex == s.encode(w): possible_soundex_corrections.append(w) d = DamerauLevenshtein() correction_distances = dict(zip(possible_soundex_corrections, [d.compute(x, word) for x in possible_soundex_corrections])) # sort dictionary by distances (converts to tuple for this purpose) sorted_corrections = sorted(correction_distances.items(), key=lambda x: x[1]) if sorted_corrections[0][1] == 0: print('Found word in dictionary.') else: print('Found the following possible correct spellings, ordered by distance:') for tuple in sorted_corrections: print(tuple)
def spellcheck(word): flw = get_four_letter_words() possible_correct_spellings = set() # case one: one letter in the word was missing for i in range(-1, len(word) + 1): for char in string.ascii_letters: word_changed = word[: i + 1] + char + word[i + 1 :] if word_changed in flw: possible_correct_spellings.add(word_changed) # case two: two letters were swapped for i in range(len(word)): for j in range(i + 1, len(word)): word_changed = list(word) word_changed[i] = word[j] word_changed[j] = word[i] word_changed = "".join(word_changed) if word_changed in flw: possible_correct_spellings.add(word_changed) return possible_correct_spellings
def spellcheck(word): flw = get_four_letter_words() possible_correct_spellings = set() # case one: one letter in the word was missing for i in range(-1, len(word) + 1): for char in string.ascii_letters: word_changed = word[:i + 1] + char + word[i + 1:] if word_changed in flw: possible_correct_spellings.add(word_changed) # case two: two letters were swapped for i in range(len(word)): for j in range(i + 1, len(word)): word_changed = list(word) word_changed[i] = word[j] word_changed[j] = word[i] word_changed = ''.join(word_changed) if word_changed in flw: possible_correct_spellings.add(word_changed) return possible_correct_spellings
from exercise_01 import get_four_letter_words import string import codecs en_dictionary = set() with codecs.open('english_dictionary.txt', encoding='iso-8859-15', mode='r') as f: for line in f.readlines(): en_dictionary.add(line.strip()) flw = get_four_letter_words() valid_constructed_flw = set() for word in flw: for i in range(len(word)): # checks substitution with upper- as well as lowercase letter. for char in string.ascii_letters: word_changed = word[:i] + char + word[i + 1:] if word_changed in en_dictionary: valid_constructed_flw.add(word_changed) print("Constructed {} new words by substituting only one letter: ".format( len(valid_constructed_flw))) print(valid_constructed_flw) # add constructed words to previous dictionary flw_incl_constructed = flw.union(valid_constructed_flw) # do something with it?
from exercise_01 import get_four_letter_words import string import codecs en_dictionary = set() with codecs.open('english_dictionary.txt', encoding='iso-8859-15', mode='r') as f: for line in f.readlines(): en_dictionary.add(line.strip()) flw = get_four_letter_words() valid_constructed_flw = set() for word in flw: for i in range(len(word)): # checks substitution with upper- as well as lowercase letter. for char in string.ascii_letters: word_changed = word[:i] + char + word[i+1:] if word_changed in en_dictionary: valid_constructed_flw.add(word_changed) print("Constructed {} new words by substituting only one letter: ".format(len(valid_constructed_flw))) print(valid_constructed_flw) # add constructed words to previous dictionary flw_incl_constructed = flw.union(valid_constructed_flw) # do something with it?