labs-edaf30/lab2/dictionary.cc
2024-11-21 07:48:45 +01:00

62 lines
1.3 KiB
C++

#include "dictionary.h"
#include "word.h"
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using std::string;
using std::vector;
Dictionary::Dictionary() {}
bool Dictionary::contains(const string &word) const { return true; }
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)) {
if (line.size() > MAXLEN)
continue;
words[line.size()].push_back(Word(line));
}
file.close();
return 0;
}