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