File: ngram_test_main.cc

package info (click to toggle)
speech-tools 1%3A2.5.0-11
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 9,988 kB
  • sloc: cpp: 67,350; ansic: 12,174; sh: 4,055; java: 3,748; makefile: 1,111; lisp: 711; perl: 396; awk: 85; xml: 9
file content (410 lines) | stat: -rw-r--r-- 12,008 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*************************************************************************/
/*                                                                       */
/*                Centre for Speech Technology Research                  */
/*                     University of Edinburgh, UK                       */
/*                      Copyright (c) 1995,1996                          */
/*                        All Rights Reserved.                           */
/*                                                                       */
/*  Permission is hereby granted, free of charge, to use and distribute  */
/*  this software and its documentation without restriction, including   */
/*  without limitation the rights to use, copy, modify, merge, publish,  */
/*  distribute, sublicense, and/or sell copies of this work, and to      */
/*  permit persons to whom this work is furnished to do so, subject to   */
/*  the following conditions:                                            */
/*   1. The code must retain the above copyright notice, this list of    */
/*      conditions and the following disclaimer.                         */
/*   2. Any modifications must be clearly marked as such.                */
/*   3. Original authors' names are not deleted.                         */
/*   4. The authors' names are not used to endorse or promote products   */
/*      derived from this software without specific prior written        */
/*      permission.                                                      */
/*                                                                       */
/*  THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK        */
/*  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      */
/*  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   */
/*  SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE     */
/*  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    */
/*  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   */
/*  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          */
/*  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       */
/*  THIS SOFTWARE.                                                       */
/*                                                                       */
/*************************************************************************/
/*                 Authors:  Simon King                                  */
/*                 Date   :  July 1995                                   */
/*-----------------------------------------------------------------------*/
/*                 EST_Ngrammar test program                             */
/*                                                                       */
/*=======================================================================*/
#include "EST.h"
#include "EST_Ngrammar.h"


/** @name <command>ngram_test</command> <emphasis> Test n-gram language model </emphasis>
    @id ngram_test_manual
  * @toc
 */

//@{


/**@name Synopsis
  */
//@{

//@synopsis

/**
ngram_test is for testing ngram models generated from
<link linkend=ngram-build-manual>ngram_build</link>.

<formalpara> <para> <title> How do we test an ngram model ?  </title>
</para>

<para> ngram_test will compute the entropy (or perplexity, see below)
of some test data, given an ngram model. The entropy gives a measure
of how likely the ngram model is to have generated the test
data. Entropy is defined (for a sliding-window type ngram) as:

\[H = -\frac{1}{Q} \sum_{i=1}^{Q} log P(w_i | w_{i-1}, w_{i-2},... w_{i-N+1}) \]

where \(Q\) is the number of words of test data and \(N\) is the order
of the ngram model. Perplexity is a more intuitive mease, defined as:

\[B = 2^H \]

The perplexity of an ngram model with vocabulary size V will be
between 1 and V. Low perplexity indicates a more predictable language,
and in speech recognition, a models with low perplexity on test data
(i.e. data NOT used to estimate the model in the first place)
typically give better accuracy recognition than models with higher 
perplexity (this is not guaranteed, however).

test_ngram works with non-sliding-window type models when the input
format is <parameter>ngram_per_line</parameter>.

</para>
</formalpara>

<formalpara>
<para><title>Input data format</title></para>
<para> The data input format options are the same as
<link linkend=ngram-build-manual>ngram_build</link>, as is the treatment of sentence start/end using
special tags. 
</para>
<para>

Note: To get meaningful entropy/perplexity figures, it is recommended that
you use the same data input format in both
<link linkend=ngram-build-manual>ngram_build</link> and <link linkend=ngram-test-manual>ngram_test</link>, and the treatment of
sentence start/end should be the same.
</para>
</formalpara>


@see ngram_build */
//@}

/**@name OPTIONS
  */
//@{

//@options

//@}


