Debian Codegraph @

src/stats/cst_viterbi.c

raw
1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 2000 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author: Alan W Black ([email protected]) */
34 /* Date: August 2000 */
35 /*************************************************************************/
36 /* */
37 /* A Viterbi decoder */
38 /* */
39 /*************************************************************************/
40
41 #include <limits.h>
42 #include "cst_viterbi.h"
43
44 static void vit_point_init_path_array(cst_vit_point *n,int num_states);
45 static void vit_point_init_dynamic_path_array(cst_vit_point *n,
46 cst_vit_cand *c);
47 static void vit_add_paths(cst_viterbi *vd,
48 cst_vit_point *point,
49 cst_vit_path *path);
50 static void vit_add_path(cst_viterbi *vd,cst_vit_point *p, cst_vit_path *np);
51 static cst_vit_path *find_best_path(cst_viterbi *vd);
52 static int betterthan(cst_viterbi *v,int a, int b);
53
54 cst_vit_cand *new_vit_cand()
55 {
56 cst_vit_cand *c = cst_alloc(struct cst_vit_cand_struct,1);
57
58 return c;
59 }
60
61 void vit_cand_set(cst_vit_cand *vc, cst_val *val)
62 {
63 if (vc->val)
64 delete_val(vc->val);
65 vc->val = val;
66 val_inc_refcount(vc->val);
67 }
68
69 void vit_cand_set_int(cst_vit_cand *vc, int ival)
70 {
71 vc->ival = ival;
72 vit_cand_set(vc, int_val(ival));
73 }
74
75 void delete_vit_cand(cst_vit_cand *vc)
76 {
77 if (vc)
78 {
79 delete_val(vc->val);
80 delete_vit_cand(vc->next);
81 cst_free(vc);
82 }
83 }
84
85 cst_vit_path *new_vit_path()
86 {
87 cst_vit_path *p = cst_alloc(struct cst_vit_path_struct,1);
88
89 return p;
90 }
91
92 void delete_vit_path(cst_vit_path *vp)
93 {
94 if (vp)
95 {
96 if (vp->f)
97 delete_features(vp->f);
98 delete_vit_path(vp->next);
99 cst_free(vp);
100 }
101 }
102
103 cst_vit_point *new_vit_point()
104 {
105 cst_vit_point *p = cst_alloc(struct cst_vit_point_struct,1);
106
107 return p;
108 }
109
110 void delete_vit_point(cst_vit_point *vp)
111 {
112 int i;
113
114 if (vp)
115 {
116 if (vp->paths)
117 delete_vit_path(vp->paths);
118 if (vp->num_states != 0)
119 {
120 for (i=0; i<vp->num_states; i++)
121 if (vp->state_paths[i])
122 delete_vit_path(vp->state_paths[i]);
123 cst_free(vp->state_paths);
124 }
125 delete_vit_cand(vp->cands);
126 delete_vit_point(vp->next);
127 cst_free(vp);
128 }
129 }
130
131 cst_viterbi *new_viterbi(cst_vit_cand_f_t *cand_func,
132 cst_vit_path_f_t *path_func)
133 {
134 cst_viterbi *v = cst_alloc(struct cst_viterbi_struct,1);
135
136 v->cand_func = cand_func;
137 v->path_func = path_func;
138 v->f = new_features();
139 return v;
140 }
141
142 void delete_viterbi(cst_viterbi *vd)
143 {
144 if (vd)
145 {
146 delete_vit_point(vd->timeline);
147 delete_features(vd->f);
148 cst_free(vd);
149 }
150
151 return;
152 }
153
154 void viterbi_initialise(cst_viterbi *vd,cst_relation *r)
155 {
156 cst_item *i;
157 cst_vit_point *last = 0;
158 cst_vit_point *n = 0;
159
160 /* Construct the timeline with points for each item in relation */
161 /* initiallising the state tables at each point */
162 for (i=relation_head(r); TRUE ; i=item_next(i))
163 {
164 n = new_vit_point();
165 n->item = i;
166 if (vd->num_states > 0) /* a state based viterbi search */
167 vit_point_init_path_array(n,vd->num_states);
168 if (last)
169 last->next = n;
170 else
171 vd->timeline = n;
172 last = n;
173 /* we need an extra one (even if there are no items) */
174 /* so we go one past end of the relation */
175 if (i == 0)
176 {
177 vd->last_point = n;
178 break;
179 }
180 }
181
182 if (vd->num_states == 0) /* its a general beam search */
183 vd->timeline->paths = new_vit_path();
184 if (vd->num_states == -1) /* Dynamic number of states (# cands) */
185 vit_point_init_path_array(vd->timeline,1);
186
187 }
188
189 static void vit_point_init_path_array(cst_vit_point *n,int num_states)
190 {
191 n->num_states = num_states;
192 n->state_paths = cst_alloc(cst_vit_path*,num_states);
193 }
194
195 static void vit_point_init_dynamic_path_array(cst_vit_point *n,
196 cst_vit_cand *c)
197 {
198 int i;
199 cst_vit_cand *cc;
200
201 for (cc=c,i=0; cc; i++,cc=cc->next)
202 cc->pos = i;
203
204 vit_point_init_path_array(n,i);
205 }
206
207 void viterbi_decode(cst_viterbi *vd)
208 {
209 cst_vit_point *p;
210 cst_vit_path *np;
211 cst_vit_cand *c;
212 int i;
213
214 /* For each time point */
215 for (p=vd->timeline; p->next != NULL; p=p->next)
216 {
217 /* Find the candidates */
218 p->cands = (*vd->cand_func)(p->item,vd);
219 if (vd->num_states != 0) /* true viterbi */
220 {
221 if (vd->num_states == -1) /* dynamic number of states (# cands) */
222 vit_point_init_dynamic_path_array(p->next,p->cands);
223 for (i=0; i<p->num_states; i++)
224 {
225
226 if (((p == vd->timeline) && i==0) ||
227 (p->state_paths[i] != 0))
228 for (c=p->cands; c; c=c->next)
229 {
230 np = (*vd->path_func)(p->state_paths[i],c,vd);
231 vit_add_paths(vd,p->next,np);
232 }
233 }
234
235 }
236 else /* general beam search */
237 {
238 cst_errmsg("viterbi, general beam search not implemented\n");
239 /*
240 for (t=p->paths; t; t=t->next)
241 {
242 for (c=p->cands; c; c=c->next)
243 {
244 np = (vd->path_func)(t,c,vd);
245 add_path(p->next,np);
246 }
247 }
248 */
249 }
250 }
251 }
252
253 static void vit_add_paths(cst_viterbi *vd,
254 cst_vit_point *point,
255 cst_vit_path *path)
256 {
257 /* Add a list of paths at point */
258 cst_vit_path *p, *next_p;
259
260 for (p=path; p; p=next_p)
261 {
262 next_p = p->next; /* as p could be deleted is not required */
263 vit_add_path(vd,point,p);
264 }
265 }
266
267 static void vit_add_path(cst_viterbi *vd,cst_vit_point *p, cst_vit_path *np)
268 {
269 if (p->state_paths[np->state] == 0)
270 { /* we don't have one yet so this is best */
271 p->state_paths[np->state] = np;
272 }
273 else if (betterthan(vd,np->score,p->state_paths[np->state]->score))
274 { /* its better than what we have already */
275 delete_vit_path(p->state_paths[np->state]);
276 p->state_paths[np->state] = np;
277 }
278 else
279 delete_vit_path(np);
280 }
281
282 int viterbi_result(cst_viterbi *vd,const char *n)
283 {
284 /* Find best path through the decoder, adding field to item named n */
285 /* with choising value */
286 cst_vit_path *p;
287
288 if ((vd->timeline == 0) || (vd->timeline->next == 0))
289 return TRUE; /* it has succeeded in the null case */
290 p = find_best_path(vd);
291 if (p == NULL) /* bummer */
292 return FALSE;
293
294 for (; p; p=p->from)
295 if (p->cand)
296 {
297 #if 1
298 item_set_int(p->cand->item,"cl_total_score",p->score);
299 item_set_int(p->cand->item,"cl_cand_score",p->cand->score);
300 #endif
301 item_set(p->cand->item,n,p->cand->val);
302 }
303 return TRUE;
304 }
305
306 void viterbi_copy_feature(cst_viterbi *vd,const char *featname)
307 {
308 /* copy a feature from the best path to related item */
309 cst_vit_path *p;
310
311 p = find_best_path(vd);
312 if (p == NULL) /* nothing to copy, emtpy stream or no solution */
313 return;
314
315 for (; p; p=p->from)
316 if (p->cand && feat_present(p->f, featname))
317 item_set(p->cand->item,featname,feat_val(p->f,featname));
318 return;
319 }
320
321 static cst_vit_path *find_best_path(cst_viterbi *vd)
322 {
323 cst_vit_point *t;
324 int best,worst;
325 cst_vit_path *best_p=NULL;
326 int i;
327
328 if (vd->big_is_good)
329 worst = -INT_MAX;
330 else
331 worst = INT_MAX;
332 best = worst; /* though we should be able to find something better */
333
334 t = vd->last_point;
335
336 if (vd->num_states != 0)
337 {
338 for (i=0; i<t->num_states; i++)
339 {
340 if ((t->state_paths[i] != NULL) &&
341 (betterthan(vd,t->state_paths[i]->score, best)))
342 {
343 best = t->state_paths[i]->score;
344 best_p = t->state_paths[i];
345 }
346 }
347 }
348
349 return best_p;
350 }
351
352 static int betterthan(cst_viterbi *v,int a, int b)
353 {
354 /* better may be most big or most small */
355 /* for probabalities big is good */
356
357 if (v->big_is_good)
358 return (a > b);
359 else
360 return (a < b);
361 }