\doxysection{sphinx\+\_\+lm\+\_\+eval.\+c}
\label{sphinx__lm__eval_8c_source}\index{src/sphinx\_lmtools/sphinx\_lm\_eval.c@{src/sphinx\_lmtools/sphinx\_lm\_eval.c}}
\textbf{ Go to the documentation of this file.}
\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)\ 2008\ 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{00041\ \textcolor{preprocessor}{\#include\ <sphinxbase/logmath.h>}}
\DoxyCodeLine{00042\ \textcolor{preprocessor}{\#include\ <sphinxbase/ngram\_model.h>}}
\DoxyCodeLine{00043\ \textcolor{preprocessor}{\#include\ <sphinxbase/cmd\_ln.h>}}
\DoxyCodeLine{00044\ \textcolor{preprocessor}{\#include\ <sphinxbase/ckd\_alloc.h>}}
\DoxyCodeLine{00045\ \textcolor{preprocessor}{\#include\ <sphinxbase/err.h>}}
\DoxyCodeLine{00046\ \textcolor{preprocessor}{\#include\ <sphinxbase/pio.h>}}
\DoxyCodeLine{00047\ \textcolor{preprocessor}{\#include\ <sphinxbase/strfuncs.h>}}
\DoxyCodeLine{00048\ }
\DoxyCodeLine{00049\ \textcolor{preprocessor}{\#include\ <stdio.h>}}
\DoxyCodeLine{00050\ \textcolor{preprocessor}{\#include\ <string.h>}}
\DoxyCodeLine{00051\ \textcolor{preprocessor}{\#include\ <math.h>}}
\DoxyCodeLine{00052\ }
\DoxyCodeLine{00053\ \textcolor{keyword}{static}\ \textcolor{keyword}{const}\ arg\_t\ defn[]\ =\ \{}
\DoxyCodeLine{00054\ \ \ \{\ \textcolor{stringliteral}{"{}-\/help"{}},}
\DoxyCodeLine{00055\ \ \ \ \ ARG\_BOOLEAN,}
\DoxyCodeLine{00056\ \ \ \ \ \textcolor{stringliteral}{"{}no"{}},}
\DoxyCodeLine{00057\ \ \ \ \ \textcolor{stringliteral}{"{}Shows\ the\ usage\ of\ the\ tool"{}}\},}
\DoxyCodeLine{00058\ }
\DoxyCodeLine{00059\ \ \ \{\ \textcolor{stringliteral}{"{}-\/logbase"{}},}
\DoxyCodeLine{00060\ \ \ \ \ ARG\_FLOAT64,}
\DoxyCodeLine{00061\ \ \ \ \ \textcolor{stringliteral}{"{}1.0001"{}},}
\DoxyCodeLine{00062\ \ \ \ \ \textcolor{stringliteral}{"{}Base\ in\ which\ all\ log-\/likelihoods\ calculated"{}}\ \},}
\DoxyCodeLine{00063\ }
\DoxyCodeLine{00064\ \ \ \{\ \textcolor{stringliteral}{"{}-\/lm"{}},}
\DoxyCodeLine{00065\ \ \ \ \ ARG\_STRING,}
\DoxyCodeLine{00066\ \ \ \ \ NULL,}
\DoxyCodeLine{00067\ \ \ \ \ \textcolor{stringliteral}{"{}Language\ model\ file"{}}\},}
\DoxyCodeLine{00068\ }
\DoxyCodeLine{00069\ \ \ \{\ \textcolor{stringliteral}{"{}-\/probdef"{}},}
\DoxyCodeLine{00070\ \ \ \ \ ARG\_STRING,}
\DoxyCodeLine{00071\ \ \ \ \ NULL,}
\DoxyCodeLine{00072\ \ \ \ \ \textcolor{stringliteral}{"{}Probability\ definition\ file\ for\ classes\ in\ LM"{}}\},}
\DoxyCodeLine{00073\ }
\DoxyCodeLine{00074\ \ \ \{\ \textcolor{stringliteral}{"{}-\/lmctlfn"{}},}
\DoxyCodeLine{00075\ \ \ \ \ ARG\_STRING,}
\DoxyCodeLine{00076\ \ \ \ \ NULL,}
\DoxyCodeLine{00077\ \ \ \ \ \textcolor{stringliteral}{"{}Control\ file\ listing\ a\ set\ of\ language\ models"{}}\},}
\DoxyCodeLine{00078\ }
\DoxyCodeLine{00079\ \ \ \{\ \textcolor{stringliteral}{"{}-\/lmname"{}},}
\DoxyCodeLine{00080\ \ \ \ \ ARG\_STRING,}
\DoxyCodeLine{00081\ \ \ \ \ NULL,}
\DoxyCodeLine{00082\ \ \ \ \ \textcolor{stringliteral}{"{}Name\ of\ language\ model\ in\ -\/lmctlfn\ to\ use\ for\ all\ utterances"{}}\ \},}
\DoxyCodeLine{00083\ }
\DoxyCodeLine{00084\ \ \ \{\ \textcolor{stringliteral}{"{}-\/lsn"{}},}
\DoxyCodeLine{00085\ \ \ \ \ ARG\_STRING,}
\DoxyCodeLine{00086\ \ \ \ \ NULL,}
\DoxyCodeLine{00087\ \ \ \ \ \textcolor{stringliteral}{"{}Transcription\ file\ to\ evaluate"{}}\},}
\DoxyCodeLine{00088\ }
\DoxyCodeLine{00089\ \ \ \{\ \textcolor{stringliteral}{"{}-\/text"{}},}
\DoxyCodeLine{00090\ \ \ \ \ ARG\_STRING,}
\DoxyCodeLine{00091\ \ \ \ \ \textcolor{stringliteral}{"{}Text\ string\ to\ evaluate"{}}\},}
\DoxyCodeLine{00092\ }
\DoxyCodeLine{00093\ \ \ \{\ \textcolor{stringliteral}{"{}-\/mmap"{}},}
\DoxyCodeLine{00094\ \ \ \ \ ARG\_BOOLEAN,}
\DoxyCodeLine{00095\ \ \ \ \ \textcolor{stringliteral}{"{}no"{}},}
\DoxyCodeLine{00096\ \ \ \ \ \textcolor{stringliteral}{"{}Use\ memory-\/mapped\ I/O\ for\ reading\ binary\ LM\ files"{}}\},}
\DoxyCodeLine{00097\ }
\DoxyCodeLine{00098\ \ \ \{\ \textcolor{stringliteral}{"{}-\/lw"{}},}
\DoxyCodeLine{00099\ \ \ \ \ ARG\_FLOAT32,}
\DoxyCodeLine{00100\ \ \ \ \ \textcolor{stringliteral}{"{}1.0"{}},}
\DoxyCodeLine{00101\ \ \ \ \ \textcolor{stringliteral}{"{}Language\ model\ weight"{}}\ \},}
\DoxyCodeLine{00102\ }
\DoxyCodeLine{00103\ \ \ \{\ \textcolor{stringliteral}{"{}-\/wip"{}},}
\DoxyCodeLine{00104\ \ \ \ \ ARG\_FLOAT32,}
\DoxyCodeLine{00105\ \ \ \ \ \textcolor{stringliteral}{"{}1.0"{}},}
\DoxyCodeLine{00106\ \ \ \ \ \textcolor{stringliteral}{"{}Word\ insertion\ probability"{}}\ \},}
\DoxyCodeLine{00107\ }
\DoxyCodeLine{00108\ \ \ \{\ \textcolor{stringliteral}{"{}-\/uw"{}},}
\DoxyCodeLine{00109\ \ \ \ \ ARG\_FLOAT32,}
\DoxyCodeLine{00110\ \ \ \ \ \textcolor{stringliteral}{"{}1.0"{}},}
\DoxyCodeLine{00111\ \ \ \ \ \textcolor{stringliteral}{"{}Unigram\ probability\ weight\ (interpolated\ with\ uniform\ distribution)"{}}\},}
\DoxyCodeLine{00112\ }
\DoxyCodeLine{00113\ \ \ \{\ \textcolor{stringliteral}{"{}-\/verbose"{}},}
\DoxyCodeLine{00114\ \ \ \ \ ARG\_BOOLEAN,}
\DoxyCodeLine{00115\ \ \ \ \ \textcolor{stringliteral}{"{}no"{}},}
\DoxyCodeLine{00116\ \ \ \ \ \textcolor{stringliteral}{"{}Print\ details\ of\ perplexity\ calculation"{}}\ \},}
\DoxyCodeLine{00117\ }
\DoxyCodeLine{00118\ \ \ \textcolor{comment}{/*\ FIXME:\ Support\ -\/lmstartsym,\ -\/lmendsym,\ -\/lmctlfn,\ -\/ctl\_lm\ */}}
\DoxyCodeLine{00119\ \ \ \{\ NULL,\ 0,\ NULL,\ NULL\ \}}
\DoxyCodeLine{00120\ \};}
\DoxyCodeLine{00121\ }
\DoxyCodeLine{00122\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{int}\ verbose;}
\DoxyCodeLine{00123\ }
\DoxyCodeLine{00124\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00125\ calc\_entropy(ngram\_model\_t\ *lm,\ \textcolor{keywordtype}{char}\ **words,\ int32\ n,}
\DoxyCodeLine{00126\ \ \ \ \ \ \ \ \ \ \ \ \ \ int32\ *out\_n\_ccs,\ int32\ *out\_n\_oovs,\ int32\ *out\_lm\_score)}
\DoxyCodeLine{00127\ \{}
\DoxyCodeLine{00128\ \ \ \ \ \ \ \ \ int32\ *wids;}
\DoxyCodeLine{00129\ \ \ \ \ \ \ \ \ int32\ startwid;}
\DoxyCodeLine{00130\ \ \ \ \ \ \ \ \ int32\ i,\ ch,\ nccs,\ noovs,\ unk;}
\DoxyCodeLine{00131\ }
\DoxyCodeLine{00132\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (n\ ==\ 0)}
\DoxyCodeLine{00133\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00134\ }
\DoxyCodeLine{00135\ \ \ \ \ \ \ \ \ unk\ =\ ngram\_unknown\_wid(lm);}
\DoxyCodeLine{00136\ }
\DoxyCodeLine{00137\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Reverse\ this\ array\ into\ an\ array\ of\ word\ IDs.\ */}}
\DoxyCodeLine{00138\ \ \ \ \ \ \ \ \ wids\ =\ ckd\_calloc(n,\ \textcolor{keyword}{sizeof}(*wids));}
\DoxyCodeLine{00139\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ n;\ ++i)}
\DoxyCodeLine{00140\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ wids[n-\/i-\/1]\ =\ ngram\_wid(lm,\ words[i]);}
\DoxyCodeLine{00141\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Skip\ <s>\ as\ it's\ a\ context\ cue\ (HACK,\ this\ should\ be\ configurable).\ */}}
\DoxyCodeLine{00142\ \ \ \ \ \ \ \ \ startwid\ =\ ngram\_wid(lm,\ \textcolor{stringliteral}{"{}<s>"{}});}
\DoxyCodeLine{00143\ }
\DoxyCodeLine{00144\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Now\ evaluate\ the\ list\ of\ words\ in\ reverse\ using\ the}}
\DoxyCodeLine{00145\ \textcolor{comment}{\ \ \ \ \ \ \ \ \ *\ remainder\ of\ the\ array\ as\ the\ history.\ */}}
\DoxyCodeLine{00146\ \ \ \ \ \ \ \ \ ch\ =\ noovs\ =\ nccs\ =\ 0;}
\DoxyCodeLine{00147\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ n;\ ++i)\ \{}
\DoxyCodeLine{00148\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ int32\ n\_used;}
\DoxyCodeLine{00149\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ int32\ prob;}
\DoxyCodeLine{00150\ }
\DoxyCodeLine{00151\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Skip\ <s>\ as\ it's\ a\ context\ cue\ (HACK,\ this\ should\ be\ configurable).\ */}}
\DoxyCodeLine{00152\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (wids[i]\ ==\ startwid)\ \{}
\DoxyCodeLine{00153\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ++nccs;}
\DoxyCodeLine{00154\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{continue};}
\DoxyCodeLine{00155\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00156\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Skip\ and\ count\ OOVs.\ */}}
\DoxyCodeLine{00157\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (wids[i]\ ==\ NGRAM\_INVALID\_WID\ ||\ wids[i]\ ==\ unk)\ \{}
\DoxyCodeLine{00158\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ++noovs;}
\DoxyCodeLine{00159\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{continue};}
\DoxyCodeLine{00160\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00161\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Sum\ up\ information\ for\ each\ N-\/gram\ */}}
\DoxyCodeLine{00162\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ prob\ =\ ngram\_ng\_score(lm,}
\DoxyCodeLine{00163\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ wids[i],\ wids\ +\ i\ +\ 1,}
\DoxyCodeLine{00164\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ n\ -\/\ i\ -\/\ 1,\ \&n\_used);}
\DoxyCodeLine{00165\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (verbose)\ \{}
\DoxyCodeLine{00166\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{int}\ m;}
\DoxyCodeLine{00167\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}log\ P(\%s|"{}},\ ngram\_word(lm,\ wids[i]));}
\DoxyCodeLine{00168\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ m\ =\ i\ +\ ngram\_model\_get\_size(lm)\ -\/\ 1;}
\DoxyCodeLine{00169\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (m\ >=\ n)}
\DoxyCodeLine{00170\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ m\ =\ n\ -\/\ 1;}
\DoxyCodeLine{00171\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{while}\ (m\ >\ i)\ \{}
\DoxyCodeLine{00172\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}\%s\ "{}},\ ngram\_word(lm,\ wids[m-\/-\/]));}
\DoxyCodeLine{00173\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00174\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{})\ =\ \%d\(\backslash\)n"{}},\ prob);}
\DoxyCodeLine{00175\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00176\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ch\ -\/=\ prob;}
\DoxyCodeLine{00177\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00178\ }
\DoxyCodeLine{00179\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (out\_n\_ccs)\ *out\_n\_ccs\ =\ nccs;}
\DoxyCodeLine{00180\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (out\_n\_oovs)\ *out\_n\_oovs\ =\ noovs;}
\DoxyCodeLine{00181\ }
\DoxyCodeLine{00182\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Calculate\ cross-\/entropy\ CH\ =\ -\/\ 1/N\ sum\ log\ P(W|H)\ */}}
\DoxyCodeLine{00183\ \ \ \ \ \ \ \ \ n\ -\/=\ (nccs\ +\ noovs);}
\DoxyCodeLine{00184\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (n\ <=\ 0)}
\DoxyCodeLine{00185\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00186\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (out\_lm\_score)}
\DoxyCodeLine{00187\ \ \ \ \ \ \ \ \ \ \ \ \ *out\_lm\_score\ =\ -\/ch;}
\DoxyCodeLine{00188\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ ch\ /\ n;}
\DoxyCodeLine{00189\ \}}
\DoxyCodeLine{00190\ }
\DoxyCodeLine{00191\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00192\ evaluate\_file(ngram\_model\_t\ *lm,\ logmath\_t\ *lmath,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *lsnfn)}
\DoxyCodeLine{00193\ \{}
\DoxyCodeLine{00194\ \ \ \ \ \ \ \ \ FILE\ *fh;}
\DoxyCodeLine{00195\ \ \ \ \ \ \ \ \ lineiter\_t\ *litor;}
\DoxyCodeLine{00196\ \ \ \ \ \ \ \ \ int32\ nccs,\ noovs,\ nwords,\ lscr;}
\DoxyCodeLine{00197\ \ \ \ \ \ \ \ \ float64\ ch,\ log\_to\_log2;;}
\DoxyCodeLine{00198\ }
\DoxyCodeLine{00199\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((fh\ =\ fopen(lsnfn,\ \textcolor{stringliteral}{"{}r"{}}))\ ==\ NULL)}
\DoxyCodeLine{00200\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ E\_FATAL\_SYSTEM(\textcolor{stringliteral}{"{}failed\ to\ open\ transcript\ file\ \%s"{}},\ lsnfn);}
\DoxyCodeLine{00201\ }
\DoxyCodeLine{00202\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ We\ have\ to\ keep\ ch\ in\ floating-\/point\ to\ avoid\ overflows,\ so}}
\DoxyCodeLine{00203\ \textcolor{comment}{\ \ \ \ \ \ \ \ \ *\ we\ might\ as\ well\ use\ log2.\ */}}
\DoxyCodeLine{00204\ \ \ \ \ \ \ \ \ log\_to\_log2\ =\ log(logmath\_get\_base(lmath))\ /\ log(2);}
\DoxyCodeLine{00205\ \ \ \ \ \ \ \ \ nccs\ =\ noovs\ =\ nwords\ =\ 0;}
\DoxyCodeLine{00206\ \ \ \ \ \ \ \ \ ch\ =\ 0.0;}
\DoxyCodeLine{00207\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (litor\ =\ lineiter\_start(fh);\ litor;\ litor\ =\ lineiter\_next(litor))\ \{}
\DoxyCodeLine{00208\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ **words;}
\DoxyCodeLine{00209\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ int32\ n,\ tmp\_ch,\ tmp\_noovs,\ tmp\_nccs,\ tmp\_lscr;}
\DoxyCodeLine{00210\ }
\DoxyCodeLine{00211\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ n\ =\ str2words(litor-\/>buf,\ NULL,\ 0);}
\DoxyCodeLine{00212\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (n\ <\ 0)}
\DoxyCodeLine{00213\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ E\_FATAL(\textcolor{stringliteral}{"{}str2words(line,\ NULL,\ 0)\ =\ \%d,\ should\ not\ happen\(\backslash\)n"{}},\ n);}
\DoxyCodeLine{00214\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (n\ ==\ 0)\ \textcolor{comment}{/*\ Do\ nothing!\ */}}
\DoxyCodeLine{00215\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{continue};}
\DoxyCodeLine{00216\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ words\ =\ ckd\_calloc(n,\ \textcolor{keyword}{sizeof}(*words));}
\DoxyCodeLine{00217\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ str2words(litor-\/>buf,\ words,\ n);}
\DoxyCodeLine{00218\ }
\DoxyCodeLine{00219\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Remove\ any\ utterance\ ID\ (FIXME:\ has\ to\ be\ a\ single\ "{}word"{})\ */}}
\DoxyCodeLine{00220\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (words[n-\/1][0]\ ==\ \textcolor{charliteral}{'('}}
\DoxyCodeLine{00221\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \&\&\ words[n-\/1][strlen(words[n-\/1])-\/1]\ ==\ \textcolor{charliteral}{')'})}
\DoxyCodeLine{00222\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ n\ =\ n\ -\/\ 1;}
\DoxyCodeLine{00223\ }
\DoxyCodeLine{00224\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ tmp\_ch\ =\ calc\_entropy(lm,\ words,\ n,\ \&tmp\_nccs,}
\DoxyCodeLine{00225\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \&tmp\_noovs,\ \&tmp\_lscr);}
\DoxyCodeLine{00226\ }
\DoxyCodeLine{00227\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ch\ +=\ (float64)\ tmp\_ch\ *\ (n\ -\/\ tmp\_nccs\ -\/\ tmp\_noovs)\ *\ log\_to\_log2;}
\DoxyCodeLine{00228\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ nccs\ +=\ tmp\_nccs;}
\DoxyCodeLine{00229\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ noovs\ +=\ tmp\_noovs;}
\DoxyCodeLine{00230\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ lscr\ +=\ tmp\_lscr;}
\DoxyCodeLine{00231\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ nwords\ +=\ n;}
\DoxyCodeLine{00232\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ }
\DoxyCodeLine{00233\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ckd\_free(words);}
\DoxyCodeLine{00234\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00235\ }
\DoxyCodeLine{00236\ \ \ \ \ \ \ \ \ ch\ /=\ (nwords\ -\/\ nccs\ -\/\ noovs);}
\DoxyCodeLine{00237\ \ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}cross-\/entropy:\ \%f\ bits\(\backslash\)n"{}},\ ch);}
\DoxyCodeLine{00238\ }
\DoxyCodeLine{00239\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Calculate\ perplexity\ pplx\ =\ exp\ CH\ */}}
\DoxyCodeLine{00240\ \ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}perplexity:\ \%f\(\backslash\)n"{}},\ pow(2.0,\ ch));}
\DoxyCodeLine{00241\ \ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}lm\ score:\ \%d\(\backslash\)n"{}},\ lscr);}
\DoxyCodeLine{00242\ }
\DoxyCodeLine{00243\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Report\ OOVs\ and\ CCs\ */}}
\DoxyCodeLine{00244\ \ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}\%d\ words\ evaluated\(\backslash\)n"{}},\ nwords);}
\DoxyCodeLine{00245\ \ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}\%d\ OOVs\ (\%.2f\%\%),\ \%d\ context\ cues\ removed\(\backslash\)n"{}},}
\DoxyCodeLine{00246\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ noovs,\ (\textcolor{keywordtype}{double})noovs\ /\ nwords\ *\ 100,\ nccs);}
\DoxyCodeLine{00247\ \}}
\DoxyCodeLine{00248\ }
\DoxyCodeLine{00249\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00250\ evaluate\_string(ngram\_model\_t\ *lm,\ logmath\_t\ *lmath,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *text)}
\DoxyCodeLine{00251\ \{}
\DoxyCodeLine{00252\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ *textfoo;}
\DoxyCodeLine{00253\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ **words;}
\DoxyCodeLine{00254\ \ \ \ \ \ \ \ \ int32\ n,\ ch,\ noovs,\ nccs,\ lscr;}
\DoxyCodeLine{00255\ }
\DoxyCodeLine{00256\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Split\ it\ into\ an\ array\ of\ strings.\ */}}
\DoxyCodeLine{00257\ \ \ \ \ \ \ \ \ textfoo\ =\ ckd\_salloc(text);}
\DoxyCodeLine{00258\ \ \ \ \ \ \ \ \ n\ =\ str2words(textfoo,\ NULL,\ 0);}
\DoxyCodeLine{00259\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (n\ <\ 0)}
\DoxyCodeLine{00260\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ E\_FATAL(\textcolor{stringliteral}{"{}str2words(textfoo,\ NULL,\ 0)\ =\ \%d,\ should\ not\ happen\(\backslash\)n"{}},\ n);}
\DoxyCodeLine{00261\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (n\ ==\ 0)\ \textcolor{comment}{/*\ Do\ nothing!\ */}}
\DoxyCodeLine{00262\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return};}
\DoxyCodeLine{00263\ \ \ \ \ \ \ \ \ words\ =\ ckd\_calloc(n,\ \textcolor{keyword}{sizeof}(*words));}
\DoxyCodeLine{00264\ \ \ \ \ \ \ \ \ str2words(textfoo,\ words,\ n);}
\DoxyCodeLine{00265\ }
\DoxyCodeLine{00266\ \ \ \ \ \ \ \ \ ch\ =\ calc\_entropy(lm,\ words,\ n,\ \&nccs,\ \&noovs,\ \&lscr);}
\DoxyCodeLine{00267\ }
\DoxyCodeLine{00268\ \ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}input:\ \%s\(\backslash\)n"{}},\ text);}
\DoxyCodeLine{00269\ \ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}cross-\/entropy:\ \%f\ bits\(\backslash\)n"{}},}
\DoxyCodeLine{00270\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ch\ *\ log(logmath\_get\_base(lmath))\ /\ log(2));}
\DoxyCodeLine{00271\ }
\DoxyCodeLine{00272\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Calculate\ perplexity\ pplx\ =\ exp\ CH\ */}}
\DoxyCodeLine{00273\ \ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}perplexity:\ \%f\(\backslash\)n"{}},\ logmath\_exp(lmath,\ ch));}
\DoxyCodeLine{00274\ \ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}lm\ score:\ \%d\(\backslash\)n"{}},\ lscr);}
\DoxyCodeLine{00275\ }
\DoxyCodeLine{00276\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Report\ OOVs\ and\ CCs\ */}}
\DoxyCodeLine{00277\ \ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}\%d\ words\ evaluated\(\backslash\)n"{}},\ n);}
\DoxyCodeLine{00278\ \ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}\%d\ OOVs,\ \%d\ context\ cues\ removed\(\backslash\)n"{}},}
\DoxyCodeLine{00279\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ noovs,\ nccs);}
\DoxyCodeLine{00280\ }
\DoxyCodeLine{00281\ \ \ \ \ \ \ \ \ ckd\_free(textfoo);}
\DoxyCodeLine{00282\ \ \ \ \ \ \ \ \ ckd\_free(words);}
\DoxyCodeLine{00283\ \}}
\DoxyCodeLine{00284\ }
\DoxyCodeLine{00285\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00286\ main(\textcolor{keywordtype}{int}\ argc,\ \textcolor{keywordtype}{char}\ *argv[])}
\DoxyCodeLine{00287\ \{}
\DoxyCodeLine{00288\ \ \ \ \ \ \ \ \ cmd\_ln\_t\ *config;}
\DoxyCodeLine{00289\ \ \ \ \ \ \ \ \ ngram\_model\_t\ *lm\ =\ NULL;}
\DoxyCodeLine{00290\ \ \ \ \ \ \ \ \ logmath\_t\ *lmath;}
\DoxyCodeLine{00291\ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *lmfn,\ *probdefn,\ *lsnfn,\ *text;}
\DoxyCodeLine{00292\ }
\DoxyCodeLine{00293\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((config\ =\ cmd\_ln\_parse\_r(NULL,\ defn,\ argc,\ argv,\ TRUE))\ ==\ NULL)}
\DoxyCodeLine{00294\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 1;}
\DoxyCodeLine{00295\ }
\DoxyCodeLine{00296\ \ \ \ \ \ \ \ \ verbose\ =\ cmd\_ln\_boolean\_r(config,\ \textcolor{stringliteral}{"{}-\/verbose"{}});}
\DoxyCodeLine{00297\ }
\DoxyCodeLine{00298\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Create\ log\ math\ object.\ */}}
\DoxyCodeLine{00299\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((lmath\ =\ logmath\_init}
\DoxyCodeLine{00300\ \ \ \ \ \ \ \ \ \ \ \ \ \ (cmd\_ln\_float64\_r(config,\ \textcolor{stringliteral}{"{}-\/logbase"{}}),\ 0,\ 0))\ ==\ NULL)\ \{}
\DoxyCodeLine{00301\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ E\_FATAL(\textcolor{stringliteral}{"{}Failed\ to\ initialize\ log\ math\(\backslash\)n"{}});}
\DoxyCodeLine{00302\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00303\ }
\DoxyCodeLine{00304\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Load\ the\ language\ model.\ */}}
\DoxyCodeLine{00305\ \ \ \ \ \ \ \ \ lmfn\ =\ cmd\_ln\_str\_r(config,\ \textcolor{stringliteral}{"{}-\/lm"{}});}
\DoxyCodeLine{00306\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (lmfn\ ==\ NULL}
\DoxyCodeLine{00307\ \ \ \ \ \ \ \ \ \ \ \ \ ||\ (lm\ =\ ngram\_model\_read(config,\ lmfn,}
\DoxyCodeLine{00308\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ NGRAM\_AUTO,\ lmath))\ ==\ NULL)\ \{}
\DoxyCodeLine{00309\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ E\_FATAL(\textcolor{stringliteral}{"{}Failed\ to\ load\ language\ model\ from\ \%s\(\backslash\)n"{}},}
\DoxyCodeLine{00310\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ cmd\_ln\_str\_r(config,\ \textcolor{stringliteral}{"{}-\/lm"{}}));}
\DoxyCodeLine{00311\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00312\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((probdefn\ =\ cmd\_ln\_str\_r(config,\ \textcolor{stringliteral}{"{}-\/probdef"{}}))\ !=\ NULL)}
\DoxyCodeLine{00313\ \ \ \ \ \ \ \ \ \ \ \ \ ngram\_model\_read\_classdef(lm,\ probdefn);}
\DoxyCodeLine{00314\ \ \ \ \ \ \ \ \ ngram\_model\_apply\_weights(lm,}
\DoxyCodeLine{00315\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ cmd\_ln\_float32\_r(config,\ \textcolor{stringliteral}{"{}-\/lw"{}}),}
\DoxyCodeLine{00316\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ cmd\_ln\_float32\_r(config,\ \textcolor{stringliteral}{"{}-\/wip"{}}),}
\DoxyCodeLine{00317\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ cmd\_ln\_float32\_r(config,\ \textcolor{stringliteral}{"{}-\/uw"{}}));}
\DoxyCodeLine{00318\ }
\DoxyCodeLine{00319\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Now\ evaluate\ some\ text.\ */}}
\DoxyCodeLine{00320\ \ \ \ \ \ \ \ \ lsnfn\ =\ cmd\_ln\_str\_r(config,\ \textcolor{stringliteral}{"{}-\/lsn"{}});}
\DoxyCodeLine{00321\ \ \ \ \ \ \ \ \ text\ =\ cmd\_ln\_str\_r(config,\ \textcolor{stringliteral}{"{}-\/text"{}});}
\DoxyCodeLine{00322\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (lsnfn)\ \{}
\DoxyCodeLine{00323\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ evaluate\_file(lm,\ lmath,\ lsnfn);}
\DoxyCodeLine{00324\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00325\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}\ (text)\ \{}
\DoxyCodeLine{00326\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ evaluate\_string(lm,\ lmath,\ text);}
\DoxyCodeLine{00327\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00328\ }
\DoxyCodeLine{00329\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00330\ \}}

\end{DoxyCode}
