forked from thunlp/THULAC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcb_tagging_decoder.h
482 lines (398 loc) · 12.6 KB
/
cb_tagging_decoder.h
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#pragma once
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <list>
#include "thulac_base.h"
#include "dat.h"
#include "cb_ngram_feature.h"
#include "cb_decoder.h"
#include "cb_model.h"
namespace thulac{
//用于分词和词性标注的类
class TaggingDecoder{
public:
char separator;
int max_length;
/*句子*/
int len;//句子长度
int* sequence;//句子
int** allowed_label_lists;///
int** pocs_to_tags;///
///*特征*//
NGramFeature* ngram_feature;
///*双数组*/
DAT* dat;
int is_old_type_dat;
///*模型参数*/
permm::Model* model;
///*解码用*/
permm::Node* nodes;//只用来存储解码用的节点的拓扑结构
int* values;//存各个节点的权重
permm::Alpha_Beta* alphas;//前向算法数据
permm::Alpha_Beta* betas;//后向算法数据
int best_score;
int* result;//存储结果
char** label_info;
///**合法转移矩阵*/
int*label_trans;
int**label_trans_pre;
int**label_trans_post;
///*后处理用*/
int threshold;
int* allow_com;
///*后处理用_ tagging*/
int tag_size;//postag的个数
int** label_looking_for;
int* is_good_choice;
/*构造函数*/
TaggingDecoder();
~TaggingDecoder();
/*初始化*/
void init(permm::Model* model, DAT* dat, char** label_info, int** pocs_to_tags,
char* label_trans=NULL);
void set_label_trans();//
/*解码*/
void put_values();
void dp();
void cal_betas();
/*接口*/
int segment(RawSentence&,POCGraph&,TaggedSentence&);
void get_result(TaggedSentence&);
void get_seg_result(SegmentedSentence&);
/*输入输出*/
void output_raw_sentence();
void output_sentence();
void output_allow_tagging();
void cs();
private:
void load_label_trans(char*filename);
};
TaggingDecoder::TaggingDecoder(){
this->separator='_';
this->max_length=10000; //就是这里!
this->len=0;
this->sequence=new int[this->max_length];
this->allowed_label_lists=new int*[this->max_length];
pocs_to_tags=NULL;
ngram_feature=NULL;
dat=NULL;
is_old_type_dat=false;
nodes=new permm::Node[this->max_length];
this->label_trans=NULL;
label_trans_pre=NULL;
label_trans_post=NULL;
this->threshold=0;
// this->allow_sep=new int[this->max_length];
this->allow_com=new int[this->max_length];
this->tag_size=0;
//this->is_good_choice=NULL;
this->model=NULL;
alphas=NULL;
betas=NULL;
}
TaggingDecoder::~TaggingDecoder(){
delete[]sequence;
delete[]allowed_label_lists;
//delete 用于释放 new 分配的空间,free 有用释放 malloc 分配的空间。
//关于delete和free的区别详情可以看http://www.cnblogs.com/zhuyp1015/archive/2012/07/20/2601698.html
for(int i=0;i<max_length;i++){
delete[](nodes[i].predecessors);
delete[](nodes[i].successors);
}
delete[](nodes);
free(values);
free(alphas);
free(betas);
free(result);
if(model!=NULL)for(int i=0;i<model->l_size;i++){
if(label_info)delete[](label_info[i]);
};
delete[](label_info);
free(label_trans);
if(model!=NULL)for(int i=0;i<model->l_size;i++){
if(label_trans_pre)free(label_trans_pre[i]);
if(label_trans_post)free(label_trans_post[i]);
}
free(label_trans_pre);
free(label_trans_post);
// delete[](allow_sep);
delete[](allow_com);
if(model!=NULL)for(int i=0;i<model->l_size;i++){
if(label_looking_for)delete[](label_looking_for[i]);
};
delete[](label_looking_for);
delete[](is_good_choice);
if(pocs_to_tags){
for(int i=1;i<16;i++){
delete[]pocs_to_tags[i];
}
}
delete[]pocs_to_tags;
if(model!=NULL)delete model;
delete dat;
}
void TaggingDecoder::init(
permm::Model* model,
DAT* dat,
char** label_info,
int** pocs_to_tags,
char* label_trans
){
/**模型*/
this->model = model;
/**解码用*/
values=(int*)calloc(sizeof(int),max_length*model->l_size);
alphas=(permm::Alpha_Beta*)calloc(sizeof(permm::Alpha_Beta),max_length*model->l_size);
betas=(permm::Alpha_Beta*)calloc(sizeof(permm::Alpha_Beta),max_length*model->l_size);
result=(int*)calloc(sizeof(int),max_length*model->l_size);
this->label_info=label_info;
for(int i=0;i<max_length;i++){
int* pr=new int[2];
pr[0]=i-1;
pr[1]=-1;
nodes[i].predecessors=pr;
pr=new int[2];
pr[0]=i+1;
pr[1]=-1;
nodes[i].successors=pr;
};
//DAT
this->dat=dat;
//Ngram Features
ngram_feature=new NGramFeature(dat,model,values);
/*pocs_to_tags*/
this->pocs_to_tags=pocs_to_tags;
label_looking_for=new int*[model->l_size];
for(int i=0;i<model->l_size;i++)
label_looking_for[i]=NULL;
for(int i=0;i<model->l_size;i++){
if(label_info[i][0]==kPOC_B || label_info[i][0]==kPOC_S)continue;
for(int j=0;j<=i;j++){
if((strcmp(label_info[i]+1,label_info[j]+1)==0)&&(label_info[j][0]==kPOC_B)){
if(label_looking_for[j]==NULL){
label_looking_for[j]=new int[2];
label_looking_for[j][0]=-1;label_looking_for[j][1]=-1;
tag_size++;
}
label_looking_for[j][label_info[i][0]-'1']=i;
break;
}
}
}
//printf("tagsize %d",tag_size);
/**label_trans*/
if(label_trans){
load_label_trans(label_trans);
}
for(int i=0;i<max_length;i++)
allowed_label_lists[i]=NULL;
is_good_choice=new int[max_length*model->l_size];
}
void TaggingDecoder::dp(){//调用cb_decoder.h里的函数
if(allowed_label_lists[0]==NULL){
allowed_label_lists[0]=pocs_to_tags[9];
}
if(allowed_label_lists[len-1]==NULL){
allowed_label_lists[len-1]=pocs_to_tags[12];
}
best_score=dp_decode(
model->l_size,//check
model->ll_weights,//check
len,//check
nodes,
values,
alphas,
result,
label_trans_pre,
allowed_label_lists
);
allowed_label_lists[0]=NULL;
allowed_label_lists[len-1]=NULL;
/*for(int i=0;i<len;i++){
printf("%s",label_info[result[i]]);
std::cout<<" ";
}std::cout<<"\n";*/
}
void TaggingDecoder::set_label_trans(){//不同位置可能出现的标签种类
int l_size=this->model->l_size;
std::list<int> *pre_labels;
std::list<int> *post_labels;
pre_labels=new std::list<int>[l_size];
post_labels=new std::list<int>[l_size];
for(int i=0;i<l_size;i++)
for(int j=0;j<l_size;j++){
// 0:B 1:M 2:E 3:S
int ni=this->label_info[i][0]-'0';
int nj=this->label_info[j][0]-'0';
int i_is_end=((ni==2)//i is end of a word
||(ni==3));
int j_is_begin=((nj==0)//j is begin of a word
||(nj==3));
int same_tag=strcmp(this->label_info[i]+1,this->label_info[j]+1);
if(same_tag==0){
if((ni==0&&nj==1)||
(ni==0&&nj==2)||
(ni==1&&nj==2)||
(ni==1&&nj==1)||
(ni==2&&nj==0)||
(ni==2&&nj==3)||
(ni==3&&nj==3)||
(ni==3&&nj==0)){
pre_labels[j].push_back(i);
post_labels[i].push_back(j);
//printf("ok\n");
}
}else{
//printf("%s <> %s\n",this->label_info[i],this->label_info[j]);
if(i_is_end&&j_is_begin){
pre_labels[j].push_back(i);
post_labels[i].push_back(j);
}
}
}
label_trans_pre=new int*[l_size];
for(int i=0;i<l_size;i++){
label_trans_pre[i]=new int[(int)pre_labels[i].size()+1];
int k=0;
for(std::list<int>::iterator plist = pre_labels[i].begin();
plist != pre_labels[i].end(); plist++){
label_trans_pre[i][k]=*plist;
k++;
};
label_trans_pre[i][k]=-1;
}
label_trans_post=new int*[l_size];
for(int i=0;i<l_size;i++){
label_trans_post[i]=new int[(int)post_labels[i].size()+1];
int k=0;
for(std::list<int>::iterator plist=post_labels[i].begin();
plist!=post_labels[i].end();++plist){
label_trans_post[i][k]=*plist;
k++;
};
label_trans_post[i][k]=-1;
}
delete []pre_labels;
delete []post_labels;
};
void TaggingDecoder::load_label_trans(char*filename){
//打开文件
FILE * pFile=fopen ( filename , "rb" );
/*得到文件大小*/
int remain_size=0;
int rtn=fread (&remain_size,sizeof(int),1,pFile);
/*得到矩阵数据*/
label_trans=new int[remain_size];
rtn=fread (label_trans,sizeof(int),remain_size,pFile);
/*计算标签个数*/
int label_size=0;
for(int i=0;i<remain_size;i++){
if(label_trans[i]==-1)label_size++;
}
label_size/=2;
/*设定各个标签的指针*/
label_trans_pre=new int*[label_size];
label_trans_post=new int*[label_size];
int ind=0;
for(int i=0;i<label_size;i++){
label_trans_pre[i]=label_trans+ind;
while(label_trans[ind]!=-1)ind++;ind++;
label_trans_post[i]=label_trans+ind;
while(label_trans[ind]!=-1)ind++;ind++;
}
fclose (pFile);
return;
}
void TaggingDecoder::put_values(){
if(!len)return;
/*nodes*/
for(int i=0;i<len;i++){
nodes[i].type=0;
}
nodes[0].type+=1;
nodes[len-1].type+=2;
/*values*/
memset(values,0,sizeof(*values)*len*model->l_size);
ngram_feature->put_values(sequence,len);
//for(int i=0;i<len;i++) std::cout << values[i] << std::endl;
}
void TaggingDecoder::output_raw_sentence(){
int c;
for(int i=0;i<len;i++){
thulac::put_character(sequence[i]);
}
}
void TaggingDecoder::output_sentence(){
int c;
for(int i=0;i<len;i++){
thulac::put_character(sequence[i]);
if((i==len-1)||(label_info[result[i]][0]==kPOC_E)||(label_info[result[i]][0]==kPOC_S)){//分词位置
if(*(label_info[result[i]]+1)){//输出标签(如果有的话)
putchar(separator);
printf("%s",label_info[result[i]]+1);
}
if((i+1)<len)putchar(' ');//在分词位置输出空格
}
}
}
int TaggingDecoder::segment(RawSentence& raw, POCGraph& graph, TaggedSentence& ts){
if(raw.size()==0)return 0;
for(int i=0;i<(int)raw.size();i++){
int pocs = graph[i];
if(pocs){
allowed_label_lists[i]=pocs_to_tags[pocs];
}else{
allowed_label_lists[i]=pocs_to_tags[15];
}
}
//std::cout<<"\n";
for(int i=0;i<(int)raw.size();i++){
sequence[i]=raw[i];
}
len=(int)raw.size();
put_values();//检索出特征值并初始化放在values数组里
dp();//动态规划搜索最优解放在result数组里
for(int i=0;i<(int)raw.size();i++){
allowed_label_lists[i]=NULL;
}
int c;
int offset=0;
ts.clear();
for(int i=0;i<len;i++){
if((i==len-1)||(label_info[result[i]][0]==kPOC_E)||(label_info[result[i]][0]==kPOC_S)){//分词位置
ts.push_back(WordWithTag(separator));
for(int j=offset;j<i+1;j++){
ts.back().word.push_back(sequence[j]);
}
offset=i+1;
if(*(label_info[result[i]]+1)){//输出标签(如果有的话)
ts.back().tag=label_info[result[i]]+1;
//printf("%s",label_info[result[i]]+1);
}
//if((i+1)<len)putchar(' ');//在分词位置输出空格
}
}
}
void TaggingDecoder::get_seg_result(SegmentedSentence& ss){
ss.clear();
/*
Raw raw;
for(int i = 0; i < len; i ++){
raw.push_back(sequence[i]);
}
std::cerr<<raw<<std::endl;
*/
for(int i=0;i<len;i++){
if((i==0)||(label_info[result[i]][0]==kPOC_B)||(label_info[result[i]][0]==kPOC_S)){
ss.push_back(Word());
}
ss.back().push_back(sequence[i]);
}
};
void TaggingDecoder::cs()
{
for(int i=0;i<1000;i++) std::cout << dat->dat[i].check << " " ;
std::cout << std::endl;
}
}