crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
AspellChecker.hpp
Go to the documentation of this file.
1 /*
2  *
3  * ---
4  *
5  * Copyright (C) 2021 Anselm Schmidt (ans[ät]ohai.su)
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version in addition to the terms of any
11  * licences already herein identified.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  *
21  * ---
22  *
23  * AspellChecker.hpp
24  *
25  * RAII wrapper for pointer to aspell spell checkers.
26  * Does NOT have ownership of the pointer, but takes care of its deletion!
27  *
28  * Created on: Feb 28, 2021
29  * Author: ans
30  */
31 
32 #ifndef WRAPPER_ASPELLCHECKER_HPP_
33 #define WRAPPER_ASPELLCHECKER_HPP_
34 
35 #include "AspellConfig.hpp"
36 #include "AspellList.hpp"
37 
38 #include "../Main/Exception.hpp"
39 
40 #include <aspell.h>
41 
42 #include <string> // std::string, std::to_string
43 #include <vector> // std::vector
44 
45 namespace crawlservpp::Wrapper {
46 
47  /*
48  * DECLARATION
49  */
50 
52 
62  class AspellChecker {
63  public:
66 
68  AspellChecker() = default;
69 
70  virtual ~AspellChecker();
71 
75 
76  [[nodiscard]] AspellSpeller * get();
77  [[nodiscard]] const AspellSpeller * getc() const;
78  [[nodiscard]] bool valid() const;
79 
83 
84  void create(AspellConfig& configuration);
85 
89 
90  [[nodiscard]] bool check(
91  const std::string& token,
92  std::vector<std::string>& suggestionsTo
93  );
94 
98 
99  void clear();
100 
102 
105 
108  AspellChecker(AspellChecker&) = delete;
109 
112 
113  AspellChecker(AspellChecker&& other) noexcept;
114  AspellChecker& operator=(AspellChecker&& other) noexcept;
115 
117 
120 
121  private:
122  // underlying pointer to the spell checker
123  AspellSpeller * ptr{nullptr};
124  };
125 
126  /*
127  * IMPLEMENTATION
128  */
129 
130  /*
131  * CONSTRUCTION AND DESTRUCTION
132  */
133 
135 
139  this->clear();
140  }
141 
142  /*
143  * GETTERS
144  */
145 
147 
154  inline AspellSpeller * AspellChecker::get() {
155  return this->ptr;
156  }
157 
159 
167  inline const AspellSpeller * AspellChecker::getc() const {
168  return this->ptr;
169  }
170 
172 
176  inline bool AspellChecker::valid() const {
177  return this->ptr != nullptr;
178  }
179 
180  /*
181  * CREATION
182  */
183 
185 
197  inline void AspellChecker::create(AspellConfig& configuration) {
198  this->clear();
199 
200  if(!configuration.valid()) {
201  throw Exception(
202  "AspellChecker::create():"
203  " The configuration is not valid"
204  );
205  }
206 
207  AspellCanHaveError * possibleError{
208  new_aspell_speller(configuration.get())
209  };
210  const auto errorNumber{
211  aspell_error_number(possibleError)
212  };
213 
214  if(errorNumber != 0) {
215  throw Exception(
216  "AspellChecker::create():"
217  " Aspell error #"
218  + std::to_string(errorNumber)
219  + ": "
220  + aspell_error_message(possibleError)
221  );
222  }
223 
224  this->ptr = to_aspell_speller(possibleError);
225  }
226 
227  /*
228  * SPELL CHECK
229  */
230 
232 
254  inline bool AspellChecker::check(
255  const std::string& token,
256  std::vector<std::string>& suggestionsTo
257  ) {
258  if(this->ptr == nullptr) {
259  throw Exception(
260  "AspellChecker::check():"
261  " The spell checker is not valid"
262  );
263  }
264 
265  const auto result{
266  aspell_speller_check(this->ptr, token.c_str(), token.size())
267  };
268 
269  if(result < 0) {
270  throw Exception(
271  "AspellChecker::check():"
272  " Aspell error #"
273  + std::to_string(aspell_speller_error_number(this->ptr))
274  + ": "
275  + aspell_speller_error_message(this->ptr)
276  );
277  }
278 
279  if(result > 0) {
280  return true;
281  }
282 
283  AspellList suggestions(aspell_speller_suggest(this->ptr, token.c_str(), token.size()));
284  std::string suggestion;
285 
286  while(suggestions.next(suggestion)) {
287  suggestionsTo.emplace_back(suggestion);
288  }
289 
290  return false;
291  }
292 
293  /*
294  * CLEANUP
295  */
296 
298  inline void AspellChecker::clear() {
299  if(this->ptr != nullptr) {
300  delete_aspell_speller(this->ptr);
301 
302  this->ptr = nullptr;
303  }
304  }
305 
306  /*
307  * COPY AND MOVE
308  */
309 
311 
324  inline AspellChecker::AspellChecker(AspellChecker&& other) noexcept : ptr(other.ptr) {
325  other.ptr = nullptr;
326  }
327 
329 
350  if(&other != this) {
351  this->clear();
352 
353  this->ptr = other.ptr;
354 
355  other.ptr = nullptr;
356  }
357 
358  return *this;
359  }
360 
361 } /* namespace crawlservpp::Wrapper */
362 
363 #endif /* WRAPPER_ASPELLCONFIG_HPP_ */
::AspellConfig * get()
Gets a pointer to the underlying configuration.
Definition: AspellConfig.hpp:141
bool valid() const
Gets whether the configuration is valid.
Definition: AspellConfig.hpp:163
void clear()
Deletes the spell checker, if necessary.
Definition: AspellChecker.hpp:298
RAII wrapper for aspell configurations.
Definition: AspellConfig.hpp:58
bool valid() const
Gets whether the spell checker is valid.
Definition: AspellChecker.hpp:176
#define MAIN_EXCEPTION_CLASS()
Macro used to easily define classes for general exceptions.
Definition: Exception.hpp:50
RAII wrapper for aspell spell checkers.
Definition: AspellChecker.hpp:62
virtual ~AspellChecker()
Destructor deleting the spell checker, if necessary.
Definition: AspellChecker.hpp:138
Class for aspell spell checker-specific exceptions.
Definition: AspellChecker.hpp:119
AspellChecker()=default
Default constructor.
Namespace for RAII wrappers and Wrapper::Database.
Definition: Database.hpp:109
RAII wrapper for aspell word lists.
Definition: AspellList.hpp:56
AspellSpeller * get()
Gets a pointer to the underlying spell checker.
Definition: AspellChecker.hpp:154
AspellChecker & operator=(AspellChecker &)=delete
Deleted copy assignment operator.
void create(AspellConfig &configuration)
Creates the spell checker.
Definition: AspellChecker.hpp:197
bool check(const std::string &token, std::vector< std::string > &suggestionsTo)
Checks whether a token is correctly spelled.
Definition: AspellChecker.hpp:254
bool next(std::string &nextTo)
Checks for the next list element.
Definition: AspellList.hpp:161
const AspellSpeller * getc() const
Gets a constant pointer to the underlying spell checker.
Definition: AspellChecker.hpp:167