Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
EST_Features.h
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1998 */
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 : March 1998 */
35 /*-----------------------------------------------------------------------*/
36 /* A class for feature value pairs */
37 /*=======================================================================*/
38 #ifndef __EST_FEATURES_H__
39 #define __EST_FEATURES_H__
40 
41 #include "EST_TKVL.h"
42 #include "EST_Val.h"
43 #include "EST_types.h"
44 #include "EST_TIterator.h"
45 #include "EST_error.h"
46 
47 class EST_TokenStream;
48 class EST_String;
49 
50 VAL_REGISTER_CLASS_DCLS(feats,EST_Features)
51 
52 // This shouldn't be here and only is for older code
53 typedef EST_Val (*EST_Item_featfunc)(class EST_Item *);
54 EST_Val est_val(const EST_Item_featfunc f);
55 
56 
57 /** A class for containing feature structures which can hold atomic
58 values (int, float, string) or other feature structures.
59 */
60 
61 
62 class EST_Features {
63  protected:
65 
66  void save_fpair(ostream &outf,
67  const EST_String &fname,
68  const EST_Val &fvalue) const;
69  public:
70  static EST_Val feature_default_value;
71  EST_Features();
72  EST_Features(const EST_Features &f);
73  ~EST_Features();
74 
75  /**@name Access functions which return EST_Val.
76  Features can
77  either be simple features, in which their name is the name of
78  an plain attribute (e.g. "name"), or path features where their
79  name is a dot separated path of concatenated attributes
80  (e.g. "df.poa.alveolar").
81  */
82  //@{
83  /** Look up directly without decomposing name as path (just simple feature)
84  */
85  const EST_Val &val(const char *name) const;
86 
87  /** Look up directly without decomposing name as path (just simple feature),
88  returning <parameter>def</parameter> if not found
89  */
90  const EST_Val &val(const char *name, const EST_Val &def) const;
91 
92  /** Look up feature name, which may be simple feature or path
93  */
94  const EST_Val &val_path(const EST_String &path) const;
95 
96  /** Look up feature name, which may be simple feature or path,
97  returning <parameter>def</parameter> if not found
98  */
99  const EST_Val &val_path(const EST_String &path, const EST_Val &def) const;
100 
101  /** Look up feature name, which may be simple feature or path.
102  */
103  const EST_Val &operator() (const EST_String &path) const
104  {return val_path(path);}
105 
106  /** Look up feature name, which may be simple feature or path,
107  returning <parameter>def</parameter> if not found
108  */
109  const EST_Val &operator() (const EST_String &path, const EST_Val &def) const
110  {return val_path(path, def);}
111 
112  /** Look up feature name, which may be simple feature or path.
113  */
114  const EST_Val &f(const EST_String &path) const
115  { return val_path(path); }
116 
117  /** Look up feature name, which may be simple feature or path,
118  returning <parameter>def</parameter> if not found
119  */
120  const EST_Val &f(const EST_String &path, const EST_Val &def) const
121  { return val_path(path,def); }
122  //@}
123 
124  /**@name Access functions which return types.
125  These functions cast
126  their EST_Val return value to a requested type, either float,
127  int, string or features (A). In all cases the name can be a
128  simple feature or a path, in which case their name is a dot
129  separated string of concatenated attributes
130  (e.g. "df.poa.alveolar"). */
131  //@{
132 
133  /** Look up feature name, which may be simple feature or path, and return
134  as a float */
135  const float F(const EST_String &path) const
136  {return val_path(path).Float(); }
137 
138  /** Look up feature name, which may be simple feature or path, and
139  return as a float, returning <parameter>def</parameter> if not
140  found */
141  const float F(const EST_String &path, float def) const
142  {return val_path(path, def).Float(); }
143 
144  /** Look up feature name, which may be simple feature or path, and return
145  as an int */
146  const int I(const EST_String &path) const
147  {return val_path(path).Int(); }
148 
149  /** Look up feature name, which may be simple feature or path, and
150  return as an int, returning <parameter>def</parameter> if not
151  found */
152  const int I(const EST_String &path, int def) const
153  {return val_path(path, def).Int(); }
154 
155  /** Look up feature name, which may be simple feature or path, and return
156  as a EST_String */
157  const EST_String S(const EST_String &path) const
158  {return val_path(path).string(); }
159 
160  /** Look up feature name, which may be simple feature or path, and
161  return as a EST_String, returning <parameter>def</parameter> if not
162  found */
163 
164  const EST_String S(const EST_String &path, const EST_String &def) const
165  {return val_path(path, def).string(); }
166 
167  /** Look up feature name, which may be simple feature or path, and return
168  as a EST_Features */
169  EST_Features &A(const EST_String &path) const
170  {return *feats(val_path(path));}
171 
172  /** Look up feature name, which may be simple feature or path, and
173  return as a EST_Features, returning <parameter>def</parameter> if not
174  found */
175  EST_Features &A(const EST_String &path, EST_Features &def) const;
176 
177  //@}
178 
179  /**@name Setting features
180  */
181  //@{
182  /** Add a new feature or set an existing feature <parameter>name<parameter>
183  to value <parameter>ival</parameter>
184  */
185  void set(const EST_String &name, int ival)
186  { EST_Val pv(ival); set_path(name, pv);}
187 
188  /** Add a new feature or set an existing feature <parameter>name<parameter>
189  to value <parameter>fval</parameter>
190  */
191  void set(const EST_String &name, float fval)
192  { EST_Val pv(fval); set_path(name, pv); }
193 
194  /** Add a new feature or set an existing feature <parameter>name<parameter>
195  to value <parameter>dval</parameter>
196  */
197  void set(const EST_String &name, double dval)
198  { EST_Val pv((float)dval); set_path(name, pv); }
199 
200  /** Add a new feature or set an existing feature <parameter>name<parameter>
201  to value <parameter>sval</parameter>
202  */
203  void set(const EST_String &name, const EST_String &sval)
204  { EST_Val pv(sval); set_path(name, pv); }
205 
206  /** Add a new feature or set an existing feature <parameter>name<parameter>
207  to value <parameter>cval</parameter>
208  */
209  void set(const EST_String &name, const char *cval)
210  { EST_Val pv(cval); set_path(name, pv); }
211 
212  /** Add a new feature or set an existing feature <parameter>name<parameter>
213  to value <parameter>val<parameter>. <parameter>Name<parameter> must be
214  not be a path.
215  */
216  void set_val(const EST_String &name, const EST_Val &sval)
217  { features->add_item(name,sval); }
218 
219  /** Add a new feature or set an existing feature <parameter>name<parameter>
220  to value <parameter>val<parameter>, where <parameter>name<parameter>
221  is a path.
222  */
223  void set_path(const EST_String &name, const EST_Val &sval);
224 
225  /** Add a new feature feature or set an existing feature
226  <parameter>name<parameter> to value <parameter>f</parameter>, which
227  is the named of a registered feature function.
228  */
229  void set_function(const EST_String &name, const EST_String &f);
230 
231  /** Add a new feature or set an existing feature
232  <parameter>name<parameter> to value <parameter>f</parameter>,
233  which itself is a EST_Features. The information in
234  <parameter>f</parameter> is copied into the features. */
235  void set(const EST_String &name, EST_Features &f)
236  { EST_Features *ff = new EST_Features(f);
237  set_path(name, est_val(ff)); }
238 
239  //@}
240 
241  /**@name Utility functions
242  */
243 
244  //@{
245  /** remove the named feature */
246  void remove(const EST_String &name)
247  { features->remove_item(name,1); }
248 
249  /** number of features in feature structure */
250  int length() const { return features->length(); }
251 
252  /** return 1 if the feature is present */
253  int present(const EST_String &name) const;
254 
255  /** Delete all features from object */
256  void clear() { features->clear(); }
257 
258  /** Feature assignment */
259  EST_Features& operator = (const EST_Features& a);
260  /** Print Features */
261  friend ostream& operator << (ostream &s, const EST_Features &f)
262  { f.save(s); return s; }
263  //@}
264 
265 
266  // Iteration
267 #if 0
268  EST_Litem *head() const { return features->list.head(); }
269  EST_String &fname(EST_Litem *p) const { return features->list(p).k; }
270  EST_Val &val(EST_Litem *p) const { return features->list(p).v; }
271  float F(EST_Litem *p) const { return features->list(p).v.Float(); }
272  EST_String S(EST_Litem *p) const { return features->list(p).v.string(); }
273  int I(EST_Litem *p) const { return features->list(p).v.Int(); }
274  EST_Features &A(EST_Litem *p) { return *feats(features->list(p).v); }
275 #endif
276 
277 
278 
279  protected:
281 
282  void point_to_first(IPointer &ip) const
283  { ip.i.begin(*features);}
284  void move_pointer_forwards(IPointer &ip) const
285  { ++(ip.i); }
286  bool points_to_something(const IPointer &ip) const
287  { return ip.i != 0; }
288  EST_TKVI<EST_String, EST_Val> &points_at(const IPointer &ip)
289  { return *(ip.i); }
290 
291  friend class EST_TIterator< EST_Features, IPointer, EST_TKVI<EST_String, EST_Val> >;
292  friend class EST_TStructIterator< EST_Features, IPointer, EST_TKVI<EST_String, EST_Val> >;
293  friend class EST_TRwIterator< EST_Features, IPointer, EST_TKVI<EST_String, EST_Val> >;
294  friend class EST_TRwStructIterator< EST_Features, IPointer, EST_TKVI<EST_String, EST_Val> >;
295 
296 public:
297 
298  /**@name Iteration
299  */
300 
301  //@{
302  typedef EST_TKVI<EST_String, EST_Val> Entry;
305  //@}
306 
307  /**@name File I/O
308  */
309 
310  //@{
311  /// load features from already opened EST_TokenStream
312  EST_read_status load(EST_TokenStream &ts);
313  /// load features from sexpression, contained in already opened EST_TokenStream
314  EST_read_status load_sexpr(EST_TokenStream &ts);
315  /// save features in already opened ostream
316  EST_write_status save(ostream &outf) const;
317  /// save features as s-expression in already opened ostream
318  EST_write_status save_sexpr(ostream &outf) const;
319 
320  //@}
321 };
322 
323 inline bool operator == (const EST_Features &a,const EST_Features &b)
324 {(void)a; (void)b; return false;}
325 
326 void merge_features(EST_Features &to,EST_Features &from);
327 EST_String error_name(const EST_Features &a);
328 
329 #endif
330