예제 #1
0
static int
write_nbest(ps_decoder_t *ps, char const *nbestdir, char const *uttid)
{
    cmd_ln_t *config;
    char *outfile;
    FILE *fh;
    ps_nbest_t *nbest;
    int32 i, n, score;
    const char* hyp;

    config = ps_get_config(ps);
    outfile = string_join(nbestdir, "/", uttid,
                          cmd_ln_str_r(config, "-nbestext"), NULL);
    n = cmd_ln_int32_r(config, "-nbest");
    fh = fopen(outfile, "w");
    if (fh == NULL) {
        E_ERROR_SYSTEM("Failed to write a lattice to file %s\n", outfile);
        return -1;
    }
    nbest = ps_nbest(ps, 0, -1, NULL, NULL);
    for (i = 0; i < n && nbest && (nbest = ps_nbest_next(nbest)); i++) {
        hyp = ps_nbest_hyp(nbest, &score);
        fprintf(fh, "%s %d\n", hyp, score);        
    }
    if (nbest)
	ps_nbest_free(nbest);
    fclose(fh);

    return 0;
}
예제 #2
0
int
main(int argc, char *argv[])
{
	ps_decoder_t *ps;
	ps_nbest_t *nbest;
	cmd_ln_t *config;
	FILE *rawfh;
	char const *hyp;
	int32 score, n;

	TEST_ASSERT(config =
		    cmd_ln_init(NULL, ps_args(), TRUE,
				"-hmm", MODELDIR "/en-us/en-us",
				"-lm", MODELDIR "/en-us/en-us.lm.bin",
				"-dict", MODELDIR "/en-us/cmudict-en-us.dict",
				"-fwdtree", "yes",
				"-fwdflat", "yes",
				"-bestpath", "yes",
				"-input_endian", "little",
				"-samprate", "16000", NULL));
	TEST_ASSERT(ps = ps_init(config));
	TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb"));
	ps_decode_raw(ps, rawfh, -1);
	fclose(rawfh);
	hyp = ps_get_hyp(ps, &score);
	printf("BESTPATH: %s (%d)\n", hyp, score);

	for (n = 1, nbest = ps_nbest(ps); nbest && n < 10; nbest = ps_nbest_next(nbest), n++) {
		ps_seg_t *seg;
		hyp = ps_nbest_hyp(nbest, &score);
		printf("NBEST %d: %s (%d)\n", n, hyp, score);
		for (seg = ps_nbest_seg(nbest); seg;
		     seg = ps_seg_next(seg)) {
			char const *word;
			int sf, ef;

			word = ps_seg_word(seg);
			ps_seg_frames(seg, &sf, &ef);
			printf("%s %d %d\n", word, sf, ef);
		}
	}
	if (nbest)
	    ps_nbest_free(nbest);
	ps_free(ps);
	cmd_ln_free_r(config);
	return 0;
}
예제 #3
0
static void acoustic_processor(context_t *ctx,
                               srs_srec_utterance_t *utt,
                               srs_srec_candidate_t *cands,
                               srs_srec_candidate_t **sorted)
{
    filter_buf_t *filtbuf;
    decoder_set_t *decset;
    decoder_t *dec;
    logmath_t *lmath;
    const char *uttid;
    const char *hyp;
    int32 score;
    double prob;
    ps_nbest_t *nb;
    ps_seg_t *seg;
    int32_t frlen;
    int32 start, end;
    size_t ncand;
    srs_srec_candidate_t *cand;
    srs_srec_token_t *tkn;
    int32_t length;

    if (!ctx || !(filtbuf = ctx->filtbuf) ||
        !(decset = ctx->decset) || !(dec = decset->curdec))
        return;

    frlen = filtbuf->frlen;
    lmath = ps_get_logmath(dec->ps);
    uttid = "<unknown>";
    hyp = ps_get_hyp(dec->ps, &score, &uttid);
    prob = logmath_exp(lmath, score);
    length = 0;

    if (prob < 0.00000001)
        prob = 0.00000001;

    for (nb  = ps_nbest(dec->ps, 0,-1, NULL,NULL), ncand = 0;
         nb != NULL;
         nb  = ps_nbest_next(nb))
    {
        if (ncand >= CANDIDATE_MAX-1) {
            break;
            ps_nbest_free(nb);
        }

        if ((seg  = ps_nbest_seg(nb, &score))) {
            while (seg && strcmp(ps_seg_word(seg), "<s>"))
                seg = ps_seg_next(seg);

            if (!seg)
                continue;

            ps_seg_frames(seg, &start, &end);

            cand = cands + ncand;

            cand->score = logmath_exp(lmath, score) / prob;
            cand->ntoken = 0;

            length = 0;

            while ((seg = ps_seg_next(seg))) {
                if ((hyp = ps_seg_word(seg))) {
                    if (!strcmp(hyp, "</s>") ||
                        cand->ntoken >= CANDIDATE_TOKEN_MAX)
                    {
                        ncand++;
                        //memset(cand+1, 0, sizeof(srs_srec_candidate_t));
                        ps_seg_frames(seg, &start, &end);
                        ps_seg_free(seg);
                        //printf("hyp=</s> ncand=%d\n", ncand);
                        length = (end + 1) * frlen;
                        break;
                    }
                    else if (!strcmp(hyp, "<sil>")) {
                        ps_seg_frames(seg, &start, &end);
                        //printf("hyp=<sil> skip it\n");
                    }
                    else {
                        tkn = cand->tokens + cand->ntoken++;
                        tkn->token = tknbase(hyp);
                        ps_seg_frames(seg, &start, &end);
                        tkn->start = start * frlen;
                        tkn->end = (end + 1) * frlen;
                        //printf("hyp=%s (%d, %d) tkn count %d\n",
                        //      tkn->token, tkn->start,tkn->end, cand->ntoken);
                    }
                }
            } /* while seg */

            if (!seg && cand->ntoken > 0) {
                ncand++;
                cand->score *= 0.9; /* some penalty */
                //memset(cand+1, 0, sizeof(srs_srec_candidate_t));
            }

            if (!length) {
                tkn = cand->tokens + (cand->ntoken - 1);
                length = tkn->end;
            }
        }
    } /* for nb */

    memset(cand+1, 0, sizeof(srs_srec_candidate_t));

    utt->id = uttid;
    utt->score = prob;
    //utt->length = length;
    utt->length = filtbuf->len;
    utt->ncand = candidate_sort(cands, sorted);
    utt->cands = sorted;
}
SWIGINTERN Nbest *new_Nbest(Decoder *d){
	    Nbest *nbest = ckd_calloc(1, sizeof(*nbest));
	    nbest->nbest = ps_nbest(d, 0, -1, NULL, NULL);
	    return nbest;
	}
