From 3f7e37a9268eab6a6ad92665ad2b473b1e46dd19 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nils=20Forss=C3=A9n?= Date: Mon, 7 Feb 2022 08:49:45 +0100 Subject: [PATCH] linux --- laboration2/autocomplete.py | 17 ++++++++++--- laboration3/uppgift_1.py | 49 +++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 laboration3/uppgift_1.py diff --git a/laboration2/autocomplete.py b/laboration2/autocomplete.py index 43a7ab8..f79bbd5 100644 --- a/laboration2/autocomplete.py +++ b/laboration2/autocomplete.py @@ -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 index 0000000..8d47d9d --- /dev/null +++ b/laboration3/uppgift_1.py @@ -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 -- 2.30.2