OPTION* _action_synt_flip(APPROX_PARAMS *ap, MODEL_PARAMS *mp, SENTENCE *sent, int do_free_partials, OPTION *opt) { opt = create_option(SYNT_STEP, 1, FLIP, FLIP, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_act, ACTION_NUM); TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps)); int s = st_pop(&opt->stack); int s_under = st_pop(&opt->stack); st_push(&opt->stack, s); st_push(&opt->stack, s_under); set_links(ap, mp, opt, sent); return opt; }
void st_free(struct stack *stp) { struct st_node *np; assert(stp != NULL); if (st_empty(stp)) { free(stp); stp = NULL; return; } do { np = st_pop(stp); if (np != NULL) { free(np); np = NULL; } } while (st_empty(stp) != 1); if (stp != NULL) { free(stp); stp = NULL; } }
/* test stack */ int main(void) { char buf[MAXLINE]; data_t *p; int i; for (i = 0; i < NUM; i++) { snprintf(buf, MAXLINE, "loop dt :%d", i); fprintf(stderr, "buf: %s\n", buf); if ((p = dt_creat(buf)) == NULL) err_quit("dt_creat failed"); st_push(p); dt_free(p); } while ((p = st_pop()) != NULL) { dt_dump(p, stdout); dt_free(p); } dt_dump(p, stdout); fprintf(stdout, "\n"); dt_free(p); }
OPTION* _action_srl_flip(APPROX_PARAMS *ap, MODEL_PARAMS *mp, SENTENCE *sent, int do_free_partials, OPTION *opt) { //create sequence of reduce operation //update queue and stack opt = create_option(SRL_STEP, 1, SRL_FLIP, SRL_FLIP, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_act, ACTION_NUM); TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps)); int srl_s = st_pop(&opt->srl_stack); int srl_s_under = st_pop(&opt->srl_stack); st_push(&opt->srl_stack, srl_s); st_push(&opt->srl_stack, srl_s_under); set_links(ap, mp, opt, sent); return opt; }
void calc(char op) { int v, v1, v2, v3; char t1, t2, t3; while( stack_index >= 3 || op == '=' ) { if( stack_index == 1 ) break; st_pop( &t3, &v3 ); if( t3 != 0 ) { printf("Syntax Error: Number need.\n"); } st_pop( &t2, &v2 ); if( t2 == 0 ) { printf("Syntax Error: Operator need.\n"); } st_pop( &t1, &v1 ); if( t3 != 0 ) { printf("Syntax Error: Number need.\n"); } if( op == '*' || op == '/' ) { switch(t2) { case '*': v = v1 * v3; break; case '/': v = v1 / v3; break; case '+': case '-': st_push( t1, v1 ); st_push( t2, v2 ); st_push( t3, v3 ); return; } } if( op == '+' || op == '-' || op == '=' ) { switch(t2) { case '*': v = v1 * v3; break; case '/': v = v1 / v3; break; case '+': v = v1 + v3; break; case '-': v = v1 - v3; break; } } st_push( 0, v); if( op != '=' ) break; } }
int main() { char line[256]; char *p; char c; char t; int v; int i = 0; int number = 0; st_init(); printf("INPUT: "); gets(line); p = line; while( (c=*p) != 0 && c!='\n') { if( c == ' ' || c == '\t' ) { p++; continue;} else if( '9' >= c && c >= '0' ) { p = get_number(p, &number); t = 0; st_push( t, number); } else { switch( c ) { case '*': case '/': case '+': case '-': calc( c ); st_push( c, 0 ); break; } p ++; } st_print(); } c = '='; calc( c ); st_print(); st_pop( &t, &v ); printf("Result: %d \n", v); }
//processes sentence and creates derivations //returns tail of the derivation OPTION *get_derivation(APPROX_PARAMS *ap, MODEL_PARAMS *mp, SENTENCE *sent, int do_free_partials) { /* if (ap->intern_synt_deproj == DEPROJ_EXHAUSTIVE && !ap->is_synt_early_reduce) { fprintf(stderr, "Error: exhaustive internal syntactic projectivization with late reduce strategy is not supported\n"); exit(EXIT_FAILURE); } if (ap->intern_srl_deproj == DEPROJ_EXHAUSTIVE && !ap->is_srl_early_reduce) { fprintf(stderr, "Error: exhaustive internal SRL projectivization with late reduce strategy is not supported\n"); exit(EXIT_FAILURE); } */ //Debug // printf("Next sentence ============\n"); ASSERT(ap->input_offset == 0 || ap->input_offset == 1); STACK st; st.size = 0; STACK srl_st; srl_st.size = 0; //OPTION *head = create_option(SYNT_STEP, 1, -1, -1, NULL, 1, &st, &srl_st, 1, pt_alloc(sent->len), ps_alloc(sent->len), &mp->out_link_act, ACTION_NUM); OPTION *head = create_option(SRL_STEP, 1, -1, -1, NULL, 1, &st, &srl_st, 1, pt_alloc(sent->len), ps_alloc(sent->len), &mp->out_link_act, ACTION_NUM); ///st_push(&head->srl_stack, 0); //JH allow 0 as an SRL argument set_links(ap, mp, head, sent); ///printf("new sentence\n");/// OPTION *opt = head; while (opt->queue != sent->len + 1) { //get next possible actions ACTION_SET as = get_next_actions(ap, mp, opt, sent); set_mask(mp, opt, &as); //===================== SYNTAX ====================================================== if (ap->is_synt_early_reduce && as.acts[RED]) { int s = st_peek(&opt->stack); if (everything_complete(opt->pt, sent, s)) { if (ap->parsing_mode != 3 || sent->head[s] != 0 || sent->deprel[s] != ROOT_DEPREL) { opt = _action_synt_red(ap, mp, sent, do_free_partials, opt); //DEBUG //printf("RED\n"); continue; } } } //check LA if (as.acts[LA]) { int d = st_peek(&opt->stack); int h = opt->queue; if (sent->head[d] == h) { //create sequence of LA operations //update queue and stack opt = create_option(SYNT_STEP, 0, opt->previous_act, LA, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_la_label, mp->deprel_num); TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps)); //opt->previous_option->pt = NULL; set_links(ap, mp, opt, sent); opt = create_option(SYNT_STEP, 1, LA, sent->deprel[d], opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_act, ACTION_NUM); TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps)); //opt->previous_option->pt = NULL; pt_add_link(opt->pt, h, d, sent->deprel[d]); opt->pt->right_connections_num[d]++; if (ap->intern_synt_deproj == DEPROJ_NO) { st_pop(&opt->stack); } else { // Do Nothing!!! } set_links(ap, mp, opt, sent); //DEBUG //printf("LA\n"); continue; } } //check RA if (as.acts[RA]) { int d = opt->queue; int h = st_peek(&opt->stack); //when agrees if (sent->head[d] == h) { //means if stack is not empty or we can make RA with empty stack if (h != 0 || (ap->parsing_mode >= 3 && sent->deprel[d] != ROOT_DEPREL // can do only once && opt->pt->nodes[d].deprel == ROOT_DEPREL)) { //create sequence of RA operations //update queue and stack opt = create_option(SYNT_STEP, 0, opt->previous_act, RA, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_ra_label, mp->deprel_num); TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps)); set_links(ap, mp, opt, sent); opt = create_option(SYNT_STEP, 1, RA, sent->deprel[d], opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_act, ACTION_NUM); TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps)); pt_add_link(opt->pt, h, d, sent->deprel[d]); if (h != 0) { opt->pt->right_connections_num[h]++; } set_links(ap, mp, opt, sent); //DEBUG //printf("RA\n"); continue; } } } // check FLIP if (as.acts[FLIP] && ap->intern_synt_deproj == DEPROJ_EXHAUSTIVE) { int s = st_peek(&opt->stack); int s_already_conns = opt->pt->right_connections_num[s]; int s_under = st_look(&opt->stack, 1); int s_under_already_conns = opt->pt->right_connections_num[s_under]; int top_is_usl = (sent->synt_right_degree[s] == s_already_conns); int under_top_is_usl = (sent->synt_right_degree[s_under] == s_under_already_conns); if (ap->is_synt_early_reduce || (!top_is_usl && !under_top_is_usl) ) { // s_under will be active earlier than s if (lexicograph_ignore_double_entries_comp( sent->synt_right_indices[s_under] + s_under_already_conns, sent->synt_right_degree[s_under] - s_under_already_conns, sent->synt_right_indices[s] + s_already_conns, sent->synt_right_degree[s] - s_already_conns) < 0) { //create sequence of reduce operation //update queue and stack opt = _action_synt_flip(ap, mp, sent, do_free_partials, opt); //DEBUG printf("(SYNT) FLIP\n"); continue; } } else { // if a top word can be reduce from the top if (top_is_usl) { int first_usf = get_useful(&opt->stack, 1, opt->pt->right_connections_num, sent->synt_right_degree); if (first_usf > 0) { // first 'useful' word in the stack should be attached to front of queue -- start remioving useless words // next words will be removed on the next rounds int next_connection = sent->synt_right_indices[first_usf][opt->pt->right_connections_num[first_usf]]; if (next_connection == opt->queue) { ASSERT(as.acts[RED]); opt = _action_synt_red(ap, mp, sent, do_free_partials, opt); //DEBUG //printf("RED\n"); continue; } } } else { // if int second_usf = get_useful(&opt->stack, 1, opt->pt->right_connections_num, sent->synt_right_degree); ASSERT(second_usf != s_under); if (second_usf > 0) { int second_usf_already_conns = opt->pt->right_connections_num[second_usf]; if (lexicograph_ignore_double_entries_comp( sent->synt_right_indices[second_usf] + second_usf_already_conns, sent->synt_right_degree[second_usf] - second_usf_already_conns, sent->synt_right_indices[s] + s_already_conns, sent->synt_right_degree[s] - s_already_conns) < 0) { opt = _action_synt_flip(ap, mp, sent, do_free_partials, opt); as = get_next_actions(ap, mp, opt, sent); set_mask(mp, opt, &as); ASSERT(as.acts[RED]); opt = _action_synt_red(ap, mp, sent, do_free_partials, opt); // DEBUG printf("FLIP, followed by RED\n"); //printf("RED, preceded by FLIP\n"); continue; } } } // !top_is_usl } // !ap->is_synt_early_reduce } // FLIP //check SWITCH if (as.acts[SWITCH]) { int q = opt->queue; if (left_part_complete(opt->pt, sent, q)) { //if atached to root with not ROOT_DEPREL and parsing_mode >= 3 then RA, not SHIFT should be peformed //option sent->deprel[q] == opt->pt->nodes[q]->deprel - relates to RA- + SHIFT sequence and means that RA- is actually preformed //and q is already attached if (sent->head[q] != 0 || ap->parsing_mode < 3 || (ap->parsing_mode >= 3 && (sent->deprel[q] == ROOT_DEPREL || sent->deprel[q] == opt->pt->nodes[q].deprel))) { opt = create_option(SRL_STEP, 1, SWITCH, SWITCH, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_act, ACTION_NUM); /* opt = create_option(SYNT_STEP, 1, SWITCH, SWITCH, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_act, ACTION_NUM); */ TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps)); set_links(ap, mp, opt, sent); //DEBUG //printf("SWITCH\n"); continue; } } } //check RED if (!ap->is_synt_early_reduce && as.acts[RED]) { int s = st_peek(&opt->stack); if (everything_complete(opt->pt, sent, s)) { if (ap->parsing_mode != 3 || sent->head[s] != 0 || sent->deprel[s] != ROOT_DEPREL) { opt = _action_synt_red(ap, mp, sent, do_free_partials, opt); //DEBUG //printf("RED\n"); continue; } } } // check FLIP if (as.acts[FLIP] && ap->intern_synt_deproj != DEPROJ_EXHAUSTIVE) { opt = _action_synt_flip(ap, mp, sent, do_free_partials, opt); //DEBUG printf("(SYNT) FLIP\n"); continue; } //===================== WORD PREDICTION ============================================= if (as.acts[SHIFT]) { //create sequence of shift operation //update queue and stack opt = create_option(SYNT_STEP, 0, opt->previous_act, SHIFT, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_pos, get_pos_out_num()); TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps)); set_links(ap, mp, opt, sent); opt = create_word_prediction_seq(ap, mp, sent, opt, do_free_partials); //DEBUG //printf("SHIFT\n"); continue; } //===================== SRL ====================================================== if (ap->is_srl_early_reduce && as.acts[SRL_RED]) { int srl_s = st_peek(&opt->srl_stack); if (srl_everything_complete(opt->ps, sent, srl_s)) { opt = _action_srl_red(ap, mp, sent, do_free_partials, opt); //DEBUG // printf("SRL_RED\n"); continue; } } //check SRL_LA int q_bank = sent->bank[opt->queue]; ///if (q_bank >= 0)/// /// printf("srl_la 0: %d %d; %d %d\n", q_bank, as.acts[SRL_LA[q_bank]], st_peek(&opt->srl_stack), opt->queue);/// if (q_bank >= 0 && as.acts[SRL_LA[q_bank]]) { int d = st_peek(&opt->srl_stack); int h = opt->queue; int role = next_srl_role(0, sent, h, d); ///printf("srl_la 1: %d %d %d\n", d, h, role);/// if (role >= 0) { int i = 0; for (; role >= 0; role = next_srl_role(i, sent, h, d)) { i++; //create sequence of SRL_LA operations //update queue and stack opt = create_option(SRL_STEP, 0, opt->previous_act, SRL_LA[q_bank], opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_sem_la_label[q_bank], mp->role_num[q_bank]); TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps)); set_links(ap, mp, opt, sent); opt = create_option(SRL_STEP, 1, SRL_LA[q_bank], role, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_act, ACTION_NUM); TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps)); ps_add_link(opt->ps, h, d, role); opt->ps->right_connections_num[d]++; set_links(ap, mp, opt, sent); //DEBUG //printf("SRL_LA[%d]\n", q_bank); } continue; } } q_bank = -1; //check SRL_RA int s_bank = sent->bank[st_peek(&opt->srl_stack)]; if (s_bank >= 0 && as.acts[SRL_RA[s_bank]]) { int d = opt->queue; int h = st_peek(&opt->srl_stack); int role = next_srl_role(0, sent, h, d); ///printf("srl_ra: %d %d %d\n", d, h, role);/// //when agrees if (role >= 0) { int i = 0; for (; role >= 0; role = next_srl_role(i, sent, h, d)) { ASSERT(i == 0); i++; //create sequence of RA operations //update queue and stack opt = create_option(SRL_STEP, 0, opt->previous_act, SRL_RA[s_bank], opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_sem_ra_label[s_bank], mp->role_num[s_bank]); TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps)); set_links(ap, mp, opt, sent); opt = create_option(SRL_STEP, 1, SRL_RA[s_bank], role, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_act, ACTION_NUM); TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps)); ps_add_link(opt->ps, h, d, role); opt->ps->right_connections_num[h]++; set_links(ap, mp, opt, sent); //DEBUG //printf("SRL-RA[%d]\n", s_bank); } // role continue; } } if (as.acts[SRL_FLIP] && ap->intern_srl_deproj == DEPROJ_EXHAUSTIVE) { int srl_s = st_peek(&opt->srl_stack); int srl_s_already_conns = opt->ps->right_connections_num[srl_s]; int srl_s_under = st_look(&opt->srl_stack, 1); int srl_s_under_already_conns = opt->ps->right_connections_num[srl_s_under]; int top_is_usl = (sent->srl_right_degree[srl_s] == srl_s_already_conns); int under_top_is_usl = (sent->srl_right_degree[srl_s_under] == srl_s_under_already_conns); if (ap->is_srl_early_reduce || (!top_is_usl && !under_top_is_usl) ) { // srl_s_under will be active earlier than srl_s if (lexicograph_ignore_double_entries_comp( sent->srl_right_indices[srl_s_under] + srl_s_under_already_conns, sent->srl_right_degree[srl_s_under] - srl_s_under_already_conns, sent->srl_right_indices[srl_s] + srl_s_already_conns, sent->srl_right_degree[srl_s] - srl_s_already_conns) < 0) { opt = _action_srl_flip(ap, mp, sent, do_free_partials, opt); //DEBUG //TMP printf("SRL_FLIP\n"); continue; } } else { // if a top word can be reduce from the top if (top_is_usl) { int first_usf = get_useful(&opt->srl_stack, 1, opt->ps->right_connections_num, sent->srl_right_degree); if (first_usf > 0) { // first 'useful' word in the stack should be attached to front of queue -- start remioving useless words // next words will be removed on the next rounds int next_connection = sent->srl_right_indices[first_usf][opt->ps->right_connections_num[first_usf]]; if (next_connection == opt->queue) { ASSERT(as.acts[SRL_RED]); opt = _action_srl_red(ap, mp, sent, do_free_partials, opt); continue; //DEBUG //printf("SRL_RED, useless reduction\n"); } } } else { // if int second_usf = get_useful(&opt->srl_stack, 1, opt->ps->right_connections_num, sent->srl_right_degree); ASSERT(second_usf != srl_s_under); if (second_usf > 0) { int second_usf_already_conns = opt->ps->right_connections_num[second_usf]; if (lexicograph_ignore_double_entries_comp( sent->srl_right_indices[second_usf] + second_usf_already_conns, sent->srl_right_degree[second_usf] - second_usf_already_conns, sent->srl_right_indices[srl_s] + srl_s_already_conns, sent->srl_right_degree[srl_s] - srl_s_already_conns) < 0) { opt = _action_srl_flip(ap, mp, sent, do_free_partials, opt); as = get_next_actions(ap, mp, opt, sent); set_mask(mp, opt, &as); ASSERT(as.acts[SRL_RED]); opt = _action_srl_red(ap, mp, sent, do_free_partials, opt); // DEBUG printf("SRL_FLIP, followed by SRL_RED\n"); //printf("SRL_RED, preceded by SRL_FLIP\n"); continue; } } } // !top_is_usl } // !ap->is_synt_early_reduce } // FLIP //check SRL_SWITCH if (as.acts[SRL_SWITCH]) { int q = opt->queue; if (srl_left_part_complete(opt->ps, sent, q)) { opt = create_option(SYNT_STEP, 1, SRL_SWITCH, SRL_SWITCH, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_act, ACTION_NUM); TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps)); set_links(ap, mp, opt, sent); //DEBUG //printf("SRL_SWITCH\n"); continue; } } //check SRL_RED if (!ap->is_srl_early_reduce && as.acts[SRL_RED]) { int srl_s = st_peek(&opt->srl_stack); if (srl_everything_complete(opt->ps, sent, srl_s)) { opt = _action_srl_red(ap, mp, sent, do_free_partials, opt); //DEBUG //printf("SRL_RED\n"); continue; } } if (as.acts[PREDIC_NO]) { int q = opt->queue; if (sent->sense[q] < 0) { opt = create_option(SRL_STEP, 1, PREDIC_NO, PREDIC_NO, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_act, ACTION_NUM); TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps)); set_links(ap, mp, opt, sent); //DEBUG //printf("PREDIC_NO\n"); continue; } } if (as.acts[PREDIC_YES]) { int q = opt->queue; int q_sense = sent->sense[q]; if (q_sense >= 0) { int q_bank = sent->bank[q]; int q_lemma = sent->lemma[q]; //create sequence of predicate prediction operations opt = create_option(SRL_STEP, 0, opt->previous_act, PREDIC_YES, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_sense[q_bank][q_lemma][0], mp->sense_num[q_bank][q_lemma]); TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps)); set_links(ap, mp, opt, sent); opt = create_option(SRL_STEP, 1, PREDIC_YES, q_sense, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_act, ACTION_NUM); TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps)); ps_set_sense(opt->ps, q, q_sense); set_links(ap, mp, opt, sent); //DEBUG //printf("PREDIC_YES\n"); continue; } } if (as.acts[SRL_FLIP] && ap->intern_srl_deproj != DEPROJ_EXHAUSTIVE) { opt = _action_srl_flip(ap, mp, sent, do_free_partials, opt); //DEBUG //printf("SRL_FLIP\n"); continue; } // if stalled in syntactic part if (IS_NOW_SYNTAX(opt->previous_act)) { //DEBUG printf("STALLED IN SYNTAX\n"); } else { //DEBUG printf("STALLED IN SRL\n"); } ASSERT(0); if (IS_NOW_SYNTAX(opt->previous_act)) { fprintf(stderr, "(Syntax) Problem: wrong parsing order or in input file (e.g. NON-PROJECTIVITY): can't make an action\n"); printf("(Syntax) Problem: wrong parsing order or in input file (e.g. NON-PROJECTIVITY): can't make an action\n"); } else { //TMP fprintf(stderr, "(SRL) Problem: wrong parsing order or in input file (e.g. NON-PROJECTIVITY): can't make an action\n"); //TMP printf("(SRL) Problem: wrong parsing order or in input file (e.g. NON-PROJECTIVITY): can't make an action\n"); } fprintf(stderr, "Try changing PARSING_ORDER in the configuration file to a larger value\n"); // printf("%s\n", print_sent(sent, 1)); //TODO this should be a fatal error fprintf(stderr, "Warning: returning partial derivation\n"); DEF_ALLOC(as_final, ACTION_SET); bzero(as_final, sizeof(ACTION_SET)); set_mask(mp, opt, as_final); free(as_final); TRY_FREE_PARTIALS(&(opt->pt), &(opt->ps)); return opt; //TODO restore! //exit(1); } //DEBUG //TMP printf("==== SENT FINISHED ===== \n"); if (!check_pt_t_equality(opt->pt, sent)) { fprintf(stderr, "Presumably: bug in parsing or in input file (e.g. NON-PROJECTIVITY): resulting syntactic trees do not match\n"); exit(1); } if (!check_ps_t_equality(opt->ps, sent)) { fprintf(stderr, "Presumably: bug in SRL parsing or in input file (e.g. NON-PROJECTIVITY): resulting predicate argument structures do not match\n"); } DEF_ALLOC(as, ACTION_SET); bzero(as, sizeof(ACTION_SET)); set_mask(mp, opt, as); free(as); TRY_FREE_PARTIALS(&(opt->pt), &(opt->ps)); return opt; }
//TODO don't forget to move pt OPTION_LIST *process_opt(APPROX_PARAMS *ap, MODEL_PARAMS *mp, SENTENCE *sent, OPTION **new_b_options, int *new_cand_num, OPTION *opt, OPTION_LIST *ol) { /* if ol longer than QUEUE_SIZE_LIMIT, then prune to half that length. Requires calling with opt=NULL to initialize count */ static int queue_size; if (opt == NULL) { queue_size = 0; OPTION_LIST *ol2; for (ol2 = ol; ol2 != NULL; ol2 = ol2->prev) queue_size++; return ol; } queue_size--; ASSERT(opt->outputs.num == ACTION_NUM); double min_lprob = - MAXDOUBLE; double min_lprob_with_pred = -MAXDOUBLE; if (*new_cand_num == ap->beam) { min_lprob = new_b_options[ap->beam - 1]->lprob - max_word_pred_lprob(ap, mp, sent, new_b_options, *new_cand_num, opt); min_lprob_with_pred = new_b_options[ap->beam - 1]->lprob; } //do not need to consider if (opt->lprob < min_lprob) { pt_free(&(opt->pt)); ps_free(&(opt->ps)); free_options(opt); return ol; } //they are not yet computed ACTION_SET as = get_next_actions(ap, mp, opt, sent); set_mask(mp, opt, &as); comp_option(ap, mp, opt); // ----------------------- SYNTAX ----------------------------------------------- //check SHIFT (first because it is the operation that updates candidate list) if (as.acts[SHIFT]) { if (log(opt->outputs.q[SHIFT]) + opt->lprob >= min_lprob) { //create sequence of shift operation //update queue and stack //TODO Implement more efficiently pruning after each decision OPTION *as_opt = create_option(SYNT_STEP, 0, opt->previous_act, SHIFT, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_pos, get_pos_out_num()); int pos = sent->pos[opt->period + ap->input_offset]; int feat = sent->feat_out[opt->period + ap->input_offset]; int word = sent->word_out[opt->period + ap->input_offset]; set_links(ap, mp, as_opt, sent); comp_option(ap, mp, as_opt); if (log(as_opt->outputs.q[pos]) + as_opt->lprob < min_lprob_with_pred) { pt_free(&(as_opt->pt)); ps_free(&(as_opt->ps)); free_option(as_opt); } else { OPTION *last_opt = create_word_prediction_seq(ap, mp, sent, as_opt, 1); OPTION *feat_opt = last_opt->previous_option->previous_option; comp_option(ap, mp, feat_opt); if (log(feat_opt->outputs.q[feat]) + feat_opt->lprob < min_lprob_with_pred) { pt_free(&(last_opt->pt)); ps_free(&(last_opt->ps)); OPTION *po = last_opt->previous_option, *ppo = last_opt->previous_option->previous_option; free_option(last_opt); free_option(po); free_option(ppo); free_option(as_opt); } else { OPTION *word_opt = last_opt->previous_option; comp_option(ap, mp, word_opt); // if (log(word_opt->outputs.q[word]) + word_opt->lprob < min_lprob_with_pred) { if (log(word_opt->outputs.q[word]) + word_opt->lprob < min_lprob_with_pred + SMALL_POS_VAL) { pt_free(&(last_opt->pt)); ps_free(&(last_opt->ps)); OPTION *po = last_opt->previous_option, *ppo = last_opt->previous_option->previous_option; free_option(last_opt); free_option(po); free_option(ppo); free_option(as_opt); } else { fill_lprob(last_opt); add_to_best_list(ap, new_b_options, new_cand_num, last_opt); } } } } } //update if (*new_cand_num == ap->beam) { min_lprob = new_b_options[ap->beam - 1]->lprob - max_word_pred_lprob(ap, mp, sent, new_b_options, *new_cand_num, opt); min_lprob_with_pred = new_b_options[ap->beam - 1]->lprob; // not necessary, but could save space: //ol = prune_options(ol, min_lprob); } //check LA if (as.acts[LA]) { int d = st_peek(&opt->stack); int h = opt->queue; if (log(opt->outputs.q[LA]) + opt->lprob >= min_lprob) { //create sequence of LA operations //update queue and stack OPTION *as_opt = create_option(SYNT_STEP, 0, opt->previous_act, LA, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_la_label, mp->deprel_num); set_links(ap, mp, as_opt, sent); comp_option(ap, mp, as_opt); double deprel_min_prob = exp(min_lprob - as_opt->lprob); int best_rels[MAX_DEPREL_SIZE]; int fanout = get_best_labels(ap, as_opt, deprel_min_prob, mp->deprel_num, best_rels); int i; for (i = 0; i < fanout; i++) { OPTION *new_opt = create_option_light(SYNT_STEP, 1, LA, best_rels[i], as_opt, as_opt->period, &as_opt->stack, &as_opt->srl_stack, as_opt->queue, pt_clone(as_opt->pt), ps_clone(as_opt->ps), &mp->out_link_act, ACTION_NUM); pt_add_link(new_opt->pt, h, d, best_rels[i]); if (ap->intern_synt_deproj == DEPROJ_NO) { st_pop(&new_opt->stack); } else { // Do nothing } set_links(ap, mp, new_opt, sent); fill_lprob(new_opt); ol = increase_list_asc(ol, new_opt); queue_size++; } pt_free(&(as_opt->pt)); ps_free(&(as_opt->ps)); as_opt->pt = NULL; as_opt->ps = NULL; if (fanout == 0) { free_option(as_opt); } } } //check RA if (as.acts[RA]) { int d = opt->queue; int h = st_peek(&opt->stack); //when agrees if (log(opt->outputs.q[RA]) + opt->lprob >= min_lprob) { //create sequence of RA operations //update queue and stack OPTION *as_opt = create_option(SYNT_STEP, 0, opt->previous_act, RA, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_ra_label, mp->deprel_num); set_links(ap, mp, as_opt, sent); comp_option(ap, mp, as_opt); double deprel_min_prob = exp(min_lprob - as_opt->lprob); int best_rels[MAX_DEPREL_SIZE]; int fanout = get_best_labels(ap, as_opt, deprel_min_prob, mp->deprel_num, best_rels); int i; for (i = 0; i < fanout; i++) { OPTION *new_opt = create_option_light(SYNT_STEP, 1, RA, best_rels[i], as_opt, as_opt->period, &as_opt->stack, &as_opt->srl_stack, as_opt->queue, pt_clone(as_opt->pt), ps_clone(as_opt->ps), &mp->out_link_act, ACTION_NUM); pt_add_link(new_opt->pt, h, d, best_rels[i]); set_links(ap, mp, new_opt, sent); fill_lprob(new_opt); ol = increase_list_asc(ol, new_opt); queue_size++; } pt_free(&(as_opt->pt)); ps_free(&(as_opt->ps)); as_opt->pt = NULL; as_opt->ps = NULL; if (fanout == 0) { free_option(as_opt); } //TODO Try processing immediately SHIFT operation //should speed-up (it will make it way to best_list and //prune other optinos) } } //check RED if (as.acts[RED]) { if (log(opt->outputs.q[RED]) + opt->lprob >= min_lprob) { //create sequence of reduce operation //update queue and stack OPTION *as_opt = create_option_light(SYNT_STEP, 1, RED, RED, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_act, ACTION_NUM); st_pop(&as_opt->stack); set_links(ap, mp, as_opt, sent); fill_lprob(as_opt); ol = increase_list_asc(ol, as_opt); queue_size++; } } //check SWITCH if (as.acts[SWITCH]) { if (log(opt->outputs.q[SWITCH]) + opt->lprob >= min_lprob) { OPTION *as_opt = create_option_light(SRL_STEP, 1, SWITCH, SWITCH, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_act, ACTION_NUM); // OPTION *as_opt = create_option_light(SYNT_STEP, 1, SWITCH, SWITCH, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, // pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_act, ACTION_NUM); set_links(ap, mp, as_opt, sent); fill_lprob(as_opt); ol = increase_list_asc(ol, as_opt); queue_size++; } } // ----------------------- SEMANTICS ----------------------------------------------- //check PREDIC_NO if (as.acts[PREDIC_NO]) { if (log(opt->outputs.q[PREDIC_NO]) + opt->lprob >= min_lprob) { OPTION *as_opt = create_option_light(SRL_STEP, 1, PREDIC_NO, PREDIC_NO, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_act, ACTION_NUM); set_links(ap, mp, as_opt, sent); fill_lprob(as_opt); ol = increase_list_asc(ol, as_opt); queue_size++; } } if (as.acts[PREDIC_YES]) { int q = opt->queue; int q_bank = sent->bank[q]; int q_lemma = sent->lemma[q]; if (log(opt->outputs.q[PREDIC_YES]) + opt->lprob >= min_lprob) { //create sequence of predicate prediction operations OPTION *as_opt = create_option(SRL_STEP, 0, opt->previous_act, PREDIC_YES, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_sense[q_bank][q_lemma][0], mp->sense_num[q_bank][q_lemma]); set_links(ap, mp, as_opt, sent); comp_option(ap, mp, as_opt); double sense_min_prob = exp(min_lprob - as_opt->lprob); int best_senses[MAX_SENSE_SIZE]; int fanout = get_best_labels(ap, as_opt, sense_min_prob, mp->sense_num[q_bank][q_lemma], best_senses); int i; for (i = 0; i < fanout; i++) { OPTION *new_opt = create_option_light(SRL_STEP, 1, PREDIC_YES, best_senses[i], as_opt, as_opt->period, &as_opt->stack, &as_opt->srl_stack, as_opt->queue, pt_clone(as_opt->pt), ps_clone(as_opt->ps), &mp->out_link_act, ACTION_NUM); ps_set_sense(new_opt->ps, q, best_senses[i]); set_links(ap, mp, new_opt, sent); fill_lprob(new_opt); ol = increase_list_asc(ol, new_opt); queue_size++; } pt_free(&(as_opt->pt)); ps_free(&(as_opt->ps)); as_opt->pt = NULL; as_opt->ps = NULL; if (fanout == 0) { free_option(as_opt); } } } //check SRL_LA int q_bank = sent->bank[opt->queue]; if (q_bank >= 0 && as.acts[SRL_LA[q_bank]]) { int d = st_peek(&opt->srl_stack); int h = opt->queue; if (log(opt->outputs.q[SRL_LA[q_bank]]) + opt->lprob >= min_lprob) { //create sequence of SRL_LA operations //update queue and stack OPTION *as_opt = create_option(SRL_STEP, 0, opt->previous_act, SRL_LA[q_bank], opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_sem_la_label[q_bank], mp->role_num[q_bank]); set_links(ap, mp, as_opt, sent); comp_option(ap, mp, as_opt); double rel_min_prob = exp(min_lprob - as_opt->lprob); int best_rels[MAX_ROLE_SIZE]; int fanout = get_best_labels(ap, as_opt, rel_min_prob, mp->role_num[q_bank], best_rels); int i; for (i = 0; i < fanout; i++) { OPTION *new_opt = create_option_light(SRL_STEP, 1, SRL_LA[q_bank], best_rels[i], as_opt, as_opt->period, &as_opt->stack, &as_opt->srl_stack, as_opt->queue, pt_clone(as_opt->pt), ps_clone(as_opt->ps), &mp->out_link_act, ACTION_NUM); ps_add_link(new_opt->ps, h, d, best_rels[i]); set_links(ap, mp, new_opt, sent); fill_lprob(new_opt); ol = increase_list_asc(ol, new_opt); queue_size++; } pt_free(&(as_opt->pt)); ps_free(&(as_opt->ps)); as_opt->pt = NULL; as_opt->ps = NULL; if (fanout == 0) { free_option(as_opt); } } } q_bank = -1; //check SRL_RA int s_bank = sent->bank[st_peek(&opt->srl_stack)]; if (s_bank >= 0 && as.acts[SRL_RA[s_bank]]) { int d = opt->queue; int h = st_peek(&opt->srl_stack); if (log(opt->outputs.q[SRL_RA[s_bank]]) + opt->lprob >= min_lprob) { //create sequence of RA operations //update queue and stack OPTION *as_opt = create_option(SRL_STEP, 0, opt->previous_act, SRL_RA[s_bank], opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_sem_ra_label[s_bank], mp->role_num[s_bank]); set_links(ap, mp, as_opt, sent); comp_option(ap, mp, as_opt); double rel_min_prob = exp(min_lprob - as_opt->lprob); int best_rels[MAX_ROLE_SIZE]; int fanout = get_best_labels(ap, as_opt, rel_min_prob, mp->role_num[s_bank], best_rels); int i; for (i = 0; i < fanout; i++) { OPTION *new_opt = create_option_light(SRL_STEP, 1, SRL_RA[s_bank], best_rels[i], as_opt, as_opt->period, &as_opt->stack, &as_opt->srl_stack, as_opt->queue, pt_clone(as_opt->pt), ps_clone(as_opt->ps), &mp->out_link_act, ACTION_NUM); ps_add_link(new_opt->ps, h, d, best_rels[i]); set_links(ap, mp, new_opt, sent); fill_lprob(new_opt); ol = increase_list_asc(ol, new_opt); queue_size++; } pt_free(&(as_opt->pt)); ps_free(&(as_opt->ps)); as_opt->pt = NULL; as_opt->ps = NULL; if (fanout == 0) { free_option(as_opt); } } } s_bank = -1; //check SRL_RED if (as.acts[SRL_RED]) { if (log(opt->outputs.q[SRL_RED]) + opt->lprob >= min_lprob) { //create sequence of reduce operation //update queue and stack OPTION *as_opt = create_option_light(SRL_STEP, 1, SRL_RED, SRL_RED, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_act, ACTION_NUM); st_pop(&as_opt->srl_stack); set_links(ap, mp, as_opt, sent); fill_lprob(as_opt); ol = increase_list_asc(ol, as_opt); queue_size++; } } //check SRL_SWITCH if (as.acts[SRL_SWITCH]) { if (log(opt->outputs.q[SRL_SWITCH]) + opt->lprob >= min_lprob) { OPTION *as_opt = create_option_light(SYNT_STEP, 1, SRL_SWITCH, SRL_SWITCH, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_act, ACTION_NUM); set_links(ap, mp, as_opt, sent); fill_lprob(as_opt); ol = increase_list_asc(ol, as_opt); queue_size++; } } if (as.acts[SRL_FLIP]) { if (log(opt->outputs.q[SRL_FLIP]) + opt->lprob >= min_lprob) { OPTION *as_opt = create_option_light(SRL_STEP, 1, SRL_FLIP, SRL_FLIP, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_act, ACTION_NUM); int srl_s = st_pop(&as_opt->srl_stack); int srl_s_under = st_pop(&as_opt->srl_stack); st_push(&as_opt->srl_stack, srl_s); st_push(&as_opt->srl_stack, srl_s_under); set_links(ap, mp, as_opt, sent); fill_lprob(as_opt); ol = increase_list_asc(ol, as_opt); queue_size++; } } pt_free(&(opt->pt)); ps_free(&(opt->ps)); opt->pt = NULL; opt->ps = NULL; if (opt->next_options->prev == NULL) { free_options(opt); } if (queue_size > QUEUE_SIZE_LIMIT) { printf("Warning: indiscriminately pruning search queue from length %d to %d\n", queue_size, (int) (QUEUE_SIZE_LIMIT / 1.5)); OPTION_LIST *ol2; int cnt = 0; for (ol2 = ol; ol2 != NULL; ol2 = ol2->prev) { cnt++; if (cnt > (int) (QUEUE_SIZE_LIMIT / 1.5)) { // reduce to 2/3 of limit prune_options(ol2, 0.0); break; } } queue_size = cnt - 1; } return ol; }