diff options
-rw-r--r-- | SwifTools/SConscript | 2 | ||||
-rw-r--r-- | SwifTools/SpellChecker.cpp | 42 | ||||
-rw-r--r-- | SwifTools/SpellChecker.h | 7 |
3 files changed, 20 insertions, 31 deletions
diff --git a/SwifTools/SConscript b/SwifTools/SConscript index 6f49e7f..aaafcbc 100644 --- a/SwifTools/SConscript +++ b/SwifTools/SConscript @@ -7,7 +7,7 @@ Import("env") if env["SCONS_STAGE"] == "flags" : env["SWIFTOOLS_FLAGS"] = { "LIBPATH": [Dir(".")], - "LIBS": ["SwifTools", "libaspell"] + "LIBS": ["SwifTools", "libhunspell"] } ################################################################################ diff --git a/SwifTools/SpellChecker.cpp b/SwifTools/SpellChecker.cpp index 8c9aeda..650b283 100644 --- a/SwifTools/SpellChecker.cpp +++ b/SwifTools/SpellChecker.cpp @@ -7,45 +7,35 @@ #include <SwifTools/SpellChecker.h> #include <algorithm> -#include <aspell.h> +#include <hunspell/hunspell.hxx> #include <boost/algorithm/string.hpp> namespace Swift { SpellChecker::SpellChecker() { - AspellCanHaveError* ret; - config_ = new_aspell_config(); - ret = new_aspell_speller(config_); - if (aspell_error(ret) != 0) { - //TODO(vladv): Proper log the error - //printf("Error: %s\n",aspell_error_message(ret)); - delete_aspell_can_have_error(ret); - } - speller_ = to_aspell_speller(ret); + const char* dictionary_path = "/usr/share/hunspell/en_US.dic"; + const char* affix_path = "/usr/share/hunspell/en_US.aff"; + speller_ = new Hunspell(affix_path, dictionary_path); +} + +SpellChecker::~SpellChecker() { + delete speller_; } bool SpellChecker::isCorrect(const std::string& word) { - if (!word.empty()) { - int aspell_error = aspell_speller_check(speller_, word.c_str(), -1); - if (aspell_error == 1) { - return true; - } else { - //TODO(vladv): Proper log the error - } - } - return false; + return speller_->spell(word.c_str()); } void SpellChecker::getSuggestions(const std::string& word, std::vector<std::string>& list) { - const AspellWordList *alist; + char **suggestList; + int words_returned; if (!word.empty()) { - alist = aspell_speller_suggest(speller_, word.c_str(), -1); - AspellStringEnumeration *els = aspell_word_list_elements(alist); - const char* aword; - while (((aword = aspell_string_enumeration_next(els)) != 0) && list.size() <= 5) { - list.push_back(std::string(aword)); - } + words_returned = speller_->suggest(&suggestList, word.c_str()); + } + for (int i = 0; i < words_returned; ++i) { + list.push_back(suggestList[i]); + free(suggestList[i]); } } diff --git a/SwifTools/SpellChecker.h b/SwifTools/SpellChecker.h index 3a9ff38..1c3148b 100644 --- a/SwifTools/SpellChecker.h +++ b/SwifTools/SpellChecker.h @@ -9,17 +9,16 @@ #pragma once -class AspellSpeller; -class AspellConfig; +class Hunspell; namespace Swift { class SpellChecker { public: SpellChecker(); + ~SpellChecker(); bool isCorrect(const std::string& word); void getSuggestions(const std::string& word, std::vector<std::string>& list); private: - AspellSpeller* speller_; - AspellConfig* config_; + Hunspell* speller_; }; } |