labs-edaf30/lab2/spell.cc

43 lines
1 KiB
C++
Raw Permalink Normal View History

2024-11-20 17:46:21 +01:00
#include "dictionary.h"
2024-11-21 09:49:35 +01:00
#include <algorithm>
2024-11-20 17:46:21 +01:00
#include <cctype>
#include <filesystem>
2021-10-27 15:15:47 +02:00
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
2024-11-20 17:46:21 +01:00
using std::string;
using std::vector;
2021-10-27 15:15:47 +02:00
2024-11-20 17:46:21 +01:00
void check_word(const string &word, const Dictionary &dict) {
2021-10-27 15:15:47 +02:00
if (dict.contains(word)) {
cout << "Correct." << endl;
} else {
vector<string> suggestions = dict.get_suggestions(word);
if (suggestions.empty()) {
cout << "Wrong, no suggestions." << endl;
} else {
cout << "Wrong. Suggestions:" << endl;
2024-11-20 17:46:21 +01:00
for (const auto &w : suggestions) {
2021-10-27 15:15:47 +02:00
cout << " " << w << endl;
}
}
}
}
2024-11-20 17:46:21 +01:00
2021-10-27 15:15:47 +02:00
int main() {
2024-11-20 17:46:21 +01:00
Dictionary dict;
string word;
dict.slurp(std::filesystem::path("/usr/share/dict/words"));
2024-11-21 09:49:35 +01:00
// dict.spit(std::filesystem::path("words.txt"));
2024-11-20 17:46:21 +01:00
2024-11-21 09:49:35 +01:00
while (cin >> word) {
std::transform(word.begin(), word.end(), word.begin(), ::tolower);
check_word(word, dict);
}
2021-10-27 15:15:47 +02:00
return 0;
}