Initial import to Tizen
[profile/ivi/pocketsphinx.git] / test / unit / test_fsg2.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 "fsg_search_internal.h"
8 #include "ps_lattice_internal.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         fsg_search_t *fsgs;
18         ps_lattice_t *dag;
19         ps_seg_t *seg;
20         int32 score;
21         ps_latlink_t *link;
22
23         TEST_ASSERT(config =
24                     cmd_ln_init(NULL, ps_args(), TRUE,
25                                 "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k",
26                                 "-fsg", DATADIR "/goforward.fsg",
27                                 "-dict", MODELDIR "/lm/en/turtle.dic",
28                                 "-bestpath", "no",
29                                 "-input_endian", "little",
30                                 "-samprate", "16000", NULL));
31         TEST_ASSERT(ps = ps_init(config));
32
33         fsgs = (fsg_search_t *)ps->search;
34         acmod = ps->acmod;
35
36         setbuf(stdout, NULL);
37         {
38                 FILE *rawfh;
39                 int16 buf[2048];
40                 size_t nread;
41                 int16 const *bptr;
42                 char const *hyp;
43                 int nfr;
44
45                 TEST_ASSERT(rawfh = fopen(DATADIR "/something.raw", "rb"));
46                 TEST_EQUAL(0, acmod_start_utt(acmod));
47                 fsg_search_start(ps_search_base(fsgs));
48                 while (!feof(rawfh)) {
49                         nread = fread(buf, sizeof(*buf), 2048, rawfh);
50                         bptr = buf;
51                         while ((nfr = acmod_process_raw(acmod, &bptr, &nread, FALSE)) > 0) {
52                                 while (acmod->n_feat_frame > 0) {
53                                         fsg_search_step(ps_search_base(fsgs),
54                                                         acmod->output_frame);
55                                         acmod_advance(acmod);
56                                 }
57                         }
58                 }
59                 fsg_search_finish(ps_search_base(fsgs));
60                 hyp = fsg_search_hyp(ps_search_base(fsgs), &score);
61                 printf("FSG: %s (%d)\n", hyp, score);
62
63                 TEST_ASSERT(acmod_end_utt(acmod) >= 0);
64                 fclose(rawfh);
65         }
66         for (seg = ps_seg_iter(ps, &score); seg;
67              seg = ps_seg_next(seg)) {
68                 char const *word;
69                 int sf, ef;
70                 int32 post, lscr, ascr, lback;
71
72                 word = ps_seg_word(seg);
73                 ps_seg_frames(seg, &sf, &ef);
74                 if (sf == ef)
75                         continue;
76                 post = ps_seg_prob(seg, &ascr, &lscr, &lback);
77                 printf("%s (%d:%d) P(w|o) = %f ascr = %d lscr = %d lback = %d\n", word, sf, ef,
78                        logmath_exp(ps_get_logmath(ps), post), ascr, lscr, lback);
79         }
80         dag = ps_get_lattice(ps);
81         ps_lattice_write(dag, "test_fsg2.lat");
82         link = ps_lattice_bestpath(dag, NULL, 1.0, 1.0/15.0);
83         printf("BESTPATH: %s\n", ps_lattice_hyp(dag, link));
84         ps_lattice_posterior(dag, NULL, 1.0/15.0);
85         while (link) {
86                 printf("%s %d P(w|o) = %d + %d - %d = %d = %f\n",
87                        dict_wordstr(ps->dict, link->from->wid),
88                        link->ef, link->alpha, link->beta, dag->norm,
89                        link->alpha + link->beta - dag->norm,
90                        logmath_exp(ps_get_logmath(ps),
91                                    link->alpha + link->beta - dag->norm));
92                 link = link->best_prev;
93         }
94         ps_free(ps);
95
96         return 0;
97 }