~ubuntu-branches/ubuntu/saucy/speech-tools/saucy-proposed

« back to all changes in this revision

Viewing changes to .pc/templatefixes.diff/stats/dynamic_program.cc

  • Committer: Package Import Robot
  • Author(s): Peter Drysdale, Peter Drysdale, Sergio Oller
  • Date: 2013-05-04 09:40:22 UTC
  • Revision ID: package-import@ubuntu.com-20130504094022-trrabtuwmofdcr02
Tags: 1:2.1~release-6
[ Peter Drysdale ]
* gcc 4.8 compliance a by-product of EsounD removal. (Closes: #701362)
* Add patch to enable compiling of with clang 3.0.
* Stop clobbering compiler/ld flags in festival build, it is not polite.
* Remove support for EsounD which has been deprecated by Gnome
* Fix Lintian flagged spelling error.
* Add missing dependency on libaudiofile-dev. Thanks
  to Alessio Treglia.  (Closes: #657603)

[ Sergio Oller ]
* Add patch to enable native PulseAudio support. Original implementation by
  Matthias Clasen.
* Add patch to enable native ALSA support. (Closes: #638394)
* Original work on release-5 bug fast-tracked to wheezy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*************************************************************************/
 
2
/*                                                                       */
 
3
/*                Centre for Speech Technology Research                  */
 
4
/*                     University of Edinburgh, UK                       */
 
5
/*                      Copyright (c) 1995,1996                          */
 
6
/*                        All Rights Reserved.                           */
 
7
/*                                                                       */
 
8
/*  Permission is hereby granted, free of charge, to use and distribute  */
 
9
/*  this software and its documentation without restriction, including   */
 
10
/*  without limitation the rights to use, copy, modify, merge, publish,  */
 
11
/*  distribute, sublicense, and/or sell copies of this work, and to      */
 
12
/*  permit persons to whom this work is furnished to do so, subject to   */
 
13
/*  the following conditions:                                            */
 
14
/*   1. The code must retain the above copyright notice, this list of    */
 
15
/*      conditions and the following disclaimer.                         */
 
16
/*   2. Any modifications must be clearly marked as such.                */
 
17
/*   3. Original authors' names are not deleted.                         */
 
18
/*   4. The authors' names are not used to endorse or promote products   */
 
19
/*      derived from this software without specific prior written        */
 
20
/*      permission.                                                      */
 
21
/*                                                                       */
 
22
/*  THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK        */
 
23
/*  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      */
 
24
/*  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   */
 
25
/*  SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE     */
 
26
/*  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    */
 
27
/*  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   */
 
28
/*  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          */
 
29
/*  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       */
 
30
/*  THIS SOFTWARE.                                                       */
 
31
/*                                                                       */
 
32
/*************************************************************************/
 
33
/*                   Author :  Simon King                                */
 
34
/*                   Date   :  June 1998                                 */
 
35
/*************************************************************************/
 
36
 
 
37
#include <cstdlib>
 
38
#include <cstdio>
 
39
#include <iostream>
 
40
#include <fstream>
 
41
#include "EST_math.h"
 
42
#include "ling_class/EST_Utterance.h"
 
43
 
 
44
typedef EST_TVector<EST_Item*> EST_Item_ptr_Vector;
 
45
#if defined(INSTANTIATE_TEMPLATES)
 
46
#include "../base_class/EST_TVector.cc"
 
47
 
 
48
template class EST_TVector<EST_Item*>;
 
49
 
 
50
#endif
 
51
 
 
52
static EST_Item *const def_val_item_ptr = NULL;
 
53
template <> EST_Item* const *EST_Item_ptr_Vector::def_val = &def_val_item_ptr;
 
54
 
 
55
static EST_Item* error_return_item_ptr = NULL;
 
56
template <> EST_Item* *EST_Item_ptr_Vector::error_return = &error_return_item_ptr;
 
57
 
 
58
typedef
 
59
float (*local_cost_function)(const EST_Item *item1,
 
60
                             const EST_Item *item2);
 
61
 
 
62
typedef
 
63
bool (*local_pruning_function)(const int i,
 
64
                               const int j,
 
65
                               const int max_i, 
 
66
                               const int max_j);
 
67
 
 
68
bool dp_sub(int i, int j,
 
69
            const EST_Item_ptr_Vector &vr1,
 
70
            const EST_Item_ptr_Vector &vr2,
 
71
            EST_IMatrix &DP_path_i, EST_IMatrix &DP_path_j, 
 
72
            local_cost_function lcf,
 
73
            local_pruning_function lpf,
 
74
            EST_Item *null_sym,
 
75
            EST_FMatrix &cost);
 
76
 
 
77
void trace_back_and_link(int i, int j,
 
78
                         EST_Item *p1, EST_Item *p2,
 
79
                         const EST_IMatrix &DP_path_i, 
 
80
                         const EST_IMatrix &DP_path_j,
 
81
                         EST_Item *null_sym);
 
82
 
 
83
inline bool null_lpf(const int,const int,const int,const int)
 
84
{
 
85
    return FALSE;
 
86
}
 
87
 
 
88
bool dp_match(const EST_Relation &lexical,
 
89
              const EST_Relation &surface,
 
90
              EST_Relation &match,
 
91
              local_cost_function lcf,
 
92
              local_pruning_function lpf,
 
93
              EST_Item *null_sym);
 
94
 
 
95
 
 
96
bool dp_match(const EST_Relation &lexical,
 
97
              const EST_Relation &surface,
 
98
              EST_Relation &match,
 
99
              local_cost_function lcf,
 
100
              EST_Item *null_sym)
 
101
{
 
102
    // dp without pruning
 
103
 
 
104
    return dp_match(lexical,surface,match,lcf,null_lpf,null_sym);
 
105
 
 
106
}
 
107
 
 
108
static float fixed_ins;
 
109
static float fixed_del;
 
110
static float fixed_sub;
 
111
 
 
112
float fixed_local_cost(const EST_Item *s1, const EST_Item *s2)
 
113
{
 
114
    EST_String null_sym = "nil";
 
115
 
 
116
    // otherwise cost is either insertion cost, or cost_matrix value
 
117
    if (s1->name() == s2->name())
 
118
        return 0;
 
119
    else
 
120
    {
 
121
        if (s1->name() == null_sym)
 
122
            return fixed_ins;
 
123
        else if (s2->name() == null_sym)
 
124
            return fixed_del;
 
125
        else 
 
126
            return fixed_sub;
 
127
    }
 
128
}
 
129
 
 
130
 
 
131
bool dp_match(const EST_Relation &lexical,
 
132
              const EST_Relation &surface,
 
133
              EST_Relation &match,
 
134
              float ins, float del, float sub)
 
135
{
 
136
    fixed_ins = ins;
 
137
    fixed_del = del;
 
138
    fixed_sub = sub;
 
139
    EST_Item null_sym;
 
140
 
 
141
    return dp_match(lexical, surface, match, fixed_local_cost, 
 
142
                    null_lpf, &null_sym);
 
143
}
 
144
 
 
145
 
 
146
bool dp_match(const EST_Relation &lexical,
 
147
              const EST_Relation &surface,
 
148
              EST_Relation &match,
 
149
              local_cost_function lcf,
 
150
              local_pruning_function lpf,
 
151
              EST_Item *null_sym)
 
152
{
 
153
    
 
154
 
 
155
    // aligns lexical and surface forms using dynamic programming
 
156
    // i.e. the lexical form is transformed into the surface form
 
157
    //      by substitutions, insertions and deletions
 
158
 
 
159
    // makes links between associated (matching or substituted) items
 
160
    // insertions and deletions are 'left dangling'
 
161
    // links are stored in a new relation called "Match"
 
162
 
 
163
    // assumes that local cost computation is cheap (no caching)
 
164
 
 
165
    EST_IMatrix DP_path_i,DP_path_j;
 
166
    EST_Item_ptr_Vector vr1,vr2;
 
167
    EST_Item *p;
 
168
    int l1,l2,i,j;
 
169
    
 
170
    l1 = lexical.length() + 1;
 
171
    l2 = surface.length() + 1;
 
172
 
 
173
    vr1.resize(l1);
 
174
    vr2.resize(l2);
 
175
 
 
176
    // prepend null_syms
 
177
    vr1[0] = null_sym;
 
178
    vr2[0] = null_sym;
 
179
 
 
180
    for (p=lexical.head(),i=1; p != 0; p = p->next(),i++)
 
181
        vr1[i] = p;
 
182
    for (p=surface.head(),i=1; p != 0; p = p->next(),i++)
 
183
        vr2[i] = p;
 
184
        
 
185
    DP_path_i.resize(l1,l2);
 
186
    DP_path_j.resize(l1,l2);
 
187
 
 
188
/*
 
189
    cerr << "Pruning" << endl;
 
190
    for(i=0;i<l1;i++)
 
191
    {
 
192
        for(j=0;j<l2;j++)
 
193
            if(lpf(i,j,l1,l2))
 
194
                cerr << "- ";
 
195
            else
 
196
                cerr << "+ ";
 
197
        cerr << endl;
 
198
    }
 
199
    cerr << endl;
 
200
*/
 
201
 
 
202
    // end conditions : must start at (0,0) and finish at (l1-1,l2-1)
 
203
    // i.e. extreme corners of grid
 
204
    EST_FMatrix cost;
 
205
    cost.resize(vr1.length(),vr2.length());
 
206
    for(i=0;i<l1;i++)
 
207
        for(j=0;j<l2;j++)
 
208
            cost.a_no_check(i,j) = -1; // means not computed yet
 
209
 
 
210
    if(!dp_sub(l1-1,l2-1,
 
211
               vr1,vr2,
 
212
               DP_path_i,DP_path_j,
 
213
               lcf,lpf,null_sym,cost))
 
214
    {
 
215
        cerr << "No path found (over pruning ?)" << endl;
 
216
        return FALSE;
 
217
    }
 
218
    // make somewhere to record the relations
 
219
    //utt.create_relation("Match");
 
220
    for (p = lexical.head(); p; p = p->next())
 
221
        match.append(p);
 
222
 
 
223
    /*
 
224
    for(i=0;i<l1;i++)
 
225
    {
 
226
        for(j=0;j<l2;j++)
 
227
            cerr << i << "," << j << "=[" << DP_path_i(i,j) << "," << DP_path_j(i,j) << "] ";
 
228
        cerr << endl;
 
229
    }
 
230
    cerr << endl;
 
231
*/
 
232
 
 
233
    trace_back_and_link(l1-1,l2-1,
 
234
                        match.last(),
 
235
                        surface.last(),
 
236
                        DP_path_i,DP_path_j,null_sym);
 
237
 
 
238
    return TRUE;
 
239
}
 
240
 
 
241
 
 
242
bool dp_sub(int i, int j,
 
243
            const EST_Item_ptr_Vector &vr1, 
 
244
            const EST_Item_ptr_Vector &vr2,
 
245
            EST_IMatrix &DP_path_i, EST_IMatrix &DP_path_j, 
 
246
            local_cost_function lcf, 
 
247
            local_pruning_function lpf, 
 
248
            EST_Item *null_sym,
 
249
            EST_FMatrix &cost)
 
250
{
 
251
 
 
252
    // the goal is to compute cost(i,j)
 
253
    
 
254
    // already done ?
 
255
    if(cost(i,j) >= 0)
 
256
        return TRUE;
 
257
 
 
258
    //cerr << "sub " << i << " " << j << endl;
 
259
 
 
260
    int best_i=-1,best_j=-1;
 
261
    float sub,ins,del;
 
262
    float best_c=MAXFLOAT;
 
263
 
 
264
    // prune ?
 
265
    if(lpf(i,j,vr1.length()-1,vr2.length()-1))
 
266
        return FALSE;
 
267
 
 
268
 
 
269
    // consider all paths into this point 
 
270
    // and select the best one (lowest cost)
 
271
 
 
272
    if(i==0)
 
273
    {
 
274
        if(j==0)
 
275
        {
 
276
 
 
277
            best_i = i;
 
278
            best_j = j;
 
279
            best_c = lcf(null_sym,null_sym);
 
280
        }
 
281
        else
 
282
        {
 
283
 
 
284
            // insert j'th item from vr2
 
285
            if(dp_sub(i,j-1,vr1,vr2,
 
286
                      DP_path_i,DP_path_j,
 
287
                      lcf,lpf, null_sym,cost))
 
288
            {
 
289
                best_i = i;
 
290
                best_j = j-1;
 
291
                best_c = lcf(null_sym,vr2(j)) + cost.a(i,j-1);
 
292
            }
 
293
            else
 
294
                return FALSE;
 
295
        }
 
296
    }
 
297
 
 
298
    else if(j==0)
 
299
    {
 
300
        
 
301
        // delete i'th item from vr1
 
302
        if(dp_sub(i-1,j,vr1,vr2,
 
303
                  DP_path_i,DP_path_j,
 
304
                  lcf,lpf, null_sym,cost))
 
305
        {
 
306
            best_i = i-1;
 
307
            best_j = j;
 
308
            best_c = lcf(vr1(i),null_sym) + cost.a(i-1,j);
 
309
        }
 
310
 
 
311
    }
 
312
 
 
313
    // this is the simplest local constraint (i.e. no constraints !)
 
314
    // which allows unlimited consecutive insertions or deletions
 
315
 
 
316
    else
 
317
    {
 
318
 
 
319
        if(dp_sub(i-1,j-1,vr1,vr2,
 
320
                  DP_path_i,DP_path_j,
 
321
                  lcf,lpf, null_sym,cost))
 
322
        {
 
323
            sub = 2 * lcf(vr1(i),vr2(j)) + cost(i-1,j-1);
 
324
            if(sub < best_c)
 
325
            {
 
326
                best_i=i-1;
 
327
                best_j=j-1;
 
328
                best_c = sub;
 
329
            }
 
330
        }
 
331
        
 
332
        if(dp_sub(i,j-1,vr1,vr2,
 
333
                  DP_path_i,DP_path_j,
 
334
                  lcf,lpf, null_sym,cost))
 
335
        {
 
336
            ins=lcf(null_sym,vr2(j)) + cost(i,j-1);
 
337
            if(ins < best_c)
 
338
            {
 
339
                best_i=i;
 
340
                best_j=j-1;
 
341
                best_c = ins;
 
342
            }
 
343
        }
 
344
        
 
345
        if(dp_sub(i-1,j,vr1,vr2,
 
346
                  DP_path_i,DP_path_j,
 
347
                  lcf,lpf, null_sym,cost))
 
348
        {
 
349
            del=lcf(vr1(i),null_sym) + cost(i-1,j);
 
350
            if(del < best_c)
 
351
            {
 
352
                best_i=i-1;
 
353
                best_j=j;
 
354
                best_c = del;
 
355
            }
 
356
        }
 
357
    }
 
358
 
 
359
    cost.a(i,j) = best_c;
 
360
    DP_path_i.a_no_check(i,j) = best_i;
 
361
    DP_path_j.a_no_check(i,j) = best_j;
 
362
 
 
363
 
 
364
    //cerr << "best " << i << "," << j << " = " << best_c << endl;
 
365
 
 
366
    if(best_c == MAXFLOAT)
 
367
        // didn't find a better path
 
368
        return FALSE;
 
369
    else
 
370
        return TRUE;
 
371
 
 
372
}
 
373
 
 
374
 
 
375
void trace_back_and_link(int i, int j,
 
376
                         EST_Item *p1, EST_Item *p2,
 
377
                         const EST_IMatrix &DP_path_i, 
 
378
                         const EST_IMatrix &DP_path_j,
 
379
                         EST_Item *null_sym)
 
380
{
 
381
 
 
382
    //cerr << "trace " << i << " " << j << endl;
 
383
 
 
384
 
 
385
    //int i,j;
 
386
    //i=utt.relation("Lexical")->index(p1);
 
387
    //j=utt.relation("Surface")->index(p2);
 
388
 
 
389
    if((p1==0)&&(p2==0))
 
390
        // reached start
 
391
        return;
 
392
 
 
393
    if(DP_path_i(i,j) == i-1)
 
394
    {
 
395
        if(DP_path_j(i,j) == j-1)
 
396
        {
 
397
            // match, or substitution
 
398
            //cerr << "sub " << p1->name() << " with " << p2->name() << endl;
 
399
            p1->append_daughter(p2);
 
400
            p1=p1->prev();
 
401
            p2=p2->prev();
 
402
        }
 
403
        else
 
404
            // deletion
 
405
            p1=p1->prev();
 
406
    }    
 
407
    else
 
408
    {
 
409
        // insertion
 
410
        // p1->append_daughter(p2); // decorative
 
411
        p2=p2->prev();
 
412
    }
 
413
 
 
414
    trace_back_and_link(DP_path_i(i,j), DP_path_j(i,j),
 
415
                        p1,p2,
 
416
                        DP_path_i,DP_path_j,
 
417
                        null_sym);
 
418
}
 
419