File: align_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 (431 lines) | stat: -rw-r--r-- 12,451 bytes parent folder | download | duplicates (2)
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*************************************************************************/
/*                                                                       */
/*                Centre for Speech Technology Research                  */
/*                     University of Edinburgh, UK                       */
/*                      Copyright (c) 1996,1997                          */
/*                        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.                                                       */
/*                                                                       */
/*************************************************************************/
/*                     Author :  Alan W Black                            */
/*                     Date   :  September 1999                          */
/*-----------------------------------------------------------------------*/
/*  Simple alignment scoring program to give number of insertions,       */
/*  deletions, errors between a string of symbols and a reference string */
/*  of symbols                                                           */
/*                                                                       */
/*=======================================================================*/
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <fstream>
#include <cstring>
#include "EST.h"
#include "EST_WFST.h"

static int align_main(int argc, char **argv);
static void nisttool_align(EST_Option &al);
static void string_align(EST_Option &al);
static void align_score(EST_Utterance &u, const EST_String &refrel,
			const EST_String &hyporel, 
			const EST_String &alignrel,
			int &total,int &ins,int &del,int &sub,int &correct);
static int name_distance(EST_Item *r,EST_Item *h);
void align(EST_Utterance &utt, 
	   const EST_String &refrel,
	   const EST_String &hyporel,
	   const EST_String &alignrel);
static void load_sentence(EST_Utterance &u, const EST_String &relname,
			  EST_TokenStream &ts);
static void load_sentence(EST_Utterance &u, const EST_String &relname,
			  EST_String &relval);

/** @name <command>align</command> <emphasis>align stream with reference stream</emphasis>
    @id align-manual
  * @toc
 */

//@{


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

//@synopsis

/**

 */

//@}

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

//@options

//@}
int main(int argc, char **argv)
{

    align_main(argc,argv);

    exit(0);
    return 0;
}

static int align_main(int argc, char **argv)
{
    // Top level function generates a WFST from rules
    EST_Option al;
    EST_StrList files;
    EST_String outfile;
    EST_String format;

    parse_command_line
	(argc, argv,
	 EST_String("[options] ...\n")+
	 "Summary: align an hypothesis with a reference string\n"+
	 "-rfile <ifile>    Reference file\n"+
	 "-hfile <ifile>    Hypothesis file\n"+
	 "-rstring <string> Reference string\n"+
	 "-hstring <string> Hypothesis string\n"+
	 "-format <string>\n"+
         "               Supported formats: strings, nisttool\n",
		       files, al);
    
    if (al.present("-o"))
	outfile = al.val("-o");
    else
	outfile = "-";

    if (al.present("-format"))
	format = al.val("-format");
    else
	format = "strings";

    if (format == "strings")
        string_align(al);
    else if (format == "nisttool")
	nisttool_align(al);
    else
        cout << "Unknown or unhandled format: " << format << endl;	

    return 0;
}

bool dp_match(const EST_Relation &lexical,
	      const EST_Relation &surface,
	      EST_Relation &match,
	      float ins, float del, float sub);

static void string_align(EST_Option &al)
{
    EST_String refStr = al.val("-rstring");
    EST_String hypStr = al.val("-hstring");
    EST_Utterance u;
    int total,ins,del,sub,correct;

    load_sentence(u,"ref",refStr);
    load_sentence(u,"hypo",hypStr);
    align(u,"ref","hypo","align");
    align_score(u,"ref","hypo","align",total,ins,del,sub,correct);
    fprintf(stdout,"words %d\n",total);
    fprintf(stdout,"insertions %d\n",ins);
    fprintf(stdout,"deletions %d\n",del);
    fprintf(stdout,"substitutions %d\n",sub);
    fprintf(stdout,"correct %d\n",correct);
    fprintf(stdout,"WER %f\n",(100.0 * (float)(ins+del+sub))/total);
}

