From 715cc238042d918b812152acf50eabfaeee14cce Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nils=20Forss=C3=A9n?= Date: Tue, 8 Feb 2022 10:37:49 +0100 Subject: [PATCH] fixed small thing --- laboration2/autocomplete.py | 38 ++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/laboration2/autocomplete.py b/laboration2/autocomplete.py index f79bbd5..7308ffb 100644 --- a/laboration2/autocomplete.py +++ b/laboration2/autocomplete.py @@ -11,25 +11,27 @@ def main(): 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): @@ -63,6 +65,8 @@ 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) @@ -110,7 +114,7 @@ def autocorrect(word, all_words, with_freq=False, all_freq=None): 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) -- 2.30.2