context.c 10.4 KB
Newer Older
Alexis Nasr's avatar
Alexis Nasr committed
1
2
3
4
5
6
7
8
9
10
11
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<getopt.h>
#include "feat_types.h"
#include "context.h"
#include "util.h"

void context_free(context *ctx)
{
12
13
14
15
16
17
18
19
20
21
  if(ctx->program_name)            free(ctx->program_name);
  if(ctx->conll_filename)          free(ctx->conll_filename);
  if(ctx->perc_model_filename)     free(ctx->perc_model_filename);
  if(ctx->dnn_model_filename)      free(ctx->dnn_model_filename);
  if(ctx->dico_features_filename)  free(ctx->dico_features_filename);
  if(ctx->dico_classes_filename)   free(ctx->dico_classes_filename);
  if(ctx->cff_filename)            free(ctx->cff_filename);
  if(ctx->fann_filename)           free(ctx->fann_filename);
  if(ctx->mcd_filename)            free(ctx->mcd_filename);
  if(ctx->stag_desc_filename)      free(ctx->stag_desc_filename);
Alexis Nasr's avatar
Alexis Nasr committed
22
  if(ctx->features_model_filename) free(ctx->features_model_filename);
23
  if(ctx->f2p_filename)            free(ctx->f2p_filename);
24
25
26
  if(ctx->maca_data_path)          free(ctx->maca_data_path);
  if(ctx->language)                free(ctx->language);
  if(ctx->root_label)              free(ctx->root_label);
Alexis Nasr's avatar
Alexis Nasr committed
27
28
29
30
31
32
33
34
35
36

  if(ctx->d_perceptron_features)
    dico_free(ctx->d_perceptron_features);

  if(ctx->mcd_struct)
    mcd_free(ctx->mcd_struct);

  if(ctx->features_model)
    feat_model_free(ctx->features_model);

37
38
39
  if(ctx->f2p)
    form2pos_free(ctx->f2p);

Alexis Nasr's avatar
Alexis Nasr committed
40
41
42
43
44
  free(ctx);
}

context *context_new(void)
{
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
  context *ctx = (context *)memalloc(sizeof(context));

  ctx->verbose = 0;
  ctx->program_name = NULL;
  ctx->conll_filename = NULL;
  ctx->perc_model_filename = NULL;
  ctx->dnn_model_filename = NULL;
  ctx->dico_features_filename = NULL;
  ctx->dico_classes_filename = NULL;
  ctx->cff_filename = NULL;
  ctx->fann_filename = NULL;
  ctx->stag_desc_filename = NULL;
  ctx->mcd_filename = NULL;
  ctx->features_model_filename = NULL;
  ctx->vocabs_filename = NULL;  
  ctx->f2p_filename = NULL;  

  ctx->maca_data_path = NULL;  
  ctx->language = strdup("fr");

  ctx->root_label = strdup("root");
  ctx->d_perceptron_features = NULL;
  ctx->mcd_struct = NULL;
  ctx->features_model = NULL;
  ctx->vocabs = NULL;
  ctx->dico_labels = NULL;
  ctx->f2p = NULL;


  ctx->iteration_nb = 4;
  ctx->debug_mode = 0;
  ctx->feature_cutoff = 0;
  ctx->help = 0;
  ctx->hash_ratio = 0.5;
  ctx->mode = TRAIN_MODE;
  ctx->beam_width = 1;
  ctx->sent_nb = 1000000;
  ctx->hidden_neurons_nb = 100;
  ctx->stream_mode = 0;

  ctx->form_column = -1;


  return ctx;
Alexis Nasr's avatar
Alexis Nasr committed
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
}

void context_general_help_message(context *ctx)
{
    fprintf(stderr, "usage: %s [options]\n", ctx->program_name);
    fprintf(stderr, "Options:\n");
    fprintf(stderr, "\t-h --help             : print this message\n");
    fprintf(stderr, "\t-v --verbose          : activate verbose mode\n");
    fprintf(stderr, "\t-r --hratio   <float> : set the occupation ratio of hash tables (default is 0.5)\n");
}

void context_model_help_message(context *ctx){
  fprintf(stderr, "\t-m --model     <file> : model file name\n");
}
void context_iterations_help_message(context *ctx){
  fprintf(stderr, "\t-n --iter       <int> : number of iterations  (default is 4)\n");
}
void context_cff_help_message(context *ctx){
  fprintf(stderr, "\t-x --cff       <file> : CFF format file name\n");
}
void context_fann_help_message(context *ctx){
  fprintf(stderr, "\t-y --fann      <file> : FANN format file name\n");
}
void context_d_features_help_message(context *ctx){
  fprintf(stderr, "\t-f --df  <file>     : features dictionnary file name\n");
}
void context_d_classes_help_message(context *ctx){
  fprintf(stderr, "\t-c --dc  <file>     : classes dictionnary file name\n");
}
void context_conll_help_message(context *ctx){
  fprintf(stderr, "\t-i --conll  <file>  : conll file name\n");
}
void context_cutoff_help_message(context *ctx){
  fprintf(stderr, "\t-u --cutoff   <int>  : cutoff value\n");
}
void context_mode_help_message(context *ctx){
  fprintf(stderr, "\t-o --mode   TEST|TRAIN\n");
}
void context_beam_help_message(context *ctx){
  fprintf(stderr, "\t-b --beam   <int> : beam width (default is 1)\n");
}
void context_sent_nb_help_message(context *ctx){
  fprintf(stderr, "\t-s --sent_nb   <int> : number of sentences to process (default is 1000000)\n");
}
void context_alphabet_help_message(context *ctx){
  fprintf(stderr, "\t-a --alphabet   <file> : name of the file containing the different dictionaries\n");
}
void context_dnn_model_help_message(context *ctx){
  fprintf(stderr, "\t-M --dnn_model   <file> : FANN model file\n");
}
void context_hidden_neurons_nb_help_message(context *ctx){
  fprintf(stderr, "\t-H --hidden_neurons_nb   <int> : number of neurons in the hidden layer (default is 100)\n");
}
void context_stag_desc_filename_help_message(context *ctx){
  fprintf(stderr, "\t-S --stag_file   <file> : name of the file containing the stag description\n");
}
void context_mcd_help_message(context *ctx){
  fprintf(stderr, "\t-C --mcd   <file> : multi column description file name\n");
}
void context_features_model_help_message(context *ctx){
  fprintf(stderr, "\t-F --feat_model   <file> : feature model file name\n");
}
void context_stream_help_message(context *ctx){
  fprintf(stderr, "\t-T --stream (0|1)  : steam mode\n");
}

void context_vocabs_help_message(context *ctx){
  fprintf(stderr, "\t-V --vocabs  : vocabularies file\n");
}

void context_language_help_message(context *ctx){
  fprintf(stderr, "\t-X --language  : identifier of the language to use\n");
}

void context_maca_data_path_help_message(context *ctx){
  fprintf(stderr, "\t-Y --maca_data_path  : path to the maca_data directory\n");
}

167
168
169
void context_root_label_help_message(context *ctx){
  fprintf(stderr, "\t-R --root_label  : name of the root label (default is \"root\")\n");
}
Alexis Nasr's avatar
Alexis Nasr committed
170

171
172
173
174
void context_f2p_filename_help_message(context *ctx){
  fprintf(stderr, "\t-P --f2p  : form to pos (f2p) filename\n");
}

Alexis Nasr's avatar
Alexis Nasr committed
175
176
177
178
179
180
181
182
context *context_read_options(int argc, char *argv[])
{
  int c;
  int option_index = 0;
  context *ctx = context_new();

  ctx->program_name = strdup(argv[0]);

183
  static struct option long_options[28] =
Alexis Nasr's avatar
Alexis Nasr committed
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
    {
      {"help",                no_argument,       0, 'h'},
      {"verbose",             no_argument,       0, 'v'},
      {"debug",               no_argument,       0, 'd'},
      {"model",               required_argument, 0, 'm'}, 
      {"df",                  required_argument, 0, 'f'}, 
      {"dc",                  required_argument, 0, 'c'},
      {"conll",               required_argument, 0, 'i'},
      {"iter",                required_argument, 0, 'n'},
      {"cff",                 required_argument, 0, 'x'},
      {"cutoff",              required_argument, 0, 'u'},
      {"hratio",              required_argument, 0, 'r'},
      {"mode",                required_argument, 0, 'o'},
      {"beam",                required_argument, 0, 'b'},
      {"fann",                required_argument, 0, 'y'},
      {"sent_nb",             required_argument, 0, 's'},
      /* {"alphabet",            required_argument, 0, 'a'}, */
      {"dnn_model",           required_argument, 0, 'M'}, 
      {"hidden_neurons_nb",   required_argument, 0, 'H'}, 
      {"stag_file",           required_argument, 0, 'S'}, 
      {"mcd",                 required_argument, 0, 'C'}, 
      {"feat_model",          required_argument, 0, 'F'}, 
      {"vocabs",              required_argument, 0, 'V'}, 
      {"stream",              required_argument, 0, 'T'},
      {"language",            required_argument, 0, 'X'},
209
      {"maca_data_path",      required_argument, 0, 'Y'},
210
211
212
      {"root_label",          required_argument, 0, 'R'},
      {"form_col",            required_argument, 0, 'O'},
      {"f2p",                 required_argument, 0, 'P'}
Alexis Nasr's avatar
Alexis Nasr committed
213
214
215
216
    };
  optind = 0;
  opterr = 0;
  
217
  while ((c = getopt_long (argc, argv, "dhvT:m:f:c:i:n:x:u:r:o:b:y:s:M:H:S:C:F:V:X:Y:R:O:P:", long_options, &option_index)) != -1){ 
Alexis Nasr's avatar
Alexis Nasr committed
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
    switch (c)
      {
      case 'd':
	ctx->debug_mode = 1;
	break;
      case 'h':
	ctx->help = 1;
	break;
      case 'v':
	ctx->verbose = 1;
	break;
      case 'T':
	ctx->stream_mode = atoi(optarg);
	break;
      case 'y':
	ctx->fann_filename = strdup(optarg);
	break;
      case 'n':
	ctx->iteration_nb = atoi(optarg);
	break;
      case 'i':
	ctx->conll_filename = strdup(optarg);
	break;
      case 'm':
	ctx->perc_model_filename = strdup(optarg);
	break;
      case 'M':
	ctx->dnn_model_filename = strdup(optarg);
	break;
      case 'f':
	ctx->dico_features_filename = strdup(optarg);
	break;
      case 'c':
	ctx->dico_classes_filename = strdup(optarg);
	break;
      case 'x':
	ctx->cff_filename = strdup(optarg);
	break;
      case 'u':
	ctx->feature_cutoff = atoi(optarg);
	break;
      case 'b':
	ctx->beam_width = atoi(optarg);
	break;
      case 'r':
	ctx->hash_ratio = atof(optarg);
	break;
      case 'o':
	ctx->mode = (!strcmp(optarg, "TEST"))? TEST_MODE : TRAIN_MODE;
	break;
      case 's':
	ctx->sent_nb = atoi(optarg);
	break;
      case 'H':
	ctx->hidden_neurons_nb = atoi(optarg);
	break;
      case 'S':
	ctx->stag_desc_filename = strdup(optarg);
	break;
      case 'C':
	ctx->mcd_filename = strdup(optarg);
279
	ctx->mcd_struct = mcd_read(ctx->mcd_filename);
Alexis Nasr's avatar
Alexis Nasr committed
280
281
282
283
284
285
286
287
288
289
290
291
292
	break;
      case 'F':
	ctx->features_model_filename = strdup(optarg);
	break;
      case 'V':
	ctx->vocabs_filename = strdup(optarg);
	break;
      case 'X':
	ctx->language = strdup(optarg);
	break;
      case 'Y':
	ctx->maca_data_path = strdup(optarg);
	break;
293
294
295
      case 'R':
	ctx->root_label = strdup(optarg);
	break;
296
297
298
299
300
301
302
      case 'O':
	ctx->form_column = atoi(optarg);
	break;
      case 'P':
	ctx->f2p_filename = strdup(optarg);
	ctx->f2p = form2pos_read(ctx->f2p_filename);
	break;
Alexis Nasr's avatar
Alexis Nasr committed
303
304
305
      }
  }

306
307


Alexis Nasr's avatar
Alexis Nasr committed
308
309
310
311
312
  /*  if(ctx->mcd_filename && ctx->conll_filename){
    ctx->mcd_struct = mcd_read(ctx->mcd_filename, ctx->conll_filename);
    ctx->mvt_nb = ctx->mcd_struct->dico_array[ctx->mcd_struct->type2col[FEAT_TYPE_LABEL]]->nbelem * 2 + 1;
    }*/
  
313
  /*
Alexis Nasr's avatar
Alexis Nasr committed
314
315
  if(ctx->features_model && ctx->mcd_struct)
    feat_model_compute_ranges(ctx->features_model, ctx->mcd_struct, ctx->mvt_nb);
316
  */
Alexis Nasr's avatar
Alexis Nasr committed
317

318
319
320
321
322
323
324
325
326
327
328
329
  /* if the form column has been set by user, change it in the mcd file */
  /*  if(ctx->form_column != -1){
    ctx->mcd_struct = mcd_new(ctx->form_column + 1);
    mcd_set_form_col(ctx->mcd_struct, ctx->form_column);
    ctx->mcd_struct->representation[ctx->form_column] = MCD_REPRESENTATION_VOCAB;
    ctx->mcd_struct->filename[ctx->form_column] = strdup("_");
    ctx->mcd_struct->dico_array[ctx->form_column] = NULL;
    ctx->mcd_struct->type_str[ctx->form_column] = strdup("FORM");

    }*/

  if(ctx->mcd_struct == NULL){
330
331
    ctx->mcd_struct = mcd_build_conll07();
  }
Alexis Nasr's avatar
Alexis Nasr committed
332
333
334
335

  return ctx;
}

336