fixed small thing
authorNils Forssén <forssennils@gmail.com>
Tue, 8 Feb 2022 09:37:49 +0000 (10:37 +0100)
committerNils Forssén <forssennils@gmail.com>
Tue, 8 Feb 2022 09:37:49 +0000 (10:37 +0100)
laboration2/autocomplete.py

index f79bbd5cc58f7b59de9f365432cf7d5bde06a0fc..7308ffb8a477c1c5d6d74b9d7c45d6a3109caf4b 100644 (file)
@@ -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)