forked from HIT-SCIR/ltp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegment_dll.cpp
211 lines (171 loc) · 5.92 KB
/
segment_dll.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "segmentor/segment_dll.h"
#include "segmentor/segmentor.h"
#include "segmentor/settings.h"
#include "utils/logging.hpp"
#include "utils/codecs.hpp"
#include <iostream>
#include <fstream>
class __ltp_dll_segmentor_wrapper: public ltp::segmentor::Segmentor {
private:
std::vector<const ltp::segmentor::Model::lexicon_t*> lexicons;
public:
__ltp_dll_segmentor_wrapper() {}
~__ltp_dll_segmentor_wrapper() {}
bool load(const char* model_file, const char * lexicon_file = NULL) {
std::ifstream mfs(model_file, std::ifstream::binary);
if (!mfs) {
return false;
}
model = new ltp::segmentor::Model;
if (!model->load(model_header.c_str(), mfs)) {
delete model;
model = 0;
return false;
}
if (NULL != lexicon_file) {
load_lexicon(lexicon_file, &model->external_lexicon);
}
lexicons.push_back(&(model->internal_lexicon));
lexicons.push_back(&(model->external_lexicon));
return true;
}
int segment(const char* str, std::vector<std::string> & words) {
ltp::framework::ViterbiFeatureContext ctx;
ltp::framework::ViterbiScoreMatrix scm;
ltp::framework::ViterbiDecoder decoder;
ltp::segmentor::Instance inst;
int ret = preprocessor.preprocess(str, inst.raw_forms, inst.forms,
inst.chartypes);
if (-1 == ret || 0 == ret) {
words.clear();
return 0;
}
ltp::segmentor::SegmentationConstrain con;
con.regist(&(inst.chartypes));
build_lexicon_match_state(lexicons, &inst);
extract_features(inst, model, &ctx, false);
calculate_scores(inst, (*model), ctx, true, &scm);
// allocate a new decoder so that the segmentor support multithreaded
// decoding. this modification was committed by niuox
decoder.decode(scm, con, inst.predict_tagsidx);
build_words(inst.raw_forms, inst.predict_tagsidx, words);
return words.size();
}
int segment(const std::string& str, std::vector<std::string> & words) {
return segment(str.c_str(), words);
}
};
class __ltp_dll_customized_segmentor_wrapper: public ltp::segmentor::Segmentor {
private:
std::vector<const ltp::segmentor::Model::lexicon_t*> lexicons;
ltp::segmentor::Model* bs_model;
public:
__ltp_dll_customized_segmentor_wrapper(): bs_model(0) {}
~__ltp_dll_customized_segmentor_wrapper() {
if (bs_model) { delete bs_model; bs_model = 0; }
}
bool load(const char* model1, const char* model2,
const char * lexicon_file = NULL) {
std::ifstream mfs(model1, std::ifstream::binary);
if (!mfs) { return false; }
model = new ltp::segmentor::Model;
if (!model->load(model_header.c_str(), mfs)) {
delete model;
model = 0;
return false;
}
mfs.close();
mfs.open(model2);
if (!mfs) { return false; }
bs_model = new ltp::segmentor::Model;
if (!bs_model->load(model_header.c_str(), mfs)) {
delete model; model = 0;
delete bs_model; bs_model = 0;
return false;
}
if (NULL != lexicon_file) {
load_lexicon(lexicon_file, &model->external_lexicon);
}
lexicons.push_back(&(bs_model->internal_lexicon));
lexicons.push_back(&(model->internal_lexicon));
lexicons.push_back(&(model->external_lexicon));
return true;
}
int segment(const char* str, std::vector<std::string> & words) {
ltp::framework::ViterbiFeatureContext ctx, bs_ctx;
ltp::framework::ViterbiScoreMatrix scm;
ltp::framework::ViterbiDecoder decoder;
ltp::segmentor::Instance inst;
int ret = preprocessor.preprocess(str, inst.raw_forms, inst.forms,
inst.chartypes);
if (-1 == ret || 0 == ret) {
words.clear();
return 0;
}
ltp::segmentor::SegmentationConstrain con;
con.regist(&(inst.chartypes));
build_lexicon_match_state(lexicons, &inst);
extract_features(inst, model, &ctx, false);
extract_features(inst, bs_model, &bs_ctx, false);
calculate_scores(inst, (*bs_model), (*model), bs_ctx, ctx, true, &scm);
// allocate a new decoder so that the segmentor support multithreaded
// decoding. this modification was committed by niuox
decoder.decode(scm, con, inst.predict_tagsidx);
build_words(inst.raw_forms, inst.predict_tagsidx, words);
return words.size();
}
int segment(const std::string& str, std::vector<std::string> & words) {
return segment(str.c_str(), words);
}
};
void * segmentor_create_segmentor(const char * path, const char * lexicon_file) {
__ltp_dll_segmentor_wrapper* wrapper = new __ltp_dll_segmentor_wrapper();
if (!wrapper->load(path, lexicon_file)) {
delete wrapper;
return 0;
}
return reinterpret_cast<void *>(wrapper);
}
int segmentor_release_segmentor(void * segmentor) {
if (!segmentor) {
return -1;
}
delete reinterpret_cast<__ltp_dll_segmentor_wrapper*>(segmentor);
return 0;
}
int segmentor_segment(void * segmentor, const std::string & str,
std::vector<std::string> & words) {
if (str.empty()) {
return 0;
}
__ltp_dll_segmentor_wrapper* wrapper = 0;
wrapper = reinterpret_cast<__ltp_dll_segmentor_wrapper*>(segmentor);
return wrapper->segment(str.c_str(), words);
}
void * customized_segmentor_create_segmentor(const char * path1,
const char* path2,
const char * lexicon_file) {
__ltp_dll_customized_segmentor_wrapper* wrapper =
new __ltp_dll_customized_segmentor_wrapper();
if (!wrapper->load(path1, path2, lexicon_file)) {
delete wrapper;
return 0;
}
return reinterpret_cast<void *>(wrapper);
}
int customized_segmentor_release_segmentor(void * segmentor) {
if (!segmentor) {
return -1;
}
delete reinterpret_cast<__ltp_dll_customized_segmentor_wrapper*>(segmentor);
return 0;
}
int customized_segmentor_segment(void * segmentor, const std::string & str,
std::vector<std::string> & words) {
if (str.empty()) {
return 0;
}
__ltp_dll_customized_segmentor_wrapper* wrapper = 0;
wrapper = reinterpret_cast<__ltp_dll_customized_segmentor_wrapper*>(segmentor);
return wrapper->segment(str.c_str(), words);
}