labs-edaf30/lab2/dictionary.cc

69 lines
1.5 KiB
C++

#include "dictionary.h"
#include "word.h"
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using std::string;
using std::vector;
Dictionary::Dictionary() {}
bool Dictionary::contains(const string &word) const {
auto 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;
}
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;
}
int Dictionary::spit(path p) {
std::ofstream file(p);
if (!file.is_open()) {
std::cerr << "Error opening file! " << std::endl;
return 1;
}
for (int a = 0; a < MAXLEN; a++) {
for (auto &word : words[a]) {
file << word;
file << std::endl;
}
}
file.flush();
file.close();
return 0;
}
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)) {
// Words larger than max gets placed in the topmost bucket
words[std::min(line.size(), static_cast<size_t>(MAXLEN) - 1)].push_back(
Word(line));
}
file.close();
return 0;
}