linux
authorNils Forssén <forssennils@gmail.com>
Mon, 7 Feb 2022 07:49:45 +0000 (08:49 +0100)
committerNils Forssén <forssennils@gmail.com>
Mon, 7 Feb 2022 07:49:45 +0000 (08:49 +0100)
laboration2/autocomplete.py
laboration3/uppgift_1.py [new file with mode: 0644]

index 43a7ab899879dae721a109ccd7e1b490a17d647d..f79bbd5cc58f7b59de9f365432cf7d5bde06a0fc 100644 (file)
@@ -1,8 +1,9 @@
 # Autocomplete
 from res.med import minimum_edit_distance as med
+import os
 
-WORDS_PATH = "res\\alphabetical_words"
-WORDS_F_PATH = "res\\alphabetical.csv"
+WORDS_PATH = os.path.dirname(__file__) + "/res/alphabetical_words"
+WORDS_F_PATH = os.path.dirname(__file__) + "/res/alphabetical.csv"
 
 
 def main():
@@ -26,6 +27,8 @@ def main():
         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)
 
 
@@ -43,6 +46,9 @@ def autocomplete(word, all_words, with_freq=False, all_freq=None):
             len(all_words) must equal len(all_freq).
     """
 
+    if word == "":
+        return []
+
     # list of tuple like (frequency of word, word)
     freq_tup = [(f, w) for w, f in zip(
         all_words, all_freq if with_freq else [1]*len(all_words))
@@ -60,7 +66,7 @@ def autocomplete(word, all_words, with_freq=False, all_freq=None):
     if word in suggest_list:
         suggest_list.remove(word)
 
-    return list(suggest_list)
+    return list(suggest_list)[:10]
 
 
 def autocorrect(word, all_words, with_freq=False, all_freq=None):
@@ -77,6 +83,9 @@ def autocorrect(word, all_words, with_freq=False, all_freq=None):
             len(all_words) must equal len(all_freq).
     """
 
+    if word == "":
+        return []
+
     # list of tuple like (med, frequency of word, word)
     med_tup = [(med(word, w), f, w) for w, f in zip(
         all_words, all_freq if with_freq else [1]*len(all_words))]
@@ -105,7 +114,7 @@ def autocorrect(word, all_words, with_freq=False, all_freq=None):
 
     _, suggest_list = zip(*freq_tup)
 
-    return list(suggest_list)
+    return list(suggest_list)[:10]
 
 
 if __name__ == "__main__":
diff --git a/laboration3/uppgift_1.py b/laboration3/uppgift_1.py
new file mode 100644 (file)
index 0000000..8d47d9d
--- /dev/null
@@ -0,0 +1,49 @@
+# Uppgift 1
+
+def key_exists(key, d):
+    return key in d
+
+
+def value_exists(value, d):
+    return value in d.values()
+
+
+def add_to_dict(key, value, d):
+    d[key] = value
+
+
+def add_new_only_to_dict(key, value, d):
+    if key not in d:
+        d[key] = value
+
+
+def increment_dictionary_value1(key, d):
+    d[key] += 1
+
+
+def increment_dictionary_value2(key, value, d):
+    if key not in value:
+        d[key] = 1
+    else:
+        d[key] += 1
+
+
+def add_to_value_list1(key, value, d):
+    d[key].append(value)
+
+
+def return_value_list2(prefix, d):
+    tot = []
+    for k, v in d.items():
+        if k[:len(prefix)] == prefix:
+            tot.append(v)
+    return tot
+
+
+def value_exists2(value, d):
+    for d in d.values:
+        if "__contains__" in dir(d):
+            if value in d:
+                return True
+    else:
+        return False