File: test_lattice.c

package info (click to toggle)
pocketsphinx 5.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,236 kB
  • sloc: ansic: 54,519; python: 2,438; sh: 566; cpp: 410; perl: 342; yacc: 93; lex: 50; makefile: 30
file content (157 lines) | stat: -rw-r--r-- 4,415 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <pocketsphinx.h>
#include <stdio.h>
#include <string.h>

#include "pocketsphinx_internal.h"
#include "ps_lattice_internal.h"
#include "test_macros.h"
#include "test_ps.c"

int
test_nodes_and_stuff(ps_lattice_t *dag)
{
	ps_latnode_iter_t *itor;
	ps_latlink_iter_t *litor;
	ps_latnode_t *forward = NULL;

	TEST_ASSERT(itor = ps_latnode_iter(dag));
	while ((itor = ps_latnode_iter_next(itor))) {
		int16 sf, fef, lef;
		ps_latnode_t *node;
		float64 post;

		node = ps_latnode_iter_node(itor);
		sf = ps_latnode_times(node, &fef, &lef);
		post = logmath_exp(ps_lattice_get_logmath(dag),
				   ps_latnode_prob(dag, node, NULL));
		if (post > 0.0001)
			printf("%s %s %d -> (%d,%d) %f\n",
			       ps_latnode_baseword(dag, node),
			       ps_latnode_word(dag, node),
			       sf, fef, lef, post);
		if (0 == strcmp(ps_latnode_baseword(dag, node), "forward"))
			forward = node;
	}
	TEST_ASSERT(forward);

	printf("FORWARD entries:\n");
	for (litor = ps_latnode_entries(forward);
	     litor; litor = ps_latlink_iter_next(litor)) {
		ps_latlink_t *link = ps_latlink_iter_link(litor);
		int16 sf, ef;
		float64 post;
		int32 ascr;

		ef = ps_latlink_times(link, &sf);
		post = logmath_exp(ps_lattice_get_logmath(dag),
				   ps_latlink_prob(dag, link, &ascr));
		if (post > 0.0001)
			printf("%s %d -> %d prob %f ascr %d\n",
			       ps_latlink_baseword(dag, link),
			       sf, ef, post, ascr);
	}
	printf("FORWARD exits:\n");
	for (litor = ps_latnode_exits(forward);
	     litor; litor = ps_latlink_iter_next(litor)) {
		ps_latlink_t *link = ps_latlink_iter_link(litor);
		int16 sf, ef;
		float64 post;
		int32 ascr;

		ef = ps_latlink_times(link, &sf);
		post = logmath_exp(ps_lattice_get_logmath(dag),
				   ps_latlink_prob(dag, link, &ascr));
		if (post > 0.0001)
			printf("%d -> %d prob %f ascr %d\n",
			       sf, ef, post, ascr);
	}
	return 0;
}

int
test_remaining_nodes(ps_lattice_t *dag) {
	ps_latnode_iter_t *itor;
	ps_latlink_iter_t *litor;
	int count, lcount;
    
	count = 0;
	lcount = 0;
	for (itor = ps_latnode_iter(dag); itor; itor = ps_latnode_iter_next(itor)) {
	    ps_latnode_t* node = ps_latnode_iter_node(itor);
	    for (litor = ps_latnode_entries(node);
		litor; litor = ps_latlink_iter_next(litor)) {
		lcount++;
	    }
	    count++;	
	}
	TEST_ASSERT(count == 3);
	TEST_ASSERT(lcount == 2);
	printf("Remaining %d nodes %d links\n", count, lcount);

	return 0;    
}


int
main(int argc, char *argv[])
{
	ps_decoder_t *ps;
	ps_lattice_t *dag;
	cmd_ln_t *config;
	FILE *rawfh;
	char const *hyp;
	int32 score;

	(void)argc;
	(void)argv;
        TEST_ASSERT(config =
                    ps_config_parse_json(
                        NULL,
                        "hmm: \"" MODELDIR "/en-us/en-us\","
                        "lm: \"" MODELDIR "/en-us/en-us.lm.bin\","
                        "dict: \"" MODELDIR "/en-us/cmudict-en-us.dict\","
                        "fwdtree: true,"
                        "fwdflat: false,"
                        "bestpath: false,"
                        "samprate: 16000"));
	TEST_ASSERT(ps = ps_init(config));
	TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb"));
	ps_decode_raw(ps, rawfh, -1);
	fclose(rawfh);
	hyp = ps_get_hyp(ps, &score);
	printf("FWDFLAT: %s (%d)\n", hyp, score);
	TEST_ASSERT(dag = ps_get_lattice(ps));
	ps_lattice_bestpath(dag, ps_get_lm(ps, PS_DEFAULT_SEARCH), 1.0, 1.0/15.0);
	score = ps_lattice_posterior(dag, ps_get_lm(ps, PS_DEFAULT_SEARCH), 1.0/15.0);
	printf("P(S|O) = %d\n", score);
	test_nodes_and_stuff(dag);
	ps_lattice_posterior_prune(dag, logmath_log(ps_lattice_get_logmath(dag), 1e-2)); 
	test_nodes_and_stuff(dag);

	TEST_EQUAL(0, ps_lattice_write(dag, "goforward.lat"));

	dag = ps_lattice_read(ps, "goforward.lat");
	TEST_ASSERT(dag);
	ps_lattice_bestpath(dag, ps_get_lm(ps, PS_DEFAULT_SEARCH), 1.0, 1.0/15.0);
	score = ps_lattice_posterior(dag, ps_get_lm(ps, PS_DEFAULT_SEARCH), 1.0/15.0);
	printf("P(S|O) = %d\n", score);
	test_nodes_and_stuff(dag);
	ps_lattice_free(dag);
	ps_free(ps);
	ps_config_free(config);

	/* Now test standalone lattices. */
	dag = ps_lattice_read(NULL, "goforward.lat");
	TEST_ASSERT(dag);
	test_nodes_and_stuff(dag);
	ps_lattice_free(dag);

	/* Test stripping the unreachable nodes. */
	dag = ps_lattice_read(NULL, DATADIR "/unreachable.lat");
	TEST_ASSERT(dag);
	ps_lattice_delete_unreachable(dag);
	test_remaining_nodes(dag);
	ps_lattice_free(dag);
	
	return 0;
}