SphinxBase  5prealpha
fsg_model.h
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3  * Copyright (c) 1999-2004 Carnegie Mellon University. All rights
4  * reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  *
19  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
20  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
23  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * ====================================================================
32  *
33  */
34 
35 /*
36  * fsg_model.h -- Word-level finite state graph
37  *
38  * **********************************************
39  * CMU ARPA Speech Project
40  *
41  * Copyright (c) 2003 Carnegie Mellon University.
42  * ALL RIGHTS RESERVED.
43  * **********************************************
44  */
45 
46 
47 #ifndef __FSG_MODEL_H__
48 #define __FSG_MODEL_H__
49 
50 /* System headers. */
51 #include <stdio.h>
52 #include <string.h>
53 
54 /* SphinxBase headers. */
55 #include <sphinxbase/prim_type.h>
56 #include <sphinxbase/glist.h>
57 #include <sphinxbase/logmath.h>
58 #include <sphinxbase/bitvec.h>
59 #include <sphinxbase/hash_table.h>
61 #include <sphinxbase/sphinxbase_export.h>
62 
63 #ifdef __cplusplus
64 extern "C" {
65 #endif
66 #if 0
67 /* Fool Emacs. */
68 }
69 #endif
70 
71 /*
72  * A single transition in the FSG.
73  */
74 typedef struct fsg_link_s {
75  int32 from_state;
76  int32 to_state;
77  int32 logs2prob;
78  int32 wid;
79 } fsg_link_t;
80 
81 /* Access macros */
82 #define fsg_link_from_state(l) ((l)->from_state)
83 #define fsg_link_to_state(l) ((l)->to_state)
84 #define fsg_link_wid(l) ((l)->wid)
85 #define fsg_link_logs2prob(l) ((l)->logs2prob)
86 
90 typedef struct trans_list_s trans_list_t;
91 
99 typedef struct fsg_model_s {
100  int refcount;
101  char *name;
102  int32 n_word;
103  int32 n_word_alloc;
104  char **vocab;
105  bitvec_t *silwords;
106  bitvec_t *altwords;
108  int32 n_state;
109  int32 start_state;
110  int32 final_state;
111  float32 lw;
115 } fsg_model_t;
116 
117 /* Access macros */
118 #define fsg_model_name(f) ((f)->name)
119 #define fsg_model_n_state(f) ((f)->n_state)
120 #define fsg_model_start_state(f) ((f)->start_state)
121 #define fsg_model_final_state(f) ((f)->final_state)
122 #define fsg_model_log(f,p) logmath_log((f)->lmath, p)
123 #define fsg_model_lw(f) ((f)->lw)
124 #define fsg_model_n_word(f) ((f)->n_word)
125 #define fsg_model_word_str(f,wid) (wid == -1 ? "(NULL)" : (f)->vocab[wid])
126 
130 typedef struct fsg_arciter_s fsg_arciter_t;
131 
135 #define fsg_model_has_sil(f) ((f)->silwords != NULL)
136 
140 #define fsg_model_has_alt(f) ((f)->altwords != NULL)
141 
142 #define fsg_model_is_filler(f,wid) \
143  (fsg_model_has_sil(f) ? bitvec_is_set((f)->silwords, wid) : FALSE)
144 #define fsg_model_is_alt(f,wid) \
145  (fsg_model_has_alt(f) ? bitvec_is_set((f)->altwords, wid) : FALSE)
146 
150 SPHINXBASE_EXPORT
151 fsg_model_t *fsg_model_init(char const *name, logmath_t *lmath,
152  float32 lw, int32 n_state);
153 
193 SPHINXBASE_EXPORT
194 fsg_model_t *fsg_model_readfile(const char *file, logmath_t *lmath, float32 lw);
195 
199 SPHINXBASE_EXPORT
200 fsg_model_t *fsg_model_read(FILE *fp, logmath_t *lmath, float32 lw);
201 
207 SPHINXBASE_EXPORT
208 fsg_model_t *fsg_model_retain(fsg_model_t *fsg);
209 
215 SPHINXBASE_EXPORT
216 int fsg_model_free(fsg_model_t *fsg);
217 
223 SPHINXBASE_EXPORT
224 int fsg_model_word_add(fsg_model_t *fsg, char const *word);
225 
231 SPHINXBASE_EXPORT
232 int fsg_model_word_id(fsg_model_t *fsg, char const *word);
233 
240 SPHINXBASE_EXPORT
241 void fsg_model_trans_add(fsg_model_t * fsg,
242  int32 from, int32 to, int32 logp, int32 wid);
243 
254 SPHINXBASE_EXPORT
255 int32 fsg_model_null_trans_add(fsg_model_t * fsg, int32 from, int32 to, int32 logp);
256 
271 SPHINXBASE_EXPORT
272 int32 fsg_model_tag_trans_add(fsg_model_t * fsg, int32 from, int32 to,
273  int32 logp, int32 wid);
274 
281 SPHINXBASE_EXPORT
282 glist_t fsg_model_null_trans_closure(fsg_model_t * fsg, glist_t nulls);
283 
287 SPHINXBASE_EXPORT
288 glist_t fsg_model_trans(fsg_model_t *fsg, int32 i, int32 j);
289 
293 SPHINXBASE_EXPORT
294 fsg_arciter_t *fsg_model_arcs(fsg_model_t *fsg, int32 i);
295 
299 SPHINXBASE_EXPORT
300 fsg_link_t *fsg_arciter_get(fsg_arciter_t *itor);
301 
305 SPHINXBASE_EXPORT
306 fsg_arciter_t *fsg_arciter_next(fsg_arciter_t *itor);
307 
311 SPHINXBASE_EXPORT
312 void fsg_arciter_free(fsg_arciter_t *itor);
316 SPHINXBASE_EXPORT
317 fsg_link_t *fsg_model_null_trans(fsg_model_t *fsg, int32 i, int32 j);
318 
325 SPHINXBASE_EXPORT
326 int fsg_model_add_silence(fsg_model_t * fsg, char const *silword,
327  int state, float32 silprob);
328 
332 SPHINXBASE_EXPORT
333 int fsg_model_add_alt(fsg_model_t * fsg, char const *baseword,
334  char const *altword);
335 
339 SPHINXBASE_EXPORT
340 void fsg_model_write(fsg_model_t *fsg, FILE *fp);
341 
345 SPHINXBASE_EXPORT
346 void fsg_model_writefile(fsg_model_t *fsg, char const *file);
347 
351 SPHINXBASE_EXPORT
352 void fsg_model_write_fsm(fsg_model_t *fsg, FILE *fp);
353 
357 SPHINXBASE_EXPORT
358 void fsg_model_writefile_fsm(fsg_model_t *fsg, char const *file);
359 
363 SPHINXBASE_EXPORT
364 void fsg_model_write_symtab(fsg_model_t *fsg, FILE *file);
365 
369 SPHINXBASE_EXPORT
370 void fsg_model_writefile_symtab(fsg_model_t *fsg, char const *file);
371 
372 #ifdef __cplusplus
373 }
374 #endif
375 
376 #endif /* __FSG_MODEL_H__ */
int32 start_state
Must be in the range [0..n_state-1].
Definition: fsg_model.h:109
int refcount
Reference count.
Definition: fsg_model.h:100
int32 final_state
Must be in the range [0..n_state-1].
Definition: fsg_model.h:110
int32 n_word_alloc
Number of words allocated in vocab.
Definition: fsg_model.h:103
float32 lw
Language weight that&#39;s been applied to transition logprobs.
Definition: fsg_model.h:111
listelem_alloc_t * link_alloc
Allocator for FSG links.
Definition: fsg_model.h:114
int32 n_word
Number of unique words in this FSG.
Definition: fsg_model.h:102
A node in a generic list.
Definition: glist.h:100
Basic type definitions used in Sphinx.
Adjacency list (opaque) for a state in an FSG.
Definition: fsg_model.c:56
Fast linked list allocator.
Implementation of arc iterator.
Definition: fsg_model.c:64
Fast memory allocator for uniformly sized objects.
Generic linked-lists maintenance.
int32 n_state
number of states in FSG
Definition: fsg_model.h:108
bitvec_t * altwords
Indicates which words are pronunciation alternates.
Definition: fsg_model.h:106
trans_list_t * trans
Transitions out of each state, if any.
Definition: fsg_model.h:113
Hash table implementation.
Word level FSG definition.
Definition: fsg_model.h:99
bitvec_t * silwords
Indicates which words are silence/fillers.
Definition: fsg_model.h:105
Fast integer logarithmic addition operations.
An implementation of bit vectors.
logmath_t * lmath
Pointer to log math computation object.
Definition: fsg_model.h:107
char * name
A unique string identifier for this FSG.
Definition: fsg_model.h:101
char ** vocab
Vocabulary for this FSG.
Definition: fsg_model.h:104