Edinburgh Speech Tools  2.1-release
EST_Discrete.cc
Go to the documentation of this file.
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 using namespace std;
49 
50 static void Discrete_val_delete_funct(void *d) { delete (int *)d; }
51 
53 {
54  nametrie.clear(Discrete_val_delete_funct);
55 }
56 
58 {
59  if(!init(vocab))
60  {
61  cerr << "WARNING from EST_Discrete ctor : invalid vocab list !";
62  nametrie.clear(Discrete_val_delete_funct);
63  namevector.resize(0);
64  }
65 }
66 
68 {
69  int i;
70  p_def_val = d.p_def_val;
71  nametrie.clear(Discrete_val_delete_funct);
72  namevector = d.namevector;
73 
74  for (i=0; i<namevector.length(); ++i)
75  {
76  int *t = new int;
77  *t = i;
78  nametrie.add(namevector(i),t);
79  }
80 }
81 
83 {
84  // initialize a new EST_Discrete to given set of names
85  EST_Litem *w;
86  int i,*tmp;
87 
88  p_def_val = -1;
89  namevector.resize(vocab.length());
90  nametrie.clear(Discrete_val_delete_funct);
91 
92  for (i=0,w=vocab.head(); w != 0; i++,w=w->next()){
93  namevector[i] = vocab(w);
94  tmp = new int;
95  *tmp = i;
96 
97  // check for repeated items - just not allowed
98  if(nametrie.lookup(vocab(w)) != NULL)
99  {
100  cerr << "EST_Discrete : found repeated item '";
101  cerr << vocab(w) << "' in vocab list !" << endl;
102  delete tmp;
103  return false;
104  }
105 
106  nametrie.add(vocab(w),tmp);
107  }
108  return true;
109 }
110 
111 
113 {
114  // assume, if name vectors are the same, the stringtries
115  // are too
116  return (bool)(namevector == d.namevector);
117 }
118 
120 {
121  return (bool)(namevector != d.namevector);
122 }
123 
125 {
126  EST_String s = "";
127  EST_String sep = "";
128  static EST_Regex needquotes(".*[()'\";., \t\n\r].*");
129  int i;
130 
131  for(i=0;i<length();i++)
132  {
133  if ((quote) && name(i).matches(needquotes))
134  s += sep + quote_string(name(i),"\"","\\",1);
135  else
136  s += sep + name(i);
137  sep = " ";
138  }
139 
140  return s;
141 }
142 
143 ostream& operator <<(ostream& s, const EST_Discrete &d)
144 {
145  int i;
146  for(i=0;i<d.length();i++)
147  s << d.name(i) << " ";
148  return s;
149 }
150 
152 {
153  int i;
154 
155  for (i=0; i<next_free; i++)
156  delete discretes[i];
157  delete[] discretes;
158 }
159 
161 {
162  // Define discrete, increasing the size of the table if need be
163  int i,pos;
164 
165  if (next_free == max)
166  {
167  EST_Discrete **new_discretes = new EST_Discrete *[max*2];
168  for (i=0; i<next_free; i++)
169  new_discretes[i] = discretes[i];
170  max *= 2;
171  delete[] discretes;
172  discretes = new_discretes;
173  }
174 
175  discretes[next_free] = new EST_Discrete(vocab);
176  pos = next_free + 10;
177  next_free++;
178 
179  return pos;
180 }
A Regular expression class to go with the CSTR EST_String class.
Definition: EST_Regex.h:56
const EST_String & name(const int n) const
The name given the index.
int length(void) const
The number of members in the discrete.
int def(const EST_StrList &members)
EST_UItem * next()
Definition: EST_UList.h:55
float max(float a, float b)
Definition: EST_cluster.cc:143
void copy(const EST_Discrete &d)
Definition: EST_Discrete.cc:67
NULL
Definition: EST_WFST.cc:55
getString int
Definition: EST_item_aux.cc:50
bool operator!=(const EST_Discrete &d)
int length() const
Definition: EST_UList.cc:57
LISP quote(LISP item)
Definition: siod.cc:252
bool init(const EST_StrList &vocab)
(re-)initialise
Definition: EST_Discrete.cc:82
bool operator==(const EST_Discrete &d)
EST_UItem * head() const
Definition: EST_UList.h:97
EST_String print_to_string(int quote=0)
EST_String quote_string(const EST_String &s, const EST_String &quote, const EST_String &escape, int force)
Definition: EST_Token.cc:844
ostream & operator<<(ostream &s, const EST_Discrete &d)
EST_StrVector vocab
Definition: dp_main.cc:85