Compare commits
No commits in common. "42f69296f65cd27ad7f29a84ac41ec67d50d18da" and "57d5f34ee530ccd290fe8b7281c2560f0b0183cd" have entirely different histories.
42f69296f6
...
57d5f34ee5
2 changed files with 12 additions and 6 deletions
|
@ -12,7 +12,7 @@ edit: test_edit_distance.o edit_distance.o
|
||||||
@echo "Building & linking $@"
|
@echo "Building & linking $@"
|
||||||
@$(CXX) $(CXXFLAGS) $^ -o $@
|
@$(CXX) $(CXXFLAGS) $^ -o $@
|
||||||
|
|
||||||
spell: spell.o word.o dictionary.o edit_distance.o
|
spell: spell.o word.o dictionary.o
|
||||||
@echo "Building & linking $@"
|
@echo "Building & linking $@"
|
||||||
@$(CXX) $(CXXFLAGS) $^ -o $@
|
@$(CXX) $(CXXFLAGS) $^ -o $@
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
#include "dictionary.h"
|
#include "dictionary.h"
|
||||||
#include "edit_distance.h"
|
|
||||||
#include "word.h"
|
#include "word.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
@ -57,12 +56,19 @@ void Dictionary::add_trigram_suggestions(std::vector<std::string> &suggestions,
|
||||||
|
|
||||||
void Dictionary::rank_suggestions(std::vector<std::string> &suggestions,
|
void Dictionary::rank_suggestions(std::vector<std::string> &suggestions,
|
||||||
const std::string &word) const {
|
const std::string &word) const {
|
||||||
// Sort suggestions based on the levenshtein distance
|
// Get trigrams of the input word
|
||||||
|
Word input_word(word);
|
||||||
|
const std::vector<std::string> &input_trigrams = input_word.get_triagrams();
|
||||||
|
|
||||||
|
// Sort suggestions based on the number of matching trigrams
|
||||||
std::sort(suggestions.begin(), suggestions.end(),
|
std::sort(suggestions.begin(), suggestions.end(),
|
||||||
[&](const std::string &a, const std::string &b) {
|
[&](const std::string &a, const std::string &b) {
|
||||||
auto dist_a = edit_distance(a, word);
|
Word word_a(a);
|
||||||
auto dist_b = edit_distance(b, word);
|
Word word_b(b);
|
||||||
return dist_a < dist_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
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue