diff --git a/maca_trans_parser/src/context.c b/maca_trans_parser/src/context.c index dabd562f1700b1c7ae7598db0cc783bf927f2cd7..bb087722250e1af04ad6bcdacfa57cf7f6de9c65 100644 --- a/maca_trans_parser/src/context.c +++ b/maca_trans_parser/src/context.c @@ -103,6 +103,10 @@ context *context_new(void) ctx->dnn_model_filename = NULL; ctx->l_rules_filename = NULL; + + ctx->proba_mode = 0; + ctx->proba_factor = 1; + return ctx; } @@ -189,6 +193,9 @@ void context_json_help_message(context *ctx){ void context_dnn_model_help_message(context *ctx){ fprintf(stderr, "\t-N --dnn_model : weight file for dnn\n"); } +void context_proba_mode_help_message(context *ctx){ + fprintf(stderr, "\t-Z --probabilistic : activate probabilistic mode with factor >0 (use 1 for default).\n"); +} context *context_read_options(int argc, char *argv[]) { @@ -198,7 +205,7 @@ context *context_read_options(int argc, char *argv[]) ctx->program_name = strdup(argv[0]); - static struct option long_options[28] = + static struct option long_options[29] = { {"help", no_argument, 0, 'h'}, {"verbose", no_argument, 0, 'v'}, @@ -227,12 +234,13 @@ context *context_read_options(int argc, char *argv[]) {"json", required_argument, 0, 'J'}, {"dnn_model", required_argument, 0, 'N'}, {"l_rules", required_argument, 0, 'l'}, + {"probabilistic", required_argument, 0, 'Z'}, {"fplm", required_argument, 0, 'w'} }; optind = 0; opterr = 0; - while ((c = getopt_long (argc, argv, "hvdcTpm:i:n:x:u:r:M:b:f:s:C:F:V:L:D:R:P:J:N:w:l:S:T:", long_options, &option_index)) != -1){ + while ((c = getopt_long (argc, argv, "hvdcTpm:i:n:x:u:r:M:b:f:s:C:F:V:L:D:R:P:J:N:w:l:S:T:Z:", long_options, &option_index)) != -1){ switch (c) { @@ -326,6 +334,10 @@ context *context_read_options(int argc, char *argv[]) case 'S': ctx->score_method = atoi(optarg); break; + case 'Z': + ctx->proba_mode = 1; + ctx->proba_factor = atoi(optarg); + break; } } diff --git a/maca_trans_parser/src/context.h b/maca_trans_parser/src/context.h index c71d3606dbc95c614489d812a4f66af6e35904c9..c96a3f1fe439b2fdcb5b9e9955e25eae2a88e2a2 100644 --- a/maca_trans_parser/src/context.h +++ b/maca_trans_parser/src/context.h @@ -115,6 +115,9 @@ typedef struct { char *json_filename; char *dnn_model_filename; char *l_rules_filename; + + int proba_mode; + float proba_factor; } context; context *context_new(void); diff --git a/maca_trans_parser/src/simple_decoder_parser_arc_eager.c b/maca_trans_parser/src/simple_decoder_parser_arc_eager.c index 72d7baf6ec65d355cc4f78fef91b69dc1bbc6760..3fd5a3b2eb7ff91ea21e3229d15c198e970a8f64 100644 --- a/maca_trans_parser/src/simple_decoder_parser_arc_eager.c +++ b/maca_trans_parser/src/simple_decoder_parser_arc_eager.c @@ -170,12 +170,15 @@ void simple_decoder_parser_arc_eager(context *ctx) double currentSumExp; double ScoreTranslation; int FlagNotInitExp; - double ProbaDivider = 15; // TO SETUP + double ProbaDivider = ctx->proba_factor; double randomFloat; - srand(mix(clock(), time(NULL), getpid())); - + if(ProbaDivider <= 0) + ProbaDivider = 1; + if(ctx->proba_mode) + srand(mix(clock(), time(NULL), getpid())); + word* word_scored; root_label = dico_string2int(ctx->dico_labels, ctx->root_label); @@ -186,7 +189,7 @@ void simple_decoder_parser_arc_eager(context *ctx) sumExp = 0; currentSumExp = 0; - ScoreTranslation = -5; // TO SETUP + ScoreTranslation = -100; // TO SETUP? FlagNotInitExp = 1; if(ctx->debug_mode){ @@ -210,89 +213,108 @@ void simple_decoder_parser_arc_eager(context *ctx) config2feat_vec_cff(ctx->features_model, c, ctx->d_perceptron_features, fv, LOOKUP_MODE); mvt_code = feature_table_argmax(fv, ft, &max); - if(ctx->debug_mode){ - vcode *vcode_array = feature_table_get_vcode_array(fv, ft); - - /* Get the probabilistic parameters */ - for(int i=0; i < ft->classes_nb; i++){ - int b1 = respect_standard_constraint(c, movement_parser_type(vcode_array[i].class_code), movement_parser_label(vcode_array[i].class_code)); - int b2 = respect_stack_constraint(ctx->partial_mode, c, movement_parser_type(vcode_array[i].class_code), movement_parser_label(vcode_array[i].class_code)); - int b3 = respect_buffer_constraint(ctx->partial_mode, c, movement_parser_type(vcode_array[i].class_code), movement_parser_label(vcode_array[i].class_code)); + if(ctx->proba_mode || ctx->debug_mode){ + vcode *vcode_array = feature_table_get_vcode_array(fv, ft); + + + if(ctx->proba_mode){ + /* Get the probabilistic parameters */ + for(int i=0; i < ft->classes_nb; i++){ + int b1 = respect_standard_constraint(c, movement_parser_type(vcode_array[i].class_code), movement_parser_label(vcode_array[i].class_code)); + int b2 = respect_stack_constraint(ctx->partial_mode, c, movement_parser_type(vcode_array[i].class_code), movement_parser_label(vcode_array[i].class_code)); + int b3 = respect_buffer_constraint(ctx->partial_mode, c, movement_parser_type(vcode_array[i].class_code), movement_parser_label(vcode_array[i].class_code)); - if(b1 && b2 && b3){ - if(FlagNotInitExp){ - ScoreTranslation += vcode_array[i].score/ProbaDivider; - FlagNotInitExp = 0; - } - if(vcode_array[i].score/ProbaDivider - ScoreTranslation > 0){ - sumExp += exp(vcode_array[i].score/ProbaDivider - ScoreTranslation); - } - } - } - - currentSumExp = 0.; - for(int i=0; i < ft->classes_nb && i < 10; i++){ - printf("%d\t", i); - movement_parser_print(stdout, vcode_array[i].class_code, ctx->dico_labels); - printf("\t%.4f", vcode_array[i].score); - fflush(stdout); - int b1 = respect_standard_constraint(c, movement_parser_type(vcode_array[i].class_code), movement_parser_label(vcode_array[i].class_code)); - int b2 = respect_stack_constraint(ctx->partial_mode, c, movement_parser_type(vcode_array[i].class_code), movement_parser_label(vcode_array[i].class_code)); - int b3 = respect_buffer_constraint(ctx->partial_mode, c, movement_parser_type(vcode_array[i].class_code), movement_parser_label(vcode_array[i].class_code)); - if(b1 && b2 && b3){ - if(vcode_array[i].score/ProbaDivider - ScoreTranslation > 0){ - printf(" %f %f %f",sumExp, currentSumExp,ScoreTranslation); - printf(" [%f-",currentSumExp/sumExp); - currentSumExp += exp(vcode_array[i].score/ProbaDivider - ScoreTranslation); - printf("%f[", currentSumExp/sumExp); - } - printf("\t<----"); - }else - printf("\t<%d,%d,%d>",b1,b2,b3); - // printf("\t%d", respect_standard_constraint(c, movement_parser_type(vcode_array[i].class_code), movement_parser_label(vcode_array[i].class_code))); - //printf("AAAAAAA\n"); - printf("\n"); - } - free(vcode_array); + if(b1 && b2 && b3){ + if(FlagNotInitExp){ + ScoreTranslation += vcode_array[i].score; + FlagNotInitExp = 0; + } + if((vcode_array[i].score - ScoreTranslation)/ProbaDivider > 0){ + sumExp += exp((vcode_array[i].score - ScoreTranslation)/ProbaDivider); + } + } + } } - if(ctx->trace_mode){ - index = word_get_index(word_buffer_b0(config_get_buffer(c))); - fprintf(stdout, "%d\t", index); - - stack_print(stdout, c->st); - fprintf(stdout, "\t"); - - movement_parser_print(stdout, mvt_code, ctx->dico_labels); - fprintf(stdout, "\t"); - feature_table_argmax_1_2(fv, ft, &argmax1, &max1, &argmax2, &max2); - printf("%f\n", max1 - max2); + currentSumExp = 0.; + for(int i=0; i < ft->classes_nb && i < 10; i++){ + if(ctx->debug_mode){ + printf("%d\t", i); + movement_parser_print(stdout, vcode_array[i].class_code, ctx->dico_labels); + printf("\t%.4f", vcode_array[i].score); + fflush(stdout); + } + int b1 = respect_standard_constraint(c, movement_parser_type(vcode_array[i].class_code), movement_parser_label(vcode_array[i].class_code)); + int b2 = respect_stack_constraint(ctx->partial_mode, c, movement_parser_type(vcode_array[i].class_code), movement_parser_label(vcode_array[i].class_code)); + int b3 = respect_buffer_constraint(ctx->partial_mode, c, movement_parser_type(vcode_array[i].class_code), movement_parser_label(vcode_array[i].class_code)); + if(b1 && b2 && b3){ + if(ctx->proba_mode && (vcode_array[i].score - ScoreTranslation)/ProbaDivider > 0){ + if(ctx->debug_mode){ + // printf(" %f %f %f",sumExp, currentSumExp,ScoreTranslation); + printf(" [%f-",currentSumExp/sumExp); + } + currentSumExp += exp((vcode_array[i].score - ScoreTranslation)/ProbaDivider); + if(ctx->debug_mode) + printf("%f[", currentSumExp/sumExp); + } + if(ctx->debug_mode) + printf("\t<----"); + }else if(ctx->debug_mode){ + printf("\t<%d,%d,%d>",b1,b2,b3); + } + // printf("\t%d", respect_standard_constraint(c, movement_parser_type(vcode_array[i].class_code), movement_parser_label(vcode_array[i].class_code))); + //printf("AAAAAAA\n"); + if(ctx->debug_mode) + printf("\n"); } + free(vcode_array); + } + + if(ctx->trace_mode){ + index = word_get_index(word_buffer_b0(config_get_buffer(c))); + fprintf(stdout, "%d\t", index); + + stack_print(stdout, c->st); + fprintf(stdout, "\t"); + + movement_parser_print(stdout, mvt_code, ctx->dico_labels); + fprintf(stdout, "\t"); + feature_table_argmax_1_2(fv, ft, &argmax1, &max1, &argmax2, &max2); + printf("%f\n", max1 - max2); + } - currentSumExp = 0.; - randomFloat = (double) rand()/(double)RAND_MAX; - - printf("< %f > is our random number. \n",randomFloat); + if(ctx->proba_mode){ + currentSumExp = 0.; + randomFloat = (double) rand()/(double)RAND_MAX; - // if(ctx->partial_mode){ // NOT YET COMPATIBLE - vcode *vcode_array = feature_table_get_vcode_array(fv, ft); - mvt_code = 0; - for(int i=0; i < ft->classes_nb; i++){ - int b1 = respect_standard_constraint(c, movement_parser_type(vcode_array[i].class_code), movement_parser_label(vcode_array[i].class_code)); - int b2 = respect_stack_constraint(ctx->partial_mode, c, movement_parser_type(vcode_array[i].class_code), movement_parser_label(vcode_array[i].class_code)); - int b3 = respect_buffer_constraint(ctx->partial_mode, c, movement_parser_type(vcode_array[i].class_code), movement_parser_label(vcode_array[i].class_code)); + if(ctx->debug_mode) + printf("< %f > is our random number. \n",randomFloat); + } + if(ctx->partial_mode || ctx->proba_mode){ + vcode *vcode_array = feature_table_get_vcode_array(fv, ft); + mvt_code = 0; + for(int i=0; i < ft->classes_nb; i++){ + int b1 = respect_standard_constraint(c, movement_parser_type(vcode_array[i].class_code), movement_parser_label(vcode_array[i].class_code)); + int b2 = respect_stack_constraint(ctx->partial_mode, c, movement_parser_type(vcode_array[i].class_code), movement_parser_label(vcode_array[i].class_code)); + int b3 = respect_buffer_constraint(ctx->partial_mode, c, movement_parser_type(vcode_array[i].class_code), movement_parser_label(vcode_array[i].class_code)); - if(b1 && b2 && b3){ - currentSumExp += exp(vcode_array[i].score/ProbaDivider - ScoreTranslation); - if(currentSumExp/sumExp >= randomFloat){ - printf("The %d th move has been selected by the probabilistic parser.\n",i); - mvt_code = vcode_array[i].class_code; - break; - } - } + if(b1 && b2 && b3){ + if(ctx->proba_mode){ + currentSumExp += exp((vcode_array[i].score - ScoreTranslation)/ProbaDivider); + if(currentSumExp/sumExp >= randomFloat){ + if(ctx->debug_mode) + printf("The %d th move has been selected by the probabilistic parser.\n",i); + mvt_code = vcode_array[i].class_code; + break; + } + }else{ + mvt_code = vcode_array[i].class_code; + break; + } } - free(vcode_array); - // } + } + free(vcode_array); + } mvt_type = movement_parser_type(mvt_code);