-
Notifications
You must be signed in to change notification settings - Fork 2
/
SENNA_main.c
359 lines (323 loc) · 11.8 KB
/
SENNA_main.c
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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "SENNA_utils.h"
#include "SENNA_Hash.h"
#include "SENNA_Tokenizer.h"
#include "SENNA_POS.h"
#include "SENNA_CHK.h"
#include "SENNA_NER.h"
#include "SENNA_VBS.h"
#include "SENNA_PT0.h"
#include "SENNA_SRL.h"
#include "SENNA_PSG.h"
/* fgets max sizes */
#define MAX_SENTENCE_SIZE 1024
#define MAX_TARGET_VB_SIZE 256
static void help(const char *name)
{
printf("\n");
printf("SENNA Tagger (POS - CHK - NER - SRL)\n");
printf("(c) Ronan Collobert 2009\n");
printf("\n");
printf("Usage: %s [options]\n", name);
printf("\n");
printf(" Takes sentence (one line per sentence) on stdin\n");
printf(" Outputs tags on stdout\n");
printf(" Typical usage: %s [options] < inputfile.txt > outputfile.txt\n", name);
printf("\n");
printf("Display options:\n");
printf(" -h Display this help\n");
printf(" -verbose Display model informations on stderr\n");
printf(" -notokentags Do not output tokens\n");
printf(" -offsettags Output start/end offset of each token\n");
printf(" -iobtags Output IOB tags instead of IOBES\n");
printf(" -brackettags Output 'bracket' tags instead of IOBES\n");
printf("\n");
printf("Data options:\n");
printf(" -path <path> Path to the SENNA data/ and hash/ directories [default: ./]\n");
printf("\n");
printf("Input options:\n");
printf(" -usrtokens Use user's tokens (space separated) instead of SENNA tokenizer\n");
printf("\n");
printf("SRL options:\n");
printf(" -posvbs Use POS verbs instead of SRL style verbs for SRL task\n");
printf(" -usrvbs <file> Use user's verbs (given in <file>) instead of SENNA verbs for SRL task\n");
printf("\n");
printf("Tagging options:\n");
printf(" -pos Output POS\n");
printf(" -chk Output CHK\n");
printf(" -ner Output NER\n");
printf(" -srl Output SRL\n");
printf(" -psg Output PSG\n");
printf("\n");
exit(-1);
}
int main(int argc, char *argv[])
{
int i, j;
/* options */
char *opt_path = NULL;
int opt_verbose = 0;
int opt_notokentags = 0;
int opt_offsettags = 0;
int opt_iobtags = 0;
int opt_brackettags = 0;
int opt_posvbs = 0;
int opt_usrtokens = 0;
int opt_pos = 0;
int opt_chk = 0;
int opt_ner = 0;
int opt_srl = 0;
int opt_psg = 0;
FILE *opt_usrvbs = NULL;
for(i = 1; i < argc; i++)
{
if(!strcmp(argv[i], "-verbose"))
opt_verbose = 1;
else if(!strcmp(argv[i], "-notokentags"))
opt_notokentags = 1;
else if(!strcmp(argv[i], "-offsettags"))
opt_offsettags = 1;
else if(!strcmp(argv[i], "-iobtags"))
opt_iobtags = 1;
else if(!strcmp(argv[i], "-brackettags"))
opt_brackettags = 1;
else if(!strcmp(argv[i], "-path"))
{
if(i+1 >= argc)
SENNA_error("please provide a path for the -path option");
opt_path = argv[i+1];
i++;
}
else if(!strcmp(argv[i], "-posvbs"))
opt_posvbs = 1;
else if(!strcmp(argv[i], "-usrtokens"))
opt_usrtokens = 1;
else if(!strcmp(argv[i], "-usrvbs"))
{
if(i+1 >= argc)
SENNA_error("please provide a filename for the -usrvbs option");
opt_usrvbs = SENNA_fopen(NULL, argv[i+1], "rb");
i++;
}
else if(!strcmp(argv[i], "-pos"))
opt_pos = 1;
else if(!strcmp(argv[i], "-chk"))
opt_chk = 1;
else if(!strcmp(argv[i], "-ner"))
opt_ner = 1;
else if(!strcmp(argv[i], "-srl"))
opt_srl = 1;
else if(!strcmp(argv[i], "-psg"))
opt_psg = 1;
else
{
printf("invalid argument: %s\n", argv[i]);
help(argv[0]);
}
}
SENNA_set_verbose_mode(opt_verbose);
if(!opt_pos && !opt_chk && !opt_ner && !opt_srl && !opt_psg) /* the user does not know what he wants... */
opt_pos = opt_chk = opt_ner = opt_srl = opt_psg = 1; /* so give him everything (aren't we insane?) */
/* the real thing */
{
char sentence[MAX_SENTENCE_SIZE];
char target_vb[MAX_TARGET_VB_SIZE];
int *chk_labels = NULL;
int *pt0_labels = NULL;
int *pos_labels = NULL;
int *ner_labels = NULL;
int *vbs_labels = NULL;
int **srl_labels = NULL;
int *psg_labels = NULL;
int n_psg_level = 0;
int is_psg_one_segment = 0;
int vbs_hash_novb_idx = 22;
int n_verbs = 0;
/* inputs */
SENNA_Hash *word_hash = SENNA_Hash_new(opt_path, "hash/words.lst");
SENNA_Hash *caps_hash = SENNA_Hash_new(opt_path, "hash/caps.lst");
SENNA_Hash *suff_hash = SENNA_Hash_new(opt_path, "hash/suffix.lst");
SENNA_Hash *gazt_hash = SENNA_Hash_new(opt_path, "hash/gazetteer.lst");
SENNA_Hash *gazl_hash = SENNA_Hash_new_with_admissible_keys(opt_path, "hash/ner.loc.lst", "data/ner.loc.dat");
SENNA_Hash *gazm_hash = SENNA_Hash_new_with_admissible_keys(opt_path, "hash/ner.msc.lst", "data/ner.msc.dat");
SENNA_Hash *gazo_hash = SENNA_Hash_new_with_admissible_keys(opt_path, "hash/ner.org.lst", "data/ner.org.dat");
SENNA_Hash *gazp_hash = SENNA_Hash_new_with_admissible_keys(opt_path, "hash/ner.per.lst", "data/ner.per.dat");
/* labels */
SENNA_Hash *pos_hash = SENNA_Hash_new(opt_path, "hash/pos.lst");
SENNA_Hash *chk_hash = SENNA_Hash_new(opt_path, "hash/chk.lst");
SENNA_Hash *pt0_hash = SENNA_Hash_new(opt_path, "hash/pt0.lst");
SENNA_Hash *ner_hash = SENNA_Hash_new(opt_path, "hash/ner.lst");
SENNA_Hash *vbs_hash = SENNA_Hash_new(opt_path, "hash/vbs.lst");
SENNA_Hash *srl_hash = SENNA_Hash_new(opt_path, "hash/srl.lst");
SENNA_Hash *psg_left_hash = SENNA_Hash_new(opt_path, "hash/psg-left.lst");
SENNA_Hash *psg_right_hash = SENNA_Hash_new(opt_path, "hash/psg-right.lst");
SENNA_POS *pos = SENNA_POS_new(opt_path, "data/pos.dat");
SENNA_CHK *chk = SENNA_CHK_new(opt_path, "data/chk.dat");
SENNA_PT0 *pt0 = SENNA_PT0_new(opt_path, "data/pt0.dat");
SENNA_NER *ner = SENNA_NER_new(opt_path, "data/ner.dat");
SENNA_VBS *vbs = SENNA_VBS_new(opt_path, "data/vbs.dat");
SENNA_SRL *srl = SENNA_SRL_new(opt_path, "data/srl.dat");
SENNA_PSG *psg = SENNA_PSG_new(opt_path, "data/psg.dat");
SENNA_Tokenizer *tokenizer = SENNA_Tokenizer_new(word_hash, caps_hash, suff_hash, gazt_hash, gazl_hash, gazm_hash, gazo_hash, gazp_hash, opt_usrtokens);
if(opt_iobtags)
{
SENNA_Hash_convert_IOBES_to_IOB(chk_hash);
SENNA_Hash_convert_IOBES_to_IOB(ner_hash);
SENNA_Hash_convert_IOBES_to_IOB(srl_hash);
}
else if(opt_brackettags)
{
SENNA_Hash_convert_IOBES_to_brackets(chk_hash);
SENNA_Hash_convert_IOBES_to_brackets(ner_hash);
SENNA_Hash_convert_IOBES_to_brackets(srl_hash);
}
SENNA_message("ready");
while(fgets(sentence, MAX_SENTENCE_SIZE, stdin))
{
SENNA_Tokens* tokens = SENNA_Tokenizer_tokenize(tokenizer, sentence);
if(tokens->n == 0)
continue;
pos_labels = SENNA_POS_forward(pos, tokens->word_idx, tokens->caps_idx, tokens->suff_idx, tokens->n);
if(opt_chk)
chk_labels = SENNA_CHK_forward(chk, tokens->word_idx, tokens->caps_idx, pos_labels, tokens->n);
if(opt_srl)
pt0_labels = SENNA_PT0_forward(pt0, tokens->word_idx, tokens->caps_idx, pos_labels, tokens->n);
if(opt_ner)
ner_labels = SENNA_NER_forward(ner, tokens->word_idx, tokens->caps_idx, tokens->gazl_idx, tokens->gazm_idx, tokens->gazo_idx, tokens->gazp_idx, tokens->n);
if(opt_srl)
{
if(opt_usrvbs)
{
vbs_labels = SENNA_realloc(vbs_labels, sizeof(int), tokens->n);
n_verbs = 0;
for(i = 0; i < tokens->n; i++)
{
if(!SENNA_fgetline(target_vb, MAX_TARGET_VB_SIZE, opt_usrvbs))
SENNA_error("invalid user verbs file\n");
vbs_labels[i] = !( (target_vb[0] == '-') && ( (target_vb[1] == '\0') || isspace(target_vb[1])) );
n_verbs += vbs_labels[i];
}
if(!SENNA_fgetline(target_vb, MAX_TARGET_VB_SIZE, opt_usrvbs))
SENNA_error("invalid user verbs file\n");
if(strlen(target_vb) > 0)
SENNA_error("sentence size does not match in user verbs file");
}
else if(opt_posvbs)
{
vbs_labels = SENNA_realloc(vbs_labels, sizeof(int), tokens->n);
n_verbs = 0;
for(i = 0; i < tokens->n; i++)
{
vbs_labels[i] = (SENNA_Hash_key(pos_hash, pos_labels[i])[0] == 'V');
n_verbs += vbs_labels[i];
}
}
else
{
vbs_labels = SENNA_VBS_forward(vbs, tokens->word_idx, tokens->caps_idx, pos_labels, tokens->n);
n_verbs = 0;
for(i = 0; i < tokens->n; i++)
{
vbs_labels[i] = (vbs_labels[i] != vbs_hash_novb_idx);
n_verbs += vbs_labels[i];
}
}
}
if(opt_srl)
srl_labels = SENNA_SRL_forward(srl, tokens->word_idx, tokens->caps_idx, pt0_labels, vbs_labels, tokens->n);
if(opt_psg)
{
SENNA_PSG_forward(psg, tokens->word_idx, tokens->caps_idx, pos_labels, tokens->n, &psg_labels, &n_psg_level);
/* check if top level takes the full sentence */
{
int *psg_top_labels = psg_labels + (n_psg_level-1)*tokens->n;
if(tokens->n == 1)
is_psg_one_segment = ((psg_top_labels[0]-1) % 4 == 3); /* S- ? */
else
is_psg_one_segment = ((psg_top_labels[0]-1) % 4 == 0) && ((psg_top_labels[tokens->n-1]-1) % 4 == 2); /* B- or E- ? */
for(i = 1; is_psg_one_segment && (i < tokens->n-1); i++)
{
if((psg_top_labels[i]-1) % 4 != 1) /* I- ? */
is_psg_one_segment = 0;
}
}
}
for(i = 0; i < tokens->n; i++)
{
if(!opt_notokentags)
printf("%15s", tokens->words[i]);
if(opt_offsettags)
printf("\t%d %d", tokens->start_offset[i], tokens->end_offset[i]);
if(opt_pos)
printf("\t%10s", SENNA_Hash_key(pos_hash, pos_labels[i]));
if(opt_chk)
printf("\t%10s", SENNA_Hash_key(chk_hash, chk_labels[i]));
if(opt_ner)
printf("\t%10s", SENNA_Hash_key(ner_hash, ner_labels[i]));
if(opt_srl)
{
printf("\t%15s", (vbs_labels[i] ? tokens->words[i] : "-"));
for(j = 0; j < n_verbs; j++)
printf("\t%10s", SENNA_Hash_key(srl_hash, srl_labels[j][i]));
}
if(opt_psg) /* last, can be long */
{
printf("\t");
if(i == 0)
{
printf("(S1");
if(!is_psg_one_segment)
printf("(S");
}
for(j = n_psg_level-1; j >= 0; j--)
printf("%s", SENNA_Hash_key(psg_left_hash, psg_labels[j*tokens->n+i]));
printf("*");
for(j = 0; j < n_psg_level; j++)
printf("%s", SENNA_Hash_key(psg_right_hash, psg_labels[j*tokens->n+i]));
if(i == tokens->n-1)
{
if(!is_psg_one_segment)
printf(")");
printf(")");
}
}
printf("\n");
}
printf("\n"); /* end of sentence */
}
if(opt_posvbs)
SENNA_free(vbs_labels);
if(opt_usrvbs)
{
SENNA_free(vbs_labels);
SENNA_fclose(opt_usrvbs);
}
SENNA_Tokenizer_free(tokenizer);
SENNA_POS_free(pos);
SENNA_CHK_free(chk);
SENNA_PT0_free(pt0);
SENNA_NER_free(ner);
SENNA_VBS_free(vbs);
SENNA_SRL_free(srl);
SENNA_PSG_free(psg);
SENNA_Hash_free(word_hash);
SENNA_Hash_free(caps_hash);
SENNA_Hash_free(suff_hash);
SENNA_Hash_free(gazt_hash);
SENNA_Hash_free(gazl_hash);
SENNA_Hash_free(gazm_hash);
SENNA_Hash_free(gazo_hash);
SENNA_Hash_free(gazp_hash);
SENNA_Hash_free(pos_hash);
SENNA_Hash_free(chk_hash);
SENNA_Hash_free(pt0_hash);
SENNA_Hash_free(ner_hash);
SENNA_Hash_free(vbs_hash);
SENNA_Hash_free(srl_hash);
SENNA_Hash_free(psg_left_hash);
SENNA_Hash_free(psg_right_hash);
}
return 0;
}