Initial import to Tizen
[profile/ivi/sphinxbase.git] / test / unit / test_ngram / test_lm_add.c
1 #include <ngram_model.h>
2 #include <logmath.h>
3 #include <strfuncs.h>
4
5 #include "test_macros.h"
6
7 #include <stdio.h>
8 #include <string.h>
9 #include <math.h>
10
11 void
12 run_tests(logmath_t *lmath, ngram_model_t *model)
13 {
14         int32 wid, score;
15
16         wid = ngram_model_add_word(model, "foobie", 1.0);
17         score = ngram_score(model, "foobie", NULL);
18         TEST_EQUAL_LOG(score, logmath_log(lmath, 1.0/400.0)); /* #unigrams */
19
20         wid = ngram_model_add_word(model, "quux", 0.5);
21         score = ngram_score(model, "quux", NULL);
22         TEST_EQUAL_LOG(score, logmath_log(lmath, 0.5/400.0)); /* #unigrams */
23
24         ngram_model_apply_weights(model, 1.0, 1.0, 0.9);
25         score = ngram_score(model, "quux", NULL);
26         TEST_EQUAL_LOG(score, logmath_log(lmath, 0.5/400.0*0.9 + 1.0/400.0*0.1));
27
28         wid = ngram_model_add_word(model, "bazbar", 0.5);
29         score = ngram_score(model, "bazbar", NULL);
30         TEST_EQUAL_LOG(score, logmath_log(lmath, 0.5/400.0*0.9 + 1.0/400.0*0.1));
31 }
32
33 int
34 main(int argc, char *argv[])
35 {
36         logmath_t *lmath;
37         ngram_model_t *model;
38
39         lmath = logmath_init(1.0001, 0, 0);
40
41         model = ngram_model_read(NULL, LMDIR "/100.arpa.DMP", NGRAM_DMP, lmath);
42         run_tests(lmath, model);
43         ngram_model_free(model);
44
45         model = ngram_model_read(NULL, LMDIR "/100.arpa.gz", NGRAM_ARPA, lmath);
46         run_tests(lmath, model);
47         ngram_model_free(model);
48
49         logmath_free(lmath);
50         return 0;
51 }