\doxysection{fsg\+\_\+model.\+c}
\label{fsg__model_8c_source}\index{src/libsphinxbase/lm/fsg\_model.c@{src/libsphinxbase/lm/fsg\_model.c}}

\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ \textcolor{comment}{/*\ -\/*-\/\ c-\/basic-\/offset:\ 4;\ indent-\/tabs-\/mode:\ nil\ -\/*-\/\ */}}
\DoxyCodeLine{00002\ \textcolor{comment}{/*\ ====================================================================}}
\DoxyCodeLine{00003\ \textcolor{comment}{\ *\ Copyright\ (c)\ 1999-\/2004\ Carnegie\ Mellon\ University.\ \ All\ rights}}
\DoxyCodeLine{00004\ \textcolor{comment}{\ *\ reserved.}}
\DoxyCodeLine{00005\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00006\ \textcolor{comment}{\ *\ Redistribution\ and\ use\ in\ source\ and\ binary\ forms,\ with\ or\ without}}
\DoxyCodeLine{00007\ \textcolor{comment}{\ *\ modification,\ are\ permitted\ provided\ that\ the\ following\ conditions}}
\DoxyCodeLine{00008\ \textcolor{comment}{\ *\ are\ met:}}
\DoxyCodeLine{00009\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00010\ \textcolor{comment}{\ *\ 1.\ Redistributions\ of\ source\ code\ must\ retain\ the\ above\ copyright}}
\DoxyCodeLine{00011\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer.\ }}
\DoxyCodeLine{00012\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00013\ \textcolor{comment}{\ *\ 2.\ Redistributions\ in\ binary\ form\ must\ reproduce\ the\ above\ copyright}}
\DoxyCodeLine{00014\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer\ in}}
\DoxyCodeLine{00015\ \textcolor{comment}{\ *\ \ \ \ the\ documentation\ and/or\ other\ materials\ provided\ with\ the}}
\DoxyCodeLine{00016\ \textcolor{comment}{\ *\ \ \ \ distribution.}}
\DoxyCodeLine{00017\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00018\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00019\ \textcolor{comment}{\ *\ THIS\ SOFTWARE\ IS\ PROVIDED\ BY\ CARNEGIE\ MELLON\ UNIVERSITY\ \`{}\`{}AS\ IS''\ AND\ }}
\DoxyCodeLine{00020\ \textcolor{comment}{\ *\ ANY\ EXPRESSED\ OR\ IMPLIED\ WARRANTIES,\ INCLUDING,\ BUT\ NOT\ LIMITED\ TO,\ }}
\DoxyCodeLine{00021\ \textcolor{comment}{\ *\ THE\ IMPLIED\ WARRANTIES\ OF\ MERCHANTABILITY\ AND\ FITNESS\ FOR\ A\ PARTICULAR}}
\DoxyCodeLine{00022\ \textcolor{comment}{\ *\ PURPOSE\ ARE\ DISCLAIMED.\ \ IN\ NO\ EVENT\ SHALL\ CARNEGIE\ MELLON\ UNIVERSITY}}
\DoxyCodeLine{00023\ \textcolor{comment}{\ *\ NOR\ ITS\ EMPLOYEES\ BE\ LIABLE\ FOR\ ANY\ DIRECT,\ INDIRECT,\ INCIDENTAL,}}
\DoxyCodeLine{00024\ \textcolor{comment}{\ *\ SPECIAL,\ EXEMPLARY,\ OR\ CONSEQUENTIAL\ DAMAGES\ (INCLUDING,\ BUT\ NOT\ }}
\DoxyCodeLine{00025\ \textcolor{comment}{\ *\ LIMITED\ TO,\ PROCUREMENT\ OF\ SUBSTITUTE\ GOODS\ OR\ SERVICES;\ LOSS\ OF\ USE,\ }}
\DoxyCodeLine{00026\ \textcolor{comment}{\ *\ DATA,\ OR\ PROFITS;\ OR\ BUSINESS\ INTERRUPTION)\ HOWEVER\ CAUSED\ AND\ ON\ ANY\ }}
\DoxyCodeLine{00027\ \textcolor{comment}{\ *\ THEORY\ OF\ LIABILITY,\ WHETHER\ IN\ CONTRACT,\ STRICT\ LIABILITY,\ OR\ TORT\ }}
\DoxyCodeLine{00028\ \textcolor{comment}{\ *\ (INCLUDING\ NEGLIGENCE\ OR\ OTHERWISE)\ ARISING\ IN\ ANY\ WAY\ OUT\ OF\ THE\ USE\ }}
\DoxyCodeLine{00029\ \textcolor{comment}{\ *\ OF\ THIS\ SOFTWARE,\ EVEN\ IF\ ADVISED\ OF\ THE\ POSSIBILITY\ OF\ SUCH\ DAMAGE.}}
\DoxyCodeLine{00030\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00031\ \textcolor{comment}{\ *\ ====================================================================}}
\DoxyCodeLine{00032\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00033\ \textcolor{comment}{\ */}}
\DoxyCodeLine{00034\ }
\DoxyCodeLine{00035\ \textcolor{comment}{/*\ System\ headers.\ */}}
\DoxyCodeLine{00036\ \textcolor{preprocessor}{\#ifdef\ \_WIN32\_WCE}}
\DoxyCodeLine{00037\ \textcolor{comment}{/*MC\ in\ a\ debug\ build\ it's\ implicitly\ included\ by\ assert.h}}
\DoxyCodeLine{00038\ \textcolor{comment}{\ \ \ \ \ but\ you\ need\ this\ in\ a\ release\ build\ */}}
\DoxyCodeLine{00039\ \textcolor{preprocessor}{\#include\ <windows.h>}}
\DoxyCodeLine{00040\ \textcolor{preprocessor}{\#else}}
\DoxyCodeLine{00041\ \textcolor{preprocessor}{\#include\ <time.h>}}
\DoxyCodeLine{00042\ \textcolor{preprocessor}{\#endif\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ }\textcolor{comment}{/*\ \_WIN32\_WCE\ */}\textcolor{preprocessor}{}}
\DoxyCodeLine{00043\ \textcolor{preprocessor}{\#include\ <stdio.h>}}
\DoxyCodeLine{00044\ \textcolor{preprocessor}{\#include\ <string.h>}}
\DoxyCodeLine{00045\ \textcolor{preprocessor}{\#include\ <assert.h>}}
\DoxyCodeLine{00046\ }
\DoxyCodeLine{00047\ \textcolor{comment}{/*\ SphinxBase\ headers.\ */}}
\DoxyCodeLine{00048\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/err.h"{}}}
\DoxyCodeLine{00049\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/pio.h"{}}}
\DoxyCodeLine{00050\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/ckd\_alloc.h"{}}}
\DoxyCodeLine{00051\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/prim\_type.h"{}}}
\DoxyCodeLine{00052\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/strfuncs.h"{}}}
\DoxyCodeLine{00053\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/hash\_table.h"{}}}
\DoxyCodeLine{00054\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/fsg\_model.h"{}}}
\DoxyCodeLine{00055\ }
\DoxyCodeLine{00063\ \textcolor{keyword}{struct\ }trans\_list\_s\ \{}
\DoxyCodeLine{00064\ \ \ \ \ hash\_table\_t\ *null\_trans;\ \ \ \textcolor{comment}{/*\ Null\ transitions\ keyed\ by\ state.\ */}}
\DoxyCodeLine{00065\ \ \ \ \ hash\_table\_t\ *trans;\ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Lists\ of\ non-\/null\ transitions\ keyed\ by\ state.\ */}}
\DoxyCodeLine{00066\ \};}
\DoxyCodeLine{00067\ }
\DoxyCodeLine{00071\ \textcolor{keyword}{struct\ }fsg\_arciter\_s\ \{}
\DoxyCodeLine{00072\ \ \ \ \ hash\_iter\_t\ *itor,\ *null\_itor;}
\DoxyCodeLine{00073\ \ \ \ \ gnode\_t\ *gn;}
\DoxyCodeLine{00074\ \};}
\DoxyCodeLine{00075\ }
\DoxyCodeLine{00076\ \textcolor{preprocessor}{\#define\ FSG\_MODEL\_BEGIN\_DECL\ \ \ \ \ \ \ \ \ \ \ \ "{}FSG\_BEGIN"{}}}
\DoxyCodeLine{00077\ \textcolor{preprocessor}{\#define\ FSG\_MODEL\_END\_DECL\ \ \ \ \ \ \ \ \ \ \ \ \ \ "{}FSG\_END"{}}}
\DoxyCodeLine{00078\ \textcolor{preprocessor}{\#define\ FSG\_MODEL\_N\_DECL\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ "{}N"{}}}
\DoxyCodeLine{00079\ \textcolor{preprocessor}{\#define\ FSG\_MODEL\_NUM\_STATES\_DECL\ \ \ \ \ \ \ "{}NUM\_STATES"{}}}
\DoxyCodeLine{00080\ \textcolor{preprocessor}{\#define\ FSG\_MODEL\_S\_DECL\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ "{}S"{}}}
\DoxyCodeLine{00081\ \textcolor{preprocessor}{\#define\ FSG\_MODEL\_START\_STATE\_DECL\ \ \ \ \ \ "{}START\_STATE"{}}}
\DoxyCodeLine{00082\ \textcolor{preprocessor}{\#define\ FSG\_MODEL\_F\_DECL\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ "{}F"{}}}
\DoxyCodeLine{00083\ \textcolor{preprocessor}{\#define\ FSG\_MODEL\_FINAL\_STATE\_DECL\ \ \ \ \ \ "{}FINAL\_STATE"{}}}
\DoxyCodeLine{00084\ \textcolor{preprocessor}{\#define\ FSG\_MODEL\_T\_DECL\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ "{}T"{}}}
\DoxyCodeLine{00085\ \textcolor{preprocessor}{\#define\ FSG\_MODEL\_TRANSITION\_DECL\ \ \ \ \ \ \ "{}TRANSITION"{}}}
\DoxyCodeLine{00086\ \textcolor{preprocessor}{\#define\ FSG\_MODEL\_COMMENT\_CHAR\ \ \ \ \ \ \ \ \ \ '\#'}}
\DoxyCodeLine{00087\ }
\DoxyCodeLine{00088\ }
\DoxyCodeLine{00089\ \textcolor{keyword}{static}\ int32}
\DoxyCodeLine{00090\ nextline\_str2words(FILE\ *\ fp,\ int32\ *\ lineno,}
\DoxyCodeLine{00091\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ **lineptr,\ \textcolor{keywordtype}{char}\ ***wordptr)}
\DoxyCodeLine{00092\ \{}
\DoxyCodeLine{00093\ \ \ \ \ \textcolor{keywordflow}{for}\ (;;)\ \{}
\DoxyCodeLine{00094\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{size\_t}\ len;}
\DoxyCodeLine{00095\ \ \ \ \ \ \ \ \ int32\ n;}
\DoxyCodeLine{00096\ }
\DoxyCodeLine{00097\ \ \ \ \ \ \ \ \ ckd\_free(*lineptr);}
\DoxyCodeLine{00098\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((*lineptr\ =\ fread\_line(fp,\ \&len))\ ==\ NULL)}
\DoxyCodeLine{00099\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00100\ }
\DoxyCodeLine{00101\ \ \ \ \ \ \ \ \ (*lineno)++;}
\DoxyCodeLine{00102\ }
\DoxyCodeLine{00103\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((*lineptr)[0]\ ==\ FSG\_MODEL\_COMMENT\_CHAR)}
\DoxyCodeLine{00104\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{continue};\ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Skip\ comment\ lines\ */}}
\DoxyCodeLine{00105\ }
\DoxyCodeLine{00106\ \ \ \ \ \ \ \ \ n\ =\ str2words(*lineptr,\ NULL,\ 0);}
\DoxyCodeLine{00107\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (n\ ==\ 0)}
\DoxyCodeLine{00108\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{continue};\ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Skip\ blank\ lines\ */}}
\DoxyCodeLine{00109\ }
\DoxyCodeLine{00110\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Abuse\ of\ realloc(),\ but\ this\ doesn't\ have\ to\ be\ fast.\ */}}
\DoxyCodeLine{00111\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (*wordptr\ ==\ NULL)}
\DoxyCodeLine{00112\ \ \ \ \ \ \ \ \ \ \ \ \ *wordptr\ =\ ckd\_calloc(n,\ \textcolor{keyword}{sizeof}(**wordptr));}
\DoxyCodeLine{00113\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{00114\ \ \ \ \ \ \ \ \ \ \ \ \ *wordptr\ =\ ckd\_realloc(*wordptr,\ n\ *\ \textcolor{keyword}{sizeof}(**wordptr));}
\DoxyCodeLine{00115\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ str2words(*lineptr,\ *wordptr,\ n);}
\DoxyCodeLine{00116\ \ \ \ \ \}}
\DoxyCodeLine{00117\ \}}
\DoxyCodeLine{00118\ }
\DoxyCodeLine{00119\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00120\ fsg\_model\_trans\_add(fsg\_model\_t\ *\ fsg,}
\DoxyCodeLine{00121\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ int32\ from,\ int32\ to,\ int32\ logp,\ int32\ wid)}
\DoxyCodeLine{00122\ \{}
\DoxyCodeLine{00123\ \ \ \ \ fsg\_link\_t\ *link;}
\DoxyCodeLine{00124\ \ \ \ \ glist\_t\ gl;}
\DoxyCodeLine{00125\ \ \ \ \ gnode\_t\ *gn;}
\DoxyCodeLine{00126\ }
\DoxyCodeLine{00127\ \ \ \ \ \textcolor{keywordflow}{if}\ (fsg-\/>trans[from].trans\ ==\ NULL)}
\DoxyCodeLine{00128\ \ \ \ \ \ \ \ \ fsg-\/>trans[from].trans\ =\ hash\_table\_new(5,\ HASH\_CASE\_YES);}
\DoxyCodeLine{00129\ }
\DoxyCodeLine{00130\ \ \ \ \ \textcolor{comment}{/*\ Check\ for\ duplicate\ link\ (i.e.,\ link\ already\ exists\ with\ label=wid)\ */}}
\DoxyCodeLine{00131\ \ \ \ \ \textcolor{keywordflow}{for}\ (gn\ =\ gl\ =\ fsg\_model\_trans(fsg,\ from,\ to);\ gn;\ gn\ =\ gnode\_next(gn))\ \{}
\DoxyCodeLine{00132\ \ \ \ \ \ \ \ \ link\ =\ (fsg\_link\_t\ *)\ gnode\_ptr(gn);}
\DoxyCodeLine{00133\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (link-\/>wid\ ==\ wid)\ \{}
\DoxyCodeLine{00134\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (link-\/>logs2prob\ <\ logp)}
\DoxyCodeLine{00135\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ link-\/>logs2prob\ =\ logp;}
\DoxyCodeLine{00136\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return};}
\DoxyCodeLine{00137\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00138\ \ \ \ \ \}}
\DoxyCodeLine{00139\ }
\DoxyCodeLine{00140\ \ \ \ \ \textcolor{comment}{/*\ Create\ transition\ object\ */}}
\DoxyCodeLine{00141\ \ \ \ \ link\ =\ listelem\_malloc(fsg-\/>link\_alloc);}
\DoxyCodeLine{00142\ \ \ \ \ link-\/>from\_state\ =\ from;}
\DoxyCodeLine{00143\ \ \ \ \ link-\/>to\_state\ =\ to;}
\DoxyCodeLine{00144\ \ \ \ \ link-\/>logs2prob\ =\ logp;}
\DoxyCodeLine{00145\ \ \ \ \ link-\/>wid\ =\ wid;}
\DoxyCodeLine{00146\ }
\DoxyCodeLine{00147\ \ \ \ \ \textcolor{comment}{/*\ Add\ it\ to\ the\ list\ of\ transitions\ and\ update\ the\ hash\ table\ */}}
\DoxyCodeLine{00148\ \ \ \ \ gl\ =\ glist\_add\_ptr(gl,\ (\textcolor{keywordtype}{void}\ *)\ link);}
\DoxyCodeLine{00149\ \ \ \ \ hash\_table\_replace\_bkey(fsg-\/>trans[from].trans,}
\DoxyCodeLine{00150\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (\textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *)\ \&link-\/>to\_state,}
\DoxyCodeLine{00151\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{sizeof}(link-\/>to\_state),\ gl);}
\DoxyCodeLine{00152\ \}}
\DoxyCodeLine{00153\ }
\DoxyCodeLine{00154\ int32}
\DoxyCodeLine{00155\ fsg\_model\_tag\_trans\_add(fsg\_model\_t\ *\ fsg,\ int32\ from,\ int32\ to,}
\DoxyCodeLine{00156\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ int32\ logp,\ int32\ wid)}
\DoxyCodeLine{00157\ \{}
\DoxyCodeLine{00158\ \ \ \ \ fsg\_link\_t\ *link,\ *link2;}
\DoxyCodeLine{00159\ }
\DoxyCodeLine{00160\ \ \ \ \ \textcolor{comment}{/*\ Check\ for\ transition\ probability\ */}}
\DoxyCodeLine{00161\ \ \ \ \ \textcolor{keywordflow}{if}\ (logp\ >\ 0)\ \{}
\DoxyCodeLine{00162\ \ \ \ \ \ \ \ \ E\_FATAL(\textcolor{stringliteral}{"{}Null\ transition\ prob\ must\ be\ <=\ 1.0\ (state\ \%d\ -\/>\ \%d)\(\backslash\)n"{}},}
\DoxyCodeLine{00163\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ from,\ to);}
\DoxyCodeLine{00164\ \ \ \ \ \}}
\DoxyCodeLine{00165\ }
\DoxyCodeLine{00166\ \ \ \ \ \textcolor{comment}{/*\ Self-\/loop\ null\ transitions\ (with\ prob\ <=\ 1.0)\ are\ redundant\ */}}
\DoxyCodeLine{00167\ \ \ \ \ \textcolor{keywordflow}{if}\ (from\ ==\ to)}
\DoxyCodeLine{00168\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00169\ }
\DoxyCodeLine{00170\ \ \ \ \ \textcolor{keywordflow}{if}\ (fsg-\/>trans[from].null\_trans\ ==\ NULL)}
\DoxyCodeLine{00171\ \ \ \ \ \ \ \ \ fsg-\/>trans[from].null\_trans\ =\ hash\_table\_new(5,\ HASH\_CASE\_YES);}
\DoxyCodeLine{00172\ }
\DoxyCodeLine{00173\ \ \ \ \ \textcolor{comment}{/*\ Check\ for\ a\ duplicate\ link;\ if\ found,\ keep\ the\ higher\ prob\ */}}
\DoxyCodeLine{00174\ \ \ \ \ link\ =\ fsg\_model\_null\_trans(fsg,\ from,\ to);}
\DoxyCodeLine{00175\ \ \ \ \ \textcolor{keywordflow}{if}\ (link)\ \{}
\DoxyCodeLine{00176\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (link-\/>logs2prob\ <\ logp)\ \{}
\DoxyCodeLine{00177\ \ \ \ \ \ \ \ \ \ \ \ \ link-\/>logs2prob\ =\ logp;}
\DoxyCodeLine{00178\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00179\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00180\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{00181\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00182\ \ \ \ \ \}}
\DoxyCodeLine{00183\ }
\DoxyCodeLine{00184\ \ \ \ \ \textcolor{comment}{/*\ Create\ null\ transition\ object\ */}}
\DoxyCodeLine{00185\ \ \ \ \ link\ =\ listelem\_malloc(fsg-\/>link\_alloc);}
\DoxyCodeLine{00186\ \ \ \ \ link-\/>from\_state\ =\ from;}
\DoxyCodeLine{00187\ \ \ \ \ link-\/>to\_state\ =\ to;}
\DoxyCodeLine{00188\ \ \ \ \ link-\/>logs2prob\ =\ logp;}
\DoxyCodeLine{00189\ \ \ \ \ link-\/>wid\ =\ -\/1;}
\DoxyCodeLine{00190\ }
\DoxyCodeLine{00191\ \ \ \ \ link2\ =\ (fsg\_link\_t\ *)}
\DoxyCodeLine{00192\ \ \ \ \ \ \ \ \ hash\_table\_enter\_bkey(fsg-\/>trans[from].null\_trans,}
\DoxyCodeLine{00193\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (\textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *)\ \&link-\/>to\_state,}
\DoxyCodeLine{00194\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{sizeof}(link-\/>to\_state),\ link);}
\DoxyCodeLine{00195\ \ \ \ \ assert(link\ ==\ link2);}
\DoxyCodeLine{00196\ }
\DoxyCodeLine{00197\ \ \ \ \ \textcolor{keywordflow}{return}\ 1;}
\DoxyCodeLine{00198\ \}}
\DoxyCodeLine{00199\ }
\DoxyCodeLine{00200\ int32}
\DoxyCodeLine{00201\ fsg\_model\_null\_trans\_add(fsg\_model\_t\ *\ fsg,\ int32\ from,\ int32\ to,}
\DoxyCodeLine{00202\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ int32\ logp)}
\DoxyCodeLine{00203\ \{}
\DoxyCodeLine{00204\ \ \ \ \ \textcolor{keywordflow}{return}\ fsg\_model\_tag\_trans\_add(fsg,\ from,\ to,\ logp,\ -\/1);}
\DoxyCodeLine{00205\ \}}
\DoxyCodeLine{00206\ }
\DoxyCodeLine{00207\ glist\_t}
\DoxyCodeLine{00208\ fsg\_model\_null\_trans\_closure(fsg\_model\_t\ *\ fsg,\ glist\_t\ nulls)}
\DoxyCodeLine{00209\ \{}
\DoxyCodeLine{00210\ \ \ \ \ gnode\_t\ *gn1;}
\DoxyCodeLine{00211\ \ \ \ \ \textcolor{keywordtype}{int}\ updated;}
\DoxyCodeLine{00212\ \ \ \ \ fsg\_link\_t\ *tl1,\ *tl2;}
\DoxyCodeLine{00213\ \ \ \ \ int32\ k,\ n;}
\DoxyCodeLine{00214\ }
\DoxyCodeLine{00215\ \ \ \ \ E\_INFO(\textcolor{stringliteral}{"{}Computing\ transitive\ closure\ for\ null\ transitions\(\backslash\)n"{}});}
\DoxyCodeLine{00216\ }
\DoxyCodeLine{00217\ \ \ \ \ \textcolor{keywordflow}{if}\ (nulls\ ==\ NULL)\ \{}
\DoxyCodeLine{00218\ \ \ \ \ \ \ \ \ fsg\_link\_t\ *null;}
\DoxyCodeLine{00219\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{int}\ i,\ j;}
\DoxyCodeLine{00220\ }
\DoxyCodeLine{00221\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ fsg-\/>n\_state;\ ++i)\ \{}
\DoxyCodeLine{00222\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (j\ =\ 0;\ j\ <\ fsg-\/>n\_state;\ ++j)\ \{}
\DoxyCodeLine{00223\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((null\ =\ fsg\_model\_null\_trans(fsg,\ i,\ j)))}
\DoxyCodeLine{00224\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ nulls\ =\ glist\_add\_ptr(nulls,\ null);}
\DoxyCodeLine{00225\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00226\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00227\ \ \ \ \ \}}
\DoxyCodeLine{00228\ }
\DoxyCodeLine{00229\ \ \ \ \ \textcolor{comment}{/*}}
\DoxyCodeLine{00230\ \textcolor{comment}{\ \ \ \ \ *\ Probably\ not\ the\ most\ efficient\ closure\ implementation,\ in\ general,\ but}}
\DoxyCodeLine{00231\ \textcolor{comment}{\ \ \ \ \ *\ probably\ reasonably\ efficient\ for\ a\ sparse\ null\ transition\ matrix.}}
\DoxyCodeLine{00232\ \textcolor{comment}{\ \ \ \ \ */}}
\DoxyCodeLine{00233\ \ \ \ \ n\ =\ 0;}
\DoxyCodeLine{00234\ \ \ \ \ \textcolor{keywordflow}{do}\ \{}
\DoxyCodeLine{00235\ \ \ \ \ \ \ \ \ updated\ =\ FALSE;}
\DoxyCodeLine{00236\ }
\DoxyCodeLine{00237\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (gn1\ =\ nulls;\ gn1;\ gn1\ =\ gnode\_next(gn1))\ \{}
\DoxyCodeLine{00238\ \ \ \ \ \ \ \ \ \ \ \ \ hash\_iter\_t\ *itor;}
\DoxyCodeLine{00239\ }
\DoxyCodeLine{00240\ \ \ \ \ \ \ \ \ \ \ \ \ tl1\ =\ (fsg\_link\_t\ *)\ gnode\_ptr(gn1);}
\DoxyCodeLine{00241\ \ \ \ \ \ \ \ \ \ \ \ \ assert(tl1-\/>wid\ <\ 0);}
\DoxyCodeLine{00242\ }
\DoxyCodeLine{00243\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (fsg-\/>trans[tl1-\/>to\_state].null\_trans\ ==\ NULL)}
\DoxyCodeLine{00244\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{continue};}
\DoxyCodeLine{00245\ }
\DoxyCodeLine{00246\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (itor\ =\ hash\_table\_iter(fsg-\/>trans[tl1-\/>to\_state].null\_trans);}
\DoxyCodeLine{00247\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ itor;\ itor\ =\ hash\_table\_iter\_next(itor))\ \{}
\DoxyCodeLine{00248\ }
\DoxyCodeLine{00249\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ tl2\ =\ (fsg\_link\_t\ *)\ hash\_entry\_val(itor-\/>ent);}
\DoxyCodeLine{00250\ }
\DoxyCodeLine{00251\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ k\ =\ fsg\_model\_null\_trans\_add(fsg,}
\DoxyCodeLine{00252\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ tl1-\/>from\_state,}
\DoxyCodeLine{00253\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ tl2-\/>to\_state,}
\DoxyCodeLine{00254\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ tl1-\/>logs2prob\ +}
\DoxyCodeLine{00255\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ tl2-\/>logs2prob);}
\DoxyCodeLine{00256\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (k\ >=\ 0)\ \{}
\DoxyCodeLine{00257\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ updated\ =\ TRUE;}
\DoxyCodeLine{00258\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (k\ >\ 0)\ \{}
\DoxyCodeLine{00259\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ nulls\ =\ glist\_add\_ptr(nulls,\ (\textcolor{keywordtype}{void}\ *)}
\DoxyCodeLine{00260\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ fsg\_model\_null\_trans}
\DoxyCodeLine{00261\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (fsg,\ tl1-\/>from\_state,}
\DoxyCodeLine{00262\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ tl2-\/>to\_state));}
\DoxyCodeLine{00263\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ n++;}
\DoxyCodeLine{00264\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00265\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00266\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00267\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00268\ \ \ \ \ \}\ \textcolor{keywordflow}{while}\ (updated);}
\DoxyCodeLine{00269\ \ \ \ \ }
\DoxyCodeLine{00270\ \ \ \ \ E\_INFO(\textcolor{stringliteral}{"{}\%d\ null\ transitions\ added\(\backslash\)n"{}},\ n);}
\DoxyCodeLine{00271\ }
\DoxyCodeLine{00272\ \ \ \ \ \textcolor{keywordflow}{return}\ nulls;}
\DoxyCodeLine{00273\ \}}
\DoxyCodeLine{00274\ }
\DoxyCodeLine{00275\ glist\_t}
\DoxyCodeLine{00276\ fsg\_model\_trans(fsg\_model\_t\ *\ fsg,\ int32\ i,\ int32\ j)}
\DoxyCodeLine{00277\ \{}
\DoxyCodeLine{00278\ \ \ \ \ \textcolor{keywordtype}{void}\ *val;}
\DoxyCodeLine{00279\ }
\DoxyCodeLine{00280\ \ \ \ \ \textcolor{keywordflow}{if}\ (fsg-\/>trans[i].trans\ ==\ NULL)}
\DoxyCodeLine{00281\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00282\ \ \ \ \ \textcolor{keywordflow}{if}\ (hash\_table\_lookup\_bkey(fsg-\/>trans[i].trans,\ (\textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *)\ \&j,}
\DoxyCodeLine{00283\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{sizeof}(j),\ \&val)\ <\ 0)}
\DoxyCodeLine{00284\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00285\ \ \ \ \ \textcolor{keywordflow}{return}\ (glist\_t)\ val;}
\DoxyCodeLine{00286\ \}}
\DoxyCodeLine{00287\ }
\DoxyCodeLine{00288\ fsg\_link\_t\ *}
\DoxyCodeLine{00289\ fsg\_model\_null\_trans(fsg\_model\_t\ *\ fsg,\ int32\ i,\ int32\ j)}
\DoxyCodeLine{00290\ \{}
\DoxyCodeLine{00291\ \ \ \ \ \textcolor{keywordtype}{void}\ *val;}
\DoxyCodeLine{00292\ }
\DoxyCodeLine{00293\ \ \ \ \ \textcolor{keywordflow}{if}\ (fsg-\/>trans[i].null\_trans\ ==\ NULL)}
\DoxyCodeLine{00294\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00295\ \ \ \ \ \textcolor{keywordflow}{if}\ (hash\_table\_lookup\_bkey(fsg-\/>trans[i].null\_trans,\ (\textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *)\ \&j,}
\DoxyCodeLine{00296\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{sizeof}(j),\ \&val)\ <\ 0)}
\DoxyCodeLine{00297\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00298\ \ \ \ \ \textcolor{keywordflow}{return}\ (fsg\_link\_t\ *)\ val;}
\DoxyCodeLine{00299\ \}}
\DoxyCodeLine{00300\ }
\DoxyCodeLine{00301\ fsg\_arciter\_t\ *}
\DoxyCodeLine{00302\ fsg\_model\_arcs(fsg\_model\_t\ *\ fsg,\ int32\ i)}
\DoxyCodeLine{00303\ \{}
\DoxyCodeLine{00304\ \ \ \ \ fsg\_arciter\_t\ *itor;}
\DoxyCodeLine{00305\ }
\DoxyCodeLine{00306\ \ \ \ \ \textcolor{keywordflow}{if}\ (fsg-\/>trans[i].trans\ ==\ NULL\ \&\&\ fsg-\/>trans[i].null\_trans\ ==\ NULL)}
\DoxyCodeLine{00307\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00308\ \ \ \ \ itor\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*itor));}
\DoxyCodeLine{00309\ \ \ \ \ \textcolor{keywordflow}{if}\ (fsg-\/>trans[i].null\_trans)}
\DoxyCodeLine{00310\ \ \ \ \ \ \ \ \ itor-\/>null\_itor\ =\ hash\_table\_iter(fsg-\/>trans[i].null\_trans);}
\DoxyCodeLine{00311\ \ \ \ \ \textcolor{keywordflow}{if}\ (fsg-\/>trans[i].trans)}
\DoxyCodeLine{00312\ \ \ \ \ \ \ \ \ itor-\/>itor\ =\ hash\_table\_iter(fsg-\/>trans[i].trans);}
\DoxyCodeLine{00313\ \ \ \ \ \textcolor{keywordflow}{if}\ (itor-\/>itor\ !=\ NULL)}
\DoxyCodeLine{00314\ \ \ \ \ \ \ \ \ itor-\/>gn\ =\ hash\_entry\_val(itor-\/>itor-\/>ent);}
\DoxyCodeLine{00315\ \ \ \ \ \textcolor{keywordflow}{return}\ itor;}
\DoxyCodeLine{00316\ \}}
\DoxyCodeLine{00317\ }
\DoxyCodeLine{00318\ fsg\_link\_t\ *}
\DoxyCodeLine{00319\ fsg\_arciter\_get(fsg\_arciter\_t\ *\ itor)}
\DoxyCodeLine{00320\ \{}
\DoxyCodeLine{00321\ \ \ \ \ \textcolor{comment}{/*\ Iterate\ over\ non-\/null\ arcs\ first.\ */}}
\DoxyCodeLine{00322\ \ \ \ \ \textcolor{keywordflow}{if}\ (itor-\/>gn)}
\DoxyCodeLine{00323\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ (fsg\_link\_t\ *)\ gnode\_ptr(itor-\/>gn);}
\DoxyCodeLine{00324\ \ \ \ \ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}\ (itor-\/>null\_itor)}
\DoxyCodeLine{00325\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ (fsg\_link\_t\ *)\ hash\_entry\_val(itor-\/>null\_itor-\/>ent);}
\DoxyCodeLine{00326\ \ \ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{00327\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00328\ \}}
\DoxyCodeLine{00329\ }
\DoxyCodeLine{00330\ fsg\_arciter\_t\ *}
\DoxyCodeLine{00331\ fsg\_arciter\_next(fsg\_arciter\_t\ *\ itor)}
\DoxyCodeLine{00332\ \{}
\DoxyCodeLine{00333\ \ \ \ \ \textcolor{comment}{/*\ Iterate\ over\ non-\/null\ arcs\ first.\ */}}
\DoxyCodeLine{00334\ \ \ \ \ \textcolor{keywordflow}{if}\ (itor-\/>gn)\ \{}
\DoxyCodeLine{00335\ \ \ \ \ \ \ \ \ itor-\/>gn\ =\ gnode\_next(itor-\/>gn);}
\DoxyCodeLine{00336\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Move\ to\ the\ next\ destination\ arc.\ */}}
\DoxyCodeLine{00337\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (itor-\/>gn\ ==\ NULL)\ \{}
\DoxyCodeLine{00338\ \ \ \ \ \ \ \ \ \ \ \ \ itor-\/>itor\ =\ hash\_table\_iter\_next(itor-\/>itor);}
\DoxyCodeLine{00339\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (itor-\/>itor\ !=\ NULL)}
\DoxyCodeLine{00340\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ itor-\/>gn\ =\ hash\_entry\_val(itor-\/>itor-\/>ent);}
\DoxyCodeLine{00341\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}\ (itor-\/>null\_itor\ ==\ NULL)}
\DoxyCodeLine{00342\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ stop\_iteration;}
\DoxyCodeLine{00343\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00344\ \ \ \ \ \}}
\DoxyCodeLine{00345\ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00346\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (itor-\/>null\_itor\ ==\ NULL)}
\DoxyCodeLine{00347\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ stop\_iteration;}
\DoxyCodeLine{00348\ \ \ \ \ \ \ \ \ itor-\/>null\_itor\ =\ hash\_table\_iter\_next(itor-\/>null\_itor);}
\DoxyCodeLine{00349\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (itor-\/>null\_itor\ ==\ NULL)}
\DoxyCodeLine{00350\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ stop\_iteration;}
\DoxyCodeLine{00351\ \ \ \ \ \}}
\DoxyCodeLine{00352\ \ \ \ \ \textcolor{keywordflow}{return}\ itor;}
\DoxyCodeLine{00353\ \ \ stop\_iteration:}
\DoxyCodeLine{00354\ \ \ \ \ fsg\_arciter\_free(itor);}
\DoxyCodeLine{00355\ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00356\ }
\DoxyCodeLine{00357\ \}}
\DoxyCodeLine{00358\ }
\DoxyCodeLine{00359\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00360\ fsg\_arciter\_free(fsg\_arciter\_t\ *\ itor)}
\DoxyCodeLine{00361\ \{}
\DoxyCodeLine{00362\ \ \ \ \ \textcolor{keywordflow}{if}\ (itor\ ==\ NULL)}
\DoxyCodeLine{00363\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return};}
\DoxyCodeLine{00364\ \ \ \ \ hash\_table\_iter\_free(itor-\/>null\_itor);}
\DoxyCodeLine{00365\ \ \ \ \ hash\_table\_iter\_free(itor-\/>itor);}
\DoxyCodeLine{00366\ \ \ \ \ ckd\_free(itor);}
\DoxyCodeLine{00367\ \}}
\DoxyCodeLine{00368\ }
\DoxyCodeLine{00369\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00370\ fsg\_model\_word\_id(fsg\_model\_t\ *\ fsg,\ \textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *word)}
\DoxyCodeLine{00371\ \{}
\DoxyCodeLine{00372\ \ \ \ \ \textcolor{keywordtype}{int}\ wid;}
\DoxyCodeLine{00373\ }
\DoxyCodeLine{00374\ \ \ \ \ \textcolor{comment}{/*\ Search\ for\ an\ existing\ word\ matching\ this.\ */}}
\DoxyCodeLine{00375\ \ \ \ \ \textcolor{keywordflow}{for}\ (wid\ =\ 0;\ wid\ <\ fsg-\/>n\_word;\ ++wid)\ \{}
\DoxyCodeLine{00376\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (0\ ==\ strcmp(fsg-\/>vocab[wid],\ word))}
\DoxyCodeLine{00377\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00378\ \ \ \ \ \}}
\DoxyCodeLine{00379\ \ \ \ \ \textcolor{comment}{/*\ If\ not\ found,\ add\ this\ to\ the\ vocab.\ */}}
\DoxyCodeLine{00380\ \ \ \ \ \textcolor{keywordflow}{if}\ (wid\ ==\ fsg-\/>n\_word)}
\DoxyCodeLine{00381\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00382\ \ \ \ \ \textcolor{keywordflow}{return}\ wid;}
\DoxyCodeLine{00383\ \}}
\DoxyCodeLine{00384\ }
\DoxyCodeLine{00385\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00386\ fsg\_model\_word\_add(fsg\_model\_t\ *\ fsg,\ \textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *word)}
\DoxyCodeLine{00387\ \{}
\DoxyCodeLine{00388\ \ \ \ \ \textcolor{keywordtype}{int}\ wid,\ old\_size;}
\DoxyCodeLine{00389\ }
\DoxyCodeLine{00390\ \ \ \ \ \textcolor{comment}{/*\ Search\ for\ an\ existing\ word\ matching\ this.\ */}}
\DoxyCodeLine{00391\ \ \ \ \ wid\ =\ fsg\_model\_word\_id(fsg,\ word);}
\DoxyCodeLine{00392\ \ \ \ \ \textcolor{comment}{/*\ If\ not\ found,\ add\ this\ to\ the\ vocab.\ */}}
\DoxyCodeLine{00393\ \ \ \ \ \textcolor{keywordflow}{if}\ (wid\ ==\ -\/1)\ \{}
\DoxyCodeLine{00394\ \ \ \ \ \ \ \ \ wid\ =\ fsg-\/>n\_word;}
\DoxyCodeLine{00395\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (fsg-\/>n\_word\ ==\ fsg-\/>n\_word\_alloc)\ \{}
\DoxyCodeLine{00396\ \ \ \ \ \ \ \ \ \ \ \ \ old\_size\ =\ fsg-\/>n\_word\_alloc;}
\DoxyCodeLine{00397\ \ \ \ \ \ \ \ \ \ \ \ \ fsg-\/>n\_word\_alloc\ +=\ 10;}
\DoxyCodeLine{00398\ \ \ \ \ \ \ \ \ \ \ \ \ fsg-\/>vocab\ =\ ckd\_realloc(fsg-\/>vocab,}
\DoxyCodeLine{00399\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ fsg-\/>n\_word\_alloc\ *}
\DoxyCodeLine{00400\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{sizeof}(*fsg-\/>vocab));}
\DoxyCodeLine{00401\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (fsg-\/>silwords)}
\DoxyCodeLine{00402\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ fsg-\/>silwords\ =}
\DoxyCodeLine{00403\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ bitvec\_realloc(fsg-\/>silwords,\ old\_size,\ fsg-\/>n\_word\_alloc);}
\DoxyCodeLine{00404\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (fsg-\/>altwords)}
\DoxyCodeLine{00405\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ fsg-\/>altwords\ =}
\DoxyCodeLine{00406\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ bitvec\_realloc(fsg-\/>altwords,\ old\_size,\ fsg-\/>n\_word\_alloc);}
\DoxyCodeLine{00407\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00408\ \ \ \ \ \ \ \ \ ++fsg-\/>n\_word;}
\DoxyCodeLine{00409\ \ \ \ \ \ \ \ \ fsg-\/>vocab[wid]\ =\ ckd\_salloc(word);}
\DoxyCodeLine{00410\ \ \ \ \ \}}
\DoxyCodeLine{00411\ \ \ \ \ \textcolor{keywordflow}{return}\ wid;}
\DoxyCodeLine{00412\ \}}
\DoxyCodeLine{00413\ }
\DoxyCodeLine{00414\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00415\ fsg\_model\_add\_silence(fsg\_model\_t\ *\ fsg,\ \textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *silword,}
\DoxyCodeLine{00416\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{int}\ state,\ float32\ silprob)}
\DoxyCodeLine{00417\ \{}
\DoxyCodeLine{00418\ \ \ \ \ int32\ logsilp;}
\DoxyCodeLine{00419\ \ \ \ \ \textcolor{keywordtype}{int}\ n\_trans,\ silwid,\ src;}
\DoxyCodeLine{00420\ }
\DoxyCodeLine{00421\ \ \ \ \ E\_INFO(\textcolor{stringliteral}{"{}Adding\ silence\ transitions\ for\ \%s\ to\ FSG\(\backslash\)n"{}},\ silword);}
\DoxyCodeLine{00422\ }
\DoxyCodeLine{00423\ \ \ \ \ silwid\ =\ fsg\_model\_word\_add(fsg,\ silword);}
\DoxyCodeLine{00424\ \ \ \ \ logsilp\ =\ (int32)\ (logmath\_log(fsg-\/>lmath,\ silprob)\ *\ fsg-\/>lw);}
\DoxyCodeLine{00425\ \ \ \ \ \textcolor{keywordflow}{if}\ (fsg-\/>silwords\ ==\ NULL)}
\DoxyCodeLine{00426\ \ \ \ \ \ \ \ \ fsg-\/>silwords\ =\ bitvec\_alloc(fsg-\/>n\_word\_alloc);}
\DoxyCodeLine{00427\ \ \ \ \ bitvec\_set(fsg-\/>silwords,\ silwid);}
\DoxyCodeLine{00428\ }
\DoxyCodeLine{00429\ \ \ \ \ n\_trans\ =\ 0;}
\DoxyCodeLine{00430\ \ \ \ \ \textcolor{keywordflow}{if}\ (state\ ==\ -\/1)\ \{}
\DoxyCodeLine{00431\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (src\ =\ 0;\ src\ <\ fsg-\/>n\_state;\ src++)\ \{}
\DoxyCodeLine{00432\ \ \ \ \ \ \ \ \ \ \ \ \ fsg\_model\_trans\_add(fsg,\ src,\ src,\ logsilp,\ silwid);}
\DoxyCodeLine{00433\ \ \ \ \ \ \ \ \ \ \ \ \ ++n\_trans;}
\DoxyCodeLine{00434\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00435\ \ \ \ \ \}}
\DoxyCodeLine{00436\ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00437\ \ \ \ \ \ \ \ \ fsg\_model\_trans\_add(fsg,\ state,\ state,\ logsilp,\ silwid);}
\DoxyCodeLine{00438\ \ \ \ \ \ \ \ \ ++n\_trans;}
\DoxyCodeLine{00439\ \ \ \ \ \}}
\DoxyCodeLine{00440\ }
\DoxyCodeLine{00441\ \ \ \ \ E\_INFO(\textcolor{stringliteral}{"{}Added\ \%d\ silence\ word\ transitions\(\backslash\)n"{}},\ n\_trans);}
\DoxyCodeLine{00442\ \ \ \ \ \textcolor{keywordflow}{return}\ n\_trans;}
\DoxyCodeLine{00443\ \}}
\DoxyCodeLine{00444\ }
\DoxyCodeLine{00445\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00446\ fsg\_model\_add\_alt(fsg\_model\_t\ *\ fsg,\ \textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *baseword,}
\DoxyCodeLine{00447\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *altword)}
\DoxyCodeLine{00448\ \{}
\DoxyCodeLine{00449\ \ \ \ \ \textcolor{keywordtype}{int}\ i,\ basewid,\ altwid;}
\DoxyCodeLine{00450\ \ \ \ \ \textcolor{keywordtype}{int}\ ntrans;}
\DoxyCodeLine{00451\ }
\DoxyCodeLine{00452\ \ \ \ \ \textcolor{comment}{/*\ FIXME:\ This\ will\ get\ slow,\ eventually...\ */}}
\DoxyCodeLine{00453\ \ \ \ \ \textcolor{keywordflow}{for}\ (basewid\ =\ 0;\ basewid\ <\ fsg-\/>n\_word;\ ++basewid)}
\DoxyCodeLine{00454\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (0\ ==\ strcmp(fsg-\/>vocab[basewid],\ baseword))}
\DoxyCodeLine{00455\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00456\ \ \ \ \ \textcolor{keywordflow}{if}\ (basewid\ ==\ fsg-\/>n\_word)\ \{}
\DoxyCodeLine{00457\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Base\ word\ \%s\ not\ present\ in\ FSG\ vocabulary!\(\backslash\)n"{}},\ baseword);}
\DoxyCodeLine{00458\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00459\ \ \ \ \ \}}
\DoxyCodeLine{00460\ \ \ \ \ altwid\ =\ fsg\_model\_word\_add(fsg,\ altword);}
\DoxyCodeLine{00461\ \ \ \ \ \textcolor{keywordflow}{if}\ (fsg-\/>altwords\ ==\ NULL)}
\DoxyCodeLine{00462\ \ \ \ \ \ \ \ \ fsg-\/>altwords\ =\ bitvec\_alloc(fsg-\/>n\_word\_alloc);}
\DoxyCodeLine{00463\ \ \ \ \ bitvec\_set(fsg-\/>altwords,\ altwid);}
\DoxyCodeLine{00464\ }
\DoxyCodeLine{00465\ \ \ \ \ E\_DEBUG(2,\ (\textcolor{stringliteral}{"{}Adding\ alternate\ word\ transitions\ (\%s,\%s)\ to\ FSG\(\backslash\)n"{}},}
\DoxyCodeLine{00466\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ baseword,\ altword));}
\DoxyCodeLine{00467\ }
\DoxyCodeLine{00468\ \ \ \ \ \textcolor{comment}{/*\ Look\ for\ all\ transitions\ involving\ baseword\ and\ duplicate\ them.\ */}}
\DoxyCodeLine{00469\ \ \ \ \ \textcolor{comment}{/*\ FIXME:\ This\ will\ also\ get\ slow,\ eventually...\ */}}
\DoxyCodeLine{00470\ \ \ \ \ ntrans\ =\ 0;}
\DoxyCodeLine{00471\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ fsg-\/>n\_state;\ ++i)\ \{}
\DoxyCodeLine{00472\ \ \ \ \ \ \ \ \ hash\_iter\_t\ *itor;}
\DoxyCodeLine{00473\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (fsg-\/>trans[i].trans\ ==\ NULL)}
\DoxyCodeLine{00474\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{continue};}
\DoxyCodeLine{00475\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (itor\ =\ hash\_table\_iter(fsg-\/>trans[i].trans);\ itor;}
\DoxyCodeLine{00476\ \ \ \ \ \ \ \ \ \ \ \ \ \ itor\ =\ hash\_table\_iter\_next(itor))\ \{}
\DoxyCodeLine{00477\ \ \ \ \ \ \ \ \ \ \ \ \ glist\_t\ trans;}
\DoxyCodeLine{00478\ \ \ \ \ \ \ \ \ \ \ \ \ gnode\_t\ *gn;}
\DoxyCodeLine{00479\ }
\DoxyCodeLine{00480\ \ \ \ \ \ \ \ \ \ \ \ \ trans\ =\ hash\_entry\_val(itor-\/>ent);}
\DoxyCodeLine{00481\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (gn\ =\ trans;\ gn;\ gn\ =\ gnode\_next(gn))\ \{}
\DoxyCodeLine{00482\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ fsg\_link\_t\ *fl\ =\ gnode\_ptr(gn);}
\DoxyCodeLine{00483\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (fl-\/>wid\ ==\ basewid)\ \{}
\DoxyCodeLine{00484\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ fsg\_link\_t\ *link;}
\DoxyCodeLine{00485\ }
\DoxyCodeLine{00486\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Create\ transition\ object\ */}}
\DoxyCodeLine{00487\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ link\ =\ listelem\_malloc(fsg-\/>link\_alloc);}
\DoxyCodeLine{00488\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ link-\/>from\_state\ =\ fl-\/>from\_state;}
\DoxyCodeLine{00489\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ link-\/>to\_state\ =\ fl-\/>to\_state;}
\DoxyCodeLine{00490\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ link-\/>logs2prob\ =\ fl-\/>logs2prob;\ \ \ \ \textcolor{comment}{/*\ FIXME!!!???\ */}}
\DoxyCodeLine{00491\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ link-\/>wid\ =\ altwid;}
\DoxyCodeLine{00492\ }
\DoxyCodeLine{00493\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ trans\ =\ glist\_add\_ptr(trans,\ (\textcolor{keywordtype}{void}\ *)\ link);}
\DoxyCodeLine{00494\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ++ntrans;}
\DoxyCodeLine{00495\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00496\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00497\ \ \ \ \ \ \ \ \ \ \ \ \ hash\_entry\_val(itor-\/>ent)\ =\ trans;}
\DoxyCodeLine{00498\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00499\ \ \ \ \ \}}
\DoxyCodeLine{00500\ }
\DoxyCodeLine{00501\ \ \ \ \ E\_DEBUG(2,\ (\textcolor{stringliteral}{"{}Added\ \%d\ alternate\ word\ transitions\(\backslash\)n"{}},\ ntrans));}
\DoxyCodeLine{00502\ \ \ \ \ \textcolor{keywordflow}{return}\ ntrans;}
\DoxyCodeLine{00503\ \}}
\DoxyCodeLine{00504\ }
\DoxyCodeLine{00505\ }
\DoxyCodeLine{00506\ fsg\_model\_t\ *}
\DoxyCodeLine{00507\ fsg\_model\_init(\textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *name,\ logmath\_t\ *\ lmath,\ float32\ lw,}
\DoxyCodeLine{00508\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ int32\ n\_state)}
\DoxyCodeLine{00509\ \{}
\DoxyCodeLine{00510\ \ \ \ \ fsg\_model\_t\ *fsg;}
\DoxyCodeLine{00511\ }
\DoxyCodeLine{00512\ \ \ \ \ \textcolor{comment}{/*\ Allocate\ basic\ stuff.\ */}}
\DoxyCodeLine{00513\ \ \ \ \ fsg\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*fsg));}
\DoxyCodeLine{00514\ \ \ \ \ fsg-\/>refcount\ =\ 1;}
\DoxyCodeLine{00515\ \ \ \ \ fsg-\/>link\_alloc\ =\ listelem\_alloc\_init(\textcolor{keyword}{sizeof}(fsg\_link\_t));}
\DoxyCodeLine{00516\ \ \ \ \ fsg-\/>lmath\ =\ lmath;}
\DoxyCodeLine{00517\ \ \ \ \ fsg-\/>name\ =\ name\ ?\ ckd\_salloc(name)\ :\ NULL;}
\DoxyCodeLine{00518\ \ \ \ \ fsg-\/>n\_state\ =\ n\_state;}
\DoxyCodeLine{00519\ \ \ \ \ fsg-\/>lw\ =\ lw;}
\DoxyCodeLine{00520\ }
\DoxyCodeLine{00521\ \ \ \ \ fsg-\/>trans\ =\ ckd\_calloc(fsg-\/>n\_state,\ \textcolor{keyword}{sizeof}(*fsg-\/>trans));}
\DoxyCodeLine{00522\ }
\DoxyCodeLine{00523\ \ \ \ \ \textcolor{keywordflow}{return}\ fsg;}
\DoxyCodeLine{00524\ \}}
\DoxyCodeLine{00525\ }
\DoxyCodeLine{00526\ fsg\_model\_t\ *}
\DoxyCodeLine{00527\ fsg\_model\_read(FILE\ *\ fp,\ logmath\_t\ *\ lmath,\ float32\ lw)}
\DoxyCodeLine{00528\ \{}
\DoxyCodeLine{00529\ \ \ \ \ fsg\_model\_t\ *fsg;}
\DoxyCodeLine{00530\ \ \ \ \ hash\_table\_t\ *vocab;}
\DoxyCodeLine{00531\ \ \ \ \ hash\_iter\_t\ *itor;}
\DoxyCodeLine{00532\ \ \ \ \ int32\ lastwid;}
\DoxyCodeLine{00533\ \ \ \ \ \textcolor{keywordtype}{char}\ **wordptr;}
\DoxyCodeLine{00534\ \ \ \ \ \textcolor{keywordtype}{char}\ *lineptr;}
\DoxyCodeLine{00535\ \ \ \ \ \textcolor{keywordtype}{char}\ *fsgname;}
\DoxyCodeLine{00536\ \ \ \ \ int32\ lineno;}
\DoxyCodeLine{00537\ \ \ \ \ int32\ n,\ i,\ j;}
\DoxyCodeLine{00538\ \ \ \ \ \textcolor{keywordtype}{int}\ n\_state,\ n\_trans,\ n\_null\_trans;}
\DoxyCodeLine{00539\ \ \ \ \ glist\_t\ nulls;}
\DoxyCodeLine{00540\ \ \ \ \ float32\ p;}
\DoxyCodeLine{00541\ }
\DoxyCodeLine{00542\ \ \ \ \ lineno\ =\ 0;}
\DoxyCodeLine{00543\ \ \ \ \ vocab\ =\ hash\_table\_new(32,\ FALSE);}
\DoxyCodeLine{00544\ \ \ \ \ wordptr\ =\ NULL;}
\DoxyCodeLine{00545\ \ \ \ \ lineptr\ =\ NULL;}
\DoxyCodeLine{00546\ \ \ \ \ nulls\ =\ NULL;}
\DoxyCodeLine{00547\ \ \ \ \ fsgname\ =\ NULL;}
\DoxyCodeLine{00548\ \ \ \ \ fsg\ =\ NULL;}
\DoxyCodeLine{00549\ }
\DoxyCodeLine{00550\ \ \ \ \ \textcolor{comment}{/*\ Scan\ upto\ FSG\_BEGIN\ header\ */}}
\DoxyCodeLine{00551\ \ \ \ \ \textcolor{keywordflow}{for}\ (;;)\ \{}
\DoxyCodeLine{00552\ \ \ \ \ \ \ \ \ n\ =\ nextline\_str2words(fp,\ \&lineno,\ \&lineptr,\ \&wordptr);}
\DoxyCodeLine{00553\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (n\ <\ 0)\ \{}
\DoxyCodeLine{00554\ \ \ \ \ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}\%s\ declaration\ missing\(\backslash\)n"{}},\ FSG\_MODEL\_BEGIN\_DECL);}
\DoxyCodeLine{00555\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ parse\_error;}
\DoxyCodeLine{00556\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00557\ }
\DoxyCodeLine{00558\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((strcmp(wordptr[0],\ FSG\_MODEL\_BEGIN\_DECL)\ ==\ 0))\ \{}
\DoxyCodeLine{00559\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (n\ >\ 2)\ \{}
\DoxyCodeLine{00560\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Line[\%d]:\ malformed\ FSG\_BEGIN\ declaration\(\backslash\)n"{}},}
\DoxyCodeLine{00561\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ lineno);}
\DoxyCodeLine{00562\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ parse\_error;}
\DoxyCodeLine{00563\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00564\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00565\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00566\ \ \ \ \ \}}
\DoxyCodeLine{00567\ \ \ \ \ \textcolor{comment}{/*\ Save\ FSG\ name,\ or\ it\ will\ get\ clobbered\ below\ :(.}}
\DoxyCodeLine{00568\ \textcolor{comment}{\ \ \ \ \ *\ If\ name\ is\ missing,\ try\ the\ default.}}
\DoxyCodeLine{00569\ \textcolor{comment}{\ \ \ \ \ */}}
\DoxyCodeLine{00570\ \ \ \ \ \textcolor{keywordflow}{if}\ (n\ ==\ 2)\ \{}
\DoxyCodeLine{00571\ \ \ \ \ \ \ \ \ fsgname\ =\ ckd\_salloc(wordptr[1]);}
\DoxyCodeLine{00572\ \ \ \ \ \}}
\DoxyCodeLine{00573\ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00574\ \ \ \ \ \ \ \ \ E\_WARN(\textcolor{stringliteral}{"{}FSG\ name\ is\ missing\(\backslash\)n"{}});}
\DoxyCodeLine{00575\ \ \ \ \ \ \ \ \ fsgname\ =\ ckd\_salloc(\textcolor{stringliteral}{"{}unknown"{}});}
\DoxyCodeLine{00576\ \ \ \ \ \}}
\DoxyCodeLine{00577\ }
\DoxyCodeLine{00578\ \ \ \ \ \textcolor{comment}{/*\ Read\ \#states\ */}}
\DoxyCodeLine{00579\ \ \ \ \ n\ =\ nextline\_str2words(fp,\ \&lineno,\ \&lineptr,\ \&wordptr);}
\DoxyCodeLine{00580\ \ \ \ \ \textcolor{keywordflow}{if}\ ((n\ !=\ 2)}
\DoxyCodeLine{00581\ \ \ \ \ \ \ \ \ ||\ ((strcmp(wordptr[0],\ FSG\_MODEL\_N\_DECL)\ !=\ 0)}
\DoxyCodeLine{00582\ \ \ \ \ \ \ \ \ \ \ \ \ \&\&\ (strcmp(wordptr[0],\ FSG\_MODEL\_NUM\_STATES\_DECL)\ !=\ 0))}
\DoxyCodeLine{00583\ \ \ \ \ \ \ \ \ ||\ (sscanf(wordptr[1],\ \textcolor{stringliteral}{"{}\%d"{}},\ \&n\_state)\ !=\ 1)}
\DoxyCodeLine{00584\ \ \ \ \ \ \ \ \ ||\ (n\_state\ <=\ 0))\ \{}
\DoxyCodeLine{00585\ \ \ \ \ \ \ \ \ E\_ERROR}
\DoxyCodeLine{00586\ \ \ \ \ \ \ \ \ \ \ \ \ (\textcolor{stringliteral}{"{}Line[\%d]:\ \#states\ declaration\ line\ missing\ or\ malformed\(\backslash\)n"{}},}
\DoxyCodeLine{00587\ \ \ \ \ \ \ \ \ \ \ \ \ \ lineno);}
\DoxyCodeLine{00588\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ parse\_error;}
\DoxyCodeLine{00589\ \ \ \ \ \}}
\DoxyCodeLine{00590\ }
\DoxyCodeLine{00591\ \ \ \ \ \textcolor{comment}{/*\ Now\ create\ the\ FSG.\ */}}
\DoxyCodeLine{00592\ \ \ \ \ fsg\ =\ fsg\_model\_init(fsgname,\ lmath,\ lw,\ n\_state);}
\DoxyCodeLine{00593\ \ \ \ \ ckd\_free(fsgname);}
\DoxyCodeLine{00594\ \ \ \ \ fsgname\ =\ NULL;}
\DoxyCodeLine{00595\ }
\DoxyCodeLine{00596\ \ \ \ \ \textcolor{comment}{/*\ Read\ start\ state\ */}}
\DoxyCodeLine{00597\ \ \ \ \ n\ =\ nextline\_str2words(fp,\ \&lineno,\ \&lineptr,\ \&wordptr);}
\DoxyCodeLine{00598\ \ \ \ \ \textcolor{keywordflow}{if}\ ((n\ !=\ 2)}
\DoxyCodeLine{00599\ \ \ \ \ \ \ \ \ ||\ ((strcmp(wordptr[0],\ FSG\_MODEL\_S\_DECL)\ !=\ 0)}
\DoxyCodeLine{00600\ \ \ \ \ \ \ \ \ \ \ \ \ \&\&\ (strcmp(wordptr[0],\ FSG\_MODEL\_START\_STATE\_DECL)\ !=\ 0))}
\DoxyCodeLine{00601\ \ \ \ \ \ \ \ \ ||\ (sscanf(wordptr[1],\ \textcolor{stringliteral}{"{}\%d"{}},\ \&(fsg-\/>start\_state))\ !=\ 1)}
\DoxyCodeLine{00602\ \ \ \ \ \ \ \ \ ||\ (fsg-\/>start\_state\ <\ 0)}
\DoxyCodeLine{00603\ \ \ \ \ \ \ \ \ ||\ (fsg-\/>start\_state\ >=\ fsg-\/>n\_state))\ \{}
\DoxyCodeLine{00604\ \ \ \ \ \ \ \ \ E\_ERROR}
\DoxyCodeLine{00605\ \ \ \ \ \ \ \ \ \ \ \ \ (\textcolor{stringliteral}{"{}Line[\%d]:\ start\ state\ declaration\ line\ missing\ or\ malformed\(\backslash\)n"{}},}
\DoxyCodeLine{00606\ \ \ \ \ \ \ \ \ \ \ \ \ \ lineno);}
\DoxyCodeLine{00607\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ parse\_error;}
\DoxyCodeLine{00608\ \ \ \ \ \}}
\DoxyCodeLine{00609\ }
\DoxyCodeLine{00610\ \ \ \ \ \textcolor{comment}{/*\ Read\ final\ state\ */}}
\DoxyCodeLine{00611\ \ \ \ \ n\ =\ nextline\_str2words(fp,\ \&lineno,\ \&lineptr,\ \&wordptr);}
\DoxyCodeLine{00612\ \ \ \ \ \textcolor{keywordflow}{if}\ ((n\ !=\ 2)}
\DoxyCodeLine{00613\ \ \ \ \ \ \ \ \ ||\ ((strcmp(wordptr[0],\ FSG\_MODEL\_F\_DECL)\ !=\ 0)}
\DoxyCodeLine{00614\ \ \ \ \ \ \ \ \ \ \ \ \ \&\&\ (strcmp(wordptr[0],\ FSG\_MODEL\_FINAL\_STATE\_DECL)\ !=\ 0))}
\DoxyCodeLine{00615\ \ \ \ \ \ \ \ \ ||\ (sscanf(wordptr[1],\ \textcolor{stringliteral}{"{}\%d"{}},\ \&(fsg-\/>final\_state))\ !=\ 1)}
\DoxyCodeLine{00616\ \ \ \ \ \ \ \ \ ||\ (fsg-\/>final\_state\ <\ 0)}
\DoxyCodeLine{00617\ \ \ \ \ \ \ \ \ ||\ (fsg-\/>final\_state\ >=\ fsg-\/>n\_state))\ \{}
\DoxyCodeLine{00618\ \ \ \ \ \ \ \ \ E\_ERROR}
\DoxyCodeLine{00619\ \ \ \ \ \ \ \ \ \ \ \ \ (\textcolor{stringliteral}{"{}Line[\%d]:\ final\ state\ declaration\ line\ missing\ or\ malformed\(\backslash\)n"{}},}
\DoxyCodeLine{00620\ \ \ \ \ \ \ \ \ \ \ \ \ \ lineno);}
\DoxyCodeLine{00621\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ parse\_error;}
\DoxyCodeLine{00622\ \ \ \ \ \}}
\DoxyCodeLine{00623\ }
\DoxyCodeLine{00624\ \ \ \ \ \textcolor{comment}{/*\ Read\ transitions\ */}}
\DoxyCodeLine{00625\ \ \ \ \ lastwid\ =\ 0;}
\DoxyCodeLine{00626\ \ \ \ \ n\_trans\ =\ n\_null\_trans\ =\ 0;}
\DoxyCodeLine{00627\ \ \ \ \ \textcolor{keywordflow}{for}\ (;;)\ \{}
\DoxyCodeLine{00628\ \ \ \ \ \ \ \ \ int32\ wid,\ tprob;}
\DoxyCodeLine{00629\ }
\DoxyCodeLine{00630\ \ \ \ \ \ \ \ \ n\ =\ nextline\_str2words(fp,\ \&lineno,\ \&lineptr,\ \&wordptr);}
\DoxyCodeLine{00631\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (n\ <=\ 0)\ \{}
\DoxyCodeLine{00632\ \ \ \ \ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Line[\%d]:\ transition\ or\ FSG\_END\ statement\ expected\(\backslash\)n"{}},}
\DoxyCodeLine{00633\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ lineno);}
\DoxyCodeLine{00634\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ parse\_error;}
\DoxyCodeLine{00635\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00636\ }
\DoxyCodeLine{00637\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((strcmp(wordptr[0],\ FSG\_MODEL\_END\_DECL)\ ==\ 0))\ \{}
\DoxyCodeLine{00638\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00639\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00640\ }
\DoxyCodeLine{00641\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((strcmp(wordptr[0],\ FSG\_MODEL\_T\_DECL)\ ==\ 0)}
\DoxyCodeLine{00642\ \ \ \ \ \ \ \ \ \ \ \ \ ||\ (strcmp(wordptr[0],\ FSG\_MODEL\_TRANSITION\_DECL)\ ==\ 0))\ \{}
\DoxyCodeLine{00643\ }
\DoxyCodeLine{00644\ }
\DoxyCodeLine{00645\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (((n\ !=\ 4)\ \&\&\ (n\ !=\ 5))}
\DoxyCodeLine{00646\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ||\ (sscanf(wordptr[1],\ \textcolor{stringliteral}{"{}\%d"{}},\ \&i)\ !=\ 1)}
\DoxyCodeLine{00647\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ||\ (sscanf(wordptr[2],\ \textcolor{stringliteral}{"{}\%d"{}},\ \&j)\ !=\ 1)}
\DoxyCodeLine{00648\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ||\ (i\ <\ 0)\ ||\ (i\ >=\ fsg-\/>n\_state)}
\DoxyCodeLine{00649\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ||\ (j\ <\ 0)\ ||\ (j\ >=\ fsg-\/>n\_state))\ \{}
\DoxyCodeLine{00650\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ E\_ERROR}
\DoxyCodeLine{00651\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (\textcolor{stringliteral}{"{}Line[\%d]:\ transition\ spec\ malformed;\ Expecting:\ from-\/state\ to-\/state\ trans-\/prob\ [word]\(\backslash\)n"{}},}
\DoxyCodeLine{00652\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ lineno);}
\DoxyCodeLine{00653\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ parse\_error;}
\DoxyCodeLine{00654\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00655\ }
\DoxyCodeLine{00656\ \ \ \ \ \ \ \ \ \ \ \ \ p\ =\ atof\_c(wordptr[3]);}
\DoxyCodeLine{00657\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((p\ <=\ 0.0)\ ||\ (p\ >\ 1.0))\ \{}
\DoxyCodeLine{00658\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ E\_ERROR}
\DoxyCodeLine{00659\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (\textcolor{stringliteral}{"{}Line[\%d]:\ transition\ spec\ malformed;\ Expecting\ float\ as\ transition\ probability\(\backslash\)n"{}},}
\DoxyCodeLine{00660\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ lineno);}
\DoxyCodeLine{00661\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ parse\_error;}
\DoxyCodeLine{00662\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00663\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00664\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00665\ \ \ \ \ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Line[\%d]:\ transition\ or\ FSG\_END\ statement\ expected\(\backslash\)n"{}},}
\DoxyCodeLine{00666\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ lineno);}
\DoxyCodeLine{00667\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ parse\_error;}
\DoxyCodeLine{00668\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00669\ }
\DoxyCodeLine{00670\ \ \ \ \ \ \ \ \ tprob\ =\ (int32)\ (logmath\_log(lmath,\ p)\ *\ fsg-\/>lw);}
\DoxyCodeLine{00671\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Add\ word\ to\ "{}dictionary"{}.\ */}}
\DoxyCodeLine{00672\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (n\ >\ 4)\ \{}
\DoxyCodeLine{00673\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (hash\_table\_lookup\_int32(vocab,\ wordptr[4],\ \&wid)\ <\ 0)\ \{}
\DoxyCodeLine{00674\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (void)\ hash\_table\_enter\_int32(vocab,}
\DoxyCodeLine{00675\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ckd\_salloc(wordptr[4]),}
\DoxyCodeLine{00676\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ lastwid);}
\DoxyCodeLine{00677\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ wid\ =\ lastwid;}
\DoxyCodeLine{00678\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ++lastwid;}
\DoxyCodeLine{00679\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00680\ \ \ \ \ \ \ \ \ \ \ \ \ fsg\_model\_trans\_add(fsg,\ i,\ j,\ tprob,\ wid);}
\DoxyCodeLine{00681\ \ \ \ \ \ \ \ \ \ \ \ \ ++n\_trans;}
\DoxyCodeLine{00682\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00683\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00684\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (fsg\_model\_null\_trans\_add(fsg,\ i,\ j,\ tprob)\ ==\ 1)\ \{}
\DoxyCodeLine{00685\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ++n\_null\_trans;}
\DoxyCodeLine{00686\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ nulls\ =}
\DoxyCodeLine{00687\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ glist\_add\_ptr(nulls,\ fsg\_model\_null\_trans(fsg,\ i,\ j));}
\DoxyCodeLine{00688\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00689\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00690\ \ \ \ \ \}}
\DoxyCodeLine{00691\ }
\DoxyCodeLine{00692\ \ \ \ \ E\_INFO(\textcolor{stringliteral}{"{}FSG:\ \%d\ states,\ \%d\ unique\ words,\ \%d\ transitions\ (\%d\ null)\(\backslash\)n"{}},}
\DoxyCodeLine{00693\ \ \ \ \ \ \ \ \ \ \ \ fsg-\/>n\_state,\ hash\_table\_inuse(vocab),\ n\_trans,\ n\_null\_trans);}
\DoxyCodeLine{00694\ }
\DoxyCodeLine{00695\ }
\DoxyCodeLine{00696\ \ \ \ \ \textcolor{comment}{/*\ Now\ create\ a\ string\ table\ from\ the\ "{}dictionary"{}\ */}}
\DoxyCodeLine{00697\ \ \ \ \ fsg-\/>n\_word\ =\ hash\_table\_inuse(vocab);}
\DoxyCodeLine{00698\ \ \ \ \ fsg-\/>n\_word\_alloc\ =\ fsg-\/>n\_word\ +\ 10;\ \ \ \ \ \ \ \textcolor{comment}{/*\ Pad\ it\ a\ bit.\ */}}
\DoxyCodeLine{00699\ \ \ \ \ fsg-\/>vocab\ =\ ckd\_calloc(fsg-\/>n\_word\_alloc,\ \textcolor{keyword}{sizeof}(*fsg-\/>vocab));}
\DoxyCodeLine{00700\ \ \ \ \ \textcolor{keywordflow}{for}\ (itor\ =\ hash\_table\_iter(vocab);\ itor;}
\DoxyCodeLine{00701\ \ \ \ \ \ \ \ \ \ itor\ =\ hash\_table\_iter\_next(itor))\ \{}
\DoxyCodeLine{00702\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *word\ =\ hash\_entry\_key(itor-\/>ent);}
\DoxyCodeLine{00703\ \ \ \ \ \ \ \ \ int32\ wid\ =\ (int32)\ (\textcolor{keywordtype}{long})\ hash\_entry\_val(itor-\/>ent);}
\DoxyCodeLine{00704\ \ \ \ \ \ \ \ \ fsg-\/>vocab[wid]\ =\ (\textcolor{keywordtype}{char}\ *)\ word;}
\DoxyCodeLine{00705\ \ \ \ \ \}}
\DoxyCodeLine{00706\ \ \ \ \ hash\_table\_free(vocab);}
\DoxyCodeLine{00707\ }
\DoxyCodeLine{00708\ \ \ \ \ \textcolor{comment}{/*\ Do\ transitive\ closure\ on\ null\ transitions\ */}}
\DoxyCodeLine{00709\ \ \ \ \ nulls\ =\ fsg\_model\_null\_trans\_closure(fsg,\ nulls);}
\DoxyCodeLine{00710\ \ \ \ \ glist\_free(nulls);}
\DoxyCodeLine{00711\ }
\DoxyCodeLine{00712\ \ \ \ \ ckd\_free(lineptr);}
\DoxyCodeLine{00713\ \ \ \ \ ckd\_free(wordptr);}
\DoxyCodeLine{00714\ }
\DoxyCodeLine{00715\ \ \ \ \ \textcolor{keywordflow}{return}\ fsg;}
\DoxyCodeLine{00716\ }
\DoxyCodeLine{00717\ \ \ parse\_error:}
\DoxyCodeLine{00718\ \ \ \ \ \textcolor{keywordflow}{for}\ (itor\ =\ hash\_table\_iter(vocab);\ itor;}
\DoxyCodeLine{00719\ \ \ \ \ \ \ \ \ \ itor\ =\ hash\_table\_iter\_next(itor))}
\DoxyCodeLine{00720\ \ \ \ \ \ \ \ \ ckd\_free((\textcolor{keywordtype}{char}\ *)\ hash\_entry\_key(itor-\/>ent));}
\DoxyCodeLine{00721\ \ \ \ \ glist\_free(nulls);}
\DoxyCodeLine{00722\ \ \ \ \ hash\_table\_free(vocab);}
\DoxyCodeLine{00723\ \ \ \ \ ckd\_free(fsgname);}
\DoxyCodeLine{00724\ \ \ \ \ ckd\_free(lineptr);}
\DoxyCodeLine{00725\ \ \ \ \ ckd\_free(wordptr);}
\DoxyCodeLine{00726\ \ \ \ \ fsg\_model\_free(fsg);}
\DoxyCodeLine{00727\ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00728\ \}}
\DoxyCodeLine{00729\ }
\DoxyCodeLine{00730\ }
\DoxyCodeLine{00731\ fsg\_model\_t\ *}
\DoxyCodeLine{00732\ fsg\_model\_readfile(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *file,\ logmath\_t\ *\ lmath,\ float32\ lw)}
\DoxyCodeLine{00733\ \{}
\DoxyCodeLine{00734\ \ \ \ \ FILE\ *fp;}
\DoxyCodeLine{00735\ \ \ \ \ fsg\_model\_t\ *fsg;}
\DoxyCodeLine{00736\ }
\DoxyCodeLine{00737\ \ \ \ \ \textcolor{keywordflow}{if}\ ((fp\ =\ fopen(file,\ \textcolor{stringliteral}{"{}r"{}}))\ ==\ NULL)\ \{}
\DoxyCodeLine{00738\ \ \ \ \ \ \ \ \ E\_ERROR\_SYSTEM(\textcolor{stringliteral}{"{}Failed\ to\ open\ FSG\ file\ '\%s'\ for\ reading"{}},\ file);}
\DoxyCodeLine{00739\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00740\ \ \ \ \ \}}
\DoxyCodeLine{00741\ \ \ \ \ fsg\ =\ fsg\_model\_read(fp,\ lmath,\ lw);}
\DoxyCodeLine{00742\ \ \ \ \ fclose(fp);}
\DoxyCodeLine{00743\ \ \ \ \ \textcolor{keywordflow}{return}\ fsg;}
\DoxyCodeLine{00744\ \}}
\DoxyCodeLine{00745\ }
\DoxyCodeLine{00746\ fsg\_model\_t\ *}
\DoxyCodeLine{00747\ fsg\_model\_retain(fsg\_model\_t\ *\ fsg)}
\DoxyCodeLine{00748\ \{}
\DoxyCodeLine{00749\ \ \ \ \ ++fsg-\/>refcount;}
\DoxyCodeLine{00750\ \ \ \ \ \textcolor{keywordflow}{return}\ fsg;}
\DoxyCodeLine{00751\ \}}
\DoxyCodeLine{00752\ }
\DoxyCodeLine{00753\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00754\ trans\_list\_free(fsg\_model\_t\ *\ fsg,\ int32\ i)}
\DoxyCodeLine{00755\ \{}
\DoxyCodeLine{00756\ \ \ \ \ hash\_iter\_t\ *itor;}
\DoxyCodeLine{00757\ }
\DoxyCodeLine{00758\ \ \ \ \ \textcolor{comment}{/*\ FIXME\ (maybe):\ FSG\ links\ will\ all\ get\ freed\ when\ we\ call}}
\DoxyCodeLine{00759\ \textcolor{comment}{\ \ \ \ \ *\ listelem\_alloc\_free()\ so\ don't\ bother\ freeing\ them\ explicitly}}
\DoxyCodeLine{00760\ \textcolor{comment}{\ \ \ \ \ *\ here.\ */}}
\DoxyCodeLine{00761\ \ \ \ \ \textcolor{keywordflow}{if}\ (fsg-\/>trans[i].trans)\ \{}
\DoxyCodeLine{00762\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (itor\ =\ hash\_table\_iter(fsg-\/>trans[i].trans);}
\DoxyCodeLine{00763\ \ \ \ \ \ \ \ \ \ \ \ \ \ itor;\ itor\ =\ hash\_table\_iter\_next(itor))\ \{}
\DoxyCodeLine{00764\ \ \ \ \ \ \ \ \ \ \ \ \ glist\_t\ gl\ =\ (glist\_t)\ hash\_entry\_val(itor-\/>ent);}
\DoxyCodeLine{00765\ \ \ \ \ \ \ \ \ \ \ \ \ glist\_free(gl);}
\DoxyCodeLine{00766\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00767\ \ \ \ \ \}}
\DoxyCodeLine{00768\ \ \ \ \ hash\_table\_free(fsg-\/>trans[i].trans);}
\DoxyCodeLine{00769\ \ \ \ \ hash\_table\_free(fsg-\/>trans[i].null\_trans);}
\DoxyCodeLine{00770\ \}}
\DoxyCodeLine{00771\ }
\DoxyCodeLine{00772\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00773\ fsg\_model\_free(fsg\_model\_t\ *\ fsg)}
\DoxyCodeLine{00774\ \{}
\DoxyCodeLine{00775\ \ \ \ \ \textcolor{keywordtype}{int}\ i;}
\DoxyCodeLine{00776\ }
\DoxyCodeLine{00777\ \ \ \ \ \textcolor{keywordflow}{if}\ (fsg\ ==\ NULL)}
\DoxyCodeLine{00778\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00779\ }
\DoxyCodeLine{00780\ \ \ \ \ \textcolor{keywordflow}{if}\ (-\/-\/fsg-\/>refcount\ >\ 0)}
\DoxyCodeLine{00781\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ fsg-\/>refcount;}
\DoxyCodeLine{00782\ }
\DoxyCodeLine{00783\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ fsg-\/>n\_word;\ ++i)}
\DoxyCodeLine{00784\ \ \ \ \ \ \ \ \ ckd\_free(fsg-\/>vocab[i]);}
\DoxyCodeLine{00785\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ fsg-\/>n\_state;\ ++i)}
\DoxyCodeLine{00786\ \ \ \ \ \ \ \ \ trans\_list\_free(fsg,\ i);}
\DoxyCodeLine{00787\ \ \ \ \ ckd\_free(fsg-\/>trans);}
\DoxyCodeLine{00788\ \ \ \ \ ckd\_free(fsg-\/>vocab);}
\DoxyCodeLine{00789\ \ \ \ \ listelem\_alloc\_free(fsg-\/>link\_alloc);}
\DoxyCodeLine{00790\ \ \ \ \ bitvec\_free(fsg-\/>silwords);}
\DoxyCodeLine{00791\ \ \ \ \ bitvec\_free(fsg-\/>altwords);}
\DoxyCodeLine{00792\ \ \ \ \ ckd\_free(fsg-\/>name);}
\DoxyCodeLine{00793\ \ \ \ \ ckd\_free(fsg);}
\DoxyCodeLine{00794\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00795\ \}}
\DoxyCodeLine{00796\ }
\DoxyCodeLine{00797\ }
\DoxyCodeLine{00798\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00799\ fsg\_model\_write(fsg\_model\_t\ *\ fsg,\ FILE\ *\ fp)}
\DoxyCodeLine{00800\ \{}
\DoxyCodeLine{00801\ \ \ \ \ int32\ i;}
\DoxyCodeLine{00802\ }
\DoxyCodeLine{00803\ \ \ \ \ fprintf(fp,\ \textcolor{stringliteral}{"{}\%s\ \%s\(\backslash\)n"{}},\ FSG\_MODEL\_BEGIN\_DECL,}
\DoxyCodeLine{00804\ \ \ \ \ \ \ \ \ \ \ \ \ fsg-\/>name\ ?\ fsg-\/>name\ :\ \textcolor{stringliteral}{"{}"{}});}
\DoxyCodeLine{00805\ \ \ \ \ fprintf(fp,\ \textcolor{stringliteral}{"{}\%s\ \%d\(\backslash\)n"{}},\ FSG\_MODEL\_NUM\_STATES\_DECL,\ fsg-\/>n\_state);}
\DoxyCodeLine{00806\ \ \ \ \ fprintf(fp,\ \textcolor{stringliteral}{"{}\%s\ \%d\(\backslash\)n"{}},\ FSG\_MODEL\_START\_STATE\_DECL,\ fsg-\/>start\_state);}
\DoxyCodeLine{00807\ \ \ \ \ fprintf(fp,\ \textcolor{stringliteral}{"{}\%s\ \%d\(\backslash\)n"{}},\ FSG\_MODEL\_FINAL\_STATE\_DECL,\ fsg-\/>final\_state);}
\DoxyCodeLine{00808\ }
\DoxyCodeLine{00809\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ fsg-\/>n\_state;\ i++)\ \{}
\DoxyCodeLine{00810\ \ \ \ \ \ \ \ \ fsg\_arciter\_t\ *itor;}
\DoxyCodeLine{00811\ }
\DoxyCodeLine{00812\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (itor\ =\ fsg\_model\_arcs(fsg,\ i);\ itor;}
\DoxyCodeLine{00813\ \ \ \ \ \ \ \ \ \ \ \ \ \ itor\ =\ fsg\_arciter\_next(itor))\ \{}
\DoxyCodeLine{00814\ \ \ \ \ \ \ \ \ \ \ \ \ fsg\_link\_t\ *tl\ =\ fsg\_arciter\_get(itor);}
\DoxyCodeLine{00815\ }
\DoxyCodeLine{00816\ \ \ \ \ \ \ \ \ \ \ \ \ fprintf(fp,\ \textcolor{stringliteral}{"{}\%s\ \%d\ \%d\ \%f\ \%s\(\backslash\)n"{}},\ FSG\_MODEL\_TRANSITION\_DECL,}
\DoxyCodeLine{00817\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ tl-\/>from\_state,\ tl-\/>to\_state,}
\DoxyCodeLine{00818\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ logmath\_exp(fsg-\/>lmath,}
\DoxyCodeLine{00819\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (int32)\ (tl-\/>logs2prob\ /\ fsg-\/>lw)),}
\DoxyCodeLine{00820\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (tl-\/>wid\ <\ 0)\ ?\ \textcolor{stringliteral}{"{}"{}}\ :\ fsg\_model\_word\_str(fsg,\ tl-\/>wid));}
\DoxyCodeLine{00821\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00822\ \ \ \ \ \}}
\DoxyCodeLine{00823\ }
\DoxyCodeLine{00824\ \ \ \ \ fprintf(fp,\ \textcolor{stringliteral}{"{}\%s\(\backslash\)n"{}},\ FSG\_MODEL\_END\_DECL);}
\DoxyCodeLine{00825\ }
\DoxyCodeLine{00826\ \ \ \ \ fflush(fp);}
\DoxyCodeLine{00827\ \}}
\DoxyCodeLine{00828\ }
\DoxyCodeLine{00829\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00830\ fsg\_model\_writefile(fsg\_model\_t\ *\ fsg,\ \textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *file)}
\DoxyCodeLine{00831\ \{}
\DoxyCodeLine{00832\ \ \ \ \ FILE\ *fp;}
\DoxyCodeLine{00833\ }
\DoxyCodeLine{00834\ \ \ \ \ assert(fsg);}
\DoxyCodeLine{00835\ }
\DoxyCodeLine{00836\ \ \ \ \ E\_INFO(\textcolor{stringliteral}{"{}Writing\ FSG\ file\ '\%s'\(\backslash\)n"{}},\ file);}
\DoxyCodeLine{00837\ }
\DoxyCodeLine{00838\ \ \ \ \ \textcolor{keywordflow}{if}\ ((fp\ =\ fopen(file,\ \textcolor{stringliteral}{"{}w"{}}))\ ==\ NULL)\ \{}
\DoxyCodeLine{00839\ \ \ \ \ \ \ \ \ E\_ERROR\_SYSTEM(\textcolor{stringliteral}{"{}Failed\ to\ open\ FSG\ file\ '\%s'\ for\ reading"{}},\ file);}
\DoxyCodeLine{00840\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return};}
\DoxyCodeLine{00841\ \ \ \ \ \}}
\DoxyCodeLine{00842\ }
\DoxyCodeLine{00843\ \ \ \ \ fsg\_model\_write(fsg,\ fp);}
\DoxyCodeLine{00844\ }
\DoxyCodeLine{00845\ \ \ \ \ fclose(fp);}
\DoxyCodeLine{00846\ \}}
\DoxyCodeLine{00847\ }
\DoxyCodeLine{00848\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00849\ fsg\_model\_write\_fsm\_trans(fsg\_model\_t\ *\ fsg,\ \textcolor{keywordtype}{int}\ i,\ FILE\ *\ fp)}
\DoxyCodeLine{00850\ \{}
\DoxyCodeLine{00851\ \ \ \ \ fsg\_arciter\_t\ *itor;}
\DoxyCodeLine{00852\ }
\DoxyCodeLine{00853\ \ \ \ \ \textcolor{keywordflow}{for}\ (itor\ =\ fsg\_model\_arcs(fsg,\ i);\ itor;}
\DoxyCodeLine{00854\ \ \ \ \ \ \ \ \ \ itor\ =\ fsg\_arciter\_next(itor))\ \{}
\DoxyCodeLine{00855\ \ \ \ \ \ \ \ \ fsg\_link\_t\ *tl\ =\ fsg\_arciter\_get(itor);}
\DoxyCodeLine{00856\ \ \ \ \ \ \ \ \ fprintf(fp,\ \textcolor{stringliteral}{"{}\%d\ \%d\ \%s\ \%f\(\backslash\)n"{}},}
\DoxyCodeLine{00857\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ tl-\/>from\_state,\ tl-\/>to\_state,}
\DoxyCodeLine{00858\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (tl-\/>wid\ <\ 0)\ ?\ \textcolor{stringliteral}{"{}<eps>"{}}\ :\ fsg\_model\_word\_str(fsg,\ tl-\/>wid),}
\DoxyCodeLine{00859\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ -\/logmath\_log\_to\_ln(fsg-\/>lmath,\ tl-\/>logs2prob\ /\ fsg-\/>lw));}
\DoxyCodeLine{00860\ \ \ \ \ \}}
\DoxyCodeLine{00861\ \}}
\DoxyCodeLine{00862\ }
\DoxyCodeLine{00863\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00864\ fsg\_model\_write\_fsm(fsg\_model\_t\ *\ fsg,\ FILE\ *\ fp)}
\DoxyCodeLine{00865\ \{}
\DoxyCodeLine{00866\ \ \ \ \ \textcolor{keywordtype}{int}\ i;}
\DoxyCodeLine{00867\ }
\DoxyCodeLine{00868\ \ \ \ \ \textcolor{comment}{/*\ Write\ transitions\ from\ initial\ state\ first.\ */}}
\DoxyCodeLine{00869\ \ \ \ \ fsg\_model\_write\_fsm\_trans(fsg,\ fsg\_model\_start\_state(fsg),\ fp);}
\DoxyCodeLine{00870\ }
\DoxyCodeLine{00871\ \ \ \ \ \textcolor{comment}{/*\ Other\ states.\ */}}
\DoxyCodeLine{00872\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ fsg-\/>n\_state;\ i++)\ \{}
\DoxyCodeLine{00873\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (i\ ==\ fsg\_model\_start\_state(fsg))}
\DoxyCodeLine{00874\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{continue};}
\DoxyCodeLine{00875\ \ \ \ \ \ \ \ \ fsg\_model\_write\_fsm\_trans(fsg,\ i,\ fp);}
\DoxyCodeLine{00876\ \ \ \ \ \}}
\DoxyCodeLine{00877\ }
\DoxyCodeLine{00878\ \ \ \ \ \textcolor{comment}{/*\ Final\ state.\ */}}
\DoxyCodeLine{00879\ \ \ \ \ fprintf(fp,\ \textcolor{stringliteral}{"{}\%d\ 0\(\backslash\)n"{}},\ fsg\_model\_final\_state(fsg));}
\DoxyCodeLine{00880\ }
\DoxyCodeLine{00881\ \ \ \ \ fflush(fp);}
\DoxyCodeLine{00882\ \}}
\DoxyCodeLine{00883\ }
\DoxyCodeLine{00884\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00885\ fsg\_model\_writefile\_fsm(fsg\_model\_t\ *\ fsg,\ \textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *file)}
\DoxyCodeLine{00886\ \{}
\DoxyCodeLine{00887\ \ \ \ \ FILE\ *fp;}
\DoxyCodeLine{00888\ }
\DoxyCodeLine{00889\ \ \ \ \ assert(fsg);}
\DoxyCodeLine{00890\ }
\DoxyCodeLine{00891\ \ \ \ \ E\_INFO(\textcolor{stringliteral}{"{}Writing\ FSM\ file\ '\%s'\(\backslash\)n"{}},\ file);}
\DoxyCodeLine{00892\ }
\DoxyCodeLine{00893\ \ \ \ \ \textcolor{keywordflow}{if}\ ((fp\ =\ fopen(file,\ \textcolor{stringliteral}{"{}w"{}}))\ ==\ NULL)\ \{}
\DoxyCodeLine{00894\ \ \ \ \ \ \ \ \ E\_ERROR\_SYSTEM(\textcolor{stringliteral}{"{}Failed\ to\ open\ fsm\ file\ '\%s'\ for\ writing"{}},\ file);}
\DoxyCodeLine{00895\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return};}
\DoxyCodeLine{00896\ \ \ \ \ \}}
\DoxyCodeLine{00897\ }
\DoxyCodeLine{00898\ \ \ \ \ fsg\_model\_write\_fsm(fsg,\ fp);}
\DoxyCodeLine{00899\ }
\DoxyCodeLine{00900\ \ \ \ \ fclose(fp);}
\DoxyCodeLine{00901\ \}}
\DoxyCodeLine{00902\ }
\DoxyCodeLine{00903\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00904\ fsg\_model\_write\_symtab(fsg\_model\_t\ *\ fsg,\ FILE\ *\ file)}
\DoxyCodeLine{00905\ \{}
\DoxyCodeLine{00906\ \ \ \ \ \textcolor{keywordtype}{int}\ i;}
\DoxyCodeLine{00907\ }
\DoxyCodeLine{00908\ \ \ \ \ fprintf(file,\ \textcolor{stringliteral}{"{}<eps>\ 0\(\backslash\)n"{}});}
\DoxyCodeLine{00909\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ fsg\_model\_n\_word(fsg);\ ++i)\ \{}
\DoxyCodeLine{00910\ \ \ \ \ \ \ \ \ fprintf(file,\ \textcolor{stringliteral}{"{}\%s\ \%d\(\backslash\)n"{}},\ fsg\_model\_word\_str(fsg,\ i),\ i\ +\ 1);}
\DoxyCodeLine{00911\ \ \ \ \ \}}
\DoxyCodeLine{00912\ \ \ \ \ fflush(file);}
\DoxyCodeLine{00913\ \}}
\DoxyCodeLine{00914\ }
\DoxyCodeLine{00915\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00916\ fsg\_model\_writefile\_symtab(fsg\_model\_t\ *\ fsg,\ \textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *file)}
\DoxyCodeLine{00917\ \{}
\DoxyCodeLine{00918\ \ \ \ \ FILE\ *fp;}
\DoxyCodeLine{00919\ }
\DoxyCodeLine{00920\ \ \ \ \ assert(fsg);}
\DoxyCodeLine{00921\ }
\DoxyCodeLine{00922\ \ \ \ \ E\_INFO(\textcolor{stringliteral}{"{}Writing\ FSM\ symbol\ table\ '\%s'\(\backslash\)n"{}},\ file);}
\DoxyCodeLine{00923\ }
\DoxyCodeLine{00924\ \ \ \ \ \textcolor{keywordflow}{if}\ ((fp\ =\ fopen(file,\ \textcolor{stringliteral}{"{}w"{}}))\ ==\ NULL)\ \{}
\DoxyCodeLine{00925\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Failed\ to\ open\ symbol\ table\ '\%s'\ for\ writing"{}},\ file);}
\DoxyCodeLine{00926\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return};}
\DoxyCodeLine{00927\ \ \ \ \ \}}
\DoxyCodeLine{00928\ }
\DoxyCodeLine{00929\ \ \ \ \ fsg\_model\_write\_symtab(fsg,\ fp);}
\DoxyCodeLine{00930\ }
\DoxyCodeLine{00931\ \ \ \ \ fclose(fp);}
\DoxyCodeLine{00932\ \}}

\end{DoxyCode}
