forked from arronzhang/mmseg-node
/
mmseg.cc
105 lines (91 loc) · 2.04 KB
/
mmseg.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "mmseg.h"
using namespace node;
using namespace v8;
Persistent<FunctionTemplate> mmseg::constructor_template;
void mmseg::Initialize (Handle<Object> target)
{
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(New);
t->InstanceTemplate()->SetInternalFieldCount(1);
NODE_SET_PROTOTYPE_METHOD(t, "segment", Segment);
NODE_SET_PROTOTYPE_METHOD(t, "close", Close);
target->Set(String::NewSymbol("mmseg"), t->GetFunction());
}
Handle<Value> mmseg::New (const Arguments& args)
{
HandleScope scope;
if (args.Length() == 0 || !args[0]->IsString()) {
return ThrowException(Exception::Error(
String::New("Need data path")));
}
Handle<String> str = args[0]->ToString();
char path[str.Length()+1];
str->WriteAscii((char*) &path);
init( path );
return Undefined();
//return args.This();
}
Handle<Value> mmseg::Segment (const Arguments& args)
{
HandleScope scope;
if (args.Length() == 0 || !args[0]->IsString()) {
return Undefined();
}
Handle<String> str = args[0]->ToString();
char words[str.Length()+1];
str->WriteAscii((char*) &words);
Local<Array> ar = segment(words);
return String::New(loc);
}
Handle<Value> mmseg::Close (const Arguments& args)
{
HandleScope scope;
destroy();
return Undefined();
}
css::SegmenterManager g_mgr;
bool g_bInited = false;
int mmseg::init(char *path)
{
if( g_bInited ) return 1;
if (g_mgr.init(path) != 0) {
perror(path);
return 1;
}
g_bInited = true;
return 0;
}
Local<Array> mmseg::segment(char *f_words)
{
Local<Array> ar = Array::New();
if( !g_bInited ) {
return ar;
}
int i = 0;
Segmenter* seg = g_mgr.getSegmenter();
seg->setBuffer((u1*)f_words, (u4)strlen(f_words));
while(1)
{
u2 len = 0, symlen = 0;
char* tok = (char*)seg->peekToken(len,symlen);
if(!tok || !*tok || !len){
break;
}
//append new item
ar->Set(i, String::New(tok, len));
i++;
seg->popToken(len);
}
return ar;
}
void mmseg::destroy()
{
if( g_bInited ){
g_bInited = false;
}
}
extern "C" void init (Handle<Object> target)
{
HandleScope scope;
mmseg::Initialize(target);
}