Initial import to Tizen
[profile/ivi/pocketsphinx.git] / test / unit / test_fwdtree_fwdflat.c
1 #include <pocketsphinx.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <time.h>
5
6 #include "pocketsphinx_internal.h"
7 #include "ngram_search_fwdtree.h"
8 #include "ngram_search_fwdflat.h"
9 #include "test_macros.h"
10
11 int
12 main(int argc, char *argv[])
13 {
14         ps_decoder_t *ps;
15         cmd_ln_t *config;
16         acmod_t *acmod;
17         ngram_search_t *ngs;
18         clock_t c;
19         int i;
20
21         TEST_ASSERT(config =
22                     cmd_ln_init(NULL, ps_args(), TRUE,
23                                 "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k",
24                                 "-lm", MODELDIR "/lm/en_US/wsj0vp.5000.DMP",
25                                 "-dict", MODELDIR "/lm/en_US/cmu07a.dic",
26                                 "-fwdtree", "yes",
27                                 "-fwdflat", "yes",
28                                 "-bestpath", "no",
29                                 "-input_endian", "little",
30                                 "-samprate", "16000", NULL));
31         TEST_ASSERT(ps = ps_init(config));
32
33         ngs = (ngram_search_t *)ps->search;
34         acmod = ps->acmod;
35         acmod_set_grow(ps->acmod, TRUE);
36
37         setbuf(stdout, NULL);
38         c = clock();
39         for (i = 0; i < 5; ++i) {
40                 FILE *rawfh;
41                 int16 buf[2048];
42                 size_t nread;
43                 int16 const *bptr;
44                 int nfr;
45
46                 TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb"));
47                 TEST_EQUAL(0, acmod_start_utt(acmod));
48                 ngram_fwdtree_start(ngs);
49                 while (!feof(rawfh)) {
50                         nread = fread(buf, sizeof(*buf), 2048, rawfh);
51                         bptr = buf;
52                         while ((nfr = acmod_process_raw(acmod, &bptr, &nread, FALSE)) > 0) {
53                                 while (acmod->n_feat_frame > 0) {
54                                         ngram_fwdtree_search(ngs, acmod->output_frame);
55                                         acmod_advance(acmod);
56                                 }
57                         }
58                 }
59
60                 TEST_ASSERT(acmod_end_utt(acmod) >= 0);
61                 while (acmod->n_feat_frame > 0) {
62                         ngram_fwdtree_search(ngs, acmod->output_frame);
63                         acmod_advance(acmod);
64                 }
65                 ngram_fwdtree_finish(ngs);
66                 printf("FWDTREE: %s\n",
67                        ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL)));
68                 fclose(rawfh);
69
70                 E_INFO("grow_feat %d output_frame %d n_mfc_alloc %d n_mfc_frame %d\n",
71                        acmod->grow_feat, acmod->output_frame, acmod->n_mfc_alloc,
72                        acmod->n_mfc_frame);
73                 E_INFO("mfc_outidx %d n_feat_alloc %d n_feat_frame %d feat_outidx %d\n",
74                        acmod->mfc_outidx, acmod->n_feat_alloc, acmod->n_feat_frame,
75                        acmod->feat_outidx);
76                 TEST_EQUAL(0, acmod_rewind(acmod));
77                 ngram_fwdflat_start(ngs);
78                 while (acmod->n_feat_frame > 0) {
79                         ngram_fwdflat_search(ngs, acmod->output_frame);
80                         acmod_advance(acmod);
81                 }
82                 ngram_fwdflat_finish(ngs);
83                 printf("FWDFLAT: %s\n",
84                        ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL)));
85         }
86         TEST_EQUAL(0, strcmp("go forward ten years",
87                              ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL))));
88         c = clock() - c;
89         printf("5 * fwdtree + fwdflat search in %.2f sec\n",
90                (double)c / CLOCKS_PER_SEC);
91         ps_free(ps);
92
93         return 0;
94 }