summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVlad Voicu <vladvoic@gmail.com>2011-11-28 16:37:32 (GMT)
committervlad <vlad@tyrion.(none)>2012-10-13 13:55:44 (GMT)
commit4ef710cfcc4aa4f171426ef55cbece00cd69af62 (patch)
tree243974bc3ae35199d7e4317bc737552e1a2a6f0b /SwifTools
parent3cf3828562d54db0ddaacfcea755b15c5b8c5e90 (diff)
downloadswift-contrib-4ef710cfcc4aa4f171426ef55cbece00cd69af62.zip
swift-contrib-4ef710cfcc4aa4f171426ef55cbece00cd69af62.tar.bz2
Initial, buggy version of a minimal spell-checker
Diffstat (limited to 'SwifTools')
-rw-r--r--SwifTools/SConscript3
-rw-r--r--SwifTools/SpellChecker.cpp52
-rw-r--r--SwifTools/SpellChecker.h25
3 files changed, 79 insertions, 1 deletions
diff --git a/SwifTools/SConscript b/SwifTools/SConscript
index 3b25269..6f49e7f 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"]
+ "LIBS": ["SwifTools", "libaspell"]
}
################################################################################
@@ -29,6 +29,7 @@ if env["SCONS_STAGE"] == "build" :
"Linkify.cpp",
"TabComplete.cpp",
"LastLineTracker.cpp",
+ "SpellChecker.cpp",
]
if swiftools_env.get("HAVE_SPARKLE", 0) :
diff --git a/SwifTools/SpellChecker.cpp b/SwifTools/SpellChecker.cpp
new file mode 100644
index 0000000..8c9aeda
--- /dev/null
+++ b/SwifTools/SpellChecker.cpp
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2011 Voicu Vlad
+ * Licensed under the Simplified BSD license.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <SwifTools/SpellChecker.h>
+
+#include <algorithm>
+#include <aspell.h>
+#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);
+}
+
+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;
+}
+
+void SpellChecker::getSuggestions(const std::string& word, std::vector<std::string>& list) {
+ const AspellWordList *alist;
+ 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));
+ }
+ }
+}
+
+}
diff --git a/SwifTools/SpellChecker.h b/SwifTools/SpellChecker.h
new file mode 100644
index 0000000..3a9ff38
--- /dev/null
+++ b/SwifTools/SpellChecker.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>
+
+#pragma once
+
+class AspellSpeller;
+class AspellConfig;
+
+namespace Swift {
+ class SpellChecker {
+ public:
+ SpellChecker();
+ bool isCorrect(const std::string& word);
+ void getSuggestions(const std::string& word, std::vector<std::string>& list);
+ private:
+ AspellSpeller* speller_;
+ AspellConfig* config_;
+ };
+}