Skip to content
Snippets Groups Projects
Select Git revision
  • 731016511501a7cbb66faed21f3ebb27a7df3e17
  • master default protected
  • test-error_interval
3 results

cython_setup.py

Blame
  • simple_decoder_parser_arc_eager.c 4.36 KiB
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<unistd.h>
    #include<getopt.h>
    #include"context.h"
    #include"movement_parser_arc_eager.h"
    #include"feat_fct.h"
    #include"config2feat_vec.h"
    #include"feature_table.h"
    #include"dico.h"
    
    void print_word_buffer(config *c, dico *dico_labels)
    {
      int i;
      word *dep;
      char *label;
      
      for(i=0; i < config_get_buffer(c)->nbelem; i++){
        dep = word_buffer_get_word_n(config_get_buffer(c), i);
        printf("%s\t", word_get_input(dep));
        printf("%d\t", word_get_gov(dep));
        label = (word_get_label(dep) == -1)? NULL : dico_int2string(dico_labels, word_get_label(dep));
        if(label != NULL)
          printf("%s\t", label) ;
        else
          printf("_\t");
        if(word_get_sent_seg(dep) == 1)
          printf("1\n") ;
        else
          printf("0\n");
      }
    }
    
    
    void simple_decoder_parser_arc_eager(context *ctx)
    {
      FILE *f = (ctx->input_filename)? myfopen(ctx->input_filename, "r") : stdin;
      feature_table *ft = feature_table_load(ctx->perc_model_filename, ctx->verbose);
      int root_label;
      int mvt_code;
      int mvt_type;
      int mvt_label;
      float max;
      feat_vec *fv = feat_vec_new(feature_types_nb);
      config *c = NULL;
      int result;
      float entropy;
      float delta;
      int argmax1, argmax2;
      float max1, max2;
      int index;
      
      root_label = dico_string2int(ctx->dico_labels, ctx->root_label);
      if(root_label == -1) root_label = 0;
      
      c = config_initial(f, ctx->mcd_struct, 5);
      while(!config_is_terminal(c)){
        config2feat_vec_cff(ctx->features_model, c, ctx->d_perceptron_features, fv, LOOKUP_MODE);
        mvt_code = feature_table_argmax(fv, ft, &max);
        mvt_type = movement_type(mvt_code);
        mvt_label = movement_label(mvt_code);
        
        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_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);
    
        }
        
        if(ctx->debug_mode){
          fprintf(stdout, "***********************************\n");
          config_print(stdout, c);      
          entropy = feature_table_entropy(fv, ft);
          /* delta = feature_table_diff_scores(fv, ft); */
          feature_table_argmax_1_2(fv, ft, &argmax1, &max1, &argmax2, &max2);
          movement_print(stdout, argmax1, ctx->dico_labels);         
          printf(":\t%f\n", max1);
          movement_print(stdout, argmax2, ctx->dico_labels);         
          printf(":\t%f\n", max2);
          printf("delta = %f\n", max1 - max2);
    
          /* delta = feature_table_first_second(fv, ft); */
           /* printf("entropy = %f delta = %f\n", entropy, delta);  */
           printf("entropy = %f\n",entropy); 
          
          /* movement_print(stdout, mvt_code, ctx->dico_labels);          */
        }
        result = 0;
        switch(mvt_type){
        case MVT_LEFT :
          result = movement_left_arc(c, mvt_label, max);
          break;
        case MVT_RIGHT:
          result = movement_right_arc(c, mvt_label, max);
          break;
        case MVT_REDUCE:
          result = movement_reduce(c, max);
          break;
        case MVT_ROOT:
          result = movement_root(c, max, root_label);
          break;
        case MVT_EOS:
          result = movement_eos(c, max);
          break;
        case MVT_SHIFT:
          result = movement_shift(c, 1, max);
        }
    
        if(result == 0){
          if(ctx->debug_mode){
    	fprintf(stdout, "WARNING : movement cannot be executed doing a SHIFT instead !\n");
          }
          movement_shift(c, 1, max);
        }
      }
      
      if(!ctx->trace_mode)
        print_word_buffer(c, ctx->dico_labels);
      
      config_free(c); 
      feat_vec_free(fv);
      feature_table_free(ft);
      if(ctx->input_filename)
        fclose(f);
    }
    
    #if 0
    void print_word_buffer(config *c, dico *dico_labels)
    {
      int i;
      word *dep;
      char *label;
      int root_position = 0;
      
      for(i=0; i < config_get_buffer(c)->nbelem; i++){
        dep = word_buffer_get_word_n(config_get_buffer(c), i);
        if(word_get_gov(dep) == 0) root_position = i;
        printf("%s\t", word_get_input(dep));
        /*    if(word_get_sent_seg(dep) == 1){
          printf("%d\teos\t1\n", root_position - i);
          }
          else{*/
          printf("%d\t", word_get_gov(dep));
          label = (word_get_label(dep) == -1)? NULL : dico_int2string(dico_labels, word_get_label(dep));
          if(label != NULL)
    	printf("%s\t0\n", label) ;
          else
    	printf("_\t0\n");
        /* } */
      }
    }
    #endif