Menu

[r130]: / src / wrapaspell.cpp  Maximize  Restore  History

Download this file

137 lines (125 with data), 4.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* 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 <iostream>
#include <cstdio>
#include <stdexcept>
#include "wrapaspell.h"
#ifdef USE_ENCHANT
#include <enchant++.h>
#endif
#include "casehandler.h"
#include "contexthandler.h"
#include "getword.h"
#ifdef __WXMSW__
#include "aspellpaths.h"
#endif
WrapAspell::WrapAspell (
std::string lang
#if !defined(USE_ENCHANT) && defined(__WXMSW__)
,
const std::string& aspellDataPathParameter,
const std::string& aspellDictPathParameter
#endif
)
{
#ifdef USE_ENCHANT
spell_broker = enchant::Broker::instance();
spell_checker = spell_broker->request_dict( lang );
#else
spell_config = new_aspell_config();
#ifdef __WXMSW__
aspell_config_replace ( spell_config, "data-dir", aspellDataPathParameter.c_str() );//ASPELL_DATA_PATH );
aspell_config_replace ( spell_config, "dict-dir", aspellDictPathParameter.c_str() );//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 );
#endif
}
WrapAspell::~WrapAspell()
{
#ifdef USE_ENCHANT
delete spell_checker;
#else
delete_aspell_speller ( spell_checker );
delete_aspell_config ( spell_config );
#endif
}
bool WrapAspell::checkWord ( const std::string &s )
{
#ifdef USE_ENCHANT
return spell_checker->check(s);
#else
return checkWord ( (char *) s.c_str(), s.size() );
#endif
}
std::string WrapAspell::getSuggestion (
std::string &s )
{
#ifdef USE_ENCHANT
std::vector<std::string> out_suggestions;
spell_checker->suggest(s, out_suggestions);
return out_suggestions.empty() ? "----" : out_suggestions[0];
#else
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 : "----";
#endif
}
void WrapAspell::checkString (
std::string &s,
std::vector<ContextMatch> &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 ( const char *s, size_t len )
{
#ifdef USE_ENCHANT
return checkWord( std::string(s, len) );
#else
return aspell_speller_check ( spell_checker, s, len );
#endif
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.