static void
gst_pocketsphinx_get_property(GObject * object, guint prop_id,
                              GValue * value, GParamSpec * pspec)
{
    GstPocketSphinx *ps = GST_POCKETSPHINX(object);

    switch (prop_id) {
    case PROP_DECODER:
        g_value_set_boxed(value, ps->ps);
        break;
    case PROP_CONFIGURED:
        g_value_set_boolean(value, ps->ps != NULL);
        break;
    case PROP_HMM_DIR:
        g_value_set_string(value, cmd_ln_str_r(ps->config, "-hmm"));
        break;
    case PROP_LM_FILE:
        g_value_set_string(value, cmd_ln_str_r(ps->config, "-lm"));
        break;
    case PROP_LMCTL_FILE:
        g_value_set_string(value, cmd_ln_str_r(ps->config, "-lmctl"));
        break;
    case PROP_LM_NAME:
        g_value_set_string(value, cmd_ln_str_r(ps->config, "-lmname"));
        break;
    case PROP_DICT_FILE:
        g_value_set_string(value, cmd_ln_str_r(ps->config, "-dict"));
        break;
    case PROP_MLLR_FILE:
        g_value_set_string(value, cmd_ln_str_r(ps->config, "-mllr"));
        break;
    case PROP_FSG_FILE:
        g_value_set_string(value, cmd_ln_str_r(ps->config, "-fsg"));
        break;
    case PROP_FWDFLAT:
        g_value_set_boolean(value, cmd_ln_boolean_r(ps->config, "-fwdflat"));
        break;
    case PROP_BESTPATH:
        g_value_set_boolean(value, cmd_ln_boolean_r(ps->config, "-bestpath"));
        break;
    case PROP_LATDIR:
        g_value_set_string(value, ps->latdir);
        break;
    case PROP_LATTICE: {
        ps_lattice_t *dag;

        if (ps->ps && (dag = ps_get_lattice(ps->ps)))
            g_value_set_boxed(value, dag);
        else
            g_value_set_boxed(value, NULL);
        break;
    }
    case PROP_MAXHMMPF:
        g_value_set_int(value, cmd_ln_int32_r(ps->config, "-maxhmmpf"));
        break;
    case PROP_MAXWPF:
        g_value_set_int(value, cmd_ln_int32_r(ps->config, "-maxwpf"));
        break;
    case PROP_BEAM:
        g_value_set_double(value, cmd_ln_float_r(ps->config, "-beam"));
        break;
    case PROP_PBEAM:
        g_value_set_double(value, cmd_ln_float_r(ps->config, "-pbeam"));
        break;
    case PROP_WBEAM:
        g_value_set_double(value, cmd_ln_float_r(ps->config, "-wbeam"));
        break;
    case PROP_DSRATIO:
        g_value_set_int(value, cmd_ln_int32_r(ps->config, "-ds"));
        break;
    case PROP_NBEST_SIZE:
        g_value_set_int(value, ps->n_best_size);
        break;
    case PROP_NBEST: {
        int i = 0, out_score = 0;
        GValueArray *arr;
        if (!ps->ps) {
            break;
        }
	arr = g_value_array_new(1);
        ps_nbest_t *ps_nbest_list = ps_nbest(ps->ps, 0, -1, NULL, NULL);   
        if (ps_nbest_list) {
            ps_nbest_list = ps_nbest_next(ps_nbest_list);
            while ((i < ps->n_best_size) && (ps_nbest_list != NULL)) {
                GValue value1 = { 0 };
                g_value_init (&value1, G_TYPE_STRING);
                const char* hyp = ps_nbest_hyp(ps_nbest_list, &out_score);
                g_value_set_string(&value1, hyp);
                g_value_array_append(arr, &value1);  
                ps_nbest_list = ps_nbest_next(ps_nbest_list);
                i++;
            }
            if (ps_nbest_list) {
                ps_nbest_free(ps_nbest_list);
            }
        }
        g_value_set_boxed (value, arr);
        break;
    }
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}