word = None
# Open relevant file
- with open(WORDS_PATH if not with_freq else WORDS_F_PATH, 'r') as file:
+ file = open(WORDS_PATH if not with_freq else WORDS_F_PATH, 'r')
- lines = file.readlines()
- lines = [w.strip() for w in lines]
+ lines = file.readlines()
+ lines = [w.strip() for w in lines]
- # Separate words and frequencies into separate lists
- if with_freq:
- words, freq = zip(*[line.split(',') for line in lines])
- freq = [int(s) for s in freq]
- else:
- words = lines
- freq = None
+ # Separate words and frequencies into separate lists
+ if with_freq:
+ words, freq = zip(*[line.split(',') for line in lines])
+ freq = [int(s) for s in freq]
+ else:
+ words = lines
+ freq = None
- while word != "":
- word = input("Type word: ").lower()
- suggestion = autocomplete(word, words, with_freq, freq)
- suggestion2 = autocorrect(word, words, with_freq, freq)
- print("Autocorrect finished: ", suggestion2)
- print("Autocomplete finished: ", suggestion)
+ file.close()
+
+ while word != "":
+ word = input("Type word: ").lower()
+ suggestion = autocomplete(word, words, with_freq, freq)
+ suggestion2 = autocorrect(word, words, with_freq, freq)
+ print("Autocorrect finished: ", suggestion2)
+ print("Autocomplete finished: ", suggestion)
def autocomplete(word, all_words, with_freq=False, all_freq=None):
_, suggest_list = zip(*freq_tup)
+ suggest_list = list(suggest_list)
+
if word in suggest_list:
suggest_list.remove(word)
freq_tup.sort(key=lambda tup: tup[0])
freq_tup.reverse()
else:
- freq_tup.sort(lambda tup: tup[1])
+ freq_tup.sort(key=lambda tup: tup[1])
_, suggest_list = zip(*freq_tup)