This commit is contained in:
Imbus 2024-11-21 13:34:17 +01:00
parent abec11b35b
commit 686fd07e08
3 changed files with 19 additions and 19 deletions

View file

@ -35,14 +35,14 @@ void Dictionary::add_trigram_suggestions(std::vector<std::string> &suggestions,
const std::string &word) const {
// Get trigrams of the input word
Word input_word(word);
const std::vector<std::string> &input_trigrams = input_word.get_triagrams();
const std::vector<std::string> &input_trigrams = input_word.get_trigrams();
// Iterate through all words in the dictionary
for (int i = 0; i < MAXLEN; ++i) {
for (const Word &dict_word : words[i]) {
// Get the trigrams of the dictionary word
const std::vector<std::string> &dict_word_trigrams =
dict_word.get_triagrams();
dict_word.get_trigrams();
// Count how many trigrams match
unsigned int match_count = dict_word.get_matches(input_trigrams);

View file

@ -7,8 +7,8 @@
using std::string;
using std::vector;
Word::Word(const string &w, const vector<string> &t) : word(w), triagrams(t) {
std::sort(triagrams.begin(), triagrams.end());
Word::Word(const string &w, const vector<string> &t) : word(w), trigrams(t) {
std::sort(trigrams.begin(), trigrams.end());
}
std::vector<std::string> get_trigrams(const std::string &text) {
@ -26,19 +26,19 @@ std::vector<std::string> get_trigrams(const std::string &text) {
}
Word::Word(const std::string &w) : word(w) {
this->triagrams = get_trigrams(w);
std::sort(triagrams.begin(), triagrams.end());
this->trigrams = ::get_trigrams(w);
std::sort(trigrams.begin(), trigrams.end());
}
string Word::get_word() const { return word; }
vector<std::string> Word::get_triagrams() const { return triagrams; }
vector<std::string> Word::get_trigrams() const { return trigrams; }
unsigned int Word::get_matches(const vector<string> &t) const {
unsigned int matches = 0;
for (const auto &triagram : t) {
if (std::binary_search(triagrams.begin(), triagrams.end(), triagram)) {
for (const auto &trigram : t) {
if (std::binary_search(trigrams.begin(), trigrams.end(), trigram)) {
++matches;
}
}
@ -49,15 +49,15 @@ std::ostream &operator<<(std::ostream &out, const Word &w) {
auto space = string(" ");
out << w.word;
out << space;
out << w.triagrams.size();
for (const auto &tria : w.triagrams) {
out << space << tria;
out << w.trigrams.size();
for (const auto &tri : w.trigrams) {
out << space << tri;
}
return out;
}
bool operator==(const Word &lhs, const Word &rhs) {
return lhs.word == rhs.word &&
std::equal(lhs.triagrams.begin(), lhs.triagrams.end(),
rhs.triagrams.begin());
std::equal(lhs.trigrams.begin(), lhs.trigrams.end(),
rhs.trigrams.begin());
}

View file

@ -4,21 +4,21 @@
#include <vector>
/*
* Contains a word and its triagrams
* Contains a word and its trigrams
*/
class Word {
public:
/** Creates a word w with the sorted trigrams t */
Word(const std::string &w, const std::vector<std::string> &t);
/** Creates a word w and derives the triagrams internally */
/** Creates a word w and derives the trigrams internally */
Word(const std::string &w);
/** Returns the word */
std::string get_word() const;
/** Returns triagrams */
std::vector<std::string> get_triagrams() const;
/** Returns trigrams */
std::vector<std::string> get_trigrams() const;
/** Returns how many of the trigrams in t that are present
in this word's trigram vector */
@ -26,7 +26,7 @@ class Word {
private:
const std::string word;
std::vector<std::string> triagrams;
std::vector<std::string> trigrams;
friend std::ostream &operator<<(std::ostream &out, const Word &o);
friend bool operator==(const Word &lhs, const Word &rhs);
};