Better distance sorting
This commit is contained in:
parent
d4970c8d76
commit
42f69296f6
1 changed files with 5 additions and 11 deletions
|
@ -1,4 +1,5 @@
|
||||||
#include "dictionary.h"
|
#include "dictionary.h"
|
||||||
|
#include "edit_distance.h"
|
||||||
#include "word.h"
|
#include "word.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
@ -56,19 +57,12 @@ 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 {
|
||||||
// Get trigrams of the input word
|
// Sort suggestions based on the levenshtein distance
|
||||||
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) {
|
||||||
Word word_a(a);
|
auto dist_a = edit_distance(a, word);
|
||||||
Word word_b(b);
|
auto dist_b = edit_distance(b, word);
|
||||||
unsigned int match_a = word_a.get_matches(input_trigrams);
|
return dist_a < dist_b;
|
||||||
unsigned int match_b = word_b.get_matches(input_trigrams);
|
|
||||||
return match_a >
|
|
||||||
match_b; // Sort in descending order of match count
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue