Initial import to Tizen
[profile/ivi/sphinxbase.git] / test / unit / test_ngram / test_lm_write.c
1 #include <ngram_model.h>
2 #include <logmath.h>
3 #include <strfuncs.h>
4 #include <err.h>
5
6 #include "test_macros.h"
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 static int
13 test_lm_vals(ngram_model_t *model)
14 {
15         int32 n_used;
16
17         TEST_ASSERT(model);
18         TEST_EQUAL(ngram_wid(model, "<UNK>"), 0);
19         TEST_EQUAL(strcmp(ngram_word(model, 0), "<UNK>"), 0);
20         TEST_EQUAL(ngram_wid(model, "absolute"), 13);
21         TEST_EQUAL(strcmp(ngram_word(model, 13), "absolute"), 0);
22         /* Test unigrams. */
23         TEST_EQUAL_LOG(ngram_score(model, "<UNK>", NULL), -75346);
24         TEST_EQUAL_LOG(ngram_bg_score(model, ngram_wid(model, "<UNK>"),
25                                   NGRAM_INVALID_WID, &n_used), -75346);
26         TEST_EQUAL(n_used, 1);
27         TEST_EQUAL_LOG(ngram_score(model, "sphinxtrain", NULL), -64208);
28         TEST_EQUAL_LOG(ngram_bg_score(model, ngram_wid(model, "sphinxtrain"),
29                                   NGRAM_INVALID_WID, &n_used), -64208);
30         TEST_EQUAL(n_used, 1);
31         printf("FOO %d\n", ngram_score(model, "huggins", "david", NULL));
32         printf("FOO %d\n", ngram_score(model, "daines", "huggins", "david", NULL));
33         /* Test bigrams. */
34         TEST_EQUAL_LOG(ngram_score(model, "huggins", "david", NULL), -831);
35         /* Test trigrams. */
36         TEST_EQUAL_LOG(ngram_score(model, "daines", "huggins", "david", NULL), -9450);
37         return 0;
38 }
39
40 int
41 main(int argc, char *argv[])
42 {
43         logmath_t *lmath;
44         ngram_model_t *model;
45
46         /* Initialize a logmath object to pass to ngram_read */
47         lmath = logmath_init(1.0001, 0, 0);
48
49         /* Convert ARPA to DMP */
50         E_INFO("Converting ARPA to DMP\n");
51         model = ngram_model_read(NULL, LMDIR "/100.arpa.bz2", NGRAM_ARPA, lmath);
52         test_lm_vals(model);
53         TEST_EQUAL(0, ngram_model_write(model, "100.tmp.DMP", NGRAM_DMP));
54         ngram_model_free(model);
55
56         /* Convert DMP to ARPA */
57         E_INFO("Converting DMP to ARPA\n");
58         model = ngram_model_read(NULL, LMDIR "/100.arpa.DMP", NGRAM_DMP, lmath);
59         test_lm_vals(model);
60         TEST_EQUAL(0, ngram_model_write(model, "100.tmp.arpa", NGRAM_ARPA));
61         ngram_model_free(model);
62
63         /* Test converted DMP */
64         E_INFO("Testing converted DMP\n");
65         model = ngram_model_read(NULL, "100.tmp.DMP", NGRAM_DMP, lmath);
66         test_lm_vals(model);
67         ngram_model_free(model);
68
69         /* Test converted ARPA */
70         E_INFO("Testing converted ARPA\n");
71         model = ngram_model_read(NULL, "100.tmp.arpa", NGRAM_ARPA, lmath);
72         test_lm_vals(model);
73         ngram_model_free(model);
74
75         /* Convert DMP back to ARPA*/
76         E_INFO("Converting ARPA back to DMP\n");
77         model = ngram_model_read(NULL, "100.tmp.arpa", NGRAM_ARPA, lmath);
78         test_lm_vals(model);
79         TEST_EQUAL(0, ngram_model_write(model, "100.tmp.DMP", NGRAM_DMP));
80         ngram_model_free(model);
81
82         /* Convert ARPA back to DMP */
83         E_INFO("Converting DMP back to ARPA\n");
84         model = ngram_model_read(NULL, "100.tmp.DMP", NGRAM_DMP, lmath);
85         test_lm_vals(model);
86         TEST_EQUAL(0, ngram_model_write(model, "100.tmp.arpa", NGRAM_ARPA));
87         ngram_model_free(model);
88
89         logmath_free(lmath);
90         return 0;
91 }