~ubuntu-branches/ubuntu/maverick/speech-tools/maverick

« back to all changes in this revision

Viewing changes to stats/EST_DProbDist.cc

  • Committer: Bazaar Package Importer
  • Author(s): Kumar Appaiah, Kartik Mistry, Kumar Appaiah
  • Date: 2010-07-17 11:32:04 UTC
  • mfrom: (3.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100717113204-mnse3jo236j107q8
Tags: 1:2.0.95~beta-1
[ Kartik Mistry ]
* debian/control:
  + [Lintian] Added missing ${misc:Depends}
  + Updated Standards-Version to 3.8.4 (no changes needed)
* debian/patches/const_char.diff:
  + Added missing patch header
* Removed unused patch invalid_const_char_conversion_fixes.diff

[ Kumar Appaiah ]
* New upstream release.
* Standards Version is now 3.9.0 (No changes needed)
* Update debian/rules to specify version numbers.

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
#include "EST_TKVL.h"
47
47
#include "EST_simplestats.h"
48
48
 
 
49
/* We share ints and pointers for two types of probability distributions */
 
50
/* The know discrete sets can be indexed by ints which is *much* faster  */
 
51
/* the indices pass around a pointers but the lower part contain ints in */
 
52
/* the discrete case                                                     */
 
53
/* On 64bit architectures this is a issue so we need have some macros    */
 
54
/* to help us here.                                                      */
 
55
 
 
56
const int est_64to32(void *c)
 
57
{   /* this returns the bottom end of the pointer as an unsigned int */
 
58
    /* I believe this is a safe way to do it, we check the bits in the */
 
59
    /* 64 bit int and multiply them out in the 32 bit one              */
 
60
    /* there might be better ways, but I think you'd need to think about */
 
61
    /* byte order then                                                 */
 
62
    long long l;
 
63
    int d;
 
64
    int i,x;
 
65
 
 
66
    l = (long long)c;
 
67
 
 
68
    for (i=0,d=0,x=1; i<24; i++)
 
69
    {
 
70
        if (l & 1)
 
71
            d += x;
 
72
        l = l >> 1;
 
73
        x += x;
 
74
    }
 
75
 
 
76
    return d;
 
77
}
 
78
/* #define tprob_int(X) ((sizeof(void *) != 8) ? est_64to32(X) : (int)X) */
 
79
#define tprob_int(X) (est_64to32(X))
 
80
 
 
81
 
49
82
EST_DiscreteProbDistribution::EST_DiscreteProbDistribution(const EST_Discrete *d,
50
83
                 const double n_samples, const EST_DVector &counts)
51
84
{
109
142
        icounts.a_no_check(i) = 0;
110
143
}
111
144
 
112
 
void EST_DiscreteProbDistribution::cumulate(const int i,double count)
 
145
void EST_DiscreteProbDistribution::cumulate(EST_Litem *i,double count)
 
146
{
 
147
    icounts[tprob_int(i)] += count;
 
148
    num_samples += count;
 
149
}
 
150
 
 
151
void EST_DiscreteProbDistribution::cumulate(int i,double count)
113
152
{
114
153
    icounts[i] += count;
115
154
    num_samples += count;
257
296
 
258
297
}
259
298
 
 
299
void EST_DiscreteProbDistribution::set_frequency(EST_Litem *i,double c)
 
300
{
 
301
    if (type == tprob_discrete)
 
302
    {
 
303
        num_samples -= icounts[tprob_int(i)];
 
304
        num_samples += c;
 
305
        icounts[tprob_int(i)] = c;
 
306
    }
 
307
    else
 
308
    {
 
309
        cerr << "ProbDistribution: can't access string type pd with int\n";
 
310
    }
 
311
 
 
312
}
 
313
 
260
314
 
261
315
void EST_DiscreteProbDistribution::override_frequency(const EST_String &s,double c)
262
316
{
274
328
        cerr << "ProbDistribution: can't access string type pd with int\n";
275
329
}
276
330
 
 
331
void EST_DiscreteProbDistribution::override_frequency(EST_Litem *i,double c)
 
332
{
 
333
    if (type == tprob_discrete)
 
334
        icounts[tprob_int(i)] = c;
 
335
    else
 
336
        cerr << "ProbDistribution: can't access string type pd with int\n";
 
337
}
 
338
 
277
339
double EST_DiscreteProbDistribution::entropy() const
278
340
{
279
341
    // Returns the entropy of the current distribution
305
367
}
306
368
 
307
369
//  For iterating through members of a probability distribution
308
 
int EST_DiscreteProbDistribution::item_start(void) const
309
 
{
310
 
    if (type == tprob_discrete)
311
 
        return 0;
312
 
    else
313
 
        return (int)scounts.list.head();
314
 
}
315
 
 
316
 
int EST_DiscreteProbDistribution::item_end(int idx) const
317
 
{
318
 
    if (type == tprob_discrete)
319
 
        return (idx >= icounts.length());
320
 
    else
321
 
        return ((EST_Litem *)idx == 0);
322
 
}
323
 
 
324
 
int EST_DiscreteProbDistribution::item_next(int idx) const
325
 
{
326
 
    if (type == tprob_discrete)
327
 
        return ++idx;
328
 
    else
329
 
        return (int)next((EST_Litem *)idx);
330
 
}
331
 
 
332
 
const EST_String &EST_DiscreteProbDistribution::item_name(int idx) const
333
 
{
334
 
    if (type == tprob_discrete)
335
 
        return discrete->name(idx);
336
 
    else
337
 
        return scounts.list((EST_Litem *)idx).k;
338
 
}
339
 
 
340
 
void EST_DiscreteProbDistribution::item_freq(int idx,EST_String &s,double &freq) const
341
 
{
342
 
    if (type == tprob_discrete)
343
 
    {
344
 
        s = discrete->name(idx);
345
 
        freq = icounts(idx);
346
 
    }
347
 
    else
348
 
    {
349
 
        s = scounts.list((EST_Litem *)idx).k;
350
 
        freq = scounts.list((EST_Litem *)idx).v;
351
 
    }
352
 
}
353
 
 
354
 
void EST_DiscreteProbDistribution::item_prob(int idx,EST_String &s,double &prob) const
355
 
{
356
 
    if (type == tprob_discrete)
357
 
    {
358
 
        prob = probability(idx);
359
 
        s = discrete->name(idx);
360
 
    }
361
 
    else
362
 
    {
363
 
        s = scounts.list((EST_Litem *)idx).k;
364
 
        prob = (double)scounts.list((EST_Litem *)idx).v/num_samples;
 
370
EST_Litem *EST_DiscreteProbDistribution::item_start(void) const
 
371
{
 
372
    if (type == tprob_discrete)
 
373
        return NULL;
 
374
    else
 
375
        return scounts.list.head();
 
376
}
 
377
 
 
378
int EST_DiscreteProbDistribution::item_end(EST_Litem *idx) const
 
379
{
 
380
    if (type == tprob_discrete)
 
381
        return (tprob_int(idx) >= icounts.length());
 
382
    else
 
383
        return (idx == 0);
 
384
}
 
385
 
 
386
EST_Litem *EST_DiscreteProbDistribution::item_next(EST_Litem *idx) const
 
387
{
 
388
    if (type == tprob_discrete)
 
389
        return (EST_Litem *)(((unsigned char *)idx)+1);
 
390
    else
 
391
        return next(idx);
 
392
}
 
393
 
 
394
const EST_String &EST_DiscreteProbDistribution::item_name(EST_Litem *idx) const
 
395
{
 
396
    if (type == tprob_discrete)
 
397
        return discrete->name(tprob_int(idx));
 
398
    else
 
399
        return scounts.list(idx).k;
 
400
}
 
401
 
 
402
void EST_DiscreteProbDistribution::item_freq(EST_Litem *idx,EST_String &s,double &freq) const
 
403
{
 
404
    if (type == tprob_discrete)
 
405
    {
 
406
        s = discrete->name(tprob_int(idx));
 
407
        freq = icounts(tprob_int(idx));
 
408
    }
 
409
    else
 
410
    {
 
411
        s = scounts.list(idx).k;
 
412
        freq = scounts.list(idx).v;
 
413
    }
 
414
}
 
415
 
 
416
void EST_DiscreteProbDistribution::item_prob(EST_Litem *idx,EST_String &s,double &prob) const
 
417
{
 
418
    if (type == tprob_discrete)
 
419
    {
 
420
        prob = probability(tprob_int(idx));
 
421
        s = discrete->name(tprob_int(idx));
 
422
    }
 
423
    else
 
424
    {
 
425
        s = scounts.list(idx).k;
 
426
        prob = (double)scounts.list(idx).v/num_samples;
365
427
    }
366
428
}
367
429
 
368
430
ostream & operator<<(ostream &s, const EST_DiscreteProbDistribution &pd)
369
431
{
370
432
    // Output best with probabilities
371
 
    int i;
 
433
    EST_Litem *i;
372
434
    double prob;
373
435
    double sum=0;
374
436
    EST_String name;