46#include "sphinxbase/fsg_model.h"
69#define FSG_MODEL_BEGIN_DECL "FSG_BEGIN"
70#define FSG_MODEL_END_DECL "FSG_END"
71#define FSG_MODEL_N_DECL "N"
72#define FSG_MODEL_NUM_STATES_DECL "NUM_STATES"
73#define FSG_MODEL_S_DECL "S"
74#define FSG_MODEL_START_STATE_DECL "START_STATE"
75#define FSG_MODEL_F_DECL "F"
76#define FSG_MODEL_FINAL_STATE_DECL "FINAL_STATE"
77#define FSG_MODEL_T_DECL "T"
78#define FSG_MODEL_TRANSITION_DECL "TRANSITION"
79#define FSG_MODEL_COMMENT_CHAR '#'
83nextline_str2words(FILE * fp, int32 * lineno,
84 char **lineptr,
char ***wordptr)
96 if ((*lineptr)[0] == FSG_MODEL_COMMENT_CHAR)
104 if (*wordptr == NULL)
107 *wordptr =
ckd_realloc(*wordptr, n *
sizeof(**wordptr));
114 int32 from, int32 to, int32 logp, int32 wid)
120 if (fsg->
trans[from].trans == NULL)
124 for (gn = gl = fsg_model_trans(fsg, from, to); gn; gn = gnode_next(gn)) {
126 if (link->
wid == wid) {
135 link->from_state = from;
143 (
char const *) &link->to_state,
144 sizeof(link->to_state), gl);
148fsg_model_tag_trans_add(
fsg_model_t * fsg, int32 from, int32 to,
149 int32 logp, int32 wid)
155 E_FATAL(
"Null transition prob must be <= 1.0 (state %d -> %d)\n",
163 if (fsg->
trans[from].null_trans == NULL)
167 link = fsg_model_null_trans(fsg, from, to);
179 link->from_state = from;
186 (
char const *) &link->to_state,
187 sizeof(link->to_state), link);
188 assert(link == link2);
194fsg_model_null_trans_add(
fsg_model_t * fsg, int32 from, int32 to,
197 return fsg_model_tag_trans_add(fsg, from, to, logp, -1);
208 E_INFO(
"Computing transitive closure for null transitions\n");
216 for (i = 0; i < fsg->
n_state; ++i) {
219 if (null_trans == NULL)
236 for (gn1 = nulls; gn1; gn1 = gnode_next(gn1)) {
240 assert(tl1->
wid < 0);
242 if (fsg->
trans[tl1->to_state].null_trans == NULL)
251 k = fsg_model_null_trans_add(fsg,
261 (fsg, tl1->from_state,
270 E_INFO(
"%d null transitions added\n", n);
276fsg_model_trans(
fsg_model_t * fsg, int32 i, int32 j)
280 if (fsg->
trans[i].trans == NULL)
283 sizeof(j), &val) < 0)
289fsg_model_null_trans(
fsg_model_t * fsg, int32 i, int32 j)
293 if (fsg->
trans[i].null_trans == NULL)
296 sizeof(j), &val) < 0)
306 if (fsg->
trans[i].trans == NULL && fsg->
trans[i].null_trans == NULL)
309 if (fsg->
trans[i].null_trans)
311 if (fsg->
trans[i].trans)
313 if (itor->itor != NULL)
324 else if (itor->null_itor)
335 itor->gn = gnode_next(itor->gn);
337 if (itor->gn == NULL) {
339 if (itor->itor != NULL)
341 else if (itor->null_itor == NULL)
346 if (itor->null_itor == NULL)
349 if (itor->null_itor == NULL)
354 fsg_arciter_free(itor);
370fsg_model_word_id(
fsg_model_t * fsg,
char const *word)
375 for (wid = 0; wid < fsg->
n_word; ++wid) {
376 if (0 == strcmp(fsg->
vocab[wid], word))
386fsg_model_word_add(
fsg_model_t * fsg,
char const *word)
391 wid = fsg_model_word_id(fsg, word);
400 sizeof(*fsg->
vocab));
417fsg_model_add_silence(
fsg_model_t * fsg,
char const *silword,
418 int state, float32 silprob)
421 int n_trans, silwid, src;
423 E_INFO(
"Adding silence transitions for %s to FSG\n", silword);
425 silwid = fsg_model_word_add(fsg, silword);
433 for (src = 0; src < fsg->
n_state; src++) {
434 fsg_model_trans_add(fsg, src, src, logsilp, silwid);
439 fsg_model_trans_add(fsg, state, state, logsilp, silwid);
443 E_INFO(
"Added %d silence word transitions\n", n_trans);
448fsg_model_add_alt(
fsg_model_t * fsg,
char const *baseword,
451 int i, basewid, altwid;
455 for (basewid = 0; basewid < fsg->
n_word; ++basewid)
456 if (0 == strcmp(fsg->
vocab[basewid], baseword))
458 if (basewid == fsg->
n_word) {
459 E_ERROR(
"Base word %s not present in FSG vocabulary!\n", baseword);
462 altwid = fsg_model_word_add(fsg, altword);
466 if (fsg_model_is_filler(fsg, basewid)) {
472 E_DEBUG(2, (
"Adding alternate word transitions (%s,%s) to FSG\n",
478 for (i = 0; i < fsg->
n_state; ++i) {
480 if (fsg->
trans[i].trans == NULL)
488 for (gn = trans; gn; gn = gnode_next(gn)) {
490 if (fl->
wid == basewid) {
495 link->from_state = fl->from_state;
496 link->to_state = fl->to_state;
508 E_DEBUG(2, (
"Added %d alternate word transitions\n", ntrans));
514fsg_model_init(
char const *name,
logmath_t * lmath, float32 lw,
534fsg_model_read(FILE * fp,
logmath_t * lmath, float32 lw)
545 int n_state, n_trans, n_null_trans;
559 n = nextline_str2words(fp, &lineno, &lineptr, &wordptr);
561 E_ERROR(
"%s declaration missing\n", FSG_MODEL_BEGIN_DECL);
565 if ((strcmp(wordptr[0], FSG_MODEL_BEGIN_DECL) == 0)) {
567 E_ERROR(
"Line[%d]: malformed FSG_BEGIN declaration\n",
581 E_WARN(
"FSG name is missing\n");
586 n = nextline_str2words(fp, &lineno, &lineptr, &wordptr);
588 || ((strcmp(wordptr[0], FSG_MODEL_N_DECL) != 0)
589 && (strcmp(wordptr[0], FSG_MODEL_NUM_STATES_DECL) != 0))
590 || (sscanf(wordptr[1],
"%d", &n_state) != 1)
593 (
"Line[%d]: #states declaration line missing or malformed\n",
599 fsg = fsg_model_init(fsgname, lmath, lw, n_state);
604 n = nextline_str2words(fp, &lineno, &lineptr, &wordptr);
606 || ((strcmp(wordptr[0], FSG_MODEL_S_DECL) != 0)
607 && (strcmp(wordptr[0], FSG_MODEL_START_STATE_DECL) != 0))
608 || (sscanf(wordptr[1],
"%d", &(fsg->
start_state)) != 1)
612 (
"Line[%d]: start state declaration line missing or malformed\n",
618 n = nextline_str2words(fp, &lineno, &lineptr, &wordptr);
620 || ((strcmp(wordptr[0], FSG_MODEL_F_DECL) != 0)
621 && (strcmp(wordptr[0], FSG_MODEL_FINAL_STATE_DECL) != 0))
622 || (sscanf(wordptr[1],
"%d", &(fsg->
final_state)) != 1)
626 (
"Line[%d]: final state declaration line missing or malformed\n",
633 n_trans = n_null_trans = 0;
637 n = nextline_str2words(fp, &lineno, &lineptr, &wordptr);
639 E_ERROR(
"Line[%d]: transition or FSG_END statement expected\n",
644 if ((strcmp(wordptr[0], FSG_MODEL_END_DECL) == 0)) {
648 if ((strcmp(wordptr[0], FSG_MODEL_T_DECL) == 0)
649 || (strcmp(wordptr[0], FSG_MODEL_TRANSITION_DECL) == 0)) {
652 if (((n != 4) && (n != 5))
653 || (sscanf(wordptr[1],
"%d", &i) != 1)
654 || (sscanf(wordptr[2],
"%d", &j) != 1)
655 || (i < 0) || (i >= fsg->
n_state)
656 || (j < 0) || (j >= fsg->
n_state)) {
658 (
"Line[%d]: transition spec malformed; Expecting: from-state to-state trans-prob [word]\n",
664 if ((p <= 0.0) || (p > 1.0)) {
666 (
"Line[%d]: transition spec malformed; Expecting float as transition probability\n",
672 E_ERROR(
"Line[%d]: transition or FSG_END statement expected\n",
687 fsg_model_trans_add(fsg, i, j, tprob, wid);
691 if (fsg_model_null_trans_add(fsg, i, j, tprob) == 1) {
699 E_INFO(
"FSG: %d states, %d unique words, %d transitions (%d null)\n",
700 fsg->
n_state, hash_table_inuse(vocab), n_trans, n_null_trans);
704 fsg->
n_word = hash_table_inuse(vocab);
709 char const *word = hash_entry_key(itor->
ent);
711 fsg->
vocab[wid] = (
char *) word;
716 nulls = fsg_model_null_trans_closure(fsg, nulls);
739fsg_model_readfile(
const char *file,
logmath_t * lmath, float32 lw)
744 if ((fp = fopen(file,
"r")) == NULL) {
748 fsg = fsg_model_read(fp, lmath, lw);
768 if (fsg->
trans[i].trans) {
790 for (i = 0; i < fsg->
n_word; ++i)
792 for (i = 0; i < fsg->
n_state; ++i)
793 trans_list_free(fsg, i);
810 fprintf(fp,
"%s %s\n", FSG_MODEL_BEGIN_DECL,
812 fprintf(fp,
"%s %d\n", FSG_MODEL_NUM_STATES_DECL, fsg->
n_state);
813 fprintf(fp,
"%s %d\n", FSG_MODEL_START_STATE_DECL, fsg->
start_state);
814 fprintf(fp,
"%s %d\n", FSG_MODEL_FINAL_STATE_DECL, fsg->
final_state);
816 for (i = 0; i < fsg->
n_state; i++) {
819 for (itor = fsg_model_arcs(fsg, i); itor;
820 itor = fsg_arciter_next(itor)) {
823 fprintf(fp,
"%s %d %d %f %s\n", FSG_MODEL_TRANSITION_DECL,
824 tl->from_state, tl->to_state,
827 (tl->
wid < 0) ?
"" : fsg_model_word_str(fsg, tl->wid));
831 fprintf(fp,
"%s\n", FSG_MODEL_END_DECL);
837fsg_model_writefile(
fsg_model_t * fsg,
char const *file)
843 E_INFO(
"Writing FSG file '%s'\n", file);
845 if ((fp = fopen(file,
"w")) == NULL) {
850 fsg_model_write(fsg, fp);
856fsg_model_write_fsm_trans(
fsg_model_t * fsg,
int i, FILE * fp)
860 for (itor = fsg_model_arcs(fsg, i); itor;
861 itor = fsg_arciter_next(itor)) {
863 fprintf(fp,
"%d %d %s %f\n",
864 tl->from_state, tl->to_state,
865 (tl->
wid < 0) ?
"<eps>" : fsg_model_word_str(fsg, tl->wid),
876 fsg_model_write_fsm_trans(fsg, fsg_model_start_state(fsg), fp);
879 for (i = 0; i < fsg->
n_state; i++) {
880 if (i == fsg_model_start_state(fsg))
882 fsg_model_write_fsm_trans(fsg, i, fp);
886 fprintf(fp,
"%d 0\n", fsg_model_final_state(fsg));
892fsg_model_writefile_fsm(
fsg_model_t * fsg,
char const *file)
898 E_INFO(
"Writing FSM file '%s'\n", file);
900 if ((fp = fopen(file,
"w")) == NULL) {
905 fsg_model_write_fsm(fsg, fp);
911fsg_model_write_symtab(
fsg_model_t * fsg, FILE * file)
915 fprintf(file,
"<eps> 0\n");
916 for (i = 0; i < fsg_model_n_word(fsg); ++i) {
917 fprintf(file,
"%s %d\n", fsg_model_word_str(fsg, i), i + 1);
923fsg_model_writefile_symtab(
fsg_model_t * fsg,
char const *file)
929 E_INFO(
"Writing FSM symbol table '%s'\n", file);
931 if ((fp = fopen(file,
"w")) == NULL) {
932 E_ERROR(
"Failed to open symbol table '%s' for writing", file);
936 fsg_model_write_symtab(fsg, fp);
An implementation of bit vectors.
#define bitvec_free(v)
Free a bit vector.
SPHINXBASE_EXPORT bitvec_t * bitvec_realloc(bitvec_t *vec, size_t old_len, size_t new_len)
Resize a bit vector, clear the remaining bits.
#define bitvec_alloc(n)
Allocate a bit vector, all bits are clear.
#define bitvec_set(v, b)
Set the b-th bit of bit vector v.
Sphinx's memory allocation/deallocation routines.
SPHINXBASE_EXPORT void ckd_free(void *ptr)
Test and free a 1-D array.
#define ckd_calloc(n, sz)
Macros to simplify the use of above functions.
#define ckd_salloc(ptr)
Macro for ckd_salloc
#define ckd_realloc(ptr, sz)
Macro for ckd_realloc
Implementation of logging routines.
#define E_ERROR(...)
Print error message to error log.
#define E_INFO(...)
Print logging information to standard error stream.
#define E_FATAL(...)
Exit with non-zero status after error message.
#define E_ERROR_SYSTEM(...)
Print error text; Call perror("");.
#define E_WARN(...)
Print warning message to error log.
#define E_DEBUG(level, x)
Print debugging information to standard error stream.
SPHINXBASE_EXPORT void glist_free(glist_t g)
Free the given generic list; user-defined data contained within is not automatically freed.
SPHINXBASE_EXPORT glist_t glist_add_ptr(glist_t g, void *ptr)
Create and prepend a new list node, with the given user-defined data, at the HEAD of the given generi...
#define gnode_ptr(g)
Head of a list of gnodes.
Hash table implementation.
SPHINXBASE_EXPORT void hash_table_free(hash_table_t *h)
Free the specified hash table; the caller is responsible for freeing the key strings pointed to by th...
SPHINXBASE_EXPORT hash_iter_t * hash_table_iter_next(hash_iter_t *itor)
Get the next key-value pair in iteration.
#define hash_table_enter_int32(h, k, v)
Add a 32-bit integer value to a hash table.
SPHINXBASE_EXPORT void * hash_table_enter_bkey(hash_table_t *h, const char *key, size_t len, void *val)
Like hash_table_enter, but with an explicitly specified key length, instead of a NULL-terminated,...
SPHINXBASE_EXPORT void * hash_table_replace_bkey(hash_table_t *h, const char *key, size_t len, void *val)
Like hash_table_replace, but with an explicitly specified key length, instead of a NULL-terminated,...
SPHINXBASE_EXPORT void hash_table_iter_free(hash_iter_t *itor)
Delete an unfinished iterator.
SPHINXBASE_EXPORT int32 hash_table_lookup_bkey(hash_table_t *h, const char *key, size_t len, void **val)
Like hash_lookup, but with an explicitly specified key length, instead of a NULL-terminated,...
#define hash_entry_val(e)
Access macros.
SPHINXBASE_EXPORT int32 hash_table_lookup_int32(hash_table_t *h, const char *key, int32 *val)
Look up a 32-bit integer value in a hash table.
SPHINXBASE_EXPORT hash_iter_t * hash_table_iter(hash_table_t *h)
Start iterating over key-value pairs in a hash table.
SPHINXBASE_EXPORT hash_table_t * hash_table_new(int32 size, int32 casearg)
Allocate a new hash table for a given expected size.
SPHINXBASE_EXPORT void listelem_alloc_free(listelem_alloc_t *le)
Finalize and release all memory associated with a list element allocator.
SPHINXBASE_EXPORT listelem_alloc_t * listelem_alloc_init(size_t elemsize)
Initialize and return a list element allocator.
#define listelem_malloc(le)
Allocate a list element and return pointer to it.
SPHINXBASE_EXPORT float64 logmath_log_to_ln(logmath_t *lmath, int logb_p)
Convert integer log in base B to natural log (in floating point).
SPHINXBASE_EXPORT float64 logmath_exp(logmath_t *lmath, int logb_p)
Convert integer log in base B to linear floating point.
SPHINXBASE_EXPORT int logmath_log(logmath_t *lmath, float64 p)
Convert linear floating point number to integer log in base B.
file IO related operations.
SPHINXBASE_EXPORT char * fread_line(FILE *stream, size_t *out_len)
Read a line of arbitrary length from a file and return it as a newly allocated string.
Basic type definitions used in Sphinx.
Miscellaneous useful string functions.
SPHINXBASE_EXPORT int32 str2words(char *line, char **wptr, int32 n_wptr)
Convert a line to an array of "words", based on whitespace separators.
SPHINXBASE_EXPORT double atof_c(char const *str)
Locale independent version of atof().
Implementation of arc iterator.
int32 logs2prob
log(transition probability)*lw
int32 wid
Word-ID; <0 if epsilon or null transition.
Word level FSG definition.
int32 n_word_alloc
Number of words allocated in vocab.
int32 start_state
Must be in the range [0..n_state-1].
char ** vocab
Vocabulary for this FSG.
int32 n_state
number of states in FSG
int32 n_word
Number of unique words in this FSG.
logmath_t * lmath
Pointer to log math computation object.
char * name
A unique string identifier for this FSG.
bitvec_t * silwords
Indicates which words are silence/fillers.
listelem_alloc_t * link_alloc
Allocator for FSG links.
trans_list_t * trans
Transitions out of each state, if any.
int32 final_state
Must be in the range [0..n_state-1].
bitvec_t * altwords
Indicates which words are pronunciation alternates.
float32 lw
Language weight that's been applied to transition logprobs.
int refcount
Reference count.
A node in a generic list.
hash_entry_t * ent
Current entry in that table.
Adjacency list (opaque) for a state in an FSG.