Created
April 7, 2012 18:18
-
-
Save rhasson/2331101 to your computer and use it in GitHub Desktop.
is my object being garbage collected?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var r = require('./build/Release/freeling'); | |
var x = new r.Tokenizer('/home/roy/freeling/free3/share/freeling/en/'); | |
var a = x.tokenize('this is my first test sentence.'); | |
console.log(a); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
'targets': [ | |
{ | |
'target_name': 'freeling', | |
'type': 'loadable_module', | |
'product_extension': 'node', | |
'product_prefix': '', | |
'include_dirs': ['.','/home/roy/freeling/free3/include', '/home/roy/cvv8/include/cvv8'], | |
'conditions': [ | |
['OS=="linux"', { | |
'libraries': ['/home/roy/freeling/free3/lib/libfreeling.so'], | |
}], | |
], | |
'sources': ['freeling.cc', 'freeling_tokenizer.cc'], | |
}, | |
], | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef FREELING_TOKENIZER_H | |
#define FREELING_TOKENIZER_H | |
#include <node.h> | |
#include <vector> | |
#include <string> | |
#include "freeling.h" | |
#include "freeling/morfo/util.h" | |
#include "v8-convert.hpp" | |
class FreeLingTokenizer : public node::ObjectWrap { | |
public: | |
static void Init(v8::Handle<v8::Object> target); | |
std::vector<tokenizer> v_tk; | |
private: | |
FreeLingTokenizer(std::wstring); | |
~FreeLingTokenizer(); | |
static v8::Handle<v8::Value> New(const v8::Arguments& args); | |
static v8::Handle<v8::Value> Tokenize(const v8::Arguments& args); | |
static v8::Handle<v8::Array> ltoa(const std::list<word>& ls); | |
v8::Persistent<v8::String> path; | |
std::list<word> lw; | |
std::list<sentence> ls; | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define FREELING_TOKENIZER | |
#include <node.h> | |
#include <v8.h> | |
#include "freeling_tokenizer.h" | |
FreeLingTokenizer::FreeLingTokenizer(std::wstring path) { | |
v_tk.push_back(new tokenizer(path)); | |
util::init_locale(L"default"); | |
}; | |
FreeLingTokenizer::~FreeLingTokenizer() { | |
}; | |
//create the constructor template function | |
static v8::Persistent<v8::FunctionTemplate> const_tpl; | |
void FreeLingTokenizer::Init(v8::Handle<v8::Object> target) { | |
v8::HandleScope scope; | |
//using the functionTemplate, create a function FreeLingTokenizer | |
v8::Local<v8::FunctionTemplate> tpl = v8::FunctionTemplate::New(FreeLingTokenizer::New); | |
const_tpl = v8::Persistent<v8::FunctionTemplate>::New(tpl); | |
const_tpl->InstanceTemplate()->SetInternalFieldCount(1); | |
const_tpl->SetClassName(v8::String::NewSymbol("Tokenizer")); | |
//create prototype chain for above function | |
NODE_SET_PROTOTYPE_METHOD(const_tpl, "tokenize", FreeLingTokenizer::Tokenize); | |
target->Set(v8::String::NewSymbol("Tokenizer"), const_tpl->GetFunction()); | |
} | |
/* | |
* FreeLingTokenizer class New operator as exposed to Javascript | |
* Arguments: path to to FreeLing tokenizer rules directory | |
* Returns: a V8 value(object), an instance of the FreeLingTokenizer object | |
*/ | |
v8::Handle<v8::Value> FreeLingTokenizer::New(const v8::Arguments& args) { | |
v8::HandleScope scope; | |
std::string file = "tokenizer.dat"; | |
std::string dir = ""; | |
if (args[0]->IsUndefined()) { | |
dir = "/usr/local/share/freeling/en/"; | |
} else { | |
/*v8::String::Utf8Value s(args[0]); | |
dir = std::string(*s, s.length());*/ | |
dir = cvv8::CastFromJS<std::string>(args[0]->ToString()); | |
} | |
std::string path = dir+file; | |
std::wstring conf = util::string2wstring(path); | |
//create an instance of the FreeLingTokenizer class | |
FreeLingTokenizer* obj = new FreeLingTokenizer(conf); | |
obj->Wrap(args.This()); | |
return args.This(); | |
} | |
/* | |
* Tokenize function | |
* Arguments: text string to be tokenized | |
* Returns: a list of words (tokens) | |
*/ | |
v8::Handle<v8::Value> FreeLingTokenizer::Tokenize(const v8::Arguments& args) { | |
v8::HandleScope scope; | |
FreeLingTokenizer* obj = node::ObjectWrap::Unwrap<FreeLingTokenizer>(args.This()); | |
v8::Handle<v8::Array> words; | |
if (args.Length() > 0) { | |
std::string text = cvv8::CastFromJS<std::string>(args[0]->ToString()); | |
//handle arguments and process data | |
if (text.length() > 0) { | |
std::list<word> lw; | |
/***** If this section is uncommented and I remove the "new tokenizer()" call from the constructor, it works fine | |
However, this creates a local copy of the tokenizer object so I cannot call it inside other of my class methods | |
unsigned long offs = 0; | |
std::string path = "/home/roy/freeling/free3/share/freeling/en/tokenizer.dat"; | |
std::wstring conf = util::string2wstring(path); | |
util::init_locale(L"default"); | |
tokenizer tk(conf); | |
*/ | |
std::wstring t = util::string2wstring(text); | |
tokenizer tk = obj->v_tk[0]; | |
lw = tk.tokenize(t, offs); | |
words = FreeLingTokenizer::ltoa(lw); | |
} | |
} | |
return scope.Close(words); | |
} | |
/* | |
* function to convert standard c++ list variables to v8 array variables | |
* Arguments: list element of type string | |
* Returns: v8 Array | |
*/ | |
v8::Handle<v8::Array> FreeLingTokenizer::ltoa(const std::list<word>& ls) { | |
v8::HandleScope scope; | |
std::list<std::string> ary; | |
for (std::list<word>::const_iterator i=ls.begin(); i!=ls.end(); i++) { | |
std::wstring t(i->get_form()); | |
ary.push_back(util::wstring2string(t)); | |
} | |
v8::Handle<v8::Value> a = cvv8::CastToJS(ary); | |
v8::Handle<v8::Array> newV8Array = v8::Handle<v8::Array>::Cast(a); | |
return newV8Array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment