Initial import to Tizen
[profile/ivi/pocketsphinx.git] / test / unit / test_lm_read.c
1 #include <pocketsphinx.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 #include "test_macros.h"
6
7 int
8 main(int argc, char *argv[])
9 {
10         char const *hyp;
11         char const *uttid;
12         ps_decoder_t *ps;
13         cmd_ln_t *config;
14         ngram_model_t *lmset, *lm;
15         FILE *rawfh;
16         int32 score;
17
18         /* First decode it with the crappy WSJ language model. */
19         TEST_ASSERT(config =
20                     cmd_ln_init(NULL, ps_args(), TRUE,
21                                 "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k",
22                                 "-lm", MODELDIR "/lm/en_US/wsj0vp.5000.DMP",
23                                 "-dict", DATADIR "/defective.dic",
24                                 "-dictcase", "yes",
25                                 /* FIXME: fwdflat causes some problems. */
26                                 "-bestpath", "no", "-fwdflat", "no",
27                                 "-input_endian", "little",
28                                 "-samprate", "16000", NULL));
29         TEST_ASSERT(ps = ps_init(config));
30         TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb"));
31
32         ps_decode_raw(ps, rawfh, "goforward", -1);
33         hyp = ps_get_hyp(ps, &score, &uttid);
34         printf("%s: %s (%d)\n", uttid, hyp, score);
35         TEST_EQUAL(0, strcmp(hyp, "go forward ten years"));
36
37         /* Now load the turtle language model. */
38         lm = ngram_model_read(config, 
39                               MODELDIR "/lm/en/turtle.DMP",
40                               NGRAM_AUTO, ps_get_logmath(ps));
41         TEST_ASSERT(lm);
42         lmset = ps_get_lmset(ps);
43         TEST_ASSERT(lmset);
44         ngram_model_set_add(lmset, lm, "turtle", 1.0, TRUE);
45         ngram_model_set_select(lmset, "turtle");
46         ps_update_lmset(ps, lmset);
47         clearerr(rawfh);
48         fseek(rawfh, 0, SEEK_SET);
49         TEST_ASSERT(ps_decode_raw(ps, rawfh, "goforward", -1));
50         hyp = ps_get_hyp(ps, &score, &uttid);
51         printf("%s: %s (%d)\n", uttid, hyp, score);
52
53         /* Oops!  It's still not correct, because METERS isn't in the
54          * dictionary that we originally loaded. */
55         TEST_EQUAL(0, strcmp(hyp, "go forward ten degrees"));
56         /* So let's add it to the dictionary. */
57         ps_add_word(ps, "foobie", "F UW B IY", FALSE);
58         ps_add_word(ps, "meters", "M IY T ER Z", TRUE);
59         /* And try again. */
60         clearerr(rawfh);
61         fseek(rawfh, 0, SEEK_SET);
62         TEST_ASSERT(ps_decode_raw(ps, rawfh, "goforward", -1));
63         hyp = ps_get_hyp(ps, &score, &uttid);
64         TEST_ASSERT(hyp);
65         printf("%s: %s (%d)\n", uttid, hyp, score);
66         ps_lattice_write(ps_get_lattice(ps), "meters.lat");
67         /* Bingo! */
68         TEST_EQUAL(0, strcmp(hyp, "go forward ten meters"));
69
70         /* Now let's test dictionary switching. */
71         TEST_EQUAL(0, ps_load_dict(ps, MODELDIR "/lm/en/turtle.dic",
72                                    NULL, NULL));
73         /* And try again. */
74         clearerr(rawfh);
75         fseek(rawfh, 0, SEEK_SET);
76         TEST_ASSERT(ps_decode_raw(ps, rawfh, "goforward", -1));
77         hyp = ps_get_hyp(ps, &score, &uttid);
78         printf("%s: %s (%d)\n", uttid, hyp, score);
79         TEST_EQUAL(0, strcmp(hyp, "go forward ten meters"));
80
81         /* Try switching back again just to make sure. */
82         TEST_EQUAL(0, ps_load_dict(ps, DATADIR "/defective.dic",
83                                    NULL, NULL));
84         clearerr(rawfh);
85         fseek(rawfh, 0, SEEK_SET);
86         TEST_ASSERT(ps_decode_raw(ps, rawfh, "goforward", -1));
87         hyp = ps_get_hyp(ps, &score, &uttid);
88         printf("%s: %s (%d)\n", uttid, hyp, score);
89         TEST_EQUAL(0, strcmp(hyp, "go forward ten degrees"));
90
91         fclose(rawfh);
92         ps_free(ps);
93
94         return 0;
95 }
96