Edinburgh Speech Tools  2.1-release
confusion.cc
Go to the documentation of this file.
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 : Paul Taylor */
34 /* Date : July 1995 */
35 /*-----------------------------------------------------------------------*/
36 /* Confusion Matrix Calculation */
37 /* */
38 /*=======================================================================*/
39 #include <cmath>
40 #include "EST_multistats.h"
41 #include "EST_math.h"
42 #include "EST_types.h"
43 
44 using namespace std;
45 
47 {
48  EST_Litem *p;
49  int i;
50 
51  for (i = 0, p = lex.head(); p!=0; p = p->next(), ++i)
52  if (name == lex(p))
53  return i;
54 
55  cerr << "Item " << name << " not found in word list\n";
56  return -1;
57 }
58 
60 {
61  EST_FMatrix a(lex.length(), lex.length());
62  EST_Litem *p;
63  int n, m;
64  a.fill(0.0);
65 
66  for (p = list.list.head(); p!=0; p = p->next())
67  {
68  m = nth(list.key(p), lex);
69  n = nth(list.val(p), lex);
70  if ((n != -1) && (m != -1))
71  a(m, n) = a(m, n) + 1;
72  }
73 
74  return a;
75 }
76 
78  EST_StrList &lex)
79 {
80 
81  int i, j;
82  EST_Litem *p;
83  cout << " ";
84  (void)list;
85  int n = a.num_rows();
86 
87  EST_FVector row_total(n);
88  EST_FVector col_total(n);
89  EST_FVector correct(n);
90 
91  for (i = 0; i < n; ++i)
92  {
93  row_total[i] = 0.0;
94  for (j = 0; j < n; ++j)
95  row_total[i] += a(i, j);
96  }
97 
98  for (j = 0; j < n; ++j)
99  {
100  col_total[j] = 0.0;
101  for (i = 0; i < n; ++i)
102  col_total[j] += a(i, j);
103  }
104 
105  for (i = 0; i < n; ++i)
106  {
107  float rt = row_total(i);
108  if (rt == 0)
109  correct[i] = 100;
110  else
111  correct[i] = 100.0 * a(i, i) / rt;
112  }
113 
114  for (p = lex.head(); p != 0; p = p->next())
115  {
116 // cout.width(4);
117 // cout.setf(ios::right);
118  cout << lex(p).before(3) << " ";
119  }
120  cout << endl;
121 
122  for (p = lex.head(), i = 0; i < n; ++i, p = p->next())
123  {
124  cout.width(12);
125  cout << lex(p);
126  for (j = 0; j < n; ++j)
127  {
128  cout.width(4);
129  cout.precision(3);
130  cout.setf(ios::right);
131  cout.setf(ios::fixed, ios::floatfield);
132  cout << ( (int) a(i, j) ) << " ";
133  }
134  cout.width(4);
135  cout << (int)row_total(i) << " ";
136  cout.width(4);
137  cout.setf(ios::right);
138  cout << "[" << ((int)a(i, i)) << "/" << ((int)row_total(i)) << "]";
139  cout.width(12);
140  cout.precision(3);
141  cout.setf(ios::right);
142 // cout.setf(ios::fixed, ios::floatfield);
143  if (isnanf(correct(i)))
144  cout << endl;
145  else
146  cout << correct(i) << endl;
147  }
148  cout << " ";
149  for (j = 0; j < n; ++j)
150  {
151  cout.width(4);
152  cout << ((int)col_total(j)) << " ";
153  }
154  cout << endl;
155 
156  // work out total correct
157  EST_FMatrix b;
158  float s, t, pp;
159  t = sum(a);
160  b = diagonalise(a);
161  s = sum(b);
162  if (s == 0)
163  pp = 0;
164  else if (t == 0)
165  pp = 100.0; // probably can't happen
166  else
167  pp = 100.0 * s/t;
168  cout << "total " << ((int)t) << " correct " << s << " "
169  << pp << "%"<< endl;
170 }
171 
172 
EST_FMatrix diagonalise(const EST_FMatrix &a)
extract leading diagonal as a matrix
Definition: vec_mat_aux.cc:158
const K & key(EST_Litem *ptr, int m=1) const
find key, reference by ptr
Definition: EST_TKVL.cc:201
A vector class for floating point numbers. EST_FVector x should be used instead of float *x wherever ...
Definition: EST_FMatrix.h:119
void print_confusion(const EST_FMatrix &a, EST_StrStr_KVL &list, EST_StrList &lex)
Definition: confusion.cc:77
STATIC void right(STATUS Change)
Definition: editline.c:538
int nth(EST_String name, EST_TList< EST_String > &lex)
Definition: confusion.cc:46
ssize_t num_rows() const
return number of rows
Definition: EST_TMatrix.h:177
EST_UItem * next()
Definition: EST_UList.h:55
EST_TList< EST_TKVI< K, V > > list
Linked list of key-val pairs. Don&#39;t use this as it will be made private in the future.
Definition: EST_TKVL.h:94
Templated Key-Value list. Objects of type EST_TKVL contain lists which are accessed by a key of type ...
Definition: EST_TKVL.h:73
getString int
Definition: EST_item_aux.cc:50
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
Definition: EST_TKVL.cc:145
int length() const
Definition: EST_UList.cc:57
EST_FMatrix confusion(EST_StrStr_KVL &list, EST_StrList &lex)
Definition: confusion.cc:59
EST_UItem * head() const
Definition: EST_UList.h:97
#define isnanf(N)
float sum(const EST_FMatrix &a)
sum of elements
Definition: vec_mat_aux.cc:147