| 1 | /* Copyright 2018-2024 Dimitrij Mijoski |
| 2 | * |
| 3 | * This file is part of Nuspell. |
| 4 | * |
| 5 | * Nuspell is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU Lesser General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * Nuspell is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public License |
| 16 | * along with Nuspell. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <nuspell/dictionary.hxx> |
| 20 | |
| 21 | #include <fstream> |
| 22 | #include <iostream> |
| 23 | |
| 24 | using namespace std; |
| 25 | |
| 26 | int main(int argc, char* argv[]) |
| 27 | { |
| 28 | if (argc < 2) |
| 29 | return 3; |
| 30 | auto test = string(argv[1]); |
| 31 | if (test.size() < 4) { |
| 32 | cerr << "Invalid test type\n"; |
| 33 | return 3; |
| 34 | } |
| 35 | auto type = test.substr(test.size() - 4); |
| 36 | auto file = ifstream(test); |
| 37 | if (!file.is_open()) { |
| 38 | cerr << "Can not open test file " << test << " \n"; |
| 39 | return 2; |
| 40 | } |
| 41 | file.close(); |
| 42 | test.replace(test.size() - 4, 4, ".aff"); |
| 43 | auto d = nuspell::Dictionary(); |
| 44 | try { |
| 45 | d.load_aff_dic(test); |
| 46 | } |
| 47 | catch (const nuspell::Dictionary_Loading_Error& err) { |
| 48 | cerr << err.what() << '\n'; |
| 49 | return 2; |
| 50 | } |
| 51 | test.erase(test.size() - 4); |
| 52 | auto word = string(); |
| 53 | if (type == ".dic") { |
| 54 | auto error = vector<string>(); |
| 55 | file.open(test + ".good"); |
| 56 | while (file >> word) { |
| 57 | if (d.spell(word) == false) |
| 58 | error.push_back(word); |
| 59 | } |
| 60 | if (!error.empty()) { |
| 61 | cout << "Good words recognised as bad:\n"; |
| 62 | for (auto& w : error) { |
| 63 | cout << w << '\n'; |
| 64 | } |
| 65 | return 1; |
| 66 | } |
| 67 | file.close(); |
| 68 | |
| 69 | error.clear(); |
| 70 | file.open(test + ".wrong"); |
| 71 | while (file >> word) { |
| 72 | if (d.spell(word)) |
| 73 | error.push_back(word); |
| 74 | } |
| 75 | if (!error.empty()) { |
| 76 | cout << "Bad words recognised as good:\n"; |
| 77 | for (auto& w : error) { |
| 78 | cout << w << '\n'; |
| 79 | } |
| 80 | return 1; |
| 81 | } |
| 82 | } |
| 83 | else if (type == ".sug") { |
| 84 | file.open(test + ".wrong"); |
| 85 | if (!file.is_open()) { |
| 86 | cerr << "Can not open test file " << test + ".wrong" |
| 87 | << " \n"; |
| 88 | return 2; |
| 89 | } |
| 90 | auto list_sugs = vector<vector<string>>(); |
| 91 | auto sug = vector<string>(); |
| 92 | while (file >> word) { |
| 93 | if (d.spell(word)) |
| 94 | continue; |
| 95 | |
| 96 | d.suggest(word, sug); |
| 97 | if (!sug.empty()) |
| 98 | list_sugs.push_back(sug); |
| 99 | } |
| 100 | file.close(); |
| 101 | |
| 102 | file.open(test + ".sug"); |
| 103 | if (!file.is_open()) |
| 104 | return 2; |
| 105 | auto expected_list_sugs = vector<vector<string>>(); |
| 106 | auto line = string(); |
| 107 | while (getline(file, line)) { |
| 108 | if (line.empty()) |
| 109 | continue; |
| 110 | sug.clear(); |
| 111 | for (size_t i = 0;;) { |
| 112 | auto j = line.find(',', i); |
| 113 | word.assign(line, i, j - i); |
| 114 | sug.push_back(word); |
| 115 | if (j == line.npos) |
| 116 | break; |
| 117 | |
| 118 | i = line.find_first_not_of(' ', j + 1); |
| 119 | if (i == line.npos) |
| 120 | break; |
| 121 | } |
| 122 | if (!sug.empty()) |
| 123 | expected_list_sugs.push_back(sug); |
| 124 | } |
| 125 | if (list_sugs != expected_list_sugs) { |
| 126 | cout << "Bad suggestions.\nExpected output:\n"; |
| 127 | for (auto& x : expected_list_sugs) { |
| 128 | for (auto& w : x) { |
| 129 | cout << w << ", "; |
| 130 | } |
| 131 | cout << '\n'; |
| 132 | } |
| 133 | cout << "\nActual output:\n"; |
| 134 | for (auto& x : list_sugs) { |
| 135 | for (auto& w : x) { |
| 136 | cout << w << ", "; |
| 137 | } |
| 138 | cout << '\n'; |
| 139 | } |
| 140 | return 1; |
| 141 | } |
| 142 | } |
| 143 | else { |
| 144 | cerr << "Invalid test type\n"; |
| 145 | return 3; |
| 146 | } |
| 147 | } |