SphinxBase  0.6
include/sphinxbase/fsg_model.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 /*
00036  * fsg_model.h -- Word-level finite state graph
00037  * 
00038  * **********************************************
00039  * CMU ARPA Speech Project
00040  *
00041  * Copyright (c) 2003 Carnegie Mellon University.
00042  * ALL RIGHTS RESERVED.
00043  * **********************************************
00044  */
00045 
00046 
00047 #ifndef __FSG_MODEL_H__
00048 #define __FSG_MODEL_H__
00049 
00050 /* System headers. */
00051 #include <stdio.h>
00052 #include <string.h>
00053 
00054 /* SphinxBase headers. */
00055 #include <sphinxbase/prim_type.h>
00056 #include <sphinxbase/glist.h>
00057 #include <sphinxbase/logmath.h>
00058 #include <sphinxbase/bitvec.h>
00059 #include <sphinxbase/hash_table.h>
00060 #include <sphinxbase/listelem_alloc.h>
00061 #include <sphinxbase/sphinxbase_export.h>
00062 
00063 /*
00064  * A single transition in the FSG.
00065  */
00066 typedef struct fsg_link_s {
00067     int32 from_state;
00068     int32 to_state;
00069     int32 logs2prob;    
00070     int32 wid;          
00071 } fsg_link_t;
00072 
00073 /* Access macros */
00074 #define fsg_link_from_state(l)  ((l)->from_state)
00075 #define fsg_link_to_state(l)    ((l)->to_state)
00076 #define fsg_link_wid(l)         ((l)->wid)
00077 #define fsg_link_logs2prob(l)   ((l)->logs2prob)
00078 
00082 typedef struct trans_list_s trans_list_t;
00083 
00091 typedef struct fsg_model_s {
00092     int refcount;       
00093     char *name;         
00094     int32 n_word;       
00095     int32 n_word_alloc; 
00096     char **vocab;       
00097     bitvec_t *silwords; 
00098     bitvec_t *altwords; 
00099     logmath_t *lmath;   
00100     int32 n_state;      
00101     int32 start_state;  
00102     int32 final_state;  
00103     float32 lw;         
00105     trans_list_t *trans; 
00106     listelem_alloc_t *link_alloc; 
00107 } fsg_model_t;
00108 
00109 /* Access macros */
00110 #define fsg_model_name(f)               ((f)->name)
00111 #define fsg_model_n_state(f)            ((f)->n_state)
00112 #define fsg_model_start_state(f)        ((f)->start_state)
00113 #define fsg_model_final_state(f)        ((f)->final_state)
00114 #define fsg_model_log(f,p)              logmath_log((f)->lmath, p)
00115 #define fsg_model_lw(f)                 ((f)->lw)
00116 #define fsg_model_n_word(f)             ((f)->n_word)
00117 #define fsg_model_word_str(f,wid)       (wid == -1 ? "(NULL)" : (f)->vocab[wid])
00118 
00122 typedef struct fsg_arciter_s fsg_arciter_t;
00123 
00127 #define fsg_model_has_sil(f)            ((f)->silwords != NULL)
00128 
00132 #define fsg_model_has_alt(f)            ((f)->altwords != NULL)
00133 
00134 #define fsg_model_is_filler(f,wid) \
00135     (fsg_model_has_sil(f) ? bitvec_is_set((f)->silwords, wid) : FALSE)
00136 #define fsg_model_is_alt(f,wid) \
00137     (fsg_model_has_alt(f) ? bitvec_is_set((f)->altwords, wid) : FALSE)
00138 
00142 SPHINXBASE_EXPORT
00143 fsg_model_t *fsg_model_init(char const *name, logmath_t *lmath,
00144                             float32 lw, int32 n_state);
00145 
00185 SPHINXBASE_EXPORT
00186 fsg_model_t *fsg_model_readfile(const char *file, logmath_t *lmath, float32 lw);
00187 
00191 SPHINXBASE_EXPORT
00192 fsg_model_t *fsg_model_read(FILE *fp, logmath_t *lmath, float32 lw);
00193 
00199 SPHINXBASE_EXPORT
00200 fsg_model_t *fsg_model_retain(fsg_model_t *fsg);
00201 
00207 SPHINXBASE_EXPORT
00208 int fsg_model_free(fsg_model_t *fsg);
00209 
00215 SPHINXBASE_EXPORT
00216 int fsg_model_word_add(fsg_model_t *fsg, char const *word);
00217 
00223 SPHINXBASE_EXPORT
00224 int fsg_model_word_id(fsg_model_t *fsg, char const *word);
00225 
00232 SPHINXBASE_EXPORT
00233 void fsg_model_trans_add(fsg_model_t * fsg,
00234                          int32 from, int32 to, int32 logp, int32 wid);
00235 
00246 SPHINXBASE_EXPORT
00247 int32 fsg_model_null_trans_add(fsg_model_t * fsg, int32 from, int32 to, int32 logp);
00248 
00263 SPHINXBASE_EXPORT
00264 int32 fsg_model_tag_trans_add(fsg_model_t * fsg, int32 from, int32 to,
00265                               int32 logp, int32 wid);
00266 
00273 SPHINXBASE_EXPORT
00274 glist_t fsg_model_null_trans_closure(fsg_model_t * fsg, glist_t nulls);
00275 
00279 SPHINXBASE_EXPORT
00280 glist_t fsg_model_trans(fsg_model_t *fsg, int32 i, int32 j);
00281 
00285 SPHINXBASE_EXPORT
00286 fsg_arciter_t *fsg_model_arcs(fsg_model_t *fsg, int32 i);
00287 
00291 SPHINXBASE_EXPORT
00292 fsg_link_t *fsg_arciter_get(fsg_arciter_t *itor);
00293 
00297 SPHINXBASE_EXPORT
00298 fsg_arciter_t *fsg_arciter_next(fsg_arciter_t *itor);
00299 
00303 SPHINXBASE_EXPORT
00304 void fsg_arciter_free(fsg_arciter_t *itor);
00308 SPHINXBASE_EXPORT
00309 fsg_link_t *fsg_model_null_trans(fsg_model_t *fsg, int32 i, int32 j);
00310 
00317 SPHINXBASE_EXPORT
00318 int fsg_model_add_silence(fsg_model_t * fsg, char const *silword,
00319                           int state, float32 silprob);
00320 
00324 SPHINXBASE_EXPORT
00325 int fsg_model_add_alt(fsg_model_t * fsg, char const *baseword,
00326                       char const *altword);
00327 
00331 SPHINXBASE_EXPORT
00332 void fsg_model_write(fsg_model_t *fsg, FILE *fp);
00333 
00337 SPHINXBASE_EXPORT
00338 void fsg_model_writefile(fsg_model_t *fsg, char const *file);
00339 
00343 SPHINXBASE_EXPORT
00344 void fsg_model_write_fsm(fsg_model_t *fsg, FILE *fp);
00345 
00349 SPHINXBASE_EXPORT
00350 void fsg_model_writefile_fsm(fsg_model_t *fsg, char const *file);
00351 
00355 SPHINXBASE_EXPORT
00356 void fsg_model_write_symtab(fsg_model_t *fsg, FILE *file);
00357 
00361 SPHINXBASE_EXPORT
00362 void fsg_model_writefile_symtab(fsg_model_t *fsg, char const *file);
00363 
00364 #endif /* __FSG_MODEL_H__ */