"Fossies" - the Fresh Open Source Software Archive 
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 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 : Alan W Black */
34 /* Date : July 1996 */
35 /*-----------------------------------------------------------------------*/
36 /* */
37 /* Discretes for mapping between alphabets and indexes */
38 /* */
39 /*=======================================================================*/
40 #include <iostream>
41 #include <fstream>
42 #include <cstdlib>
43 #include <cstdio>
44 #include <cstring>
45 #include "EST_String.h"
46 #include "EST_simplestats.h"
47
48 static void Discrete_val_delete_funct(void *d) { delete (int *)d; }
49
50 EST_Discrete::~EST_Discrete()
51 {
52 nametrie.clear(Discrete_val_delete_funct);
53 }
54
55 EST_Discrete::EST_Discrete(const EST_StrList &vocab)
56 {
57 if(!init(vocab))
58 {
59 cerr << "WARNING from EST_Discrete ctor : invalid vocab list !";
60 nametrie.clear(Discrete_val_delete_funct);
61 namevector.resize(0);
62 }
63 }
64
65 void EST_Discrete::copy(const EST_Discrete &d)
66 {
67 int i;
68 p_def_val = d.p_def_val;
69 nametrie.clear(Discrete_val_delete_funct);
70 namevector = d.namevector;
71
72 for (i=0; i<namevector.length(); ++i)
73 {
74 int *t = new int;
75 *t = i;
76 nametrie.add(namevector(i),t);
77 }
78 }
79
80 bool EST_Discrete::init(const EST_StrList &vocab)
81 {
82 // initialize a new EST_Discrete to given set of names
83 EST_Litem *w;
84 int i,*tmp;
85
86 p_def_val = -1;
87 namevector.resize(vocab.length());
88 nametrie.clear(Discrete_val_delete_funct);
89
90 for (i=0,w=vocab.head(); w != 0; i++,w=w->next()){
91 namevector[i] = vocab(w);
92 tmp = new int;
93 *tmp = i;
94
95 // check for repeated items - just not allowed
96 if(nametrie.lookup(vocab(w)) != NULL)
97 {
98 cerr << "EST_Discrete : found repeated item '";
99 cerr << vocab(w) << "' in vocab list !" << endl;
100 return false;
101 }
102
103 nametrie.add(vocab(w),tmp);
104 }
105 return true;
106 }
107
108
109 bool EST_Discrete::operator ==(const EST_Discrete &d)
110 {
111 // assume, if name vectors are the same, the stringtries
112 // are too
113 return (bool)(namevector == d.namevector);
114 }
115
116 bool EST_Discrete::operator !=(const EST_Discrete &d)
117 {
118 return (bool)(namevector != d.namevector);
119 }
120
121 EST_String EST_Discrete::print_to_string(int quote)
122 {
123 EST_String s = "";
124 EST_String sep = "";
125 static EST_Regex needquotes(".*[()'\";., \t\n\r].*");
126 int i;
127
128 for(i=0;i<length();i++)
129 {
130 if ((quote) && name(i).matches(needquotes))
131 s += sep + quote_string(name(i),"\"","\\",1);
132 else
133 s += sep + name(i);
134 sep = " ";
135 }
136
137 return s;
138 }
139
140 ostream& operator <<(ostream& s, const EST_Discrete &d)
141 {
142 int i;
143 for(i=0;i<d.length();i++)
144 s << d.name(i) << " ";
145 return s;
146 }
147
148 Discretes::~Discretes()
149 {
150 int i;
151
152 for (i=0; i<next_free; i++)
153 delete discretes[i];
154 delete [] discretes;
155 }
156
157 const int Discretes::def(const EST_StrList &vocab)
158 {
159 // Define discrete, increasing the size of the table if need be
160 int i,pos;
161
162 if ((next_free == max) && (max > 0))
163 {
164 EST_Discrete **new_discretes = new EST_Discrete *[max*2];
165 for (i=0; i<next_free; i++)
166 new_discretes[i] = discretes[i];
167 max *= 2;
168 delete [] discretes;
169 discretes = new_discretes;
170 }
171
172 discretes[next_free] = new EST_Discrete(vocab);
173 pos = next_free + 10;
174 next_free++;
175
176 return pos;
177 }