PocketSphinx  0.6
src/libpocketsphinx/fsg_history.h
00001 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
00002 /* ====================================================================
00003  * Copyright (c) 1999-2004 Carnegie Mellon University.  All rights
00004  * reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer. 
00012  *
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in
00015  *    the documentation and/or other materials provided with the
00016  *    distribution.
00017  *
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 
00020  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
00021  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00022  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
00023  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
00025  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
00026  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
00027  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00028  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
00029  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  *
00031  * ====================================================================
00032  *
00033  */
00034 /*
00035  * fsg_history.h -- FSG Viterbi decode history
00036  * 
00037  * **********************************************
00038  * CMU ARPA Speech Project
00039  *
00040  * Copyright (c) 1999 Carnegie Mellon University.
00041  * ALL RIGHTS RESERVED.
00042  * **********************************************
00043  * 
00044  * HISTORY
00045  * 
00046  * $Log: fsg_history.h,v $
00047  * Revision 1.1.1.1  2006/05/23 18:45:02  dhuggins
00048  * re-importation
00049  *
00050  * Revision 1.1  2004/07/16 00:57:12  egouvea
00051  * Added Ravi's implementation of FSG support.
00052  *
00053  * Revision 1.7  2004/07/07 22:30:35  rkm
00054  * *** empty log message ***
00055  *
00056  * Revision 1.6  2004/07/07 13:56:33  rkm
00057  * Added reporting of (acoustic score - best senone score)/frame
00058  *
00059  * Revision 1.5  2004/06/25 14:49:08  rkm
00060  * Optimized size of history table and speed of word transitions by maintaining only best scoring word exits at each state
00061  *
00062  * Revision 1.4  2004/06/23 20:32:16  rkm
00063  * *** empty log message ***
00064  *
00065  * Revision 1.3  2004/05/27 15:16:08  rkm
00066  * *** empty log message ***
00067  *
00068  * 
00069  * 25-Feb-2004  M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
00070  *              Started, based on S3.3 version.
00071  */
00072 
00073 
00074 #ifndef __S2_FSG_HISTORY_H__
00075 #define __S2_FSG_HISTORY_H__
00076 
00077 
00078 /* SphinxBase headers. */
00079 #include <sphinxbase/prim_type.h>
00080 #include <sphinxbase/fsg_model.h>
00081 
00082 /* Local headers. */
00083 #include "blkarray_list.h"
00084 #include "fsg_lextree.h"
00085 #include "dict.h"
00086 
00087 /*
00088  * The Viterbi history structure.  This is a tree, with the root at the
00089  * FSG start state, at frame 0, with a null predecessor.
00090  */
00091 
00092 /*
00093  * A single Viterbi history entry
00094  */
00095 typedef struct fsg_hist_entry_s {
00096     fsg_link_t *fsglink;                /* Link taken result in this entry */
00097     int32 score;                        /* Total path score at the end of this
00098                                            transition */
00099     int32 pred;                         /* Predecessor entry; -1 if none */
00100     int16 frame;                        /* Ending frame for this entry */
00101     int16 lc;                           /* Left context provided by this entry to
00102                                            succeeding words */
00103     fsg_pnode_ctxt_t rc;                /* Possible right contexts to which this entry
00104                                            applies */
00105 } fsg_hist_entry_t;
00106 
00107 /* Access macros */
00108 #define fsg_hist_entry_fsglink(v)       ((v)->fsglink)
00109 #define fsg_hist_entry_frame(v)         ((v)->frame)
00110 #define fsg_hist_entry_score(v)         ((v)->score)
00111 #define fsg_hist_entry_pred(v)          ((v)->pred)
00112 #define fsg_hist_entry_lc(v)            ((v)->lc)
00113 #define fsg_hist_entry_rc(v)            ((v)->rc)
00114 
00115 
00116 /*
00117  * The entire tree of history entries (fsg_history_t.entries).
00118  * Optimization: In a given frame, there may be several history entries, with
00119  * the same left and right phonetic context, terminating in a particular state.
00120  * Only the best scoring one of these needs to be saved, since everything else
00121  * will be pruned according to the Viterbi algorithm.  frame_entries is used
00122  * temporarily in each frame to determine these best scoring entries in that
00123  * frame.  Only the ones not pruned are transferred to entries at the end of
00124  * the frame.  However, null transitions are a problem since they create
00125  * entries that depend on entries created in the CURRENT frame.  Hence, this
00126  * pruning is done in two stages: first for the non-null transitions, and then
00127  * for the null transitions alone.  (This solution is sub-optimal, and can be
00128  * improved with a little more work.  SMOP.)
00129  * Why is frame_entries a list?  Each entry has a unique terminating state,
00130  * and has a unique lc CIphone.  But it has a SET of rc CIphones.
00131  * frame_entries[s][lc] is an ordered list of entries created in the current
00132  * frame, terminating in state s, and with left context lc.  The list is in
00133  * descending order of path score.  When a new entry with (s,lc) arrives,
00134  * its position in the list is determined.  Then its rc set is modified by
00135  * subtracting the union of the rc's of all its predecessors (i.e., better
00136  * scoring entries).  If the resulting rc set is empty, the entry is discarded.
00137  * Otherwise, it is inserted, and the rc sets of all downstream entries in the
00138  * list are updated by subtracting the new entry's rc.  If any of them becomes
00139  * empty, it is also discarded.
00140  * As mentioned earlier, this procedure is applied in two stages, for the
00141  * non-null transitions, and the null transitions, separately.
00142  */
00143 typedef struct fsg_history_s {
00144     fsg_model_t *fsg;           /* The FSG for which this object applies */
00145     blkarray_list_t *entries;   /* A list of history table entries; the root
00146                                    entry is the first element of the list */
00147     glist_t **frame_entries;
00148     int n_ciphone;
00149 } fsg_history_t;
00150 
00151 
00152 /*
00153  * One-time intialization: Allocate and return an initially empty history
00154  * module.
00155  */
00156 fsg_history_t *fsg_history_init(fsg_model_t *fsg, dict_t *dict);
00157 
00158 void fsg_history_utt_start(fsg_history_t *h);
00159 
00160 void fsg_history_utt_end(fsg_history_t *h);
00161 
00162 
00163 /*
00164  * Create a history entry recording the completion of the given FSG
00165  * transition, at the end of the given frame, with the given score, and
00166  * the given predecessor history entry.
00167  * The entry is initially temporary, and may be superseded by another
00168  * with a higher score.  The surviving entries must be transferred to
00169  * the main history table, via fsg_history_end_frame().
00170  */
00171 void fsg_history_entry_add (fsg_history_t *h,
00172                             fsg_link_t *l,      /* FSG transition */
00173                             int32 frame,
00174                             int32 score,
00175                             int32 pred,
00176                             int32 lc,
00177                             fsg_pnode_ctxt_t rc);
00178 
00179 /*
00180  * Transfer the surviving history entries for this frame into the permanent
00181  * history table.  This function can be called several times during a frame.
00182  * Each time, the entries surviving so far are transferred, and the temporary
00183  * lists cleared.  This feature is used to handle the entries due to non-null
00184  * transitions and null transitions separately.
00185  */
00186 void fsg_history_end_frame (fsg_history_t *h);
00187 
00188 
00189 /* Clear the hitory table */
00190 void fsg_history_reset (fsg_history_t *h);
00191 
00192 
00193 /* Return the number of valid entries in the given history table */
00194 int32 fsg_history_n_entries (fsg_history_t *h);
00195 
00196 /*
00197  * Return a ptr to the history entry for the given ID; NULL if there is no
00198  * such entry.
00199  */
00200 fsg_hist_entry_t *fsg_history_entry_get(fsg_history_t *h, int32 id);
00201 
00202 
00203 /*
00204  * Switch the FSG associated with the given history module.  Should be done
00205  * when the history list is empty.  If not empty, the list is cleared.
00206  */
00207 void fsg_history_set_fsg (fsg_history_t *h, fsg_model_t *fsg, dict_t *dict);
00208 
00209 /* Free the given Viterbi search history object */
00210 void fsg_history_free (fsg_history_t *h);
00211                                      
00212 #endif