diff options
Diffstat (limited to 'labb2/kod.py')
| -rw-r--r-- | labb2/kod.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/labb2/kod.py b/labb2/kod.py new file mode 100644 index 0000000..752ba43 --- /dev/null +++ b/labb2/kod.py @@ -0,0 +1,54 @@ +from lib import minimum_edit_distance + + +words = {} +for line in open("alphabetical.csv", "r").readlines(): + word, freq = line.split(",") + words[word] = int(freq) + + +def main(): + word = "" + while word != "q": + word = input("Type word: ").lower() + print("Autocompletion finished: ", autocomplete(word)) + print("Sorted autocompletion: ", autocomplete_best(word)) + print("Best three: ", autocomplete_best(word, 3)) + print("Autocorrect: ", autocorrect(word)) + + +def autocomplete(search_word): + """Return autocomplete suggestions.""" + for word in words.keys(): + if word.startswith(search_word): + return word + return None + + +def autocomplete_best(search_word, amount=None): + """Return the /amount/ most common autocompletions.""" + matching_words = {word: freq for word, freq in words.items() + if word.startswith(search_word)} + matching_words_sorted = {word: freq for word, freq in + sorted(matching_words.items(), + key=lambda item: item[1], + reverse=True)} + if amount: + return ", ".join(list(matching_words_sorted.keys())[:amount]) + else: + return ", ".join(list(matching_words_sorted.keys())) + + +def autocorrect(search_word): + """Return the word with the smallest Levhenstein distance""" + best = None + for word, _ in words.items(): + edit_distance = minimum_edit_distance(search_word, word) + if not best: + best = (edit_distance, word) + if edit_distance < best[0]: + best = (edit_distance, word) + return best[1] + + +main() |
