1 #include <ngram_model.h>
5 #include "test_macros.h"
13 main(int argc, char *argv[])
20 /* Initialize a logmath object to pass to ngram_read */
21 lmath = logmath_init(1.0001, 0, 0);
22 /* Read a language model */
23 model = ngram_model_read(NULL, LMDIR "/100.arpa.DMP", NGRAM_DMP, lmath);
26 for (i = 0, itor = ngram_model_mgrams(model, 0);
27 itor; ++i, itor = ngram_iter_next(itor)) {
29 int32 const *wids = ngram_iter_get(itor, &score, &bowt);
32 printf("%.4f %s %.4f\n",
33 logmath_log_to_log10(lmath, score),
34 ngram_word(model, wids[0]),
35 logmath_log_to_log10(lmath, bowt));
38 if (i == 0) TEST_EQUAL(wids[0], ngram_wid(model, "<UNK>"));
39 if (i == 1) TEST_EQUAL(wids[0], ngram_wid(model, "'s"));
42 for (i = 0, itor = ngram_model_mgrams(model, 1);
43 itor; ++i, itor = ngram_iter_next(itor)) {
45 int32 const *wids = ngram_iter_get(itor, &score, &bowt);
48 printf("%.4f %s %s %.4f\n",
49 logmath_log_to_log10(lmath, score),
50 ngram_word(model, wids[0]),
51 ngram_word(model, wids[1]),
52 logmath_log_to_log10(lmath, bowt));
55 /* FIXME: These tests are not sufficient - actually we
56 * need to make sure all word IDs line up
58 if (i == 0) TEST_EQUAL(wids[0], ngram_wid(model, "'s"));
59 if (i == 0) TEST_EQUAL(wids[1], ngram_wid(model, "an"));
60 if (i == 1) TEST_EQUAL(wids[0], ngram_wid(model, "'s"));
61 if (i == 1) TEST_EQUAL(wids[1], ngram_wid(model, "going"));
64 for (i = 0, itor = ngram_model_mgrams(model, 2);
65 itor; ++i, itor = ngram_iter_next(itor)) {
67 int32 const *wids = ngram_iter_get(itor, &score, &bowt);
70 printf("%.4f %s %s %s\n",
71 logmath_log_to_log10(lmath, score),
72 ngram_word(model, wids[0]),
73 ngram_word(model, wids[1]),
74 ngram_word(model, wids[2]));
77 /* FIXME: These tests are not sufficient - actually we
78 * need to make sure all word IDs line up
80 if (i == 0) TEST_EQUAL(wids[0], ngram_wid(model, "'s"));
81 if (i == 0) TEST_EQUAL(wids[1], ngram_wid(model, "an"));
82 if (i == 0) TEST_EQUAL(wids[2], ngram_wid(model, "r"));
83 if (i == 1) TEST_EQUAL(wids[0], ngram_wid(model, "'s"));
84 if (i == 1) TEST_EQUAL(wids[1], ngram_wid(model, "going"));
85 if (i == 1) TEST_EQUAL(wids[2], ngram_wid(model, "so"));
89 ngram_iter_t *itor2, *itor3;
93 /* Test the boundary condition - successors of last 1-gram. */
94 itor = ngram_ng_iter(model, ngram_model_get_counts(model)[0] - 1,
96 wids = ngram_iter_get(itor, &score, &bowt);
97 printf("%.4f %s %.4f\n",
98 logmath_log_to_log10(lmath, score),
99 ngram_word(model, wids[0]),
100 logmath_log_to_log10(lmath, bowt));
101 TEST_EQUAL(wids[0], ngram_wid(model, "~"));
103 for (itor2 = ngram_iter_successors(itor);
104 itor2; itor2 = ngram_iter_next(itor2)) {
105 wids = ngram_iter_get(itor2, &score, &bowt);
106 printf("%.4f %s %s %.4f\n",
107 logmath_log_to_log10(lmath, score),
108 ngram_word(model, wids[0]),
109 ngram_word(model, wids[1]),
110 logmath_log_to_log10(lmath, bowt));
111 TEST_EQUAL(wids[0], ngram_wid(model, "~"));
112 TEST_EQUAL(wids[1], ngram_wid(model, "eleven"));
114 itor2 = ngram_iter_successors(itor);
115 for (itor3 = ngram_iter_successors(itor2);
116 itor3; itor3 = ngram_iter_next(itor3)) {
117 wids = ngram_iter_get(itor3, &score, &bowt);
118 printf("%.4f %s %s %s\n",
119 logmath_log_to_log10(lmath, score),
120 ngram_word(model, wids[0]),
121 ngram_word(model, wids[1]),
122 ngram_word(model, wids[2]));
123 TEST_EQUAL(wids[0], ngram_wid(model, "~"));
124 TEST_EQUAL(wids[1], ngram_wid(model, "eleven"));
125 TEST_EQUAL(wids[2], ngram_wid(model, "per"));
127 ngram_iter_free(itor2);
128 ngram_iter_free(itor);
131 TEST_EQUAL(0, ngram_model_free(model));