Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
confusion.cc
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 int nth(EST_String name, EST_TList<EST_String> &lex)
45 {
46  EST_Litem *p;
47  int i;
48 
49  for (i = 0, p = lex.head(); p!=0; p = p->next(), ++i)
50  if (name == lex(p))
51  return i;
52 
53  cerr << "Item " << name << " not found in word list\n";
54  return -1;
55 }
56 
57 EST_FMatrix confusion(EST_StrStr_KVL &list, EST_StrList &lex)
58 {
59  EST_FMatrix a(lex.length(), lex.length());
60  EST_Litem *p;
61  int n, m;
62  a.fill(0.0);
63 
64  for (p = list.list.head(); p!=0; p = p->next())
65  {
66  m = nth(list.key(p), lex);
67  n = nth(list.val(p), lex);
68  if ((n != -1) && (m != -1))
69  a(m, n) = a(m, n) + 1;
70  }
71 
72  return a;
73 }
74 
75 void print_confusion(const EST_FMatrix &a, EST_StrStr_KVL &list,
76  EST_StrList &lex)
77 {
78 
79  int i, j;
80  EST_Litem *p;
81  cout << " ";
82  (void)list;
83  int n = a.num_rows();
84 
85  EST_FVector row_total(n);
86  EST_FVector col_total(n);
87  EST_FVector correct(n);
88 
89  for (i = 0; i < n; ++i)
90  {
91  row_total[i] = 0.0;
92  for (j = 0; j < n; ++j)
93  row_total[i] += a(i, j);
94  }
95 
96  for (j = 0; j < n; ++j)
97  {
98  col_total[j] = 0.0;
99  for (i = 0; i < n; ++i)
100  col_total[j] += a(i, j);
101  }
102 
103  for (i = 0; i < n; ++i)
104  {
105  float rt = row_total(i);
106  if (rt == 0)
107  correct[i] = 100;
108  else
109  correct[i] = 100.0 * a(i, i) / rt;
110  }
111 
112  for (p = lex.head(); p != 0; p = p->next())
113  {
114 // cout.width(4);
115 // cout.setf(ios::right);
116  cout << lex(p).before(3) << " ";
117  }
118  cout << endl;
119 
120  for (p = lex.head(), i = 0; i < n; ++i, p = p->next())
121  {
122  cout.width(12);
123  cout << lex(p);
124  for (j = 0; j < n; ++j)
125  {
126  cout.width(4);
127  cout.precision(3);
128  cout.setf(ios::right);
129  cout.setf(ios::fixed, ios::floatfield);
130  cout << ( (int) a(i, j) ) << " ";
131  }
132  cout.width(4);
133  cout << (int)row_total(i) << " ";
134  cout.width(4);
135  cout.setf(ios::right);
136  cout << "[" << ((int)a(i, i)) << "/" << ((int)row_total(i)) << "]";
137  cout.width(12);
138  cout.precision(3);
139  cout.setf(ios::right);
140 // cout.setf(ios::fixed, ios::floatfield);
141  if (isnanf(correct(i)))
142  cout << endl;
143  else
144  cout << correct(i) << endl;
145  }
146  cout << " ";
147  for (j = 0; j < n; ++j)
148  {
149  cout.width(4);
150  cout << ((int)col_total(j)) << " ";
151  }
152  cout << endl;
153 
154  // work out total correct
155  EST_FMatrix b;
156  float s, t, pp;
157  t = sum(a);
158  b = diagonalise(a);
159  s = sum(b);
160  if (s == 0)
161  pp = 0;
162  else if (t == 0)
163  pp = 100.0; // probably can't happen
164  else
165  pp = 100.0 * s/t;
166  cout << "total " << ((int)t) << " correct " << s << " "
167  << pp << "%"<< endl;
168 }
169 
170