-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcsegment.cc
101 lines (76 loc) · 2.27 KB
/
csegment.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
#include <node.h>
#include <v8.h>
#include <stdlib.h>
#include <string.h>
#include "ICTCLAS50.h"
using namespace v8;
using namespace node;
static int ICTCLAS_INIT = 0;
typedef struct segment_task {
Persistent<Function> cb;
char *rst;
char content[1];
} segment_task;
Handle<Value> doSegmentAsync(const Arguments& args);
static void doSegment(uv_work_t *req);
static void doSegmentAfter(uv_work_t *req, int status);
Handle<Value> doSegmentAsync(const Arguments& args) {
HandleScope scope;
const char *usage = "usage: doWork(content, cb)";
if (args.Length() != 2) {
return ThrowException(Exception::Error(String::New(usage)));
}
if(ICTCLAS_INIT == 0) {
if(!ICTCLAS_Init()) {
return ThrowException(Exception::Error(String::New("ICTCLAS INIT FAILURE!")));
}
ICTCLAS_SetPOSmap(2);
ICTCLAS_INIT = 1;
}
String::Utf8Value content(args[0]);
Local<Function> cb = Local<Function>::Cast(args[1]);
segment_task *task = (segment_task *)malloc(sizeof(segment_task) + content.length() + 1);
memset(task, 0, sizeof(segment_task) + content.length() + 1);
task->cb = Persistent<Function>::New(cb);
strncpy(task->content, *content, content.length());
uv_work_t *req = new uv_work_t;
req->data = task;
uv_queue_work(uv_default_loop(), req, doSegment, doSegmentAfter);
return Undefined();
}
static void doSegment(uv_work_t *req) {
segment_task *task = (segment_task *)req->data;
unsigned int nLen = strlen(task->content);
char* sRst = (char *)malloc(nLen * 6);
memset(sRst, 0, nLen * 6);
int nRstLen = 0;
nRstLen = ICTCLAS_ParagraphProcess(task->content, nLen, sRst, CODE_TYPE_UNKNOWN, 1);
task->rst = sRst;
//ICTCLAS_Exit();
}
static void doSegmentAfter(uv_work_t *req, int status) {
HandleScope scope;
segment_task *task = (segment_task *)req->data;
Local<Value> argv[1];
argv[0] = String::New(task->rst);
TryCatch try_catch;
task->cb->Call(Context::GetCurrent()->Global(), 1, argv);
if (try_catch.HasCaught()) {
task->cb.Dispose();
free(task->rst);
free(task);
FatalException(try_catch);
}
else {
task->cb.Dispose();
free(task->rst);
free(task);
}
}
extern "C" {
void init(Handle<Object> target) {
target->Set(String::NewSymbol("doWork"),
FunctionTemplate::New(doSegmentAsync)->GetFunction());
}
NODE_MODULE(csegment, init)
}