Skip to content

Commit

Permalink
クラス化してコンストラクタの引数でオプションを指定できるようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
KOBA789 committed Nov 4, 2010
1 parent 65d73c2 commit c4dba2e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 14 deletions.
51 changes: 38 additions & 13 deletions mecab.cc
Original file line number Diff line number Diff line change
@@ -1,28 +1,53 @@
#include <v8.h>
#include <node.h>
#include <iostream>
#include <mecab.h>
#include "node-mecab.h"

using namespace v8;
using namespace node;
using namespace v8;
using namespace std;

MeCab::Tagger *tagger;
Handle<Value> Tagger::New (const Arguments& args) {
HandleScope scope;

Tagger *p = new Tagger();
p->Wrap(args.This());

Handle<Value> Parse(const Arguments& args) {
p->tagger = MeCab::createTagger((args.Length() >= 1 && args[0]->IsString()) ? *String::Utf8Value(args[0]->ToString()) : "");

return args.This();
}

Handle<Value> Tagger::Parse (const Arguments& args) {
HandleScope scope;
if (args.Length() != 1 || !args[0]->IsString()) {
// 引数がおかしい場合は例外を投げる
return ThrowException(Exception::Error(String::New("Bad argument.")));
}

}

Tagger *self = ObjectWrap::Unwrap<Tagger>(args.This());

String::Utf8Value input(args[0]->ToString());
const char *result = tagger->parse(*input);

const char *result = self->tagger->parse(*input);

return String::New(result);
}

extern "C" void init(Handle<Object> target) {
Tagger::~Tagger(){
delete tagger;
}

void AddonInitialize (Handle<Object> target) {
HandleScope scope;

tagger = MeCab::createTagger("");

target->Set(String::New("parse"), FunctionTemplate::New(Parse)->GetFunction());

Local<FunctionTemplate> t = FunctionTemplate::New(Tagger::New);
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(String::NewSymbol("Tagger"));

NODE_SET_PROTOTYPE_METHOD(t, "parse", Tagger::Parse);

target->Set(String::New("Tagger"), t->GetFunction());
}

NODE_MODULE(mecab, AddonInitialize);
25 changes: 25 additions & 0 deletions node-mecab.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef _ECHO_
#define _ECHO_

#include <node.h>
#include <v8.h>
#include <mecab.h>

using namespace v8;
using namespace node;

// node.js が用意しているObjectWrapを継承することで自作のクラスもGC対象にする
class Tagger : ObjectWrap {
public:
// node.jsにメソッドを登録するためにstaticにしてる
// インスタンスを作る関数はNewにするのが通例っぽい
static Handle<Value> New(const Arguments& args);
static Handle<Value> Parse(const Arguments& args);
Tagger(){};
~Tagger();
private:
MeCab::Tagger *tagger;
};

static void AddonInitialize( Handle<Object> target );
#endif
8 changes: 7 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
var MeCab = require('./build/default/mecab');

console.log(MeCab.parse('すもももももももものうち'));
//そのまま使えば普通の出力だよん
var nomal = new MeCab.Tagger();
console.log(nomal.parse("すもももももももものうち"));

//コンストラクタに引数を渡せばわかち書きとかになるよん
var wakati = new MeCab.Tagger("-O wakati");
console.log(wakati.parse("すもももももももものうち"));

0 comments on commit c4dba2e

Please sign in to comment.