# 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():
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)
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))
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):
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))]
_, suggest_list = zip(*freq_tup)
- return list(suggest_list)
+ return list(suggest_list)[:10]
if __name__ == "__main__":
--- /dev/null
+# 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