2024-11-20 17:46:21 +01:00
|
|
|
#include "dictionary.h"
|
|
|
|
#include "word.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <filesystem>
|
2021-10-27 15:15:47 +02:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2024-11-20 17:46:21 +01:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2021-10-27 15:15:47 +02:00
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
2024-11-20 17:46:21 +01:00
|
|
|
|
|
|
|
Dictionary::Dictionary() {}
|
|
|
|
|
2024-11-21 07:48:49 +01:00
|
|
|
bool Dictionary::contains(const string &word) const {
|
|
|
|
int l = word.length();
|
|
|
|
Word w = Word(word);
|
|
|
|
if (std::find(this->words[l].begin(), this->words[l].end(), w) !=
|
|
|
|
std::end(this->words[l])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-27 15:15:47 +02:00
|
|
|
|
2024-11-20 17:46:21 +01:00
|
|
|
vector<string> Dictionary::get_suggestions(const string &word) const {
|
|
|
|
vector<string> suggestions;
|
|
|
|
// add_trigram_suggestions(suggestions, word);
|
|
|
|
// rank_suggestions(suggestions, word);
|
|
|
|
// trim_suggestions(suggestions);
|
|
|
|
return suggestions;
|
2021-10-27 15:15:47 +02:00
|
|
|
}
|
|
|
|
|
2024-11-20 17:46:21 +01:00
|
|
|
int Dictionary::spit(path p) {
|
|
|
|
std::ofstream file(p);
|
|
|
|
|
|
|
|
if (!file.is_open()) {
|
|
|
|
std::cerr << "Error opening file! " << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-11-20 19:11:48 +01:00
|
|
|
for (int a = 0; a < MAXLEN; a++) {
|
2024-11-20 17:46:21 +01:00
|
|
|
for (auto &word : words[a]) {
|
2024-11-21 07:48:45 +01:00
|
|
|
file << word;
|
2024-11-20 17:46:21 +01:00
|
|
|
file << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
file.flush();
|
|
|
|
file.close();
|
|
|
|
return 0;
|
2021-10-27 15:15:47 +02:00
|
|
|
}
|
|
|
|
|
2024-11-20 17:46:21 +01:00
|
|
|
int Dictionary::slurp(path p) {
|
|
|
|
std::ifstream file(p.string());
|
|
|
|
|
|
|
|
if (!file.is_open()) {
|
|
|
|
std::cerr << "Error opening file! " << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string line;
|
|
|
|
while (std::getline(file, line)) {
|
2024-11-20 19:11:48 +01:00
|
|
|
if (line.size() > MAXLEN)
|
|
|
|
continue;
|
2024-11-21 07:48:45 +01:00
|
|
|
words[line.size()].push_back(Word(line));
|
2024-11-20 17:46:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
return 0;
|
2021-10-27 15:15:47 +02:00
|
|
|
}
|