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 '#'
83 nextline_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);
148 fsg_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);
194 fsg_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);
276 fsg_model_trans(
fsg_model_t * fsg, int32 i, int32 j)
280 if (fsg->
trans[i].trans == NULL)
283 sizeof(j), &val) < 0)
289 fsg_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);
370 fsg_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))
386 fsg_model_word_add(
fsg_model_t * fsg,
char const *word)
391 wid = fsg_model_word_id(fsg, word);
400 sizeof(*fsg->
vocab));
417 fsg_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);
448 fsg_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));
514 fsg_model_init(
char const *name,
logmath_t * lmath, float32 lw,
534 fsg_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);
739 fsg_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);
837 fsg_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);
856 fsg_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));
892 fsg_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);
911 fsg_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);
923 fsg_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);