From 404b087a11144c66416513929eb57c9786f41ffe Mon Sep 17 00:00:00 2001
From: Vlad Voicu <vladvoic@gmail.com>
Date: Tue, 29 Nov 2011 03:15:18 +0200
Subject: Modified the Spellchecker to use hunspell instead of aspell


diff --git a/SwifTools/SConscript b/SwifTools/SConscript
index 3e1a105..ac2dc6b 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_;
 	};
 }
-- 
cgit v0.10.2-6-g49f6