Skip to content

Commit

Permalink
Added copyHunspellVector helper and logged iconvstr errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreyhorner committed Feb 4, 2012
1 parent a91d0f7 commit 006efff
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 55 deletions.
71 changes: 33 additions & 38 deletions src/cpp/core/spelling/HunspellSpellChecker.cpp
Expand Up @@ -68,41 +68,43 @@ class HunspellSpellChecker : public SpellChecker
return Success();
}

private:

// helpers
void copyToHunspellVector(std::vector<std::string>* pVec,
char **wlst,
int len)
{
for (int i=0; i < len; i++)
{
pVec->push_back(wlst[i]);
}
pHunspell_->free_list(&wlst, len);
}

public:
Error checkSpelling(const std::string& word, bool *pCorrect)
{
std::string encoded;

Error error = iconvstrFunc_(word,"UTF-8",encoding_,false,&encoded);
if (error)
{
error = iconvstrFunc_(word,"UTF-8",encoding_,true,&encoded);
if (error)
return error;
}
*pCorrect = pHunspell_->spell(encoded.c_str());
return Success();
std::string encoded;

Error error = iconvstrFunc_(word,"UTF-8",encoding_,false,&encoded);
if (error)
return error;
*pCorrect = pHunspell_->spell(encoded.c_str());
return Success();
}

Error suggestionList(const std::string& word, std::vector<std::string>* pSug)
{
char ** wlst;
std::string encoded;

Error error = iconvstrFunc_(word,"UTF-8",encoding_,false,&encoded);
if (error)
{
error = iconvstrFunc_(word,"UTF-8",encoding_,true,&encoded);
if (error)
return error;
}
int ns = pHunspell_->suggest(&wlst,encoded.c_str());
for (int i=0; i < ns; i++)
{
pSug->push_back(wlst[i]);
}
pHunspell_->free_list(&wlst, ns);
return Success();
char ** wlst;
std::string encoded;

Error error = iconvstrFunc_(word,"UTF-8",encoding_,false,&encoded);
if (error)
return error;
int ns = pHunspell_->suggest(&wlst,encoded.c_str());
copyToHunspellVector(pSug,wlst,ns);
return Success();
}

Error analyzeWord(const std::string& word, std::vector<std::string>* pResult)
Expand All @@ -112,20 +114,13 @@ class HunspellSpellChecker : public SpellChecker

Error error = iconvstrFunc_(word,"UTF-8",encoding_,false,&encoded);
if (error)
{
error = iconvstrFunc_(word,"UTF-8",encoding_,true,&encoded);
if (error)
return error;
}
return error;
int ns = pHunspell_->analyze(&wlst,encoded.c_str());
for (int i=0; i < ns; i++)
{
pResult->push_back(wlst[i]);
}
pHunspell_->free_list(&wlst, ns);
copyToHunspellVector(pResult,wlst,ns);
return Success();
}


private:
boost::scoped_ptr<Hunspell> pHunspell_;
IconvstrFunction iconvstrFunc_;
Expand Down
44 changes: 27 additions & 17 deletions src/cpp/session/modules/SessionSpelling.cpp
Expand Up @@ -44,6 +44,13 @@ SEXP rs_checkSpelling(SEXP wordSEXP)

Error error = s_pSpellChecker->checkSpelling(word,&isCorrect);

// We'll return true here so as not to tie up the front end.
if (error)
{
LOG_ERROR(error);
isCorrect = true;
}

r::sexp::Protect rProtect;
return r::sexp::create(isCorrect, &rProtect);
}
Expand All @@ -53,7 +60,10 @@ SEXP rs_suggestionList(SEXP wordSEXP)
std::string word = r::sexp::asString(wordSEXP);
std::vector<std::string> sugs;

s_pSpellChecker->suggestionList(word,&sugs);
Error error = s_pSpellChecker->suggestionList(word,&sugs);

if (error)
LOG_ERROR(error);

r::sexp::Protect rProtect;
return r::sexp::create(sugs,&rProtect);
Expand All @@ -77,22 +87,22 @@ SEXP rs_analyzeWord(SEXP wordSEXP)
Error initialize()
{
// register rs_ensureFileHidden with R
R_CallMethodDef checkSpellingMethodDef;

checkSpellingMethodDef.name = "rs_checkSpelling" ;
checkSpellingMethodDef.fun = (DL_FUNC) rs_checkSpelling ;
checkSpellingMethodDef.numArgs = 1;
r::routines::addCallMethod(checkSpellingMethodDef);

checkSpellingMethodDef.name = "rs_suggestionList" ;
checkSpellingMethodDef.fun = (DL_FUNC) rs_suggestionList ;
checkSpellingMethodDef.numArgs = 1;
r::routines::addCallMethod(checkSpellingMethodDef);

checkSpellingMethodDef.name = "rs_analyzeWord" ;
checkSpellingMethodDef.fun = (DL_FUNC) rs_analyzeWord ;
checkSpellingMethodDef.numArgs = 1;
r::routines::addCallMethod(checkSpellingMethodDef);
R_CallMethodDef methodDef;

methodDef.name = "rs_checkSpelling" ;
methodDef.fun = (DL_FUNC) rs_checkSpelling ;
methodDef.numArgs = 1;
r::routines::addCallMethod(methodDef);

methodDef.name = "rs_suggestionList" ;
methodDef.fun = (DL_FUNC) rs_suggestionList ;
methodDef.numArgs = 1;
r::routines::addCallMethod(methodDef);

methodDef.name = "rs_analyzeWord" ;
methodDef.fun = (DL_FUNC) rs_analyzeWord ;
methodDef.numArgs = 1;
r::routines::addCallMethod(methodDef);

// initialize the spell checker
using namespace core::spelling;
Expand Down

0 comments on commit 006efff

Please sign in to comment.