labs-edaf30/lab2/word.cc

64 lines
1.6 KiB
C++
Raw Permalink Normal View History

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;
2024-11-21 13:34:17 +01:00
Word::Word(const string &w, const vector<string> &t) : word(w), trigrams(t) {
std::sort(trigrams.begin(), trigrams.end());
2024-11-20 17:46:21 +01:00
}
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) {
2024-11-21 13:34:17 +01:00
this->trigrams = ::get_trigrams(w);
std::sort(trigrams.begin(), trigrams.end());
2024-11-21 07:48:45 +01:00
}
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 13:34:17 +01:00
vector<std::string> Word::get_trigrams() const { return trigrams; }
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-21 13:34:17 +01:00
for (const auto &trigram : t) {
if (std::binary_search(trigrams.begin(), trigrams.end(), trigram)) {
2024-11-20 17:46:21 +01:00
++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 13:34:17 +01:00
out << w.trigrams.size();
for (const auto &tri : w.trigrams) {
out << space << tri;
2024-11-20 17:46:21 +01:00
}
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 &&
2024-11-21 13:34:17 +01:00
std::equal(lhs.trigrams.begin(), lhs.trigrams.end(),
rhs.trigrams.begin());
2024-11-21 07:48:45 +01:00
}