apply spelling corrections from the context menu.

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1274 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2008-12-30 19:51:41 +00:00
parent 2c824c9507
commit e36dc6e6cb
9 changed files with 759 additions and 636 deletions
+24 -8
View File
@@ -71,8 +71,7 @@ void SpellChecker::destroyAll() {
// ----------------------------------------------------------------------------- : Spell checker : use
bool SpellChecker::spell(const String& word) {
if (word.empty()) return true; // empty word is okay
bool SpellChecker::convert_encoding(const String& word, CharBuffer& out) {
// fix curly quotes, especially apstrophes
String fixed;
FOR_EACH_CONST(c,word) {
@@ -96,16 +95,20 @@ bool SpellChecker::spell(const String& word) {
}
}
// convert encoding
#ifdef UNICODE
wxCharBuffer str = fixed.mb_str(encoding);
#else
wxCharBuffer str = fixed.mb_str(encoding);
#endif
if (*str == '\0') {
out = fixed.mb_str(encoding);
if (*out == '\0') {
// If encoding fails we get an empty string, since the word was not empty this can never happen
// words that can't be encoded are not in the dictionary, so they are wrong.
return false;
} else {
return true;
}
}
bool SpellChecker::spell(const String& word) {
if (word.empty()) return true; // empty word is okay
CharBuffer str;
if (!convert_encoding(word,str)) return false;
return Hunspell::spell(str);
}
@@ -115,3 +118,16 @@ bool SpellChecker::spell_with_punctuation(const String& word) {
if (start >= end) return true; // just punctuation is wrong
return spell(word.substr(start,end-start));
}
void SpellChecker::suggest(const String& word, vector<String>& suggestions_out) {
CharBuffer str;
if (!convert_encoding(word,str)) return;
// call Hunspell
char** suggestions;
int num_suggestions = Hunspell::suggest(&suggestions, str);
// copy sugestions
for (int i = 0 ; i < num_suggestions ; ++i) {
suggestions_out.push_back(String(suggestions[i],encoding));
}
free(suggestions);
}
+10
View File
@@ -14,6 +14,12 @@
DECLARE_POINTER_TYPE(SpellChecker);
#ifdef UNICODE
typedef wxCharBuffer CharBuffer;
#else
typedef char* CharBuffer;
#endif
// ----------------------------------------------------------------------------- : Spell checker
/// A spelling checker for a particular language
@@ -33,9 +39,13 @@ class SpellChecker : public Hunspell, public IntrusivePtrBase<SpellChecker> {
/// Check the spelling of a single word, ignore punctuation
bool spell_with_punctuation(const String& word);
/// Give spelling suggestions
void suggest(const String& word, vector<String>& suggestions_out);
private:
/// Convert between String and dictionary encoding
wxCSConv encoding;
bool convert_encoding(const String& word, CharBuffer& out);
SpellChecker(const char* aff_path, const char* dic_path);
static map<String,SpellCheckerP> spellers; //< Cached checkers for each language
+7 -1
View File
@@ -120,8 +120,14 @@ enum ChildMenuID {
, ID_FORMAT_REMINDER
, ID_INSERT_SYMBOL
// Spelling errors
, ID_SPELLING_ADD_TO_DICT = 6301
, ID_SPELLING_NO_SUGGEST
, ID_SPELLING_SUGGEST
, ID_SPELLING_SUGGEST_MAX = 6399
// Graph menu
, ID_GRAPH_PIE = 6301 // corresponds to GraphType
, ID_GRAPH_PIE = 6401 // corresponds to GraphType
, ID_GRAPH_BAR
, ID_GRAPH_STACK
, ID_GRAPH_SCATTER