diff options
Diffstat (limited to 'SwifTools')
-rw-r--r-- | SwifTools/HunspellChecker.cpp | 40 | ||||
-rw-r--r-- | SwifTools/HunspellChecker.h | 25 | ||||
-rw-r--r-- | SwifTools/SConscript | 3 | ||||
-rw-r--r-- | SwifTools/SpellChecker.h | 13 | ||||
-rw-r--r-- | SwifTools/SpellCheckerFactory.cpp | 32 | ||||
-rw-r--r-- | SwifTools/SpellCheckerFactory.h | 18 |
6 files changed, 121 insertions, 10 deletions
diff --git a/SwifTools/HunspellChecker.cpp b/SwifTools/HunspellChecker.cpp new file mode 100644 index 0000000..2a36930 --- /dev/null +++ b/SwifTools/HunspellChecker.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2011 Voicu Vlad + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/GPLv3.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]); + } +} + +} diff --git a/SwifTools/HunspellChecker.h b/SwifTools/HunspellChecker.h new file mode 100644 index 0000000..7117ccd --- /dev/null +++ b/SwifTools/HunspellChecker.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2011 Voicu Vlad + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include <vector> +#include <boost/algorithm/string.hpp> +#include <SwifTools/SpellChecker.h> + +#pragma once + +class Hunspell; + +namespace Swift { + class HunspellChecker : public SpellChecker { + public: + HunspellChecker(const char* affix_path, const char* dict_path); + virtual ~HunspellChecker(); + virtual bool isCorrect(const std::string& word); + virtual void getSuggestions(const std::string& word, std::vector<std::string>& list); + private: + Hunspell* speller_; + }; +} diff --git a/SwifTools/SConscript b/SwifTools/SConscript index aaafcbc..ee1b262 100644 --- a/SwifTools/SConscript +++ b/SwifTools/SConscript @@ -29,7 +29,8 @@ if env["SCONS_STAGE"] == "build" : "Linkify.cpp", "TabComplete.cpp", "LastLineTracker.cpp", - "SpellChecker.cpp", + "SpellCheckerFactory.cpp", + "HunspellChecker.cpp", ] if swiftools_env.get("HAVE_SPARKLE", 0) : diff --git a/SwifTools/SpellChecker.h b/SwifTools/SpellChecker.h index 1c3148b..64a7c95 100644 --- a/SwifTools/SpellChecker.h +++ b/SwifTools/SpellChecker.h @@ -4,21 +4,16 @@ * See Documentation/Licenses/GPLv3.txt for more information. */ -#include <vector> #include <boost/algorithm/string.hpp> +#include <vector> #pragma once -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: - Hunspell* speller_; + virtual ~SpellChecker() { }; + virtual bool isCorrect(const std::string& word) = 0; + virtual void getSuggestions(const std::string& word, std::vector<std::string>& list) = 0; }; } diff --git a/SwifTools/SpellCheckerFactory.cpp b/SwifTools/SpellCheckerFactory.cpp new file mode 100644 index 0000000..46c8f65 --- /dev/null +++ b/SwifTools/SpellCheckerFactory.cpp @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2011 Voicu Vlad + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include <SwifTools/SpellChecker.h> +#include <SwifTools/HunspellChecker.h> +#include <SwifTools/SpellCheckerFactory.h> + +#ifdef HAVE_HUNSPELL +#include <hunspell/hunspell.hxx> +#else +#define HAVE_NO_SPELL_CHECK +#endif + +namespace Swift { + +SpellCheckerFactory::SpellCheckerFactory() { +} + +SpellChecker* SpellCheckerFactory::createSpellChecker() { +#ifdef HAVE_HUNSPELL + //TODO(vladv): Do something smart about finding available dictionaries, + // maybe a UI button to select language from available dictionaries? + const char* dictPath = "/usr/share/hunspell/en_US.dic"; + const char* affixPath = "/usr/share/hunspell/en_US.aff"; + return new HunspellChecker(affixPath, dictPath); +#endif +} + +} diff --git a/SwifTools/SpellCheckerFactory.h b/SwifTools/SpellCheckerFactory.h new file mode 100644 index 0000000..72ed911 --- /dev/null +++ b/SwifTools/SpellCheckerFactory.h @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2011 Voicu Vlad + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#pragma once + +#define HAVE_HUNSPELL + +namespace Swift { + class SpellChecker; + class SpellCheckerFactory { + public: + SpellCheckerFactory(); + SpellChecker* createSpellChecker(); + }; +} |