summaryrefslogtreecommitdiffstats
path: root/labb2/kod.py
diff options
context:
space:
mode:
authorGustav Sörnäs <gusso230@student.liu.se>2020-01-24 00:45:40 +0100
committerGustav Sörnäs <gusso230@student.liu.se>2020-01-24 00:45:40 +0100
commit4c12ef4ac7008dc797e83f400312d25ae37493c6 (patch)
tree61de45d6a6f54514c3cba93ce3c727ddc0190375 /labb2/kod.py
parentc320d11817c359c01e57c2d3ef5eae90c0b01a86 (diff)
downloadtdde44-4c12ef4ac7008dc797e83f400312d25ae37493c6.tar.gz
Do labb 2
Diffstat (limited to 'labb2/kod.py')
-rw-r--r--labb2/kod.py54
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()