-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmseg.cpp
85 lines (75 loc) · 1.39 KB
/
mmseg.cpp
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
#include <iostream>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <mmseg/SegmenterManager.h>
#include <mmseg/Segmenter.h>
#include <mmseg/csr_utils.h>
#include "mmseg.h"
using namespace std;
using namespace css;
SegmenterManager *mgr_ = NULL;
int mmseg_init(char *path)
{
if (!path) {
return -1;
}
if (mgr_) {
return 0;
}
SegmenterManager *mgr = new SegmenterManager();
int ret = mgr->init(path);
if (ret != 0) {
delete mgr;
cerr << "error: invalid dict path\n";
return -1;
}
mgr_ = mgr;
return 0;
}
int mmseg_segment(char *text, Token ** tokens)
{
if (!text) {
return -1;
}
if (!mgr_) {
cerr << "error: init not called\n";
return -1;
}
Segmenter *seg = mgr_->getSegmenter(0);
seg->setBuffer((u1 *) text, (u4) strlen(text));
vector<Token> segs;
while (1) {
u2 len = 0, symlen = 0;
char *token = (char *)seg->peekToken(len, symlen);
if (!token || !*token || !len) {
break;
}
Token t = {token, int(len)};
segs.push_back(t);
seg->popToken(len);
}
int size = segs.size();
if (!tokens) {
delete seg;
return size;
}
(*tokens) = (Token *) malloc(segs.size() * sizeof(Token));
int i = 0;
vector < Token >::iterator iter = segs.begin();
for (; iter != segs.end(); iter++) {
(*tokens)[i] = *iter;
i++;
}
delete seg;
return size;
}
int mmseg_destroy()
{
delete mgr_;
mgr_ = NULL;
}
Token *next_token(Token * p)
{
return ++p;
}