From d4970c8d7606f38e1a103b7b1d4e15c8fe74a260 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Thu, 21 Nov 2024 09:46:54 +0100 Subject: [PATCH 1/2] Include edit_distance in spell target --- lab2/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lab2/Makefile b/lab2/Makefile index 0b87009..aaa709f 100644 --- a/lab2/Makefile +++ b/lab2/Makefile @@ -12,7 +12,7 @@ edit: test_edit_distance.o edit_distance.o @echo "Building & linking $@" @$(CXX) $(CXXFLAGS) $^ -o $@ -spell: spell.o word.o dictionary.o +spell: spell.o word.o dictionary.o edit_distance.o @echo "Building & linking $@" @$(CXX) $(CXXFLAGS) $^ -o $@ From 42f69296f65cd27ad7f29a84ac41ec67d50d18da Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Thu, 21 Nov 2024 09:47:11 +0100 Subject: [PATCH 2/2] Better distance sorting --- lab2/dictionary.cc | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/lab2/dictionary.cc b/lab2/dictionary.cc index 7bfd397..ac522ff 100644 --- a/lab2/dictionary.cc +++ b/lab2/dictionary.cc @@ -1,4 +1,5 @@ #include "dictionary.h" +#include "edit_distance.h" #include "word.h" #include #include @@ -56,19 +57,12 @@ void Dictionary::add_trigram_suggestions(std::vector &suggestions, void Dictionary::rank_suggestions(std::vector &suggestions, const std::string &word) const { - // Get trigrams of the input word - Word input_word(word); - const std::vector &input_trigrams = input_word.get_triagrams(); - - // Sort suggestions based on the number of matching trigrams + // Sort suggestions based on the levenshtein distance std::sort(suggestions.begin(), suggestions.end(), [&](const std::string &a, const std::string &b) { - Word word_a(a); - Word word_b(b); - unsigned int match_a = word_a.get_matches(input_trigrams); - unsigned int match_b = word_b.get_matches(input_trigrams); - return match_a > - match_b; // Sort in descending order of match count + auto dist_a = edit_distance(a, word); + auto dist_b = edit_distance(b, word); + return dist_a < dist_b; }); }