From 084b2b0afbb635aeb9a9db02081885ced08e1deb Mon Sep 17 00:00:00 2001 From: Gerald Schmidt Date: Tue, 1 Jul 2008 23:28:38 +0000 Subject: [PATCH] Update for 1.1.0.7-2 --- src/wrapaspell.cpp | 104 +++++++++++++++++++++++++++++++++++++++++++++ src/wrapaspell.h | 44 +++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100755 src/wrapaspell.cpp create mode 100755 src/wrapaspell.h diff --git a/src/wrapaspell.cpp b/src/wrapaspell.cpp new file mode 100755 index 0000000..50b1f51 --- /dev/null +++ b/src/wrapaspell.cpp @@ -0,0 +1,104 @@ +/* + * Copyright 2005-2007 Gerald Schmidt. + * + * This file is part of Xml Copy Editor. + * + * Xml Copy Editor is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * Xml Copy Editor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Xml Copy Editor; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include "wrapaspell.h" +#include "aspell.h" +#include "casehandler.h" +#include "contexthandler.h" +#include "getword.h" + +#ifdef __WXMSW__ + #include "aspellpaths.h" +#endif + +WrapAspell::WrapAspell ( std::string lang ) +{ + spell_config = new_aspell_config(); + +#ifdef __WXMSW__ + aspell_config_replace ( spell_config, "data-dir", ASPELL_DATA_PATH ); + aspell_config_replace ( spell_config, "dict-dir", ASPELL_DICT_PATH ); +#endif + + aspell_config_replace ( spell_config, "lang", lang.c_str() ); + AspellCanHaveError * possible_err = new_aspell_speller ( spell_config ); + spell_checker = 0; + if ( aspell_error_number ( possible_err ) != 0) + { + puts ( aspell_error_message ( possible_err ) ); + throw; + } + else + spell_checker = to_aspell_speller ( possible_err ); +} + +WrapAspell::~WrapAspell() +{ + delete_aspell_speller ( spell_checker ); + delete_aspell_config ( spell_config ); +} + +bool WrapAspell::checkWord ( std::string &s ) +{ + return checkWord ( (char *) s.c_str(), s.size() ); +} + +std::string WrapAspell::getSuggestion ( + std::string &s ) +{ + const AspellWordList *suggestions = aspell_speller_suggest ( spell_checker, s.c_str(), s.size() ); + AspellStringEnumeration *elements = aspell_word_list_elements ( suggestions ); + const char *word = aspell_string_enumeration_next ( elements ); // no iteration req'd + return (word) ? word : "----"; +} + +void WrapAspell::checkString ( + std::string &s, + std::vector &v, + int contextRange ) +{ + std::string suggestion; + size_t len; + char *origin, *iterator, *ptr; + origin = iterator = ( char * ) s.c_str(); + while ( ( ptr = GetWord::run ( &iterator, &len, true ) ) != NULL ) + if ( !checkWord ( ptr, len ) ) + { + ContextMatch m = ContextHandler::getContext ( + ptr, + len, + origin, + contextRange ); + + // handle suggestion + suggestion = getSuggestion ( m.match ); + m.replace.append ( suggestion ); + m.elementCount = 0; + m.offset = ptr - origin; + v.push_back ( m ); + } +} + +bool WrapAspell::checkWord ( char *s, size_t len ) +{ + return aspell_speller_check ( spell_checker, s, len ); +} + diff --git a/src/wrapaspell.h b/src/wrapaspell.h new file mode 100755 index 0000000..5d016e3 --- /dev/null +++ b/src/wrapaspell.h @@ -0,0 +1,44 @@ +/* + * Copyright 2005-2007 Gerald Schmidt. + * + * This file is part of Xml Copy Editor. + * + * Xml Copy Editor is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * Xml Copy Editor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Xml Copy Editor; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef WRAP_ASPELL_H +#define WRAP_ASPELL_H + +#include +#include +#include "contexthandler.h" +#include "aspell.h" + +class WrapAspell +{ + public: + WrapAspell ( std::string lang = "en_US" ); + ~WrapAspell(); + inline bool checkWord ( std::string &s ); + void checkString ( + std::string &s, + std::vector &v, + int contextRange ); + std::string getSuggestion ( std::string &s ); + private: + AspellConfig *spell_config; + AspellSpeller *spell_checker; + bool checkWord ( char *s, size_t len ); +}; +#endif