Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

language_classifier binding #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@
"<!(node -e \"require('nan')\")",
"/usr/local/include"
]
},
{
"target_name": "language_classifier",
"sources": [ "src/language_classifier.cc" ],
"libraries": [
"-lpostal", "-L/usr/local/lib"
],
"include_dirs": [
"<!(node -e \"require('nan')\")",
"/usr/local/include"
]
}
]
}
73 changes: 73 additions & 0 deletions src/language_classifier.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <libpostal/libpostal.h>
#include <nan.h>
#include <string.h>

void LanguageClassifier(const Nan::FunctionCallbackInfo<v8::Value>& info) {
v8::Isolate *isolate = info.GetIsolate();

if (info.Length() < 1) {
Nan::ThrowTypeError("Usage: language_classifier(text)");
return;
}

if (!info[0]->IsString()) {
Nan::ThrowTypeError("First argument must be a string");
return;
}

Nan::Utf8String text_utf8(info[0]);
char *text = *text_utf8;

if (text == NULL) {
Nan::ThrowTypeError("Could not convert first argument to string");
return;
}

libpostal_language_classifier_response_t *response = libpostal_classify_language(text);

if (response != NULL) {
v8::Local<v8::Array> lang_array = Nan::New<v8::Array>(response->num_languages);

for (size_t i = 0; i < response->num_languages; ++i) {
const char *language = response->languages[i];
const double probability = response->probs[i];

v8::Local<v8::Object> lang_obj = Nan::New<v8::Object>();
Nan::Set(lang_obj, Nan::New("language").ToLocalChecked(), Nan::New(language).ToLocalChecked());
Nan::Set(lang_obj, Nan::New("probability").ToLocalChecked(), Nan::New(probability));

Nan::Set(lang_array, i, lang_obj);
}
libpostal_language_classifier_response_destroy(response);
info.GetReturnValue().Set(lang_array);
}
}

void cleanup(void*) {
libpostal_teardown();
libpostal_teardown_language_classifier();
}

void init(v8::Local<v8::Object> exports) {
if (!libpostal_setup() || !libpostal_setup_language_classifier()) {
Nan::ThrowError("Could not load libpostal");
return;
}

v8::Local<v8::Context> context = exports->CreationContext();

exports->Set(
context,
Nan::New("language_classifier").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(LanguageClassifier)->GetFunction(context).ToLocalChecked()
);

#if NODE_MAJOR_VERSION >= 12
node::Environment* env = node::GetCurrentEnvironment(Nan::GetCurrentContext());
node::AtExit(env, cleanup, NULL);
#else
node::AtExit(cleanup);
#endif
}

NODE_MODULE(language_classifier, init)