コード例 #1
0
ファイル: test_senfh.c プロジェクト: IncBin94/pocketsphinx
int
main(int argc, char *argv[])
{
    ps_decoder_t *ps;
    cmd_ln_t *config;
    acmod_t *acmod;
    ngram_search_t *ngs;

    TEST_ASSERT(config =
            cmd_ln_init(NULL, ps_args(), TRUE,
                "-hmm", MODELDIR "/en-us/en-us",
                "-lm", MODELDIR "/en-us/en-us.lm.dmp",
                "-dict", MODELDIR "/en-us/cmudict-en-us.dict",
                "-fwdtree", "no",
                "-fwdflat", "yes",
                "-bestpath", "no",
                "-input_endian", "little",
                "-samprate", "16000", NULL));
    TEST_ASSERT(ps = ps_init(config));

    ngs = (ngram_search_t *)ps->search;
    acmod = ps->acmod;

    setbuf(stdout, NULL);
    {
        FILE *rawfh, *senfh;
        int16 buf[2048];
        size_t nread;
        int16 const *bptr;
        int nfr;

        TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb"));
        TEST_EQUAL(0, acmod_start_utt(acmod));
        TEST_ASSERT(senfh = fopen("goforward.sen", "wb"));
        TEST_EQUAL(0, acmod_set_senfh(acmod, senfh));
        ngram_fwdflat_start(ngs);
        while (!feof(rawfh)) {
            nread = fread(buf, sizeof(*buf), 2048, rawfh);
            bptr = buf;
            while ((nfr = acmod_process_raw(acmod, &bptr, &nread, FALSE)) > 0) {
                while (acmod->n_feat_frame > 0) {
                    ngram_fwdflat_search(ngs, acmod->output_frame);
                    acmod_advance(acmod);
                }
            }
        }
        ngram_fwdflat_finish(ngs);
        printf("%s\n",
               ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL, NULL)));

        TEST_ASSERT(acmod_end_utt(acmod) >= 0);
        fclose(rawfh);
        TEST_EQUAL(0, strcmp("go forward ten meters",
                     ngram_search_bp_hyp(ngs,
                             ngram_search_find_exit(ngs, -1, NULL, NULL))));

        TEST_EQUAL(0, acmod_set_senfh(acmod, NULL));
        TEST_EQUAL(0, acmod_start_utt(acmod));
        TEST_ASSERT(senfh = fopen("goforward.sen", "rb"));
        TEST_EQUAL(0, acmod_set_insenfh(acmod, senfh));
        ngram_fwdflat_start(ngs);
        while ((nfr = acmod_read_scores(acmod)) > 0) {
            while (acmod->n_feat_frame > 0) {
                ngram_fwdflat_search(ngs, acmod->output_frame);
                acmod_advance(acmod);
            }
        }
        ngram_fwdflat_finish(ngs);
        printf("%s\n",
               ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL, NULL)));

        TEST_ASSERT(acmod_end_utt(acmod) >= 0);
        TEST_EQUAL(0, strcmp("go forward ten meters",
                     ngram_search_bp_hyp(ngs,
                             ngram_search_find_exit(ngs, -1, NULL, NULL))));
        fclose(senfh);
    }
    ps_free(ps);
    cmd_ln_free_r(config);

    return 0;
}
コード例 #2
0
int
test_decode(ps_decoder_t *ps)
{
	FILE *rawfh;
	int16 buf[2048];
	size_t nread;
	int16 const *bptr;
	int nfr;
	ps_lattice_t *dag;
	acmod_t *acmod;
	ngram_search_t *ngs;
	int i, j;
	ps_latlink_t *link;
	ps_latnode_t *node;
	latlink_list_t *x;
	int32 norm, post;

	ngs = (ngram_search_t *)ps->search;
	acmod = ps->acmod;

	/* Decode stuff and build a DAG. */
	TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb"));
	TEST_EQUAL(0, acmod_start_utt(acmod));
	ngram_fwdtree_start(ngs);
	while (!feof(rawfh)) {
		nread = fread(buf, sizeof(*buf), 2048, rawfh);
		bptr = buf;
		while ((nfr = acmod_process_raw(acmod, &bptr, &nread, FALSE)) > 0) {
			while (acmod->n_feat_frame > 0) {
				ngram_fwdtree_search(ngs, acmod->output_frame);
				acmod_advance(acmod);
			}
		}
	}
	ngram_fwdtree_finish(ngs);
	printf("FWDTREE: %s\n",
	       ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL, NULL)));

	TEST_ASSERT(acmod_end_utt(acmod) >= 0);
	fclose(rawfh);

	dag = ngram_search_lattice(ps->search);
	if (dag == NULL) {
		E_ERROR("Failed to build DAG!\n");
		return -1;
	}

	/* Write lattice to disk. */
	TEST_EQUAL(0, ps_lattice_write(dag, "test_posterior.lat"));

	/* Do a bunch of checks on the DAG generation and traversal code: */
	/* Verify that forward and backward iteration give the same number of edges. */
	i = j = 0;
	for (link = ps_lattice_traverse_edges(dag, NULL, NULL);
	     link; link = ps_lattice_traverse_next(dag, NULL)) {
		++i;
	}
	for (link = ps_lattice_reverse_edges(dag, NULL, NULL);
	     link; link = ps_lattice_reverse_next(dag, NULL)) {
		++j;
	}
	printf("%d forward edges, %d reverse edges\n", i, j);
	TEST_EQUAL(i,j);
	/* Verify that the same links are reachable via entries and exits. */
	for (node = dag->nodes; node; node = node->next) {
		for (x = node->exits; x; x = x->next)
			x->link->alpha = -42;
	}
	for (node = dag->nodes; node; node = node->next) {
		for (x = node->entries; x; x = x->next)
			TEST_EQUAL(x->link->alpha, -42);
	}
	/* Verify that forward iteration is properly ordered. */
	for (link = ps_lattice_traverse_edges(dag, NULL, NULL);
	     link; link = ps_lattice_traverse_next(dag, NULL)) {
		link->alpha = 0;
		for (x = link->from->entries; x; x = x->next) {
			TEST_EQUAL(x->link->alpha, 0);
		}
	}
	/* Verify that backward iteration is properly ordered. */
	for (node = dag->nodes; node; node = node->next) {
		for (x = node->exits; x; x = x->next)
			x->link->alpha = -42;
	}
	for (link = ps_lattice_reverse_edges(dag, NULL, NULL);
	     link; link = ps_lattice_reverse_next(dag, NULL)) {
		link->alpha = 0;
		for (x = link->to->exits; x; x = x->next) {
			TEST_EQUAL(x->link->alpha, 0);
		}
	}
	
	/* Find and print best path. */
	link = ps_lattice_bestpath(dag, ngs->lmset, 1.0, 1.0/20.0);
	printf("BESTPATH: %s\n", ps_lattice_hyp(dag, link));

	/* Calculate betas. */
	post = ps_lattice_posterior(dag, ngs->lmset, 1.0/20.0);
	printf("Best path score: %d\n",
	       link->path_scr + dag->final_node_ascr);
	printf("P(S|O) = %d\n", post);

	/* Verify that sum of final alphas and initial alphas+betas is
	 * sufficiently similar. */
	norm = logmath_get_zero(acmod->lmath);
	for (x = dag->start->exits; x; x = x->next)
		norm = logmath_add(acmod->lmath, norm, x->link->beta + x->link->alpha);
	E_INFO("Sum of final alphas+betas = %d\n", dag->norm);
	E_INFO("Sum of initial alphas+betas = %d\n", norm);
	TEST_EQUAL_LOG(dag->norm, norm);

	/* Print posterior probabilities for each link in best path. */
	while (link) {
		printf("P(%s,%d) = %d = %f\n",
		       dict_wordstr(ps->search->dict, link->from->wid),
		       link->ef,
		       link->alpha + link->beta - dag->norm,
		       logmath_exp(acmod->lmath, link->alpha + link->beta - dag->norm));
		link = link->best_prev;
	}

	return 0;
}
コード例 #3
0
ファイル: test_fwdflat.c プロジェクト: OmkarKirpan/CMUSphinx
int
main(int argc, char *argv[])
{
	ps_decoder_t *ps;
	cmd_ln_t *config;
	acmod_t *acmod;
	ngram_search_t *ngs;
	clock_t c;

	TEST_ASSERT(config =
		    cmd_ln_init(NULL, ps_args(), TRUE,
				"-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k",
				"-lm", MODELDIR "/lm/en_US/wsj0vp.5000.DMP",
				"-dict", MODELDIR "/lm/en_US/cmu07a.dic",
				"-fwdtree", "no",
				"-fwdflat", "yes",
				"-bestpath", "no",
				"-input_endian", "little",
				"-samprate", "16000", NULL));
	TEST_ASSERT(ps = ps_init(config));

	ngs = (ngram_search_t *)ps->search;
	acmod = ps->acmod;

	setbuf(stdout, NULL);
	c = clock();
	{
		FILE *rawfh;
		int16 buf[2048];
		size_t nread;
		int16 const *bptr;
		int nfr;

		TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb"));
		TEST_EQUAL(0, acmod_start_utt(acmod));
		ngram_fwdflat_start(ngs);
		while (!feof(rawfh)) {
			nread = fread(buf, sizeof(*buf), 2048, rawfh);
			bptr = buf;
			while ((nfr = acmod_process_raw(acmod, &bptr, &nread, FALSE)) > 0) {
				while (acmod->n_feat_frame > 0) {
					ngram_fwdflat_search(ngs,acmod->output_frame);
					acmod_advance(acmod);
				}
			}
		}
		ngram_fwdflat_finish(ngs);
		printf("%s\n",
		       ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL)));

		TEST_ASSERT(acmod_end_utt(acmod) >= 0);
		fclose(rawfh);
	}
	TEST_EQUAL(0, strcmp("go forward and users",
			     ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL))));
	c = clock() - c;
	printf("2 * fwdflat search in %.2f sec\n",
	       (double)c / CLOCKS_PER_SEC);
	ps_free(ps);

	return 0;
}
コード例 #4
0
int
main(int argc, char *argv[])
{
    ps_decoder_t *ps;
    cmd_ln_t *config;
    acmod_t *acmod;
    ngram_search_t *ngs;
    ps_lattice_t *dag;
    clock_t c;
    int i;

    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", "no",
                "-bestpath", "yes",
                "-samprate", "16000", NULL));
    TEST_ASSERT(ps = ps_init(config));

    ngs = (ngram_search_t *)ps->search;
    acmod = ps->acmod;
        acmod_set_grow(ps->acmod, TRUE);

    setbuf(stdout, NULL);
    c = clock();
    for (i = 0; i < 5; ++i) {
        FILE *rawfh;
        int16 buf[2048];
        size_t nread;
        int16 const *bptr;
        int nfr;
        ps_astar_t *nbest;
        ps_latpath_t *path;
        char *besthyp;
        const char *astar_besthyp = NULL;
        int32 astar_hyp_score;
        int i;

        /* PocketSphinx API would do this for us but we have to do it manually here. */
        ps_lattice_free(ps->search->dag);
        ps->search->dag = NULL;

        TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb"));
        TEST_EQUAL(0, acmod_start_utt(acmod));
        ngram_fwdtree_start(ngs);
        while (!feof(rawfh)) {
            nread = fread(buf, sizeof(*buf), 2048, rawfh);
            bptr = buf;
            while ((nfr = acmod_process_raw(acmod, &bptr, &nread, FALSE)) > 0) {
                while (acmod->n_feat_frame > 0) {
                    ngram_fwdtree_search(ngs, acmod->output_frame);
                    acmod_advance(acmod);
                }
            }
        }
        ngram_fwdtree_finish(ngs);
        printf("FWDTREE: %s\n",
               ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL)));

        TEST_ASSERT(acmod_end_utt(acmod) >= 0);
        fclose(rawfh);

        dag = ngram_search_lattice(ps->search);
        if (dag == NULL) {
            E_ERROR("Failed to build DAG!\n");
            return 1;
        }
        besthyp = ckd_salloc
            (ps_lattice_hyp(dag, ps_lattice_bestpath
                    (dag, ngs->lmset, 9.5/6.5, 1.0)));
        printf("BESTPATH: %s\n", besthyp);

        TEST_ASSERT(nbest = ps_astar_start(dag, ngs->lmset, 9.5/6.5, 0, -1, -1, -1));
        i = 0;
        astar_hyp_score = WORST_SCORE;
        while ((path = ps_astar_next(nbest))) {
            if (i < 10)
                printf("NBEST %d: %s (%d)\n", i, ps_astar_hyp(nbest, path), path->score);
            if (path->score > astar_hyp_score) {
                astar_hyp_score = path->score;
                astar_besthyp = ps_astar_hyp(nbest, path);
            }
            i++;
        }
        TEST_EQUAL(0, strcmp(besthyp, astar_besthyp));
        ps_astar_finish(nbest);
        ckd_free(besthyp);
    }
    printf("%s\n", ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL)));
    TEST_EQUAL(0, strcmp("go forward ten meters",
                 ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL))));
    c = clock() - c;
    printf("5 * fwdtree + bestpath + N-best search in %.2f sec\n",
           (double)c / CLOCKS_PER_SEC);
    ps_free(ps);
    cmd_ln_free_r(config);

    return 0;
}