int main(int argc, char **argv)
{
    //int order;
    EST_StrList files,script;
    EST_Option al, op;
    EST_String wordlist_file, script_file, in_file, format;
    EST_String prev_tag, prev_prev_tag, last_tag;
    EST_Litem *p;
    //EST_Ngrammar::representation_t representation = 
    //EST_Ngrammar::dense;

    EST_StrList wordlist;
    EST_Ngrammar ngrammar;
    bool per_file_stats=false;
    bool raw_stats=false;
    bool brief=false;
    EST_String input_format;

    double raw_entropy,count,entropy,perplexity,total_raw_H,total_count;
    total_count = 0;
    total_raw_H = 0;

    parse_command_line
    (argc, argv, 
     EST_String("[input file0] [input file1] ...\n")+
	 "-g <ifile>   grammar file (required)\n"+
	 "-w <ifile>   filename containing word list (required for some grammar formats)\n"+
	 "-S <ifile>   script file\n"+
         "-raw_stats   print unnormalised entropy and sample count\n"+
         "-brief       print results in brief format\n"+
         "-f           print stats for each file\n"+
   	 "\n"+
	 "-input_format <string>\n"+
         "             format of input data (default sentence_per_line)\n"+
	 "             may also be sentence_per_file, or ngram_per_line.\n"+
         "\n"+
	 "Pseudo-words :\n"+
	 "-prev_tag <string>\n"+
         "             tag before sentence start\n"+
	 "-prev_prev_tag <string>\n"+
         "             all words before 'prev_tag'\n"+
	 "-last_tag <string>\n"+
         "             after sentence end\n"+
	 "-default_tags\n"+
         "             use default tags of "+SENTENCE_START_MARKER+
			","+SENTENCE_END_MARKER+" and "+SENTENCE_END_MARKER+"\n"+
         "             respectively\n",
			files, al);


    if (al.present("-w"))
	wordlist_file = al.val("-w");
    else{
	wordlist_file = "";
    }

    if (al.present("-f"))
	per_file_stats = true;
    if (al.present("-input_format"))
	input_format = al.val("-input_format");
    else
	input_format = "sentence_per_line";

    if ( al.present("-raw_stats") || al.present("-r"))
	raw_stats = true;

    if ( al.present("-brief") || al.present("-b") )
	brief = true;


    if (al.present("-default_tags"))
    {	
	prev_tag = SENTENCE_START_MARKER;
	prev_prev_tag = SENTENCE_END_MARKER;
	last_tag = SENTENCE_END_MARKER;
    }
    
    if (al.present("-prev_tag"))
    {
	if (al.present("-default_tags"))
	    cerr << "test_ngram: WARNING : -prev_tag overrides -default_tags"
		<< endl;
	prev_tag = al.val("-prev_tag");
    }

    if (al.present("-prev_prev_tag"))
    {
	if (al.present("-default_tags"))
	    cerr << "test_ngram: WARNING : -prev_prev_tag overrides -default_tags" << endl;
	prev_prev_tag = al.val("-prev_prev_tag");
    }	
    
    if (al.present("-last_tag"))
    {
	if (al.present("-default_tags"))
	    cerr << "test_ngram: WARNING : -last_tag overrides -default_tags" << endl;
	last_tag = al.val("-last_tag");
    }

    if (   ( (prev_tag=="") ||  (prev_prev_tag=="") || (last_tag=="") )
	&& ( (prev_tag!="") ||  (prev_prev_tag!="") || (last_tag!="") )   )
    {
	cerr << "test_ngram: ERROR : if any tags are given, ALL must be given" << endl;
	exit(1);
    }


    // script
    if (al.present("-S"))
    {
	script_file = al.val("-S");
    
	if(load_StrList(script_file,script) != format_ok)
	{
	    cerr << "test_ngram: Could not read script from file " 
		<< script_file << endl;
	    exit(1);
	}
    }

    if (al.present("-g"))
	in_file = al.val("-g");
    else
    {
	cerr << "test_ngram: Must give a grammar filename using -g" << endl;
	exit(1);
    }

    // plus any files on command line
    // except file "-" unless there is no script
    if(script.head()==NULL)
	script += files;
    else
	for(p=files.head();p!=0;p=p->next())
	    if(files(p) != "-")
		script.append(files(p));

    if(script.head() == NULL)
    {
	cerr << "test_ngram: No test files given" << endl;
	exit(1);
    }

    if (wordlist_file != "")
    {
	// load wordlist
	if (load_StrList(wordlist_file,wordlist) != format_ok)
	{
	    cerr << "test_ngram: Could not read wordlist from file " << wordlist_file
		<< endl;
	    exit(1);
	}
    
	// load grammar using wordlist
	if (ngrammar.load(in_file,wordlist) != format_ok)
	{
	    cerr << "test_ngram: Failed to load grammar" << endl;
	    exit(1);
	}
    }
    else
    {
	if (ngrammar.load(in_file) != format_ok)
	{
	    cerr << "test_ngram: Failed to load grammar" << endl;
	    exit(1);
	}
    }

    if (!brief)
    {
	cout << "Ngram Test Results" << endl;
	cout << "==================" << endl;
    }

    for (p = script.head(); p; p = p->next())
    {
	// test each file
	if (test_stats(ngrammar,
		       script(p),
		       raw_entropy,count,
		       entropy,perplexity,
		       input_format,
		       prev_tag,
		       prev_prev_tag))
	{
	    total_raw_H += raw_entropy;
	    total_count += count;
	    
	    if(per_file_stats)
	    {
		if (brief)
		    cout << basename(script(p)) << " \t";
		else
		    cout << script(p) << endl;

		if(raw_stats)
		{
		    if (brief)
			 cout << raw_entropy << " " << count << " ";
		    else
		    {
			cout << " raw entropy " << raw_entropy << endl;
			cout << " count       " << count << endl;
		    }
		}
		
		if (brief)
		    cout << entropy << " " << perplexity << endl;
		else
		{
		    cout << " entropy     " << entropy << endl;
		    cout << " perplexity  " << perplexity << endl << endl;
		}
	    }
	}
	else
	{
	    cerr << "test_ngram: WARNING : file '" << script(p)
		<< "' could not be processed" << endl;
	}
	
    }
    if (total_count > 0)
    {
	if (!brief)
	    cout << "Summary for grammar " << in_file << endl;
	else
	    if (per_file_stats)
		cout << "summary \t";

	if(raw_stats)
	{
	    if (brief)
		cout << total_raw_H << " " << total_count << " ";
	    else
	    {
		cout << " raw entropy " << total_raw_H << endl;
		cout << " count       " << total_count << endl;
	    }
	}
	if (brief)
	{
	    cout << total_raw_H / total_count;
	    cout << " " << pow(2.0,total_raw_H / total_count);
	    cout << endl;
	}
	else
	{
	    cout << " entropy     " << total_raw_H / total_count << endl;
	    cout << " perplexity  " <<  pow(2.0,total_raw_H / total_count);
	    cout << endl;
	}
    }
    else
    {
	cerr << "test_ngram: No data processed" << endl;
    }
    
    // everything went okay
    return 0;
}


void override_lib_ops(EST_Option &a_list, EST_Option &al)
{
    (void)a_list;
    (void)al;
}

/** @name Hints

<title>I got a perplexity of Infinity - what went wrong ?</title>

A perplexity of Infinity means that at least one of the ngrams in your
test data had a probability of zero. Possible reasons for this include:

<itemizedlist>

<listitem><para>The training data had no examples of this ngram, and
you did not specify a floor for zero frequency ngrams in
\Ref{build_ngram} </para></listitem>
<listitem><para>You used differing input formats for \Ref{ngram_build}
and \Ref{ngram_test}. </para></listitem>
<listitem><para>You used differing sentence start/end treatments in
\Ref{ngram_build} and \Ref{ngram_test}. </para></listitem>
</itemizedlist>

*/

  //@{
  //@}

//@}