Ejemplo n.º 1
0
int main(void)
{
	int result;
	char input_buffer[256];

	printf("Input Text>");
	gets(input_buffer);

	NODE** tree = NULL;
	tree = &save_sentence(input_buffer);
	printf("\nProcess:Complete!>%s\n",input_buffer);
	print_tree(*tree);

	for(;;)
	{
		print_tree(*tree);
		printf("Delete Sentence>");
		scanf("%s",input_buffer);
		if(strcmp(input_buffer,"Exit")==0)
		{
			break;
		}

		result = delete_sentence(tree,input_buffer);

		switch(result)
		{
		case DELETE_FAIL:
			printf("削除失敗:単語が登録されていません\n");
			break;
		case DELETE_OK:
			printf("削除完了\n");
			break;
		default:
			break;
		}
	}

	tree = clean_node(*tree);
	printf("Clean tree:Complete!\n");

	return 0;
}
Ejemplo n.º 2
0
void process_mbr_pred_str(APPROX_PARAMS *ap, MODEL_PARAMS *mp) {
    char cand_fname[MAX_NAME];
    strcpy(cand_fname, ap->out_file);
    strcat(cand_fname, ".top");

    char out_file[MAX_NAME];
    strcpy(out_file, ap->out_file);
    strcat(out_file, ".mbr_ps");
    
    DEF_FOPEN(ifp, cand_fname, "r");
    DEF_FOPEN(ofp, out_file, "w");

    SENTENCE *sents[ap->cand_num];
    double lprobs[ap->cand_num];
    int ind_num, c = 0;
    printf("[MBR] Reranking...");
    while ((ind_num = read_cand_num(ifp) ) > 0) {
        if (ind_num > ap->cand_num) {
            fprintf(stderr, "Error: Number of candiates in file (%d) in file exceeded maximum (CAND_NUM = %d)\n", ind_num, ap->cand_num);
            exit(1);
        }
        int i;
        for (i = 0; i < ind_num; i++) {
            lprobs[i] =  read_lprob(ifp);
            sents[i] = read_sentence(mp, ifp, 1);
            ASSERT(sents[i] != NULL);
        }

        double del = lprobs[ind_num - 1];
        for (i = 0; i < ind_num; i++) {
            lprobs[i] -= del;
        }
        
        DEF_ALLOC(res_sent, SENTENCE);
        memcpy(res_sent, sents[0], sizeof(SENTENCE));
       
        int t; 
        for (t = 1; t < res_sent->len + 1; t++) {
            double max_gain = -1; int best_id = -1;
            for(i = 0; i < ind_num; i++) {
                double gain = 0;
                int j;
                for (j = 0; j < ind_num; j++) {
                    int equals = (sents[i]->head[t] == sents[j]->head[t]) 
                        && (strcmp(sents[i]->s_deprel[t], sents[j]->s_deprel[t]) == 0);
                    gain += equals * exp(lprobs[j]);
                }
                if (gain > max_gain) {
                    max_gain = gain;
                    best_id = i;
                }    
            }   
        
            ASSERT(best_id >= 0);
            res_sent->head[t] = sents[best_id]->head[t];
            strcpy(res_sent->s_deprel[t], sents[best_id]->s_deprel[t]);
            res_sent->deprel[t] = sents[best_id]->deprel[t];
            
        }
            
        save_sentence(ofp, res_sent, 1);
        free(res_sent);
        for (i = 0; i < ind_num; i++) {
            free(sents[i]);
            sents[i] = NULL;
        }
        
        if  (c != 0 && c % 1 == 0) {
            printf(".");
            fflush(stdout);
        }
        fflush(stdout);
        c++;
    }
    printf("done. Processed %d sentences\n", c);
    fclose(ifp);
    fclose(ofp);
}
Ejemplo n.º 3
0
void process_mbr_rerank(APPROX_PARAMS *ap, MODEL_PARAMS *mp) {
    char cand_fname[MAX_NAME];
    strcpy(cand_fname, ap->out_file);
    strcat(cand_fname, ".top");

    char out_file[MAX_NAME];
    strcpy(out_file, ap->out_file);
    strcat(out_file, ".mbr_rr");
    
    DEF_FOPEN(ifp, cand_fname, "r");
    DEF_FOPEN(ofp, out_file, "w");

    SENTENCE *sents[ap->cand_num];
    double lprobs[ap->cand_num];
    int ind_num, c = 0;
    printf("[MBR] Reranking...");
    while ((ind_num = read_cand_num(ifp) ) > 0) {
        if (ind_num > ap->cand_num) {
            fprintf(stderr, "Error: Number of candiates in file (%d) in file exceeded maximum (CAND_NUM = %d)\n", ind_num, ap->cand_num);
            exit(1);
        }
        int i;
        for (i = 0; i < ind_num; i++) {
            lprobs[i] =  read_lprob(ifp);
            sents[i] = read_sentence(mp, ifp, 1);
            ASSERT(sents[i] != NULL);
        }

        double del = lprobs[ind_num - 1];
        for (i = 0; i < ind_num; i++) {
            lprobs[i] -= del;
        }

        double max_gain = -1; int best_id = -1;
        for(i = 0; i < ind_num; i++) {
            double gain = 0;
            int j;
            for (j = 0; j < ind_num; j++) {
                gain += get_matched_syntax(sents[i], sents[j], 1) * exp(lprobs[j] * ap->mbr_coeff);
            }
            if (gain > max_gain) {
                max_gain = gain;
                best_id = i;
            }    
        }   
        
        ASSERT(best_id >= 0);
        save_sentence(ofp, sents[best_id], 1);

        for (i = 0; i < ind_num; i++) {
            free(sents[i]);
            sents[i] = NULL;
        }
        if  (c != 0 && c % 1 == 0) {
            printf(".");
            fflush(stdout);
        }
        fflush(stdout);
        c++;
    }
    printf("done. Processed %d sentences\n", c);
    fclose(ifp);
    fclose(ofp);
}