-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
70 additions
and
14 deletions.
There are no files selected for viewing
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
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); |
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
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 |
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
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("すもももももももものうち")); |