static void nisttool_align(EST_Option &al)
{
    // Using the format used by the NIST tools for alignment
    // Sentence per line with parenthesized id at end
    EST_String reffile = al.val("-rfile");
    EST_String hypofile = al.val("-hfile");
    EST_TokenStream rts,hts;
    EST_Item *r, *h;
    static EST_Regex id("^(.*)$");
    int sents=0;
    int total,ins,del,sub,correct;
    int s_total,s_ins,s_del,s_sub,s_correct;

    rts.open(reffile);
    hts.open(hypofile);
    s_total=s_ins=s_del=s_sub=s_correct=0;

    while (!rts.eof())
    {
	EST_Utterance u;
	
	load_sentence(u,"ref",rts);
	load_sentence(u,"hypo",hts);
	r = u.relation("ref")->rlast();
	h = u.relation("hypo")->rlast();
	if ((!r->name().matches(id)) ||
	    (r->name() != h->name()))
	{
	    cerr << "Align: failed to match sentence " <<
		sents << " at id " << r->name() << endl;
	}
	else
	{
	    // Ids aren't counted as words
	    r->unref_all();
	    h->unref_all();
	    align(u,"ref","hypo","align");
	    // This doesn't give exactly the same as the NIST tools
	    // even though it should (actually I think its better)
//	    dp_match(*u.relation("ref"),
//		     *u.relation("hypo"),
//		     *u.create_relation("align"),
//		     3,3,4);
	    align_score(u,"ref","hypo","align",
			total,ins,del,sub,correct);
	    s_total += total;
	    s_ins += ins;
	    s_del += del;
	    s_sub += sub;
	    s_correct += correct;
	}
        sents++;
    }

    rts.close();
    hts.close();
    fprintf(stdout,"sentences %d\n",sents);
    fprintf(stdout,"words %d\n",s_total);
    fprintf(stdout,"insertions %d\n",s_ins);
    fprintf(stdout,"deletions %d\n",s_del);
    fprintf(stdout,"substitutions %d\n",s_sub);
    fprintf(stdout,"correct %d\n",s_correct);
    fprintf(stdout,"WER %f\n",(100.0 * (float)(s_ins+s_del+s_sub))/s_total);
}

static void load_sentence(EST_Utterance &u,
			  const EST_String &relname,
			  EST_TokenStream &ts)
{
    EST_Relation *r = u.create_relation(relname);

    do
    {
	EST_Item *i = r->append();
	i->set_name(ts.get());
    }
    while ((!ts.eoln()) && (!ts.eof()));
}

static void load_sentence(EST_Utterance &u,
			  const EST_String &relname,
			  EST_String &relval)
{
    EST_Relation *r = u.create_relation(relname);
    EST_StrList strlist;
    StringtoStrList(relval, strlist, " ");
    EST_StrList::Entries iter;

    for (iter.begin(strlist); iter; ++iter)
    {
        EST_Item *i = r->append();
	i->set_name(*iter);
    }
}

static void align_score(EST_Utterance &u, const EST_String &refrel,
			const EST_String &hyporel, 
			const EST_String &alignrel,
			int &total,int &ins,int &del,int &sub,int &correct)
{
    // Score alignment
    EST_Item *ri,*hi;
    total=ins=del=correct=sub=0;

    for (ri=u.relation(refrel)->first(),
	 hi=u.relation(hyporel)->first();
	 ri;
	 ri=inext(ri),hi=inext(hi))
    {
	for ( ; (as(hi,alignrel) == 0) && hi ; hi=inext(hi))
	{
	    fprintf(stdout,"inserted: %s\n",(const char *)hi->name());
	    ins++;
	}
	for ( ; (daughter1(ri,alignrel) == 0) && ri; ri=inext(ri))
	{
	    fprintf(stdout,"deleted: %s\n",(const char *)ri->name());
	    del++;
	}
	if (!ri)
	    break;
	if (name_distance(ri,daughter1(ri,alignrel)) == 0)
	{
	    fprintf(stdout,"correct: %s\n",(const char *)ri->name());
	    correct++;
	}
	else
	{
	    fprintf(stdout,"substituted: %s\n",(const char *)ri->name());
	    sub++;
	}
    }
    // For trailing hypothesized (or ref is nil)
    for ( ;  hi ; hi=inext(hi))
    {
	fprintf(stdout,"inserted: %s\n",(const char *)hi->name());
	ins++;
    }

    total = u.relation(refrel)->length();


//    fprintf(stdout,"total %d ins %d del %d subs %d correct %d\n",
//	    total, ins, del, sub, correct);
}

