summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVlad Voicu <vladvoic@gmail.com>2011-11-29 02:41:07 (GMT)
committerKevin Smith <git@kismith.co.uk>2012-03-09 15:04:00 (GMT)
commitb50c56cbeafa1ea15d71829ef2f633cb212f8fcc (patch)
treec9f7a639d8c77fb4894ec4a827f9494e99afb097
parent404b087a11144c66416513929eb57c9786f41ffe (diff)
downloadswift-contrib-b50c56cbeafa1ea15d71829ef2f633cb212f8fcc.zip
swift-contrib-b50c56cbeafa1ea15d71829ef2f633cb212f8fcc.tar.bz2
Added Factory for creeating SpellCheckers
-rw-r--r--SwifTools/HunspellChecker.cpp40
-rw-r--r--SwifTools/HunspellChecker.h25
-rw-r--r--SwifTools/SConscript3
-rw-r--r--SwifTools/SpellChecker.h13
-rw-r--r--SwifTools/SpellCheckerFactory.cpp32
-rw-r--r--SwifTools/SpellCheckerFactory.h18
-rw-r--r--Swift/QtUI/QtTextEdit.cpp7
7 files changed, 126 insertions, 12 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 ac2dc6b..9a50b38 100644
--- a/SwifTools/SConscript
+++ b/SwifTools/SConscript
@@ -28,7 +28,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();
+ };
+}
diff --git a/Swift/QtUI/QtTextEdit.cpp b/Swift/QtUI/QtTextEdit.cpp
index e1fdd26..0d0f31c 100644
--- a/Swift/QtUI/QtTextEdit.cpp
+++ b/Swift/QtUI/QtTextEdit.cpp
@@ -4,6 +4,7 @@
* See Documentation/Licenses/GPLv3.txt for more information.
*/
+#include <SwifTools/SpellCheckerFactory.h>
#include <SwifTools/SpellChecker.h>
#include <Swift/QtUI/QtTextEdit.h>
@@ -16,10 +17,12 @@
namespace Swift {
-QtTextEdit::QtTextEdit(QWidget* parent) : QTextEdit(parent){
+QtTextEdit::QtTextEdit(QWidget* parent) : QTextEdit(parent) {
connect(this, SIGNAL(textChanged()), this, SLOT(handleTextChanged()));
handleTextChanged();
- checker_ = new SpellChecker();
+ SpellCheckerFactory *checkerFactory = new SpellCheckerFactory();
+ checker_ = checkerFactory->createSpellChecker();
+ delete checkerFactory;
};
QtTextEdit::~QtTextEdit() {