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 { const std::string &word) const {
// Get trigrams of the input word // Get trigrams of the input word
Word input_word(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 // Iterate through all words in the dictionary
for (int i = 0; i < MAXLEN; ++i) { for (int i = 0; i < MAXLEN; ++i) {
for (const Word &dict_word : words[i]) { for (const Word &dict_word : words[i]) {
// Get the trigrams of the dictionary word // Get the trigrams of the dictionary word
const std::vector<std::string> &dict_word_trigrams = const std::vector<std::string> &dict_word_trigrams =
dict_word.get_triagrams(); dict_word.get_trigrams();
// Count how many trigrams match // Count how many trigrams match
unsigned int match_count = dict_word.get_matches(input_trigrams); unsigned int match_count = dict_word.get_matches(input_trigrams);

View file

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

View file

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