46 #include "sphinxbase/fsg_model.h" 68 #define FSG_MODEL_BEGIN_DECL "FSG_BEGIN" 69 #define FSG_MODEL_END_DECL "FSG_END" 70 #define FSG_MODEL_N_DECL "N" 71 #define FSG_MODEL_NUM_STATES_DECL "NUM_STATES" 72 #define FSG_MODEL_S_DECL "S" 73 #define FSG_MODEL_START_STATE_DECL "START_STATE" 74 #define FSG_MODEL_F_DECL "F" 75 #define FSG_MODEL_FINAL_STATE_DECL "FINAL_STATE" 76 #define FSG_MODEL_T_DECL "T" 77 #define FSG_MODEL_TRANSITION_DECL "TRANSITION" 78 #define FSG_MODEL_COMMENT_CHAR '#' 82 nextline_str2words(FILE * fp, int32 * lineno,
83 char **lineptr,
char ***wordptr)
95 if ((*lineptr)[0] == FSG_MODEL_COMMENT_CHAR)
103 if (*wordptr == NULL)
106 *wordptr =
ckd_realloc(*wordptr, n *
sizeof(**wordptr));
113 int32 from, int32 to, int32 logp, int32 wid)
119 if (fsg->
trans[from].trans == NULL)
123 for (gn = gl = fsg_model_trans(fsg, from, to); gn; gn = gnode_next(gn)) {
125 if (link->
wid == wid) {
134 link->from_state = from;
142 (
char const *) &link->to_state,
143 sizeof(link->to_state), gl);
147 fsg_model_tag_trans_add(
fsg_model_t * fsg, int32 from, int32 to,
148 int32 logp, int32 wid)
154 E_FATAL(
"Null transition prob must be <= 1.0 (state %d -> %d)\n",
162 if (fsg->
trans[from].null_trans == NULL)
166 link = fsg_model_null_trans(fsg, from, to);
178 link->from_state = from;
185 (
char const *) &link->to_state,
186 sizeof(link->to_state), link);
187 assert(link == link2);
193 fsg_model_null_trans_add(
fsg_model_t * fsg, int32 from, int32 to,
196 return fsg_model_tag_trans_add(fsg, from, to, logp, -1);
207 E_INFO(
"Computing transitive closure for null transitions\n");
215 for (i = 0; i < fsg->
n_state; ++i) {
218 if (null_trans == NULL)
235 for (gn1 = nulls; gn1; gn1 = gnode_next(gn1)) {
239 assert(tl1->
wid < 0);
241 if (fsg->
trans[tl1->to_state].null_trans == NULL)
250 k = fsg_model_null_trans_add(fsg,
260 (fsg, tl1->from_state,
269 E_INFO(
"%d null transitions added\n", n);
275 fsg_model_trans(
fsg_model_t * fsg, int32 i, int32 j)
279 if (fsg->
trans[i].trans == NULL)
282 sizeof(j), &val) < 0)
288 fsg_model_null_trans(
fsg_model_t * fsg, int32 i, int32 j)
292 if (fsg->
trans[i].null_trans == NULL)
295 sizeof(j), &val) < 0)
305 if (fsg->
trans[i].trans == NULL && fsg->
trans[i].null_trans == NULL)
308 if (fsg->
trans[i].null_trans)
310 if (fsg->
trans[i].trans)
312 if (itor->itor != NULL)
323 else if (itor->null_itor)
334 itor->gn = gnode_next(itor->gn);
336 if (itor->gn == NULL) {
338 if (itor->itor != NULL)
340 else if (itor->null_itor == NULL)
345 if (itor->null_itor == NULL)
348 if (itor->null_itor == NULL)
353 fsg_arciter_free(itor);
369 fsg_model_word_id(
fsg_model_t * fsg,
char const *word)
374 for (wid = 0; wid < fsg->
n_word; ++wid) {
375 if (0 == strcmp(fsg->
vocab[wid], word))
385 fsg_model_word_add(
fsg_model_t * fsg,
char const *word)
390 wid = fsg_model_word_id(fsg, word);
399 sizeof(*fsg->
vocab));
416 fsg_model_add_silence(
fsg_model_t * fsg,
char const *silword,
417 int state, float32 silprob)
420 int n_trans, silwid, src;
422 E_INFO(
"Adding silence transitions for %s to FSG\n", silword);
424 silwid = fsg_model_word_add(fsg, silword);
432 for (src = 0; src < fsg->
n_state; src++) {
433 fsg_model_trans_add(fsg, src, src, logsilp, silwid);
438 fsg_model_trans_add(fsg, state, state, logsilp, silwid);
442 E_INFO(
"Added %d silence word transitions\n", n_trans);
447 fsg_model_add_alt(
fsg_model_t * fsg,
char const *baseword,
450 int i, basewid, altwid;
454 for (basewid = 0; basewid < fsg->
n_word; ++basewid)
455 if (0 == strcmp(fsg->
vocab[basewid], baseword))
457 if (basewid == fsg->
n_word) {
458 E_ERROR(
"Base word %s not present in FSG vocabulary!\n", baseword);
461 altwid = fsg_model_word_add(fsg, altword);
465 if (fsg_model_is_filler(fsg, basewid)) {
471 E_DEBUG(2, (
"Adding alternate word transitions (%s,%s) to FSG\n",
477 for (i = 0; i < fsg->
n_state; ++i) {
479 if (fsg->
trans[i].trans == NULL)
487 for (gn = trans; gn; gn = gnode_next(gn)) {
489 if (fl->
wid == basewid) {
494 link->from_state = fl->from_state;
495 link->to_state = fl->to_state;
507 E_DEBUG(2, (
"Added %d alternate word transitions\n", ntrans));
513 fsg_model_init(
char const *name,
logmath_t * lmath, float32 lw,
533 fsg_model_read(FILE * fp,
logmath_t * lmath, float32 lw)
544 int n_state, n_trans, n_null_trans;
558 n = nextline_str2words(fp, &lineno, &lineptr, &wordptr);
560 E_ERROR(
"%s declaration missing\n", FSG_MODEL_BEGIN_DECL);
564 if ((strcmp(wordptr[0], FSG_MODEL_BEGIN_DECL) == 0)) {
566 E_ERROR(
"Line[%d]: malformed FSG_BEGIN declaration\n",
580 E_WARN(
"FSG name is missing\n");
585 n = nextline_str2words(fp, &lineno, &lineptr, &wordptr);
587 || ((strcmp(wordptr[0], FSG_MODEL_N_DECL) != 0)
588 && (strcmp(wordptr[0], FSG_MODEL_NUM_STATES_DECL) != 0))
589 || (sscanf(wordptr[1],
"%d", &n_state) != 1)
592 (
"Line[%d]: #states declaration line missing or malformed\n",
598 fsg = fsg_model_init(fsgname, lmath, lw, n_state);
603 n = nextline_str2words(fp, &lineno, &lineptr, &wordptr);
605 || ((strcmp(wordptr[0], FSG_MODEL_S_DECL) != 0)
606 && (strcmp(wordptr[0], FSG_MODEL_START_STATE_DECL) != 0))
607 || (sscanf(wordptr[1],
"%d", &(fsg->
start_state)) != 1)
611 (
"Line[%d]: start state declaration line missing or malformed\n",
617 n = nextline_str2words(fp, &lineno, &lineptr, &wordptr);
619 || ((strcmp(wordptr[0], FSG_MODEL_F_DECL) != 0)
620 && (strcmp(wordptr[0], FSG_MODEL_FINAL_STATE_DECL) != 0))
621 || (sscanf(wordptr[1],
"%d", &(fsg->
final_state)) != 1)
625 (
"Line[%d]: final state declaration line missing or malformed\n",
632 n_trans = n_null_trans = 0;
636 n = nextline_str2words(fp, &lineno, &lineptr, &wordptr);
638 E_ERROR(
"Line[%d]: transition or FSG_END statement expected\n",
643 if ((strcmp(wordptr[0], FSG_MODEL_END_DECL) == 0)) {
647 if ((strcmp(wordptr[0], FSG_MODEL_T_DECL) == 0)
648 || (strcmp(wordptr[0], FSG_MODEL_TRANSITION_DECL) == 0)) {
651 if (((n != 4) && (n != 5))
652 || (sscanf(wordptr[1],
"%d", &i) != 1)
653 || (sscanf(wordptr[2],
"%d", &j) != 1)
654 || (i < 0) || (i >= fsg->
n_state)
655 || (j < 0) || (j >= fsg->
n_state)) {
657 (
"Line[%d]: transition spec malformed; Expecting: from-state to-state trans-prob [word]\n",
663 if ((p <= 0.0) || (p > 1.0)) {
665 (
"Line[%d]: transition spec malformed; Expecting float as transition probability\n",
671 E_ERROR(
"Line[%d]: transition or FSG_END statement expected\n",
686 fsg_model_trans_add(fsg, i, j, tprob, wid);
690 if (fsg_model_null_trans_add(fsg, i, j, tprob) == 1) {
698 E_INFO(
"FSG: %d states, %d unique words, %d transitions (%d null)\n",
699 fsg->
n_state, hash_table_inuse(vocab), n_trans, n_null_trans);
703 fsg->
n_word = hash_table_inuse(vocab);
708 char const *word = hash_entry_key(itor->
ent);
710 fsg->
vocab[wid] = (
char *) word;
715 nulls = fsg_model_null_trans_closure(fsg, nulls);
738 fsg_model_readfile(
const char *file,
logmath_t * lmath, float32 lw)
743 if ((fp = fopen(file,
"r")) == NULL) {
747 fsg = fsg_model_read(fp, lmath, lw);
767 if (fsg->
trans[i].trans) {
789 for (i = 0; i < fsg->
n_word; ++i)
791 for (i = 0; i < fsg->
n_state; ++i)
792 trans_list_free(fsg, i);
809 fprintf(fp,
"%s %s\n", FSG_MODEL_BEGIN_DECL,
811 fprintf(fp,
"%s %d\n", FSG_MODEL_NUM_STATES_DECL, fsg->
n_state);
812 fprintf(fp,
"%s %d\n", FSG_MODEL_START_STATE_DECL, fsg->
start_state);
813 fprintf(fp,
"%s %d\n", FSG_MODEL_FINAL_STATE_DECL, fsg->
final_state);
815 for (i = 0; i < fsg->
n_state; i++) {
818 for (itor = fsg_model_arcs(fsg, i); itor;
819 itor = fsg_arciter_next(itor)) {
822 fprintf(fp,
"%s %d %d %f %s\n", FSG_MODEL_TRANSITION_DECL,
823 tl->from_state, tl->to_state,
826 (tl->
wid < 0) ?
"" : fsg_model_word_str(fsg, tl->
wid));
830 fprintf(fp,
"%s\n", FSG_MODEL_END_DECL);
836 fsg_model_writefile(
fsg_model_t * fsg,
char const *file)
842 E_INFO(
"Writing FSG file '%s'\n", file);
844 if ((fp = fopen(file,
"w")) == NULL) {
849 fsg_model_write(fsg, fp);
855 fsg_model_write_fsm_trans(
fsg_model_t * fsg,
int i, FILE * fp)
859 for (itor = fsg_model_arcs(fsg, i); itor;
860 itor = fsg_arciter_next(itor)) {
862 fprintf(fp,
"%d %d %s %f\n",
863 tl->from_state, tl->to_state,
864 (tl->
wid < 0) ?
"<eps>" : fsg_model_word_str(fsg, tl->
wid),
875 fsg_model_write_fsm_trans(fsg, fsg_model_start_state(fsg), fp);
878 for (i = 0; i < fsg->
n_state; i++) {
879 if (i == fsg_model_start_state(fsg))
881 fsg_model_write_fsm_trans(fsg, i, fp);
885 fprintf(fp,
"%d 0\n", fsg_model_final_state(fsg));
891 fsg_model_writefile_fsm(
fsg_model_t * fsg,
char const *file)
897 E_INFO(
"Writing FSM file '%s'\n", file);
899 if ((fp = fopen(file,
"w")) == NULL) {
904 fsg_model_write_fsm(fsg, fp);
910 fsg_model_write_symtab(
fsg_model_t * fsg, FILE * file)
914 fprintf(file,
"<eps> 0\n");
915 for (i = 0; i < fsg_model_n_word(fsg); ++i) {
916 fprintf(file,
"%s %d\n", fsg_model_word_str(fsg, i), i + 1);
922 fsg_model_writefile_symtab(
fsg_model_t * fsg,
char const *file)
928 E_INFO(
"Writing FSM symbol table '%s'\n", file);
930 if ((fp = fopen(file,
"w")) == NULL) {
931 E_ERROR(
"Failed to open symbol table '%s' for writing", file);
935 fsg_model_write_symtab(fsg, fp);
#define E_ERROR_SYSTEM(...)
Print error text; Call perror("");.
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.
int32 start_state
Must be in the range [0..n_state-1].
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, C-style key string.
Miscellaneous useful string functions.
#define E_INFO(...)
Print logging information to standard error stream.
int refcount
Reference count.
hash_entry_t * ent
Current entry in that table.
int32 final_state
Must be in the range [0..n_state-1].
int32 n_word_alloc
Number of words allocated in vocab.
#define ckd_calloc(n, sz)
Macros to simplify the use of above functions.
#define E_ERROR(...)
Print error message to error log.
#define hash_table_enter_int32(h, k, v)
Add a 32-bit integer value to a hash table.
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, C-style key string.
float32 lw
Language weight that's been applied to transition logprobs.
#define E_DEBUG(level, x)
Print debugging information to standard error stream.
listelem_alloc_t * link_alloc
Allocator for FSG links.
#define listelem_malloc(le)
Allocate a list element and return pointer to it.
Sphinx's memory allocation/deallocation routines.
SPHINXBASE_EXPORT void hash_table_iter_free(hash_iter_t *itor)
Delete an unfinished iterator.
int32 n_word
Number of unique words in this FSG.
int32 logs2prob
log(transition probability)*lw
SPHINXBASE_EXPORT int logmath_log(logmath_t *lmath, float64 p)
Convert linear floating point number to integer log in base B.
A node in a generic list.
SPHINXBASE_EXPORT hash_iter_t * hash_table_iter(hash_table_t *h)
Start iterating over key-value pairs in a hash table.
#define ckd_salloc(ptr)
Macro for ckd_salloc
#define hash_entry_val(e)
Access macros.
Basic type definitions used in Sphinx.
SPHINXBASE_EXPORT hash_table_t * hash_table_new(int32 size, int32 casearg)
Allocate a new hash table for a given expected size.
Adjacency list (opaque) for a state in an FSG.
SPHINXBASE_EXPORT void ckd_free(void *ptr)
Test and free a 1-D array.
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...
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 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 double atof_c(char const *str)
Locale independent version of atof().
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.
Implementation of arc iterator.
SPHINXBASE_EXPORT void glist_free(glist_t g)
Free the given generic list; user-defined data contained within is not automatically freed...
#define gnode_ptr(g)
Head of a list of gnodes.
Implementation of logging routines.
#define E_WARN(...)
Print warning message to error log.
int32 n_state
number of states in FSG
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 hash_iter_t * hash_table_iter_next(hash_iter_t *itor)
Get the next key-value pair in iteration.
SPHINXBASE_EXPORT listelem_alloc_t * listelem_alloc_init(size_t elemsize)
Initialize and return a list element allocator.
bitvec_t * altwords
Indicates which words are pronunciation alternates.
trans_list_t * trans
Transitions out of each state, if any.
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.
int32 wid
Word-ID; <0 if epsilon or null transition.
Hash table implementation.
#define bitvec_set(v, b)
Set the b-th bit of bit vector v.
#define E_FATAL(...)
Exit with non-zero status after error message.
Word level FSG definition.
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...
bitvec_t * silwords
Indicates which words are silence/fillers.
SPHINXBASE_EXPORT float64 logmath_exp(logmath_t *lmath, int logb_p)
Convert integer log in base B to linear floating point.
#define ckd_realloc(ptr, sz)
Macro for ckd_realloc
logmath_t * lmath
Pointer to log math computation object.
#define bitvec_alloc(n)
Allocate a bit vector, all bits are clear.
file IO related operations.
#define bitvec_free(v)
Free a bit vector.
char * name
A unique string identifier for this FSG.
SPHINXBASE_EXPORT void listelem_alloc_free(listelem_alloc_t *le)
Finalize and release all memory associated with a list element allocator.
char ** vocab
Vocabulary for this FSG.