From 51d10cff836c10f51011891d60e0d57fc1410224 Mon Sep 17 00:00:00 2001 From: "Alexander.Trofimov" Date: Wed, 22 Jul 2015 10:56:57 +0000 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D0=BB=20no?= =?UTF-8?q?dehun=20(2.0.6).=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=D1=81=D1=8F=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20isCorrect.=20?= =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BB=20=D0=BA=D0=BE=D0=BC=D0=BF=D0=B8?= =?UTF-8?q?=D0=BB=D1=8F=D1=86=D0=B8=D1=8E=20=D1=81=D0=B2=D0=BE=D0=B8=D1=85?= =?UTF-8?q?=20=D0=B8=D1=81=D1=85=D0=BE=D0=B4=D0=BD=D0=B8=D0=BA=D0=BE=D0=B2?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: svn://192.168.3.15/activex/AVS/Sources/TeamlabOffice/trunk/nodeJSProjects@63640 954022d7-b5bf-4e40-9824-e11837661b57 --- .../install/BuildNodeJSSpellCheck.bat | 21 - .../nodehun/src/post0.12.0/nodehun.cpp | 586 ----------------- .../nodehun/src/pre0.12.0/nodehun.cpp | 615 ------------------ SpellChecker/package.json | 2 +- SpellChecker/sources/spellCheck.js | 6 +- 5 files changed, 4 insertions(+), 1226 deletions(-) delete mode 100644 SpellChecker/install/BuildNodeJSSpellCheck.bat delete mode 100644 SpellChecker/nodehun/src/post0.12.0/nodehun.cpp delete mode 100644 SpellChecker/nodehun/src/pre0.12.0/nodehun.cpp diff --git a/SpellChecker/install/BuildNodeJSSpellCheck.bat b/SpellChecker/install/BuildNodeJSSpellCheck.bat deleted file mode 100644 index 10c33e40..00000000 --- a/SpellChecker/install/BuildNodeJSSpellCheck.bat +++ /dev/null @@ -1,21 +0,0 @@ -ECHO OFF - -SET RUN_FOLDER=%CD% - -CD /D %~dp0..\ || exit /b 1 - -ECHO. -ECHO ---------------------------------------- -ECHO Build node.js module spellCheck (nodehun) -ECHO ---------------------------------------- - -call npm list -g node-gyp || call npm install -g node-gyp || exit /b 1 - -XCOPY /S nodehun node_modules\nodehun\ /Y - -cd /D node_modules\nodehun || exit /b 1 -call node-gyp rebuild || exit /b 1 - -CD /D %RUN_FOLDER% || exit /b 1 - -exit /b 0 diff --git a/SpellChecker/nodehun/src/post0.12.0/nodehun.cpp b/SpellChecker/nodehun/src/post0.12.0/nodehun.cpp deleted file mode 100644 index ca448be5..00000000 --- a/SpellChecker/nodehun/src/post0.12.0/nodehun.cpp +++ /dev/null @@ -1,586 +0,0 @@ -#include "nodehun.hpp" - -using namespace v8; - -Persistent Nodehun::SpellDictionary::constructor; - -void Nodehun::SpellDictionary::Init(Handle exports, Handle module) -{ - Isolate* isolate = Isolate::GetCurrent(); - Local tpl = FunctionTemplate::New(isolate, New); - tpl->SetClassName(String::NewFromUtf8(isolate, "NodehunDictionary")); - tpl->InstanceTemplate()->SetInternalFieldCount(1); - //static - NODE_SET_METHOD(tpl, "createNewNodehun" , createNewNodehun); - //prototype - NODE_SET_PROTOTYPE_METHOD(tpl, "spellSuggest", spellSuggest); - NODE_SET_PROTOTYPE_METHOD(tpl, "spellSuggestions", spellSuggestions); - NODE_SET_PROTOTYPE_METHOD(tpl, "addDictionary", addDictionary); - NODE_SET_PROTOTYPE_METHOD(tpl, "addWord", addWord); - NODE_SET_PROTOTYPE_METHOD(tpl, "removeWord", removeWord); - NODE_SET_PROTOTYPE_METHOD(tpl, "stem", stem); - NODE_SET_PROTOTYPE_METHOD(tpl, "generate", generate); - NODE_SET_PROTOTYPE_METHOD(tpl, "analyze", analyze); - - constructor.Reset(isolate, tpl->GetFunction()); - module->Set(String::NewFromUtf8(isolate, "exports"), tpl->GetFunction()); -} - -void Nodehun::SpellDictionary::createNewNodehun(const FunctionCallbackInfo& args) -{ - Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); - - int argl = args.Length(); - if(argl > 2 && args[2]->IsFunction()) { - Local callback = Local::Cast(args[2]); - const unsigned argc = 2; - Local argv[argc]; - argv[1] = Local::New(isolate, Null(isolate)); - if(!node::Buffer::HasInstance(args[0])) { - argv[0] = Exception::TypeError(String::NewFromUtf8(isolate, "First argument must be a buffer")); - callback->Call(isolate->GetCurrentContext()->Global(), argc, argv); - } - else if(!node::Buffer::HasInstance(args[1])) { - argv[0] = Exception::TypeError(String::NewFromUtf8(isolate, "Second argument must be a buffer")); - callback->Call(isolate->GetCurrentContext()->Global(), argc, argv); - } - else { - Nodehun::NodehunData* nodeData = new Nodehun::NodehunData(); - nodeData->isolate = isolate; - nodeData->callback.Reset(isolate, callback); - nodeData->aff = new char[node::Buffer::Length(args[0])]; - strcpy(nodeData->aff, node::Buffer::Data(args[0].As())); - nodeData->dict = new char[node::Buffer::Length(args[1])]; - strcpy(nodeData->dict, node::Buffer::Data(args[1].As())); - nodeData->request.data = nodeData; - uv_queue_work(uv_default_loop(), &nodeData->request, - Nodehun::SpellDictionary::createNewNodehunWork, Nodehun::SpellDictionary::createNewNodehunFinish); - } - } - args.GetReturnValue().SetUndefined(); -} - -void Nodehun::SpellDictionary::createNewNodehunWork(uv_work_t* request) -{ - Nodehun::NodehunData* nodeData = static_cast(request->data); - nodeData->obj = new Hunspell(nodeData->aff, nodeData->dict, NULL, true); - delete nodeData->aff; - delete nodeData->dict; -} - -void Nodehun::SpellDictionary::createNewNodehunFinish(uv_work_t* request, int i) -{ - Nodehun::NodehunData* nodeData = static_cast(request->data); - Isolate *isolate = nodeData->isolate;; - HandleScope scope(isolate); - const unsigned argc = 2; - Local argv[argc]; - Handle ext = External::New(isolate, nodeData->obj); - argv[0] = Local::New(isolate, Null(isolate)); - Local cons = Local::New(isolate, constructor); - argv[1] = cons->NewInstance(1, &ext); - Local cb = Local::New(isolate, nodeData->callback); - cb->Call(isolate->GetCurrentContext()->Global(), argc, argv); - nodeData->callback.Reset(); - delete nodeData; -} - - -Nodehun::SpellDictionary::SpellDictionary(const char *affbuf, const char *dictbuf) -{ - spellClass = new Hunspell(affbuf, dictbuf, NULL, true); -} - -Nodehun::SpellDictionary::SpellDictionary(Hunspell *obj) -{ - spellClass = obj; -} - -void Nodehun::SpellDictionary::New(const FunctionCallbackInfo& args) -{ - Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); - int argl = args.Length(); - if (!args.IsConstructCall()){ - isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate, "Use the new operator to create an instance of this object."))); - return; - } - if(argl > 0 && args[0]->IsExternal()) { - Local ext = Local::Cast(args[0]); - void *ptr = ext->Value(); - Nodehun::SpellDictionary *obj = new Nodehun::SpellDictionary(static_cast(ptr)); - uv_rwlock_init(&(obj->rwlock)); - obj->Wrap(args.This()); - } - else { - if(argl < 2){ - isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate, "Constructor requires two arguments."))); - return; - } - if(!node::Buffer::HasInstance(args[0])){ - isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "First argument must be a buffer"))); - return; - } - if(!node::Buffer::HasInstance(args[1])){ - isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Second argument must be a buffer"))); - return; - } - - Nodehun::SpellDictionary *obj = new Nodehun::SpellDictionary(node::Buffer::Data(args[0].As()), node::Buffer::Data(args[1].As())); - uv_rwlock_init(&(obj->rwlock)); - obj->Wrap(args.This()); - } - args.GetReturnValue().Set(args.This()); -} - -void Nodehun::SpellDictionary::spellSuggest(const FunctionCallbackInfo& args) -{ - Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); - int argl = args.Length(); - if(argl > 1 && args[1]->IsFunction()) { - Local callback = Local::Cast(args[1]); - const unsigned argc = 3; - Local argv[argc]; - argv[1] = Local::New(isolate, Null(isolate)); - argv[2] = Local::New(isolate, Null(isolate)); - if(argl < 1 || !args[0]->IsString()) { - argv[0] = Exception::TypeError(String::NewFromUtf8(isolate, "First argument must be a string")); - callback->Call(isolate->GetCurrentContext()->Global(), argc, argv); - } - else { - Nodehun::SpellDictionary* obj = ObjectWrap::Unwrap(args.This()); - Nodehun::SpellData* spellData = new Nodehun::SpellData(); - String::Utf8Value arg0(args[0]->ToString()); - - spellData->isolate = isolate; - spellData->callback.Reset(isolate, callback); - spellData->request.data = spellData; - spellData->word.append(*arg0); - spellData->obj = obj; - spellData->multiple = false; - uv_queue_work(uv_default_loop(), &spellData->request, - Nodehun::SpellDictionary::checkSuggestions, Nodehun::SpellDictionary::sendSuggestions); - } - } - args.GetReturnValue().SetUndefined(); -} - -void Nodehun::SpellDictionary::spellSuggestions(const FunctionCallbackInfo& args) -{ - Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); - int argl = args.Length(); - if(argl > 1 && args[1]->IsFunction()) { - Local callback = Local::Cast(args[1]); - const unsigned argc = 3; - Local argv[argc]; - argv[1] = Local::New(isolate, Null(isolate)); - argv[2] = Local::New(isolate, Null(isolate)); - if(argl < 1 || !args[0]->IsString()) { - argv[0] = Exception::TypeError(String::NewFromUtf8(isolate, "First argument must be a string")); - callback->Call(isolate->GetCurrentContext()->Global(), argc, argv); - } - else { - Nodehun::SpellDictionary* obj = ObjectWrap::Unwrap(args.Holder()); - Nodehun::SpellData* spellData = new Nodehun::SpellData(); - String::Utf8Value arg0(args[0]->ToString()); - - spellData->isolate = isolate; - spellData->callback.Reset(isolate, callback); - spellData->request.data = spellData; - spellData->word.append(*arg0); - spellData->obj = obj; - spellData->multiple = true; - uv_queue_work(uv_default_loop(), &spellData->request, - Nodehun::SpellDictionary::checkSuggestions, Nodehun::SpellDictionary::sendSuggestions); - } - } - args.GetReturnValue().SetUndefined(); -} - -void Nodehun::SpellDictionary::checkSuggestions(uv_work_t* request) -{ - Nodehun::SpellData* spellData = static_cast(request->data); - uv_rwlock_rdlock(&(spellData->obj->rwlock)); - spellData->wordCorrect = spellData->obj->spellClass->spell(spellData->word.c_str()); - if (!spellData->wordCorrect && spellData->multiple) - spellData->numSuggest = spellData->obj->spellClass->suggest(&(spellData->suggestions), spellData->word.c_str()); - else - spellData->numSuggest = 0; - uv_rwlock_rdunlock(&(spellData->obj->rwlock)); -} - -void Nodehun::SpellDictionary::sendSuggestions(uv_work_t* request, int i) -{ - Nodehun::SpellData* spellData = static_cast(request->data); - Isolate* isolate = spellData->isolate; - HandleScope scope(isolate); - - const unsigned argc = 4; - Local argv[argc]; - argv[0] = Local::New(isolate, Null(isolate)); - argv[1] = Local::New(isolate, Boolean::New(isolate, spellData->wordCorrect)); - argv[3] = Local::New(isolate, String::NewFromUtf8(isolate, spellData->word.c_str())); - if(spellData->wordCorrect || spellData->numSuggest == 0) { - if(spellData->multiple) - argv[2] = Array::New(isolate, 0); - else - argv[2] = Local::New(isolate, Null(isolate)); - } - else if(spellData->numSuggest > 0) { - if(spellData->multiple) { - Local suglist = Array::New(isolate, spellData->numSuggest); - for(int t = 0; t < spellData->numSuggest; t++) - suglist->Set(t,String::NewFromUtf8(isolate, spellData->suggestions[t])); - argv[2] = suglist; - } - else { - argv[2] = String::NewFromUtf8(isolate, spellData->suggestions[0]); - } - } - spellData->obj->spellClass->free_list(&(spellData->suggestions), spellData->numSuggest); - Local cb = Local::New(isolate, spellData->callback); - cb->Call(isolate->GetCurrentContext()->Global(), argc, argv); - spellData->callback.Reset(); - delete spellData; -} - -void Nodehun::SpellDictionary::addDictionary(const FunctionCallbackInfo& args) -{ - Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); - int argl = args.Length(); - if(argl > 0) { - Nodehun::SpellDictionary* obj = ObjectWrap::Unwrap(args.Holder()); - Nodehun::DictData* dictData = new Nodehun::DictData(); - dictData->callbackExists = false; - if(argl > 1 && args[1]->IsFunction()) { - Local callback = Local::Cast(args[1]); - const unsigned argc = 1; - Local argv[argc]; - if(!node::Buffer::HasInstance(args[0])) { - argv[0] = Exception::TypeError(String::NewFromUtf8(isolate, "First argument must be a buffer")); - callback->Call(isolate->GetCurrentContext()->Global(), argc, argv); - delete dictData; - args.GetReturnValue().SetUndefined(); - return; - } - dictData->callback.Reset(isolate, callback); - dictData->callbackExists = true; - } - if(!node::Buffer::HasInstance(args[0])) { - delete dictData; - isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "First argument must be a buffer"))); - return; - } - dictData->isolate = isolate; - dictData->dict = new char[node::Buffer::Length(args[0])]; - strcpy(dictData->dict, node::Buffer::Data(args[0].As())); - dictData->obj = obj; - dictData->request.data = dictData; - - uv_queue_work(uv_default_loop(), &dictData->request, - Nodehun::SpellDictionary::addDictionaryWork, Nodehun::SpellDictionary::addDictionaryFinish); - } - args.GetReturnValue().SetUndefined(); -} - -void Nodehun::SpellDictionary::addDictionaryWork(uv_work_t* request) -{ - Nodehun::DictData* dictData = static_cast(request->data); - uv_rwlock_wrlock(&(dictData->obj->rwlock)); - int status = dictData->obj->spellClass->add_dic(dictData->dict); - dictData->success = status == 0; - uv_rwlock_wrunlock(&(dictData->obj->rwlock)); -} - -void Nodehun::SpellDictionary::addDictionaryFinish(uv_work_t* request, int i) -{ - Nodehun::DictData* dictData = static_cast(request->data); - Isolate* isolate = dictData->isolate; - HandleScope scope(isolate); - - if(dictData->callbackExists) { - const unsigned argc = 1; - Local argv[argc]; - argv[0] = dictData->success ? Local::New(isolate, Null(isolate)) : Exception::TypeError(String::NewFromUtf8(isolate, "There was an error adding the dictionary to the nodehun class, because the buffer was deformed.")); - Local cb = Local::New(isolate, dictData->callback); - cb->Call(isolate->GetCurrentContext()->Global(), argc, argv); - dictData->callback.Reset(); - } - delete dictData->dict; - delete dictData; -} - -void Nodehun::SpellDictionary::addWord(const FunctionCallbackInfo& args) -{ - Nodehun::SpellDictionary::addRemoveWordInit(args, false); -} - -void Nodehun::SpellDictionary::removeWord(const FunctionCallbackInfo& args) -{ - Nodehun::SpellDictionary::addRemoveWordInit(args, true); -} - -void Nodehun::SpellDictionary::addRemoveWordInit(const FunctionCallbackInfo& args, bool remove) -{ - Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); - int argl = args.Length(); - if(argl > 0) { - Nodehun::SpellDictionary* obj = ObjectWrap::Unwrap(args.Holder()); - String::Utf8Value arg0(args[0]->ToString()); - Nodehun::WordData* wordData = new Nodehun::WordData(); - wordData->callbackExists = false; - if(argl > 1 && args[1]->IsFunction()) { - Local callback = Local::New(isolate, Local::Cast(args[1])); - const unsigned argc = 2; - Local argv[argc]; - if (!args[0]->IsString()) { - argv[0] = Exception::TypeError(String::NewFromUtf8(isolate, "First argument must be a string")); - argv[1] = Local::New(isolate, Null(isolate)); - callback->Call(isolate->GetCurrentContext()->Global(), argc, argv); - delete wordData; - args.GetReturnValue().SetUndefined(); - return; - } - wordData->callback.Reset(isolate, callback); - wordData->callbackExists = true; - } - else if (!args[0]->IsString()) { - delete wordData; - isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "First argument must be a string."))); - return; - } - wordData->isolate = isolate; - wordData->word.append(*arg0); - wordData->removeWord = remove; - wordData->obj = obj; - wordData->request.data = wordData; - uv_queue_work(uv_default_loop(), &wordData->request, - Nodehun::SpellDictionary::addRemoveWordWork, Nodehun::SpellDictionary::addRemoveWordFinish); - - } - args.GetReturnValue().SetUndefined(); -} - -void Nodehun::SpellDictionary::addRemoveWordWork(uv_work_t* request) -{ - Nodehun::WordData* wordData = static_cast(request->data); - uv_rwlock_wrlock(&(wordData->obj->rwlock)); - int status; - if(wordData->removeWord) - status = wordData->obj->spellClass->remove(wordData->word.c_str()); - else - status = wordData->obj->spellClass->add(wordData->word.c_str()); - wordData->success = status == 0; - uv_rwlock_wrunlock(&(wordData->obj->rwlock)); -} - -void Nodehun::SpellDictionary::addRemoveWordFinish(uv_work_t* request, int i) -{ - Nodehun::WordData* wordData = static_cast(request->data); - Isolate* isolate = wordData->isolate; - HandleScope scope(isolate); - - if(wordData->callbackExists) { - const unsigned argc = 2; - Local argv[argc]; - argv[0] = wordData->success ? Local::New(isolate, Null(isolate)) : Exception::TypeError(String::NewFromUtf8(isolate, "There was an error changing the status of the word. The dictionary may be corrupted, or the word may be malfored.")); - argv[1] = wordData->success ? Local::New(isolate, String::NewFromUtf8(isolate, wordData->word.c_str())) : Local::New(isolate, Null(isolate)); - Local cb = Local::New(isolate, wordData->callback); - cb->Call(isolate->GetCurrentContext()->Global(), argc, argv); - wordData->callback.Reset(); - } - delete wordData; -} - -void Nodehun::SpellDictionary::stem(const FunctionCallbackInfo& args) -{ - Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); - int argl = args.Length(); - if(argl > 1 && args[1]->IsFunction()) { - Local callback = Local::Cast(args[1]); - if (!args[0]->IsString()) { - const unsigned argc = 2; - Local argv[argc]; - argv[0] = Exception::TypeError(String::NewFromUtf8(isolate, "First argument must be a string")); - argv[1] = Local::New(isolate, Null(isolate)); - callback->Call(isolate->GetCurrentContext()->Global(), argc, argv); - } - else{ - Nodehun::SpellDictionary* obj = ObjectWrap::Unwrap(args.Holder()); - Nodehun::StemData* stemData = new Nodehun::StemData(); - v8::String::Utf8Value arg0(args[0]->ToString()); - stemData->isolate = isolate; - stemData->word.append(*arg0); - stemData->callback.Reset(isolate, callback); - stemData->obj = obj; - stemData->request.data = stemData; - uv_queue_work(uv_default_loop(), &stemData->request, - Nodehun::SpellDictionary::stemWork, Nodehun::SpellDictionary::stemFinish); - } - } - args.GetReturnValue().SetUndefined(); -} - -void Nodehun::SpellDictionary::stemWork(uv_work_t* request) -{ - Nodehun::StemData* stemData = static_cast(request->data); - uv_rwlock_rdlock(&(stemData->obj->rwlock)); - stemData->numResults = stemData->obj->spellClass->stem(&stemData->results, stemData->word.c_str()); - uv_rwlock_rdunlock(&(stemData->obj->rwlock)); -} - -void Nodehun::SpellDictionary::stemFinish(uv_work_t* request, int i) -{ - Nodehun::StemData* stemData = static_cast(request->data); - Isolate* isolate = stemData->isolate; - HandleScope scope(isolate); - - const unsigned int argc = 2; - Local argv[argc]; - Local suglist = Array::New(isolate, stemData->numResults); - - for(int t = 0; t < stemData->numResults; t++) - suglist->Set(t,String::NewFromUtf8(isolate, stemData->results[t])); - stemData->obj->spellClass->free_list(&stemData->results,stemData->numResults); - argv[0] = Local::New(isolate, Null(isolate)); - argv[1] = suglist; - Local cb = Local::New(isolate, stemData->callback); - cb->Call(isolate->GetCurrentContext()->Global(), argc, argv); - stemData->callback.Reset(); - delete stemData; -} - -void Nodehun::SpellDictionary::generate(const FunctionCallbackInfo& args) -{ - Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); - int argl = args.Length(); - if(argl > 2 && args[2]->IsFunction()) { - Local callback = Local::Cast(args[2]); - if (!args[0]->IsString()) { - const unsigned argc = 2; - Local argv[argc]; - argv[0] = Exception::TypeError(String::NewFromUtf8(isolate, "First argument must be a string")); - argv[1] = Local::New(isolate, Null(isolate)); - callback->Call(isolate->GetCurrentContext()->Global(), argc, argv); - } - else if (!args[1]->IsString()) { - const unsigned argc = 2; - Local argv[argc]; - argv[0] = Exception::TypeError(String::NewFromUtf8(isolate, "Second argument must be a string")); - argv[1] = Local::New(isolate, Null(isolate)); - callback->Call(isolate->GetCurrentContext()->Global(), argc, argv); - } - else{ - Nodehun::SpellDictionary* obj = ObjectWrap::Unwrap(args.Holder()); - Nodehun::GenerateData* generateData = new Nodehun::GenerateData(); - v8::String::Utf8Value arg0(args[0]->ToString()); - v8::String::Utf8Value arg1(args[1]->ToString()); - generateData->isolate = isolate; - generateData->word.append(*arg0); - generateData->word2.append(*arg1); - generateData->callback.Reset(isolate, callback); - generateData->obj = obj; - generateData->request.data = generateData; - uv_queue_work(uv_default_loop(), &generateData->request, - Nodehun::SpellDictionary::generateWork, Nodehun::SpellDictionary::generateFinish); - } - } - args.GetReturnValue().SetUndefined(); -} - -void Nodehun::SpellDictionary::generateWork(uv_work_t* request) -{ - Nodehun::GenerateData* generateData = static_cast(request->data); - uv_rwlock_rdlock(&(generateData->obj->rwlock)); - generateData->numResults = generateData->obj->spellClass->generate(&generateData->results, generateData->word.c_str(), generateData->word2.c_str()); - uv_rwlock_rdunlock(&(generateData->obj->rwlock)); -} - -void Nodehun::SpellDictionary::generateFinish(uv_work_t* request, int i) -{ - Nodehun::GenerateData* generateData = static_cast(request->data); - Isolate* isolate = generateData->isolate; - HandleScope scope(isolate); - - const unsigned int argc = 2; - Local argv[argc]; - Local suglist = Array::New(isolate, generateData->numResults); - - for(int t = 0; t < generateData->numResults; t++) - suglist->Set(t,String::NewFromUtf8(isolate, generateData->results[t])); - generateData->obj->spellClass->free_list(&generateData->results,generateData->numResults); - argv[0] = Local::New(isolate, Null(isolate)); - argv[1] = suglist; - Local cb = Local::New(isolate, generateData->callback); - cb->Call(isolate->GetCurrentContext()->Global(), argc, argv); - generateData->callback.Reset(); - delete generateData; -} - -void Nodehun::SpellDictionary::analyze(const FunctionCallbackInfo& args) -{ - Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); - int argl = args.Length(); - if(argl > 1 && args[1]->IsFunction()) { - Local callback = Local::Cast(args[1]); - if (!args[0]->IsString()) { - const unsigned argc = 2; - Local argv[argc]; - argv[0] = Exception::TypeError(String::NewFromUtf8(isolate, "First argument must be a string")); - argv[1] = Local::New(isolate, Null(isolate)); - callback->Call(isolate->GetCurrentContext()->Global(), argc, argv); - } - else{ - Nodehun::SpellDictionary* obj = ObjectWrap::Unwrap(args.Holder()); - Nodehun::AnalyzeData* analyzeData = new Nodehun::AnalyzeData(); - v8::String::Utf8Value arg0(args[0]->ToString()); - analyzeData->isolate = isolate; - analyzeData->word.append(*arg0); - analyzeData->callback.Reset(isolate, callback); - analyzeData->obj = obj; - analyzeData->request.data = analyzeData; - uv_queue_work(uv_default_loop(), &analyzeData->request, - Nodehun::SpellDictionary::analyzeWork, Nodehun::SpellDictionary::analyzeFinish); - } - } - args.GetReturnValue().SetUndefined(); -} - -void Nodehun::SpellDictionary::analyzeWork(uv_work_t* request) -{ - Nodehun::AnalyzeData* analyzeData = static_cast(request->data); - uv_rwlock_rdlock(&(analyzeData->obj->rwlock)); - analyzeData->numResults = analyzeData->obj->spellClass->analyze(&analyzeData->results, analyzeData->word.c_str()); - uv_rwlock_rdunlock(&(analyzeData->obj->rwlock)); -} - -void Nodehun::SpellDictionary::analyzeFinish(uv_work_t* request, int i) -{ - Nodehun::AnalyzeData* analyzeData = static_cast(request->data); - Isolate* isolate = analyzeData->isolate; - HandleScope scope(isolate); - - const unsigned int argc = 2; - Local argv[argc]; - Local suglist = Array::New(isolate, analyzeData->numResults); - - for(int t = 0; t < analyzeData->numResults; t++) - suglist->Set(t,String::NewFromUtf8(isolate, analyzeData->results[t])); - analyzeData->obj->spellClass->free_list(&analyzeData->results,analyzeData->numResults); - argv[0] = Local::New(isolate, Null(isolate)); - argv[1] = suglist; - Local cb = Local::New(isolate, analyzeData->callback); - cb->Call(isolate->GetCurrentContext()->Global(), argc, argv); - analyzeData->callback.Reset(); - delete analyzeData; -} - -NODE_MODULE(nodehun, Nodehun::SpellDictionary::Init); diff --git a/SpellChecker/nodehun/src/pre0.12.0/nodehun.cpp b/SpellChecker/nodehun/src/pre0.12.0/nodehun.cpp deleted file mode 100644 index 37a4d4f5..00000000 --- a/SpellChecker/nodehun/src/pre0.12.0/nodehun.cpp +++ /dev/null @@ -1,615 +0,0 @@ -#include "nodehun.hpp" - -using namespace v8; -using node::Buffer; - -Persistent Nodehun::SpellDictionary::constructor; - -void Nodehun::SpellDictionary::Init(Handle exports, Handle module) -{ - HandleScope scope; - - Local tpl = FunctionTemplate::New(New); - - constructor = Persistent::New(tpl); - constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(String::NewSymbol("NodehunDictionary")); - //static - NODE_SET_METHOD(constructor, "createNewNodehun" , createNewNodehun); - //prototype - NODE_SET_PROTOTYPE_METHOD(constructor, "spellSuggest", spellSuggest); - NODE_SET_PROTOTYPE_METHOD(constructor, "spellSuggestions", spellSuggestions); - NODE_SET_PROTOTYPE_METHOD(constructor, "addDictionary", addDictionary); - NODE_SET_PROTOTYPE_METHOD(constructor, "addWord", addWord); - NODE_SET_PROTOTYPE_METHOD(constructor, "removeWord", removeWord); - NODE_SET_PROTOTYPE_METHOD(constructor, "stem", stem); - NODE_SET_PROTOTYPE_METHOD(constructor, "generate", generate); - NODE_SET_PROTOTYPE_METHOD(constructor, "analyze", analyze); - - module->Set(String::NewSymbol("exports"), constructor->GetFunction()); -} - -Handle Nodehun::SpellDictionary::createNewNodehun(const v8::Arguments& args) -{ - HandleScope scope; - int argl = args.Length(); - if(argl > 2 && args[2]->IsFunction()) { - Persistent callback = Persistent::New(Local::Cast(args[2])); - const unsigned argc = 2; - Local argv[argc]; - argv[1] = Local::New(Null()); - if(!Buffer::HasInstance(args[0])) { - TryCatch try_catch; - argv[0] = Exception::TypeError(String::New("First argument must be a buffer")); - callback->Call(Context::GetCurrent()->Global(), argc, argv); - if (try_catch.HasCaught()) - node::FatalException(try_catch); - callback.Dispose(); - } - else if(!Buffer::HasInstance(args[1])) { - TryCatch try_catch; - argv[0] = Exception::TypeError(String::New("Second argument must be a buffer")); - callback->Call(Context::GetCurrent()->Global(), argc, argv); - if (try_catch.HasCaught()) - node::FatalException(try_catch); - callback.Dispose(); - } - else { - Nodehun::NodehunData* nodeData = new Nodehun::NodehunData(); - nodeData->callback = callback; - nodeData->aff = new char[Buffer::Length(args[0])]; - strcpy(nodeData->aff, Buffer::Data(args[0].As())); - nodeData->dict = new char[Buffer::Length(args[1])]; - strcpy(nodeData->dict, Buffer::Data(args[1].As())); - nodeData->request.data = nodeData; - uv_queue_work(uv_default_loop(), &nodeData->request, - Nodehun::SpellDictionary::createNewNodehunWork, Nodehun::SpellDictionary::createNewNodehunFinish); - } - } - return scope.Close(Undefined()); -} - -void Nodehun::SpellDictionary::createNewNodehunWork(uv_work_t* request) -{ - Nodehun::NodehunData* nodeData = static_cast(request->data); - nodeData->obj = new Hunspell(nodeData->aff, nodeData->dict, NULL, true); - delete nodeData->aff; - delete nodeData->dict; -} - -void Nodehun::SpellDictionary::createNewNodehunFinish(uv_work_t* request, int i) -{ - HandleScope scope; - Nodehun::NodehunData* nodeData = static_cast(request->data); - const unsigned argc = 2; - Local argv[argc]; - Handle ext = External::New(nodeData->obj); - argv[0] = Local::New(Null()); - argv[1] = constructor->GetFunction()->NewInstance(1, &ext); - TryCatch try_catch; - nodeData->callback->Call(Context::GetCurrent()->Global(), argc, argv); - if (try_catch.HasCaught()) - node::FatalException(try_catch); - nodeData->callback.Dispose(); - delete nodeData; -} - - -Nodehun::SpellDictionary::SpellDictionary(const char *affbuf, const char *dictbuf) -{ - spellClass = new Hunspell(affbuf, dictbuf, NULL, true); -} - -Nodehun::SpellDictionary::SpellDictionary(Hunspell *obj) -{ - spellClass = obj; -} - -Handle Nodehun::SpellDictionary::New(const Arguments& args) -{ - HandleScope scope; - int argl = args.Length(); - if (!args.IsConstructCall()) - return scope.Close(ThrowException(Exception::Error(String::New("Use the new operator to create an instance of this object.")))); - if(argl > 0 && args[0]->IsExternal()) { - Local ext = Local::Cast(args[0]); - void *ptr = ext->Value(); - Nodehun::SpellDictionary *obj = new Nodehun::SpellDictionary(static_cast(ptr)); - uv_rwlock_init(&(obj->rwlock)); - obj->Wrap(args.This()); - } - else { - if(argl < 2) - return scope.Close(ThrowException(Exception::Error(String::New("Constructor requires two arguments.")))); - if(!Buffer::HasInstance(args[0])) - return scope.Close(ThrowException(Exception::TypeError(String::New("First argument must be a buffer")))); - if(!Buffer::HasInstance(args[1])) - return scope.Close(ThrowException(Exception::TypeError(String::New("Second argument must be a buffer")))); - - Nodehun::SpellDictionary *obj = new Nodehun::SpellDictionary(Buffer::Data(args[0].As()), Buffer::Data(args[1].As())); - uv_rwlock_init(&(obj->rwlock)); - obj->Wrap(args.This()); - } - return scope.Close(args.This()); -} - -Handle Nodehun::SpellDictionary::spellSuggest(const Arguments& args) -{ - HandleScope scope; - int argl = args.Length(); - if(argl > 1 && args[1]->IsFunction()) { - Persistent callback = Persistent::New(Local::Cast(args[1])); - const unsigned argc = 3; - Local argv[argc]; - argv[1] = Local::New(Null()); - argv[2] = Local::New(Null()); - if(argl < 1 || !args[0]->IsString()) { - TryCatch try_catch; - argv[0] = Exception::TypeError(String::New("First argument must be a string")); - callback->Call(Context::GetCurrent()->Global(), argc, argv); - if (try_catch.HasCaught()) - node::FatalException(try_catch); - callback.Dispose(); - } - else { - Nodehun::SpellDictionary* obj = ObjectWrap::Unwrap(args.This()); - Nodehun::SpellData* spellData = new Nodehun::SpellData(); - String::Utf8Value arg0(args[0]->ToString()); - - spellData->callback = callback; - spellData->request.data = spellData; - spellData->word.append(*arg0); - spellData->obj = obj; - spellData->multiple = false; - uv_queue_work(uv_default_loop(), &spellData->request, - Nodehun::SpellDictionary::checkSuggestions, Nodehun::SpellDictionary::sendSuggestions); - } - } - return scope.Close(Undefined()); -} - -Handle Nodehun::SpellDictionary::spellSuggestions(const Arguments& args) -{ - HandleScope scope; - int argl = args.Length(); - if(argl > 1 && args[1]->IsFunction()) { - Persistent callback = Persistent::New(Local::Cast(args[1])); - const unsigned argc = 3; - Local argv[argc]; - argv[1] = Local::New(Null()); - argv[2] = Local::New(Null()); - if(argl < 1 || !args[0]->IsString()) { - TryCatch try_catch; - argv[0] = Exception::TypeError(String::New("First argument must be a string")); - callback->Call(Context::GetCurrent()->Global(), argc, argv); - if (try_catch.HasCaught()) - node::FatalException(try_catch); - callback.Dispose(); - } - else { - - Nodehun::SpellDictionary* obj = ObjectWrap::Unwrap(args.This()); - Nodehun::SpellData* spellData = new Nodehun::SpellData(); - String::Utf8Value arg0(args[0]->ToString()); - - spellData->callback = callback; - spellData->request.data = spellData; - - spellData->word.append(*arg0); - spellData->obj = obj; - spellData->multiple = true; - uv_queue_work(uv_default_loop(), &spellData->request, - Nodehun::SpellDictionary::checkSuggestions, Nodehun::SpellDictionary::sendSuggestions); - } - } - return scope.Close(Undefined()); -} - -void Nodehun::SpellDictionary::checkSuggestions(uv_work_t* request) -{ - Nodehun::SpellData* spellData = static_cast(request->data); - uv_rwlock_rdlock(&(spellData->obj->rwlock)); - spellData->wordCorrect = spellData->obj->spellClass->spell(spellData->word.c_str()); - if (!spellData->wordCorrect && spellData->multiple) - spellData->numSuggest = spellData->obj->spellClass->suggest(&(spellData->suggestions), spellData->word.c_str()); - else - spellData->numSuggest = 0; - uv_rwlock_rdunlock(&(spellData->obj->rwlock)); -} - -void Nodehun::SpellDictionary::sendSuggestions(uv_work_t* request, int i) -{ - HandleScope scope; - Nodehun::SpellData* spellData = static_cast(request->data); - const unsigned argc = 4; - Local argv[argc]; - argv[0] = Local::New(Null()); - argv[1] = Local::New(Boolean::New(spellData->wordCorrect)); - argv[3] = Local::New(String::New(spellData->word.c_str())); - if(spellData->wordCorrect || spellData->numSuggest == 0) { - if(spellData->multiple) - argv[2] = Array::New(0); - else - argv[2] = Local::New(Null()); - } - else if(spellData->numSuggest > 0) { - if(spellData->multiple) { - Local suglist = Array::New(spellData->numSuggest); - for(int t = 0; t < spellData->numSuggest; t++) - suglist->Set(t,String::New(spellData->suggestions[t])); - argv[2] = suglist; - } - else { - argv[2] = String::New(spellData->suggestions[0]); - } - } - spellData->obj->spellClass->free_list(&(spellData->suggestions), spellData->numSuggest); - TryCatch try_catch; - spellData->callback->Call(Context::GetCurrent()->Global(), argc, argv); - if (try_catch.HasCaught()) - node::FatalException(try_catch); - spellData->callback.Dispose(); - delete spellData; -} - -Handle Nodehun::SpellDictionary::addDictionary(const Arguments& args) -{ - HandleScope scope; - int argl = args.Length(); - if(argl > 0) { - Nodehun::SpellDictionary* obj = ObjectWrap::Unwrap(args.This()); - Nodehun::DictData* dictData = new Nodehun::DictData(); - dictData->callbackExists = false; - if(argl > 1 && args[1]->IsFunction()) { - Persistent callback = Persistent::New(Local::Cast(args[1])); - const unsigned argc = 1; - Local argv[argc]; - if(!Buffer::HasInstance(args[0])) { - TryCatch try_catch; - argv[0] = Exception::TypeError(String::New("First argument must be a buffer")); - callback->Call(Context::GetCurrent()->Global(), argc, argv); - if (try_catch.HasCaught()) - node::FatalException(try_catch); - callback.Dispose(); - delete dictData; - return scope.Close(Undefined()); - } - dictData->callback = callback; - dictData->callbackExists = true; - } - if(!Buffer::HasInstance(args[0])) { - delete dictData; - return scope.Close(ThrowException(Exception::TypeError(String::New("First argument must be a buffer")))); - } - dictData->dict = new char[Buffer::Length(args[0])]; - strcpy(dictData->dict, Buffer::Data(args[0].As())); - dictData->obj = obj; - dictData->request.data = dictData; - - uv_queue_work(uv_default_loop(), &dictData->request, - Nodehun::SpellDictionary::addDictionaryWork, Nodehun::SpellDictionary::addDictionaryFinish); - - } - return scope.Close(Undefined()); -} - -void Nodehun::SpellDictionary::addDictionaryWork(uv_work_t* request) -{ - Nodehun::DictData* dictData = static_cast(request->data); - uv_rwlock_wrlock(&(dictData->obj->rwlock)); - int status = dictData->obj->spellClass->add_dic(dictData->dict); - dictData->success = status == 0; - uv_rwlock_wrunlock(&(dictData->obj->rwlock)); -} - -void Nodehun::SpellDictionary::addDictionaryFinish(uv_work_t* request, int i) -{ - HandleScope scope; - Nodehun::DictData* dictData = static_cast(request->data); - - if(dictData->callbackExists) { - const unsigned argc = 1; - Local argv[argc]; - argv[0] = dictData->success ? Local::New(Null()) : Exception::TypeError(String::New("There was an error adding the dictionary to the nodehun class, because the buffer was deformed.")); - TryCatch try_catch; - dictData->callback->Call(Context::GetCurrent()->Global(), argc, argv); - if (try_catch.HasCaught()) { - node::FatalException(try_catch); - } - dictData->callback.Dispose(); - } - delete dictData->dict; - delete dictData; -} - -Handle Nodehun::SpellDictionary::addWord(const Arguments& args) -{ - return Nodehun::SpellDictionary::addRemoveWordInit(args, false); -} - -Handle Nodehun::SpellDictionary::removeWord(const Arguments& args) -{ - return Nodehun::SpellDictionary::addRemoveWordInit(args, true); -} - -Handle Nodehun::SpellDictionary::addRemoveWordInit(const Arguments& args, bool remove) -{ - HandleScope scope; - int argl = args.Length(); - if(argl > 0) { - Nodehun::SpellDictionary* obj = ObjectWrap::Unwrap(args.This()); - String::Utf8Value arg0(args[0]->ToString()); - Nodehun::WordData* wordData = new Nodehun::WordData(); - wordData->callbackExists = false; - if(argl > 1 && args[1]->IsFunction()) { - Persistent callback = Persistent::New(Local::Cast(args[1])); - const unsigned argc = 2; - Local argv[argc]; - wordData->callback = callback; - wordData->callbackExists = true; - if (!args[0]->IsString()) { - TryCatch try_catch; - argv[0] = Exception::TypeError(String::New("First argument must be a string")); - argv[1] = Local::New(Null()); - callback->Call(Context::GetCurrent()->Global(), argc, argv); - if (try_catch.HasCaught()) - node::FatalException(try_catch); - callback.Dispose(); - delete wordData; - return scope.Close(Undefined()); - } - } - if (!args[0]->IsString()) { - delete wordData; - return scope.Close(ThrowException(Exception::TypeError(String::New("First argument must be a string.")))); - } - wordData->word.append(*arg0); - wordData->removeWord = remove; - wordData->obj = obj; - wordData->request.data = wordData; - uv_queue_work(uv_default_loop(), &wordData->request, - Nodehun::SpellDictionary::addRemoveWordWork, Nodehun::SpellDictionary::addRemoveWordFinish); - - } - return scope.Close(Undefined()); -} - -void Nodehun::SpellDictionary::addRemoveWordWork(uv_work_t* request) -{ - Nodehun::WordData* wordData = static_cast(request->data); - uv_rwlock_wrlock(&(wordData->obj->rwlock)); - int status; - if(wordData->removeWord) - status = wordData->obj->spellClass->remove(wordData->word.c_str()); - else - status = wordData->obj->spellClass->add(wordData->word.c_str()); - wordData->success = status == 0; - uv_rwlock_wrunlock(&(wordData->obj->rwlock)); -} - -void Nodehun::SpellDictionary::addRemoveWordFinish(uv_work_t* request, int i) -{ - HandleScope scope; - Nodehun::WordData* wordData = static_cast(request->data); - - if(wordData->callbackExists) { - const unsigned argc = 2; - Local argv[argc]; - argv[0] = wordData->success ? Local::New(Null()) : Exception::TypeError(String::New("There was an error changing the status of the word. The dictionary may be corrupted, or the word may be malfored.")); - argv[1] = wordData->success ? Local::New(String::New(wordData->word.c_str())) : Local::New(Null()); - TryCatch try_catch; - wordData->callback->Call(Context::GetCurrent()->Global(), argc, argv); - if (try_catch.HasCaught()) - node::FatalException(try_catch); - wordData->callback.Dispose(); - } - delete wordData; -} - -Handle Nodehun::SpellDictionary::stem(const Arguments& args) -{ - HandleScope scope; - int argl = args.Length(); - if(argl > 1 && args[1]->IsFunction()) { - Persistent callback = Persistent::New(Local::Cast(args[1])); - if (!args[0]->IsString()) { - const unsigned argc = 2; - Local argv[argc]; - TryCatch try_catch; - argv[0] = Exception::TypeError(String::New("First argument must be a string")); - argv[1] = Local::New(Null()); - callback->Call(Context::GetCurrent()->Global(), argc, argv); - if (try_catch.HasCaught()) - node::FatalException(try_catch); - callback.Dispose(); - return scope.Close(Undefined()); - } - - Nodehun::SpellDictionary* obj = ObjectWrap::Unwrap(args.This()); - Nodehun::StemData* stemData = new Nodehun::StemData(); - v8::String::Utf8Value arg0(args[0]->ToString()); - stemData->word.append(*arg0); - stemData->callback = callback; - stemData->obj = obj; - stemData->request.data = stemData; - uv_queue_work(uv_default_loop(), &stemData->request, - Nodehun::SpellDictionary::stemWork, Nodehun::SpellDictionary::stemFinish); - } - return scope.Close(Undefined()); -} - -void Nodehun::SpellDictionary::stemWork(uv_work_t* request) -{ - Nodehun::StemData* stemData = static_cast(request->data); - uv_rwlock_rdlock(&(stemData->obj->rwlock)); - stemData->numResults = stemData->obj->spellClass->stem(&stemData->results, stemData->word.c_str()); - uv_rwlock_rdunlock(&(stemData->obj->rwlock)); -} - -void Nodehun::SpellDictionary::stemFinish(uv_work_t* request, int i) -{ - HandleScope scope; - Nodehun::StemData* stemData = static_cast(request->data); - - const unsigned int argc = 2; - Local argv[argc]; - Local suglist = Array::New(stemData->numResults); - - for(int t = 0; t < stemData->numResults; t++) - suglist->Set(t,String::New(stemData->results[t])); - stemData->obj->spellClass->free_list(&stemData->results,stemData->numResults); - argv[0] = Local::New(Null()); - argv[1] = suglist; - TryCatch try_catch; - stemData->callback->Call(Context::GetCurrent()->Global(), argc, argv); - - if(try_catch.HasCaught()) - node::FatalException(try_catch); - stemData->callback.Dispose(); - delete stemData; -} - - -Handle Nodehun::SpellDictionary::generate(const Arguments& args) -{ - HandleScope scope; - int argl = args.Length(); - if(argl > 2 && args[2]->IsFunction()) { - Persistent callback = Persistent::New(Local::Cast(args[2])); - if (!args[0]->IsString()) { - const unsigned argc = 2; - Local argv[argc]; - TryCatch try_catch; - argv[0] = Exception::TypeError(String::New("First argument must be a string")); - argv[1] = Local::New(Null()); - callback->Call(Context::GetCurrent()->Global(), argc, argv); - if (try_catch.HasCaught()) - node::FatalException(try_catch); - callback.Dispose(); - return scope.Close(Undefined()); - } - - if (!args[1]->IsString()) { - const unsigned argc = 2; - Local argv[argc]; - TryCatch try_catch; - argv[0] = Exception::TypeError(String::New("Second argument must be a string")); - argv[1] = Local::New(Null()); - callback->Call(Context::GetCurrent()->Global(), argc, argv); - if (try_catch.HasCaught()) - node::FatalException(try_catch); - callback.Dispose(); - return scope.Close(Undefined()); - } - - Nodehun::SpellDictionary* obj = ObjectWrap::Unwrap(args.This()); - Nodehun::GenerateData* generateData = new Nodehun::GenerateData(); - v8::String::Utf8Value arg0(args[0]->ToString()); - v8::String::Utf8Value arg1(args[1]->ToString()); - generateData->word.append(*arg0); - generateData->word2.append(*arg1); - generateData->callback = callback; - generateData->obj = obj; - generateData->request.data = generateData; - uv_queue_work(uv_default_loop(), &generateData->request, - Nodehun::SpellDictionary::generateWork, Nodehun::SpellDictionary::generateFinish); - } - return scope.Close(Undefined()); -} - -void Nodehun::SpellDictionary::generateWork(uv_work_t* request) -{ - Nodehun::GenerateData* generateData = static_cast(request->data); - uv_rwlock_rdlock(&(generateData->obj->rwlock)); - generateData->numResults = generateData->obj->spellClass->generate(&generateData->results, generateData->word.c_str(), generateData->word2.c_str()); - uv_rwlock_rdunlock(&(generateData->obj->rwlock)); -} - -void Nodehun::SpellDictionary::generateFinish(uv_work_t* request, int i) -{ - HandleScope scope; - Nodehun::GenerateData* generateData = static_cast(request->data); - - const unsigned int argc = 2; - Local argv[argc]; - Local suglist = Array::New(generateData->numResults); - - for(int t = 0; t < generateData->numResults; t++) - suglist->Set(t,String::New(generateData->results[t])); - generateData->obj->spellClass->free_list(&generateData->results,generateData->numResults); - argv[0] = Local::New(Null()); - argv[1] = suglist; - TryCatch try_catch; - generateData->callback->Call(Context::GetCurrent()->Global(), argc, argv); - - if(try_catch.HasCaught()) - node::FatalException(try_catch); - generateData->callback.Dispose(); - delete generateData; -} - - -Handle Nodehun::SpellDictionary::analyze(const Arguments& args) -{ - HandleScope scope; - int argl = args.Length(); - if(argl > 1 && args[1]->IsFunction()) { - Persistent callback = Persistent::New(Local::Cast(args[1])); - if (!args[0]->IsString()) { - const unsigned argc = 2; - Local argv[argc]; - TryCatch try_catch; - argv[0] = Exception::TypeError(String::New("First argument must be a string")); - argv[1] = Local::New(Null()); - callback->Call(Context::GetCurrent()->Global(), argc, argv); - if (try_catch.HasCaught()) - node::FatalException(try_catch); - callback.Dispose(); - return scope.Close(Undefined()); - } - - Nodehun::SpellDictionary* obj = ObjectWrap::Unwrap(args.This()); - Nodehun::AnalyzeData* analyzeData = new Nodehun::AnalyzeData(); - v8::String::Utf8Value arg0(args[0]->ToString()); - analyzeData->word.append(*arg0); - analyzeData->callback = callback; - analyzeData->obj = obj; - analyzeData->request.data = analyzeData; - uv_queue_work(uv_default_loop(), &analyzeData->request, - Nodehun::SpellDictionary::analyzeWork, Nodehun::SpellDictionary::analyzeFinish); - } - return scope.Close(Undefined()); -} - -void Nodehun::SpellDictionary::analyzeWork(uv_work_t* request) -{ - Nodehun::AnalyzeData* analyzeData = static_cast(request->data); - uv_rwlock_rdlock(&(analyzeData->obj->rwlock)); - analyzeData->numResults = analyzeData->obj->spellClass->analyze(&analyzeData->results, analyzeData->word.c_str()); - uv_rwlock_rdunlock(&(analyzeData->obj->rwlock)); -} - -void Nodehun::SpellDictionary::analyzeFinish(uv_work_t* request, int i) -{ - HandleScope scope; - Nodehun::AnalyzeData* analyzeData = static_cast(request->data); - - const unsigned int argc = 2; - Local argv[argc]; - Local suglist = Array::New(analyzeData->numResults); - - for(int t = 0; t < analyzeData->numResults; t++) - suglist->Set(t,String::New(analyzeData->results[t])); - analyzeData->obj->spellClass->free_list(&analyzeData->results,analyzeData->numResults); - argv[0] = Local::New(Null()); - argv[1] = suglist; - TryCatch try_catch; - analyzeData->callback->Call(Context::GetCurrent()->Global(), argc, argv); - - if(try_catch.HasCaught()) - node::FatalException(try_catch); - analyzeData->callback.Dispose(); - delete analyzeData; -} - -NODE_MODULE(nodehun, Nodehun::SpellDictionary::Init); diff --git a/SpellChecker/package.json b/SpellChecker/package.json index 0674f0a8..d0f34184 100644 --- a/SpellChecker/package.json +++ b/SpellChecker/package.json @@ -6,6 +6,6 @@ "dependencies": { "express" : "4.13.1", "sockjs" : "0.3.15", - "nodehun" : "2.0.4" + "nodehun" : "2.0.6" } } \ No newline at end of file diff --git a/SpellChecker/sources/spellCheck.js b/SpellChecker/sources/spellCheck.js index 54c3521f..b2533e9f 100644 --- a/SpellChecker/sources/spellCheck.js +++ b/SpellChecker/sources/spellCheck.js @@ -19,8 +19,8 @@ var arrDictionaries = {}; /*function CheckDictionary (dict, correct, unCorrect) { if (dict) { - dict.spellSuggest(correct, function (err, correct, suggestion, origWord) { - console.log(err, correct, suggestion, origWord); + dict.isCorrect(correct, function (err, correct, origWord) { + console.log(err, correct, origWord); if (err || !correct) logger.error('Error: spelling correct word %s failed!', correct); }); @@ -82,7 +82,7 @@ exports.install = function (server, callbackFunction) { --data.usrWordsLength; checkEnd(); } else if ("spell" === data.type) { - oDictionary.spellSuggest(word, function (err, correct, suggestion, origWord) { + oDictionary.isCorrect(word, function (err, correct, origWord) { data.usrCorrect[index] = (!err && correct); --data.usrWordsLength; checkEnd();