Edinburgh Speech Tools  2.1-release
EST_viterbi.h
Go to the documentation of this file.
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1996,1997 */
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 /* THE UNIVERSITY OF EDINBURGH 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 THE UNIVERSITY OF EDINBURGH 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 /* Authors: Alan W Black */
34 /* Date : July 1996 */
35 /*-----------------------------------------------------------------------*/
36 /* A viterbi decoder */
37 /* */
38 /* User provides the candidates, target and combine score function */
39 /* and it searches for the best path through the candidates */
40 /* */
41 /*=======================================================================*/
42 
43 #ifndef __VERTERBI_H__
44 #define __VERTERBI_H__
45 
46 #include "EST_cutils.h"
47 #include "EST_Features.h"
49 
50 /** \class EST_VTCandidate
51  Internal class to \ref EST_Viterbi_Decoder used to a represent
52  a candidate.
53 
54  These objects need to be created and set by the user of the Viterbi
55  decoder.
56 
57  @author Alan W Black (awb@cstr.ed.ac.uk): July 1996
58 */
60  private:
61  public:
62  EST_VTCandidate() {score=0.0; next=0; s=0; pos=0; }
63  ~EST_VTCandidate() {if (next != 0) delete next;}
64  float score;
66  int pos;
69 };
70 
71 
72 /** \class EST_VTPath
73  Internal class to \ref EST_Viterbi_Decoder used to a represent
74  a link in a path the candidates.
75 
76  @author Alan W Black (awb@cstr.ed.ac.uk): July 1996
77 */
78 class EST_VTPath {
79  private:
80  public:
81  EST_VTPath() {score=0.0; from=0; next=0; c=0; state=0;}
82  ~EST_VTPath() {if (next != 0) delete next;}
83  double score; /* cumulative score for path */
84  int state;
89 };
90 
91 /** \class EST_VTPoint
92  Internal class to \ref EST_Viterbi_Decoder used to a node in
93  the decoder table
94 
95  @author Alan W Black (awb@cstr.ed.ac.uk): July 1996
96 */
97 class EST_VTPoint {
98  private:
99  public:
100  EST_VTPoint() {next=0; s=0; paths=0; num_paths=0; cands=0; st_paths=0; num_states=0;}
101  ~EST_VTPoint();
109 };
110 
111 typedef EST_VTCandidate *(*uclist_f_t)(EST_Item *s,EST_Features &f);
112 typedef EST_VTPath *(*unpath_f_t)(EST_VTPath *p,EST_VTCandidate *c,
113  EST_Features &f);
114 
115 /** \class EST_Viterbi_Decoder
116  \brief A class that offers a generalised Viterbi decoder.
117 
118  This class can be used to find the best path through a set
119  of candidates based on likelihoods of the candidates and
120  some combination function. The candidate list and joining
121  are not included in the decoder itself but are user defined functions
122  that are specified at construction time.
123 
124  Those functions need to return a list of candidates and score
125  a join of a path to a candidate and (optionally define a state).
126 
127  Although this offers a full Viterbi search it may also be used as
128  a generalised beam search.
129 
130  See `viterbi_main.cc` for an example of using this.
131 
132  @author Alan W Black (awb@cstr.ed.ac.uk): July 1996
133 */
135  private:
136  int num_states;
137 
138  /// very detailed info - for developers
139  int debug;
140 
141  /// less detailed info than debug - for users
142  int trace;
143 
144  int beam_width;
145  int cand_width;
146  int big_is_good;
147  uclist_f_t user_clist;
148  unpath_f_t user_npath;
149  EST_VTPoint *timeline;
150 
151  /// pruning parameters
152  bool do_pruning;
153  float overall_path_pruning_envelope_width;
154  float candidate_pruning_envelope_width;
155 
156  void add_path(EST_VTPoint *p, EST_VTPath *np);
157  void vit_add_path(EST_VTPoint *p, EST_VTPath *np);
158  void vit_add_paths(EST_VTPoint *p, EST_VTPath *np);
159  EST_VTPath *find_best_end() const;
160  int betterthan(const float a,const float b) const;
161  void prune_initialize(EST_VTPoint *p,
162  double &best_score, double &best_candidate_score,
163  double &score_cutoff, double &candidate_cutoff,
164  int &cand_count);
165  public:
166 
167  /// For holding values to pass to user called functions
169 
170  /// Unfortunately using MAX_DOUBLE doesn't do the right thing
171  /// (e.g. comparison don't work with MAX_DOUBLE on alphas), so
172  /// we declare our own large number.
173  const double vit_a_big_number;
174 
175  /** Construct a decoder with given candidate function and join function,
176  as number of states is given this implies a beam search
177  */
179  /** Construct a decoder with given candidate function and join function
180  with a state size as specified.
181  */
182  EST_Viterbi_Decoder(uclist_f_t a, unpath_f_t b, int num_states);
183  ///
185  /// Only for use in beam search mode: number of paths to consider
186  void set_beam_width(int w) {beam_width = w;}
187  /// Only for use in beam search mode: number of candidates to consider
188  void set_cand_width(int w) {cand_width = w;}
189  /// Output some debugging information
190  void set_debug(int d) {debug = d;}
191 
192  /** Define whether good scores are bigger or smaller. This allows
193  the search to work for likelihoods probabilities, scores or
194  whatever
195  */
196  void set_big_is_good(int flag) { big_is_good = flag; }
197  /** Add a new candidate to list if better than others, pruning
198  the list if required.
199  */
200  EST_VTCandidate *add_cand_prune(EST_VTCandidate *newcand,
201  EST_VTCandidate *allcands);
202 
203  bool vit_prune_path(double path_score, double score_cutoff);
204 
205  /// Build the initial table from a \ref EST_Relation
206  void initialise(EST_Relation *r);
207 
208  /// set beam widths for pruning
209  void set_pruning_parameters(float beam, float ob_beam);
210 
211  void turn_on_debug();
212  void turn_on_trace();
213 
214  /// Do the the actual search
215  void search(void);
216  /** Extract the result from the table and store it as a feature
217  on the related \ref EST_Item in the given \ref EST_Relation
218  named as `n`. Return FALSE if no path is found.
219  */
220  bool result(const EST_String &n);
221 
222  /** Extract the end point of the best path found during search.
223  Return FALSE if no path is found.
224  */
225  bool result( EST_VTPath **bestPathEnd );
226 
227  /// Copy named feature from the best path to related stream item
228  void copy_feature(const EST_String &n);
229 };
230 
231 
232 #endif // __VERTERBI_H__
EST_VTPath *(* unpath_f_t)(EST_VTPath *p, EST_VTCandidate *c, EST_Features &f)
Definition: EST_viterbi.h:112
EST_VTCandidate * cands
Definition: EST_viterbi.h:105
void set_cand_width(int w)
Only for use in beam search mode: number of candidates to consider.
Definition: EST_viterbi.h:188
EST_VTPoint * next
Definition: EST_viterbi.h:108
void set_big_is_good(int flag)
Definition: EST_viterbi.h:196
void set_beam_width(int w)
Only for use in beam search mode: number of paths to consider.
Definition: EST_viterbi.h:186
EST_VTPath ** st_paths
Definition: EST_viterbi.h:107
void set_debug(int d)
Output some debugging information.
Definition: EST_viterbi.h:190
double score
Definition: EST_viterbi.h:83
EST_VTCandidate *(* uclist_f_t)(EST_Item *s, EST_Features &f)
Definition: EST_viterbi.h:111
const double vit_a_big_number
Unfortunately using MAX_DOUBLE doesn't do the right thing (e.g. comparison don't work with MAX_DOUBLE...
Definition: EST_viterbi.h:173
EST_Item * s
Definition: EST_viterbi.h:102
EST_VTPath * from
Definition: EST_viterbi.h:87
EST_Features f
For holding values to pass to user called functions.
Definition: EST_viterbi.h:168
EST_Item * s
Definition: EST_viterbi.h:67
f
Definition: EST_item_aux.cc:48
EST_Features f
Definition: EST_viterbi.h:85
EST_VTCandidate * next
Definition: EST_viterbi.h:68
EST_VTCandidate * c
Definition: EST_viterbi.h:86
A class that offers a generalised Viterbi decoder.
Definition: EST_viterbi.h:134
EST_VTPath * next
Definition: EST_viterbi.h:88
EST_VTPath * paths
Definition: EST_viterbi.h:106