diff options
author | Vlad Voicu <vladvoic@gmail.com> | 2011-11-28 16:37:32 (GMT) |
---|---|---|
committer | Vlad Voicu <vladvoic@gmail.com> | 2013-03-15 09:21:52 (GMT) |
commit | 2061b06eccca67595c50edd81c44c5b961bf108b (patch) | |
tree | 7fdc9e4cc80a9d8ddbe5364a531ef3449f72ab2b /SwifTools/HunspellChecker.cpp | |
parent | a069a0df0f51a948a86e34d99f952a33eecd97ba (diff) | |
download | swift-2061b06eccca67595c50edd81c44c5b961bf108b.zip swift-2061b06eccca67595c50edd81c44c5b961bf108b.tar.bz2 |
Spell checker implementation using Hunspell
Change-Id: Ia15b6532edf6eef7c45bdfb273e77f65ce998f13
License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details
Diffstat (limited to 'SwifTools/HunspellChecker.cpp')
-rw-r--r-- | SwifTools/HunspellChecker.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/SwifTools/HunspellChecker.cpp b/SwifTools/HunspellChecker.cpp new file mode 100644 index 0000000..ecd352e --- /dev/null +++ b/SwifTools/HunspellChecker.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2011 Vlad Voicu + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include <SwifTools/HunspellChecker.h> + +#include <algorithm> +#include <hunspell/hunspell.hxx> +#include <boost/algorithm/string.hpp> + + +namespace Swift { + +HunspellChecker::HunspellChecker(const char* affix_path, const char* dictionary_path) { + speller_ = new Hunspell(affix_path, dictionary_path); +} + +HunspellChecker::~HunspellChecker() { + delete speller_; +} + +bool HunspellChecker::isCorrect(const std::string& word) { + return speller_->spell(word.c_str()); +} + +void HunspellChecker::getSuggestions(const std::string& word, std::vector<std::string>& list) { + char **suggestList; + int words_returned; + if (!word.empty()) { + words_returned = speller_->suggest(&suggestList, word.c_str()); + } + for (int i = 0; i < words_returned; ++i) { + list.push_back(suggestList[i]); + free(suggestList[i]); + } + free(suggestList); +} + +void HunspellChecker::checkFragment(const std::string& fragment, PositionPairList& misspelledPositions) { + if (!fragment.empty()) { + parser_->check(fragment, misspelledPositions); + for (PositionPairList::iterator it = misspelledPositions.begin(); it != misspelledPositions.end();) { + if (isCorrect(fragment.substr(boost::get<0>(*it), boost::get<1>(*it) - boost::get<0>(*it)))) { + it = misspelledPositions.erase(it); + } + else { + ++it; + } + } + } +} + +} |