2024-11-20 17:46:21 +01:00
|
|
|
#include "word.h"
|
2024-11-21 07:48:45 +01:00
|
|
|
#include "dictionary.h"
|
2024-11-20 17:46:21 +01:00
|
|
|
#include <algorithm>
|
2021-10-27 15:15:47 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using std::string;
|
2024-11-20 17:46:21 +01:00
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
Word::Word(const string &w, const vector<string> &t) : word(w), triagrams(t) {
|
|
|
|
std::sort(triagrams.begin(), triagrams.end());
|
|
|
|
}
|
|
|
|
|
2024-11-21 07:48:45 +01:00
|
|
|
std::vector<std::string> get_trigrams(const std::string &text) {
|
|
|
|
std::vector<std::string> trigrams;
|
|
|
|
if (text.size() < 3) {
|
|
|
|
return trigrams; // Return an empty vector if the input is too short
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i <= text.size() - 3; ++i) {
|
|
|
|
trigrams.push_back(
|
|
|
|
text.substr(i, 3)); // Extract a substring of length 3
|
|
|
|
}
|
|
|
|
|
|
|
|
return trigrams;
|
|
|
|
}
|
|
|
|
|
|
|
|
Word::Word(const std::string &w) : word(w) {
|
|
|
|
this->triagrams = get_trigrams(w);
|
|
|
|
std::sort(triagrams.begin(), triagrams.end());
|
|
|
|
}
|
|
|
|
|
2024-11-21 09:18:48 +01:00
|
|
|
string Word::get_word() const { return word; }
|
2021-10-27 15:15:47 +02:00
|
|
|
|
2024-11-21 09:18:48 +01:00
|
|
|
vector<std::string> Word::get_triagrams() const { return triagrams; }
|
2024-11-21 08:47:45 +01:00
|
|
|
|
2024-11-20 17:46:21 +01:00
|
|
|
unsigned int Word::get_matches(const vector<string> &t) const {
|
|
|
|
unsigned int matches = 0;
|
2021-10-27 15:15:47 +02:00
|
|
|
|
2024-11-20 17:46:21 +01:00
|
|
|
for (const auto &triagram : t) {
|
|
|
|
if (std::binary_search(triagrams.begin(), triagrams.end(), triagram)) {
|
|
|
|
++matches;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return matches;
|
2021-10-27 15:15:47 +02:00
|
|
|
}
|
|
|
|
|
2024-11-20 17:46:21 +01:00
|
|
|
std::ostream &operator<<(std::ostream &out, const Word &w) {
|
|
|
|
auto space = string(" ");
|
|
|
|
out << w.word;
|
|
|
|
out << space;
|
2024-11-21 07:48:45 +01:00
|
|
|
out << w.triagrams.size();
|
2024-11-20 17:46:21 +01:00
|
|
|
for (const auto &tria : w.triagrams) {
|
|
|
|
out << space << tria;
|
|
|
|
}
|
|
|
|
return out;
|
2021-10-27 15:15:47 +02:00
|
|
|
}
|
2024-11-21 07:48:45 +01:00
|
|
|
|
|
|
|
bool operator==(const Word &lhs, const Word &rhs) {
|
|
|
|
return lhs.word == rhs.word &&
|
|
|
|
std::equal(lhs.triagrams.begin(), lhs.triagrams.end(),
|
|
|
|
rhs.triagrams.begin());
|
|
|
|
}
|