\doxysection{lm3g\+\_\+model.\+c}
\label{lm3g__model_8c_source}\index{src/libsphinxbase/lm/lm3g\_model.c@{src/libsphinxbase/lm/lm3g\_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-\/2007\ 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}{\ *\ This\ work\ was\ supported\ in\ part\ by\ funding\ from\ the\ Defense\ Advanced\ }}
\DoxyCodeLine{00019\ \textcolor{comment}{\ *\ Research\ Projects\ Agency\ and\ the\ National\ Science\ Foundation\ of\ the\ }}
\DoxyCodeLine{00020\ \textcolor{comment}{\ *\ United\ States\ of\ America,\ and\ the\ CMU\ Sphinx\ Speech\ Consortium.}}
\DoxyCodeLine{00021\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00022\ \textcolor{comment}{\ *\ THIS\ SOFTWARE\ IS\ PROVIDED\ BY\ CARNEGIE\ MELLON\ UNIVERSITY\ \`{}\`{}AS\ IS''\ AND\ }}
\DoxyCodeLine{00023\ \textcolor{comment}{\ *\ ANY\ EXPRESSED\ OR\ IMPLIED\ WARRANTIES,\ INCLUDING,\ BUT\ NOT\ LIMITED\ TO,\ }}
\DoxyCodeLine{00024\ \textcolor{comment}{\ *\ THE\ IMPLIED\ WARRANTIES\ OF\ MERCHANTABILITY\ AND\ FITNESS\ FOR\ A\ PARTICULAR}}
\DoxyCodeLine{00025\ \textcolor{comment}{\ *\ PURPOSE\ ARE\ DISCLAIMED.\ \ IN\ NO\ EVENT\ SHALL\ CARNEGIE\ MELLON\ UNIVERSITY}}
\DoxyCodeLine{00026\ \textcolor{comment}{\ *\ NOR\ ITS\ EMPLOYEES\ BE\ LIABLE\ FOR\ ANY\ DIRECT,\ INDIRECT,\ INCIDENTAL,}}
\DoxyCodeLine{00027\ \textcolor{comment}{\ *\ SPECIAL,\ EXEMPLARY,\ OR\ CONSEQUENTIAL\ DAMAGES\ (INCLUDING,\ BUT\ NOT\ }}
\DoxyCodeLine{00028\ \textcolor{comment}{\ *\ LIMITED\ TO,\ PROCUREMENT\ OF\ SUBSTITUTE\ GOODS\ OR\ SERVICES;\ LOSS\ OF\ USE,\ }}
\DoxyCodeLine{00029\ \textcolor{comment}{\ *\ DATA,\ OR\ PROFITS;\ OR\ BUSINESS\ INTERRUPTION)\ HOWEVER\ CAUSED\ AND\ ON\ ANY\ }}
\DoxyCodeLine{00030\ \textcolor{comment}{\ *\ THEORY\ OF\ LIABILITY,\ WHETHER\ IN\ CONTRACT,\ STRICT\ LIABILITY,\ OR\ TORT\ }}
\DoxyCodeLine{00031\ \textcolor{comment}{\ *\ (INCLUDING\ NEGLIGENCE\ OR\ OTHERWISE)\ ARISING\ IN\ ANY\ WAY\ OUT\ OF\ THE\ USE\ }}
\DoxyCodeLine{00032\ \textcolor{comment}{\ *\ OF\ THIS\ SOFTWARE,\ EVEN\ IF\ ADVISED\ OF\ THE\ POSSIBILITY\ OF\ SUCH\ DAMAGE.}}
\DoxyCodeLine{00033\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00034\ \textcolor{comment}{\ *\ ====================================================================}}
\DoxyCodeLine{00035\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00036\ \textcolor{comment}{\ */}}
\DoxyCodeLine{00037\ \textcolor{comment}{/*}}
\DoxyCodeLine{00038\ \textcolor{comment}{\ *\ \(\backslash\)file\ lm3g\_model.c\ Core\ Sphinx\ 3-\/gram\ code\ used\ in}}
\DoxyCodeLine{00039\ \textcolor{comment}{\ *\ DMP/DMP32/ARPA\ (for\ now)\ model\ code.}}
\DoxyCodeLine{00040\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00041\ \textcolor{comment}{\ *\ Author:\ A\ cast\ of\ thousands,\ probably.}}
\DoxyCodeLine{00042\ \textcolor{comment}{\ */}}
\DoxyCodeLine{00043\ \textcolor{preprocessor}{\#include\ <string.h>}}
\DoxyCodeLine{00044\ \textcolor{preprocessor}{\#include\ <assert.h>}}
\DoxyCodeLine{00045\ \textcolor{preprocessor}{\#include\ <limits.h>}}
\DoxyCodeLine{00046\ }
\DoxyCodeLine{00047\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/listelem\_alloc.h"{}}}
\DoxyCodeLine{00048\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/ckd\_alloc.h"{}}}
\DoxyCodeLine{00049\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/err.h"{}}}
\DoxyCodeLine{00050\ }
\DoxyCodeLine{00051\ \textcolor{preprocessor}{\#include\ "{}lm3g\_model.h"{}}}
\DoxyCodeLine{00052\ }
\DoxyCodeLine{00053\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00054\ lm3g\_tginfo\_free(ngram\_model\_t\ *base,\ lm3g\_model\_t\ *lm3g)}
\DoxyCodeLine{00055\ \{}
\DoxyCodeLine{00056\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (lm3g-\/>tginfo\ ==\ NULL)}
\DoxyCodeLine{00057\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return};}
\DoxyCodeLine{00058\ \ \ \ \ \ \ \ \ listelem\_alloc\_free(lm3g-\/>le);}
\DoxyCodeLine{00059\ \ \ \ \ \ \ \ \ ckd\_free(lm3g-\/>tginfo);}
\DoxyCodeLine{00060\ \}}
\DoxyCodeLine{00061\ }
\DoxyCodeLine{00062\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00063\ lm3g\_tginfo\_reset(ngram\_model\_t\ *base,\ lm3g\_model\_t\ *lm3g)}
\DoxyCodeLine{00064\ \{}
\DoxyCodeLine{00065\ \ \ \ \ \textcolor{keywordflow}{if}\ (lm3g-\/>tginfo\ ==\ NULL)}
\DoxyCodeLine{00066\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return};}
\DoxyCodeLine{00067\ \ \ \ \ listelem\_alloc\_free(lm3g-\/>le);}
\DoxyCodeLine{00068\ \ \ \ \ memset(lm3g-\/>tginfo,\ 0,\ base-\/>n\_counts[0]\ *\ \textcolor{keyword}{sizeof}(tginfo\_t\ *));}
\DoxyCodeLine{00069\ \ \ \ \ lm3g-\/>le\ =\ listelem\_alloc\_init(\textcolor{keyword}{sizeof}(tginfo\_t));}
\DoxyCodeLine{00070\ \}}
\DoxyCodeLine{00071\ }
\DoxyCodeLine{00072\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00073\ lm3g\_apply\_weights(ngram\_model\_t\ *base,}
\DoxyCodeLine{00074\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ lm3g\_model\_t\ *lm3g,}
\DoxyCodeLine{00075\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ float32\ lw,\ float32\ wip,\ float32\ uw)}
\DoxyCodeLine{00076\ \{}
\DoxyCodeLine{00077\ \ \ \ \ int32\ log\_wip,\ log\_uw,\ log\_uniform\_weight;}
\DoxyCodeLine{00078\ \ \ \ \ \textcolor{keywordtype}{int}\ i;}
\DoxyCodeLine{00079\ }
\DoxyCodeLine{00080\ \ \ \ \ \textcolor{comment}{/*\ Precalculate\ some\ log\ values\ we\ will\ like.\ */}}
\DoxyCodeLine{00081\ \ \ \ \ log\_wip\ =\ logmath\_log(base-\/>lmath,\ wip);}
\DoxyCodeLine{00082\ \ \ \ \ log\_uw\ =\ logmath\_log(base-\/>lmath,\ uw);}
\DoxyCodeLine{00083\ \ \ \ \ log\_uniform\_weight\ =\ logmath\_log(base-\/>lmath,\ 1.0\ -\/\ uw);}
\DoxyCodeLine{00084\ }
\DoxyCodeLine{00085\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ base-\/>n\_counts[0];\ ++i)\ \{}
\DoxyCodeLine{00086\ \ \ \ \ \ \ \ \ int32\ prob1,\ bo\_wt,\ n\_used;}
\DoxyCodeLine{00087\ }
\DoxyCodeLine{00088\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Backoff\ weights\ just\ get\ scaled\ by\ the\ lw.\ */}}
\DoxyCodeLine{00089\ \ \ \ \ \ \ \ \ bo\_wt\ =\ (int32)(lm3g-\/>unigrams[i].bo\_wt1.l\ /\ base-\/>lw);}
\DoxyCodeLine{00090\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Unscaling\ unigram\ probs\ is\ a\ bit\ more\ complicated,\ so\ punt}}
\DoxyCodeLine{00091\ \textcolor{comment}{\ \ \ \ \ \ \ \ \ *\ it\ back\ to\ the\ general\ code.\ */}}
\DoxyCodeLine{00092\ \ \ \ \ \ \ \ \ prob1\ =\ ngram\_ng\_prob(base,\ i,\ NULL,\ 0,\ \&n\_used);}
\DoxyCodeLine{00093\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Now\ compute\ the\ new\ scaled\ probabilities.\ */}}
\DoxyCodeLine{00094\ \ \ \ \ \ \ \ \ lm3g-\/>unigrams[i].bo\_wt1.l\ =\ (int32)(bo\_wt\ *\ lw);}
\DoxyCodeLine{00095\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (strcmp(base-\/>word\_str[i],\ \textcolor{stringliteral}{"{}<s>"{}})\ ==\ 0)\ \{\ \textcolor{comment}{/*\ FIXME:\ configurable\ start\_sym\ */}}
\DoxyCodeLine{00096\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Apply\ language\ weight\ and\ WIP\ */}}
\DoxyCodeLine{00097\ \ \ \ \ \ \ \ \ \ \ \ \ lm3g-\/>unigrams[i].prob1.l\ =\ (int32)(prob1\ *\ lw)\ +\ log\_wip;}
\DoxyCodeLine{00098\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00099\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00100\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Interpolate\ unigram\ probability\ with\ uniform.\ */}}
\DoxyCodeLine{00101\ \ \ \ \ \ \ \ \ \ \ \ \ prob1\ +=\ log\_uw;}
\DoxyCodeLine{00102\ \ \ \ \ \ \ \ \ \ \ \ \ prob1\ =\ logmath\_add(base-\/>lmath,\ prob1,\ base-\/>log\_uniform\ +\ log\_uniform\_weight);}
\DoxyCodeLine{00103\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Apply\ language\ weight\ and\ WIP\ */}}
\DoxyCodeLine{00104\ \ \ \ \ \ \ \ \ \ \ \ \ lm3g-\/>unigrams[i].prob1.l\ =\ (int32)(prob1\ *\ lw)\ +\ log\_wip;}
\DoxyCodeLine{00105\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00106\ \ \ \ \ \}}
\DoxyCodeLine{00107\ }
\DoxyCodeLine{00108\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ lm3g-\/>n\_prob2;\ ++i)\ \{}
\DoxyCodeLine{00109\ \ \ \ \ \ \ \ \ int32\ prob2;}
\DoxyCodeLine{00110\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Can't\ just\ punt\ this\ back\ to\ general\ code\ since\ it\ is\ quantized.\ */}}
\DoxyCodeLine{00111\ \ \ \ \ \ \ \ \ prob2\ =\ (int32)((lm3g-\/>prob2[i].l\ -\/\ base-\/>log\_wip)\ /\ base-\/>lw);}
\DoxyCodeLine{00112\ \ \ \ \ \ \ \ \ lm3g-\/>prob2[i].l\ =\ (int32)(prob2\ *\ lw)\ +\ log\_wip;}
\DoxyCodeLine{00113\ \ \ \ \ \}}
\DoxyCodeLine{00114\ }
\DoxyCodeLine{00115\ \ \ \ \ \textcolor{keywordflow}{if}\ (base-\/>n\ >\ 2)\ \{}
\DoxyCodeLine{00116\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ lm3g-\/>n\_bo\_wt2;\ ++i)\ \{}
\DoxyCodeLine{00117\ \ \ \ \ \ \ \ \ \ \ \ \ lm3g-\/>bo\_wt2[i].l\ =\ (int32)(lm3g-\/>bo\_wt2[i].l\ \ /\ base-\/>lw\ *\ lw);}
\DoxyCodeLine{00118\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00119\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ lm3g-\/>n\_prob3;\ i++)\ \{}
\DoxyCodeLine{00120\ \ \ \ \ \ \ \ \ \ \ \ \ int32\ prob3;}
\DoxyCodeLine{00121\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Can't\ just\ punt\ this\ back\ to\ general\ code\ since\ it\ is\ quantized.\ */}}
\DoxyCodeLine{00122\ \ \ \ \ \ \ \ \ \ \ \ \ prob3\ =\ (int32)((lm3g-\/>prob3[i].l\ -\/\ base-\/>log\_wip)\ /\ base-\/>lw);}
\DoxyCodeLine{00123\ \ \ \ \ \ \ \ \ \ \ \ \ lm3g-\/>prob3[i].l\ =\ (int32)(prob3\ *\ lw)\ +\ log\_wip;}
\DoxyCodeLine{00124\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00125\ \ \ \ \ \}}
\DoxyCodeLine{00126\ }
\DoxyCodeLine{00127\ \ \ \ \ \textcolor{comment}{/*\ Store\ updated\ values\ in\ the\ model.\ */}}
\DoxyCodeLine{00128\ \ \ \ \ base-\/>log\_wip\ =\ log\_wip;}
\DoxyCodeLine{00129\ \ \ \ \ base-\/>log\_uw\ =\ log\_uw;}
\DoxyCodeLine{00130\ \ \ \ \ base-\/>log\_uniform\_weight\ =\ log\_uniform\_weight;}
\DoxyCodeLine{00131\ \ \ \ \ base-\/>lw\ =\ lw;}
\DoxyCodeLine{00132\ \}}
\DoxyCodeLine{00133\ }
\DoxyCodeLine{00134\ int32}
\DoxyCodeLine{00135\ lm3g\_add\_ug(ngram\_model\_t\ *base,}
\DoxyCodeLine{00136\ \ \ \ \ \ \ \ \ \ \ \ \ lm3g\_model\_t\ *lm3g,\ int32\ wid,\ int32\ lweight)}
\DoxyCodeLine{00137\ \{}
\DoxyCodeLine{00138\ \ \ \ \ int32\ score;}
\DoxyCodeLine{00139\ }
\DoxyCodeLine{00140\ \ \ \ \ \textcolor{comment}{/*\ This\ would\ be\ very\ bad\ if\ this\ happened!\ */}}
\DoxyCodeLine{00141\ \ \ \ \ assert(!NGRAM\_IS\_CLASSWID(wid));}
\DoxyCodeLine{00142\ }
\DoxyCodeLine{00143\ \ \ \ \ \textcolor{comment}{/*\ Reallocate\ unigram\ array.\ */}}
\DoxyCodeLine{00144\ \ \ \ \ lm3g-\/>unigrams\ =\ ckd\_realloc(lm3g-\/>unigrams,}
\DoxyCodeLine{00145\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{sizeof}(*lm3g-\/>unigrams)\ *\ base-\/>n\_1g\_alloc);}
\DoxyCodeLine{00146\ \ \ \ \ memset(lm3g-\/>unigrams\ +\ base-\/>n\_counts[0],\ 0,}
\DoxyCodeLine{00147\ \ \ \ \ \ \ \ \ \ \ \ (base-\/>n\_1g\_alloc\ -\/\ base-\/>n\_counts[0])\ *\ \textcolor{keyword}{sizeof}(*lm3g-\/>unigrams));}
\DoxyCodeLine{00148\ \ \ \ \ \textcolor{comment}{/*\ Reallocate\ tginfo\ array.\ */}}
\DoxyCodeLine{00149\ \ \ \ \ lm3g-\/>tginfo\ =\ ckd\_realloc(lm3g-\/>tginfo,}
\DoxyCodeLine{00150\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{sizeof}(*lm3g-\/>tginfo)\ *\ base-\/>n\_1g\_alloc);}
\DoxyCodeLine{00151\ \ \ \ \ memset(lm3g-\/>tginfo\ +\ base-\/>n\_counts[0],\ 0,}
\DoxyCodeLine{00152\ \ \ \ \ \ \ \ \ \ \ \ (base-\/>n\_1g\_alloc\ -\/\ base-\/>n\_counts[0])\ *\ \textcolor{keyword}{sizeof}(*lm3g-\/>tginfo));}
\DoxyCodeLine{00153\ \ \ \ \ \textcolor{comment}{/*\ FIXME:\ we\ really\ ought\ to\ update\ base-\/>log\_uniform\ *and*}}
\DoxyCodeLine{00154\ \textcolor{comment}{\ \ \ \ \ *\ renormalize\ all\ the\ other\ unigrams.\ \ This\ is\ really\ slow,\ so\ I}}
\DoxyCodeLine{00155\ \textcolor{comment}{\ \ \ \ \ *\ will\ probably\ just\ provide\ a\ function\ to\ renormalize\ after}}
\DoxyCodeLine{00156\ \textcolor{comment}{\ \ \ \ \ *\ adding\ unigrams,\ for\ anyone\ who\ really\ cares.\ */}}
\DoxyCodeLine{00157\ \ \ \ \ \textcolor{comment}{/*\ This\ could\ be\ simplified\ but\ then\ we\ couldn't\ do\ it\ in\ logmath\ */}}
\DoxyCodeLine{00158\ \ \ \ \ score\ =\ lweight\ +\ base-\/>log\_uniform\ +\ base-\/>log\_uw;}
\DoxyCodeLine{00159\ \ \ \ \ score\ =\ logmath\_add(base-\/>lmath,\ score,}
\DoxyCodeLine{00160\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ base-\/>log\_uniform\ +\ base-\/>log\_uniform\_weight);}
\DoxyCodeLine{00161\ \ \ \ \ lm3g-\/>unigrams[wid].prob1.l\ =\ score;}
\DoxyCodeLine{00162\ \ \ \ \ \textcolor{comment}{/*\ This\ unigram\ by\ definition\ doesn't\ participate\ in\ any\ bigrams,}}
\DoxyCodeLine{00163\ \textcolor{comment}{\ \ \ \ \ *\ so\ its\ backoff\ weight\ and\ bigram\ pointer\ are\ both\ undefined.\ */}}
\DoxyCodeLine{00164\ \ \ \ \ lm3g-\/>unigrams[wid].bo\_wt1.l\ =\ 0;}
\DoxyCodeLine{00165\ \ \ \ \ lm3g-\/>unigrams[wid].bigrams\ =\ 0;}
\DoxyCodeLine{00166\ \ \ \ \ \textcolor{comment}{/*\ Finally,\ increase\ the\ unigram\ count\ */}}
\DoxyCodeLine{00167\ \ \ \ \ ++base-\/>n\_counts[0];}
\DoxyCodeLine{00168\ \ \ \ \ \textcolor{comment}{/*\ FIXME:\ Note\ that\ this\ can\ actually\ be\ quite\ bogus\ due\ to\ the}}
\DoxyCodeLine{00169\ \textcolor{comment}{\ \ \ \ \ *\ presence\ of\ class\ words.\ \ If\ wid\ falls\ outside\ the\ unigram}}
\DoxyCodeLine{00170\ \textcolor{comment}{\ \ \ \ \ *\ count,\ increase\ it\ to\ compensate,\ at\ the\ cost\ of\ no\ longer}}
\DoxyCodeLine{00171\ \textcolor{comment}{\ \ \ \ \ *\ really\ knowing\ how\ many\ unigrams\ we\ have\ :(\ */}}
\DoxyCodeLine{00172\ \ \ \ \ \textcolor{keywordflow}{if}\ (wid\ >=\ base-\/>n\_counts[0])}
\DoxyCodeLine{00173\ \ \ \ \ \ \ \ \ base-\/>n\_counts[0]\ =\ wid\ +\ 1;}
\DoxyCodeLine{00174\ }
\DoxyCodeLine{00175\ \ \ \ \ \textcolor{keywordflow}{return}\ score;}
\DoxyCodeLine{00176\ \}}
\DoxyCodeLine{00177\ }
\DoxyCodeLine{00178\ \textcolor{preprocessor}{\#define\ INITIAL\_SORTED\_ENTRIES\ \ MAX\_UINT16}}
\DoxyCodeLine{00179\ }
\DoxyCodeLine{00180\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00181\ init\_sorted\_list(sorted\_list\_t\ *\ l)}
\DoxyCodeLine{00182\ \{}
\DoxyCodeLine{00183\ \ \ \ \ l-\/>list\ =\ ckd\_calloc(INITIAL\_SORTED\_ENTRIES,\ \textcolor{keyword}{sizeof}(sorted\_entry\_t));}
\DoxyCodeLine{00184\ \ \ \ \ l-\/>list[0].val.l\ =\ INT\_MIN;}
\DoxyCodeLine{00185\ \ \ \ \ l-\/>list[0].lower\ =\ 0;}
\DoxyCodeLine{00186\ \ \ \ \ l-\/>list[0].higher\ =\ 0;}
\DoxyCodeLine{00187\ \ \ \ \ l-\/>free\ =\ 1;}
\DoxyCodeLine{00188\ \ \ \ \ l-\/>size\ =\ INITIAL\_SORTED\_ENTRIES;}
\DoxyCodeLine{00189\ \}}
\DoxyCodeLine{00190\ }
\DoxyCodeLine{00191\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00192\ free\_sorted\_list(sorted\_list\_t\ *\ l)}
\DoxyCodeLine{00193\ \{}
\DoxyCodeLine{00194\ \ \ \ \ free(l-\/>list);}
\DoxyCodeLine{00195\ \}}
\DoxyCodeLine{00196\ }
\DoxyCodeLine{00197\ lmprob\_t\ *}
\DoxyCodeLine{00198\ vals\_in\_sorted\_list(sorted\_list\_t\ *\ l)}
\DoxyCodeLine{00199\ \{}
\DoxyCodeLine{00200\ \ \ \ \ lmprob\_t\ *vals;}
\DoxyCodeLine{00201\ \ \ \ \ int32\ i;}
\DoxyCodeLine{00202\ }
\DoxyCodeLine{00203\ \ \ \ \ vals\ =\ ckd\_calloc(l-\/>free,\ \textcolor{keyword}{sizeof}(lmprob\_t));}
\DoxyCodeLine{00204\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ l-\/>free;\ i++)}
\DoxyCodeLine{00205\ \ \ \ \ \ \ \ \ vals[i]\ =\ l-\/>list[i].val;}
\DoxyCodeLine{00206\ \ \ \ \ \textcolor{keywordflow}{return}\ (vals);}
\DoxyCodeLine{00207\ \}}
\DoxyCodeLine{00208\ }
\DoxyCodeLine{00209\ int32}
\DoxyCodeLine{00210\ sorted\_id(sorted\_list\_t\ *\ l,\ int32\ *val)}
\DoxyCodeLine{00211\ \{}
\DoxyCodeLine{00212\ \ \ \ \ int32\ i\ =\ 0;}
\DoxyCodeLine{00213\ }
\DoxyCodeLine{00214\ \ \ \ \ \textcolor{keywordflow}{for}\ (;;)\ \{}
\DoxyCodeLine{00215\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (*val\ ==\ l-\/>list[i].val.l)}
\DoxyCodeLine{00216\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ (i);}
\DoxyCodeLine{00217\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (*val\ <\ l-\/>list[i].val.l)\ \{}
\DoxyCodeLine{00218\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (l-\/>list[i].lower\ ==\ 0)\ \{}
\DoxyCodeLine{00219\ }
\DoxyCodeLine{00220\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (l-\/>free\ >=\ l-\/>size)\ \{}
\DoxyCodeLine{00221\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{int}\ newsize\ =\ l-\/>size\ +\ INITIAL\_SORTED\_ENTRIES;}
\DoxyCodeLine{00222\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ l-\/>list\ =\ ckd\_realloc(l-\/>list,\ \textcolor{keyword}{sizeof}(sorted\_entry\_t)\ *\ newsize);}
\DoxyCodeLine{00223\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ memset(l-\/>list\ +\ l-\/>size,\ }
\DoxyCodeLine{00224\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 0,\ INITIAL\_SORTED\_ENTRIES\ *\ \textcolor{keyword}{sizeof}(sorted\_entry\_t));}
\DoxyCodeLine{00225\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ l-\/>size\ =\ newsize;}
\DoxyCodeLine{00226\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00227\ }
\DoxyCodeLine{00228\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ l-\/>list[i].lower\ =\ l-\/>free;}
\DoxyCodeLine{00229\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (l-\/>free)++;}
\DoxyCodeLine{00230\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ i\ =\ l-\/>list[i].lower;}
\DoxyCodeLine{00231\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ l-\/>list[i].val.l\ =\ *val;}
\DoxyCodeLine{00232\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ (i);}
\DoxyCodeLine{00233\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00234\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{00235\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ i\ =\ l-\/>list[i].lower;}
\DoxyCodeLine{00236\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00237\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00238\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (l-\/>list[i].higher\ ==\ 0)\ \{}
\DoxyCodeLine{00239\ }
\DoxyCodeLine{00240\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (l-\/>free\ >=\ l-\/>size)\ \{}
\DoxyCodeLine{00241\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{int}\ newsize\ =\ l-\/>size\ +\ INITIAL\_SORTED\_ENTRIES;}
\DoxyCodeLine{00242\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ l-\/>list\ =\ ckd\_realloc(l-\/>list,\ \textcolor{keyword}{sizeof}(sorted\_entry\_t)\ *\ newsize);}
\DoxyCodeLine{00243\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ memset(l-\/>list\ +\ l-\/>size,\ }
\DoxyCodeLine{00244\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 0,\ INITIAL\_SORTED\_ENTRIES\ *\ \textcolor{keyword}{sizeof}(sorted\_entry\_t));}
\DoxyCodeLine{00245\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ l-\/>size\ =\ newsize;}
\DoxyCodeLine{00246\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00247\ }
\DoxyCodeLine{00248\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ l-\/>list[i].higher\ =\ l-\/>free;}
\DoxyCodeLine{00249\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (l-\/>free)++;}
\DoxyCodeLine{00250\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ i\ =\ l-\/>list[i].higher;}
\DoxyCodeLine{00251\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ l-\/>list[i].val.l\ =\ *val;}
\DoxyCodeLine{00252\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ (i);}
\DoxyCodeLine{00253\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00254\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{00255\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ i\ =\ l-\/>list[i].higher;}
\DoxyCodeLine{00256\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00257\ \ \ \ \ \}}
\DoxyCodeLine{00258\ \}}

\end{DoxyCode}