static int name_distance(EST_Item *r,EST_Item *h)
{
    EST_String rname = r->name();
    EST_String hname = h->name();
    if ((rname == hname) ||
	(downcase(rname) == downcase(hname)))
	return 0;
    else
	return 1;
}

void align(EST_Utterance &utt,
	   const EST_String &refrel,
	   const EST_String &hyporel,
	   const EST_String &alignrel)
{
    // Align refrel to hyporel by alignrel
    int r_size = utt.relation(refrel)->length();
    int h_size = utt.relation(hyporel)->length();
    EST_Item *ri = utt.relation(refrel)->first();
    EST_Item *hi = utt.relation(hyporel)->first();
    int i,j;
    int insdel_cost = 3;
    int subs_cost = 4;
    float to_insert,to_del,to_subs;
    float cost;

    EST_Relation *ar = utt.create_relation(alignrel);

    EST_FMatrix dpt(r_size+1,h_size+1);
    EST_IMatrix dpp(r_size+1,h_size+1);

    // Initialise first row and column
    dpt(0,0) = subs_cost * name_distance(ri,hi);
    dpp(0,0) = 0;
    for (i=1; i<r_size+1; i++)
    {
	dpt(i,0) = insdel_cost + dpt(i-1,0);
	dpp(i,0) = -1;  // deletion
    }
    for (j=1; j < h_size+1; j++)
    {
	dpt(0,j) = insdel_cost + dpt(0,j-1);
	dpp(0,j) = 1;   // insertion
    }

    ri = utt.relation(refrel)->first();
    for (i=1; ri; ri=inext(ri),i++)
    {
	ar->append(ri);  // for use later
	hi = utt.relation(hyporel)->first();
	for (j=1; hi; hi=inext(hi),j++)
	{
	    cost = name_distance(ri,hi);
	    to_insert = insdel_cost + dpt(i,j-1);
	    to_del = insdel_cost + dpt(i-1,j);
	    to_subs = (cost * subs_cost) + dpt(i-1,j-1);
	    if (to_insert < to_del)
	    {
		if (to_insert < to_subs)
		{
		    dpt(i,j) = to_insert;
		    dpp(i,j) = 1;
		}
	        else
		{
		    dpt(i,j) = to_subs;
		    dpp(i,j) = 0;
		}
	    }
	    else
	    {
		if (to_del < to_subs)
		{
		    dpt(i,j) = to_del;
		    dpp(i,j) = -1;
		}
	        else
		{
		    dpt(i,j) = to_subs;
		    dpp(i,j) = 0;
		}
	    }
	}
    }

//      for (i=1,ri=utt.relation(refrel)->first(); i < r_size+1; i++,ri=inext(ri))
//      {
//  	fprintf(stdout,"%10s  ",(const char *)ri->name());
//  	for (j=1,hi=utt.relation(hyporel)->first(); j<h_size+1; j++,hi=inext(hi))
//  	    fprintf(stdout,"%3d/%2d ",(int)dpt(i,j),dpp(i,j));
//  	fprintf(stdout,"\n");
//      }

    for (i=r_size,j=h_size,
	     ri=utt.relation(refrel)->rlast(),
	     hi=utt.relation(hyporel)->rlast();
	 ri; i--,ri=iprev(ri))
    {
	while (dpp(i,j) == 1)
	{
	    j--;
//	    fprintf(stdout,"skipping hi %s\n",
//		    (const char *)hi->name());
	    hi=iprev(hi);
	}
	if (dpp(i,j) == 0)
	{
//	    fprintf(stdout,"linking %s %s\n",
//		    (const char *)ri->name(),
//		    (const char *)hi->name());
	    append_daughter(ri,alignrel,hi);
	    j--;
	    hi=iprev(hi);
	}
//	else
//	    fprintf(stdout,"skipping ri %s\n",
//		    (const char *)ri->name());
    }
}