Edinburgh Speech Tools  2.1-release
EST_Item.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 /* Permission is hereby granted, free of charge, to use and distribute */
8 /* this software and its documentation without restriction, including */
9 /* without limitation the rights to use, copy, modify, merge, publish, */
10 /* distribute, sublicense, and/or sell copies of this work, and to */
11 /* permit persons to whom this work is furnished to do so, subject to */
12 /* the following conditions: */
13 /* 1. The code must retain the above copyright notice, this list of */
14 /* conditions and the following disclaimer. */
15 /* 2. Any modifications must be clearly marked as such. */
16 /* 3. Original authors' names are not deleted. */
17 /* 4. The authors' names are not used to endorse or promote products */
18 /* derived from this software without specific prior written */
19 /* permission. */
20 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
21 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
22 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
23 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
24 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
25 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
26 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
27 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
28 /* THIS SOFTWARE. */
29 /* */
30 /*************************************************************************/
31 /* Author : Alan W Black */
32 /* Date : February 1998 */
33 /* --------------------------------------------------------------------- */
34 /* */
35 /* General class for representing linguistic information within a */
36 /* EST_Relation. This consists of two parts, the relation specific */
37 /* part and the information content part. The information content */
38 /* part may be shared between multiple EST_Items. */
39 /* */
40 /* This is typically used to represent things like words, phones but */
41 /* more abstract entities like NPs and nodes in metrical trees. */
42 /* */
43 /*************************************************************************/
44 #ifndef __EST_ITEM_H__
45 #define __EST_ITEM_H__
46 
47 #include "EST_String.h"
48 #include "EST_Features.h"
50 
55 
56 class EST_Relation;
57 class ling_class_init;
58 
59 
60 /** @class EST_Item
61  @ingroup estlingclasses
62 
63 A class for containing individual linguistic objects such as
64 words or phones.
65 
66 These contain two types of information. This first is specific to the
67 \ref EST_Relation we are viewing this ling item from, the second part
68 consists of a set of features. These features may be shared by
69 instances of this ling item in different EST_Relation
70 within the same EST_Utterance.
71 
72 The shared part of an EST_Item is
73 represented by the class EST_Item_Content. It should not normally be
74 accessed by the general users as reverse links from the contents to
75 each of the EST_Items it is part of are held ensure the
76 integrity of the structures. Changing these without maintain the
77 appropriate links is unlikely to be stable.
78 
79 We believe this structure is the most efficient for the most natural
80 use we envisage. Traversal of the items ....
81 
82 */
83 class EST_Item
84 {
85  private:
86  EST_Item_Content *p_contents;
87  EST_Relation *p_relation;
88  // In general (when we need it)
89  // EST_TKVL <EST_String, EST_Item *> arcs;
90  // but specifically
91  EST_Item *n;
92  EST_Item *p;
93  EST_Item *u;
94  EST_Item *d;
95 
96  void unref_contents();
97  void ref_contents();
98  void copy(const EST_Item &s);
99 
100  // Internal manipulation functions
101  // Get the daughters of node, removing reference to them
102  EST_Item *grab_daughters(void);
103  /* Get the contents, removing reference to them, this doesn't
104  delete the contents if this item is the only reference */
105  EST_Item_Content *grab_contents(void);
106 
107 protected:
108  static void class_init(void);
109 
110  public:
111  /**@name Constructor Functions */
112  ///@{
113  /// Default constructor
114  EST_Item();
115  /// Copy constructor only makes reference to contents
116  EST_Item(const EST_Item &item);
117  /// Includes reference to relation
118  EST_Item(EST_Relation *rel);
119  /// Most common form of construction
120  EST_Item(EST_Relation *rel, EST_Item *si);
121  /// Deletes it and references to it in its contents
122  ~EST_Item();
123  ///@}
124 
125 
126  /**@name Feature access functions.
127  These functions are wrap-around functions to the basic access
128  functions in the \ref EST_Features class. In all these
129  functions, if the optional argument `m` is set to 1, an
130  error is thrown if the feature does not exist*/
131 
132  ///@{
133  /** return the value of the feature `name`
134  cast as a float */
135  float F(const EST_String &name) const {return f(name).Float();}
136 
137  /** return the value of the feature `name` cast
138  as a float, returning `def` if not found.*/
139  float F(const EST_String &name,float def) const
140  {return f(name,def).Float();}
141 
142  /** return the value of the feature `name`
143  cast as a EST_String */
144  const EST_String S(const EST_String &name) const {return f(name).string();}
145 
146  /** return the value of the feature `name`
147  cast as a EST_String,
148  returning `def` if not found.
149  */
150  const EST_String S(const EST_String &name, const EST_String &def) const
151  {return f(name, def).string();}
152 
153  /** return the value of the feature `name`
154  cast as a int */
155  int I(const EST_String &name) const {return f(name).Int();}
156 
157  /** return the value of the feature `name` cast as a int
158  returning `def` if not found.*/
159  int I(const EST_String &name, int def) const
160  {return f(name, def).Int();}
161 
162  /** return the value of the feature `name`
163  cast as a EST_Features */
164  EST_Features &A(const EST_String &name) const {return *feats(f(name));}
165 
166  /** return the value of the feature `name`
167  cast as a EST_Features,
168  returning `def` if not found.
169  */
171  {EST_Features *ff = new EST_Features(def);
172  return *feats(f(name, est_val(ff)));}
173  ///@}
174 
175  /**@name Feature setting functions.
176  A separate function is provided for each permissible value type
177  */
178  ///@{
179  #if (SSIZE_MAX != INT_MAX)
180  /** set feature `name` to `val` */
181  void set(const EST_String &name, int ival)
182  { EST_Val pv(ival);features().set_path(name, pv); }
183  #endif
184  /** set feature `name` to `val` */
185  void set(const EST_String &name, ssize_t ival)
186  { EST_Val pv(ival);features().set_path(name, pv); }
187 
188  /** set feature `name` to `val` */
189  void set(const EST_String &name, float fval)
190  { EST_Val pv(fval); features().set_path(name,pv); }
191 
192  /** set feature `name` to `val` */
193  void set(const EST_String &name, double fval)
194  { EST_Val pv((float)fval);features().set_path(name,pv); }
195 
196  /** set feature `name` to `val` */
197  void set(const EST_String &name, const EST_String &sval)
198  { EST_Val pv(sval);features().set_path(name,pv); }
199 
200  /** set feature `name` to `val` */
201  void set(const EST_String &name, const char *cval)
202  { EST_Val pv(cval);features().set_path(name,pv); }
203 
204  /** set feature `name` to `val`,
205  a function registered in
206  the feature function list. */
207  void set_function(const EST_String &name, const EST_String &funcname)
208  { features().set_function(name,funcname); }
209 
210  /** set feature `name` to `f`,
211  a set of features, which is copied into the object.
212  */
213  void set(const EST_String &name, EST_Features &f)
214  { EST_Features *ff = new EST_Features(f);
215  features().set_path(name, est_val(ff)); }
216 
217  /** set feature `name` to `f`,
218  whose type is EST_Val.
219  */
220  void set_val(const EST_String &name, const EST_Val &sval)
221  { features().set_path(name,sval); }
222  ///@}
223 
224  /**@name Utility feature functions
225  */
226  ///@{
227  /** remove feature `name` */
228  void f_remove(const EST_String &name)
229  { features().remove(name); }
230 
231  /** find all the attributes whose values are functions, and
232  replace them with their evaluation. */
233  void evaluate_features();
234 
235  /** TRUE if feature is present, FALSE otherwise */
236  int f_present(const EST_String &name) const
237  {return features().present(name); }
238 
239  // Number of items (including this) until no next item.
240  int length() const;
241  ///@}
242 
243  // get contents from item
244  EST_Item_Content *contents() const { return (this == 0) ? 0 : p_contents;}
245  // used by tree manipulation functions
246  void set_contents(EST_Item_Content *li);
247 
248  // These should be deleted.
249  // The item's name
250  const EST_String name() const
251  { return (this == 0) ? EST_String::Empty : f("name",0).string(); }
252 
253  // Set item's name
254  void set_name(const EST_String &name) const
255  { if (this != 0) p_contents->set_name(name); }
256 
257  // Shouldn't normally be needed, except for iteration
258  EST_Features &features() const { return p_contents->f; }
259 
260  const EST_Val f(const EST_String &name) const
261  {
262  EST_Val v;
263  if (this == 0)
264  {
265  EST_error("item is null so has no %s feature",(const char *)name);
266  }
267  else
268  {
269  for (v=p_contents->f.val_path(name);
270  v.type() == val_type_featfunc && featfunc(v) != NULL;
271  v=(featfunc(v))((EST_Item *)(void *)this));
272  if (v.type() == val_type_featfunc)
273  EST_error("NULL %s function",(const char *)name);
274  }
275  return v;
276  }
277 
278 #if 0
279  const EST_Val &f(const EST_String &name, const EST_Val &def) const
280  {
281  if (this == 0)
282  return def;
283  else
284  {
285  const EST_Val *v;
286  for (v=&(p_contents->f.val_path(name, def));
287  v->type() == val_type_featfunc && featfunc(*v) != NULL;
288  v=&(featfunc(*v))((EST_Item *)(void *)this));
289  if (v->type() == val_type_featfunc)
290  v = &def;
291  return *v;
292  }
293  }
294 #endif
295 
296  const EST_Val f(const EST_String &name, const EST_Val &def) const
297  {
298  if (this == 0)
299  return def;
300  else
301  {
302  EST_Val v;
303  for (v=p_contents->f.val_path(name, def);
304  v.type() == val_type_featfunc && featfunc(v) != NULL;
305  v=(featfunc(v))((EST_Item *)(void *)this));
306  if (v.type() == val_type_featfunc)
307  v = def;
308  return v;
309  }
310  }
311 
312  /**@name Cross relational access */
313  ///@{
314 
315  /// View item from another relation (const char *) method
316  EST_Item *as_relation(const char *relname) const
317  { return (this == 0) ? 0 : p_contents->Relation(relname); }
318 
319  /// TRUE if this item is in named relation
320  int in_relation(const EST_String &relname) const
321  { return (this == 0) ? 0 : p_contents->in_relation(relname); }
322 
323  /// Access to the relation links
325 
326  /// The relation name of this particular item
327  const EST_String &relation_name() const;
328 
329  /// The relation of this particular item
330  EST_Relation *relation(void) const
331  { return (this == 0) ? 0 : p_relation; }
332 
333  /// True if li is the same item ignoring its relation viewpoint
334  int same_item(const EST_Item *li) const
335  { return contents() && li->contents() && (contents() == li->contents()); }
336  ///@}
337 
338  // The remaining functions should not be accessed, they are should be
339  // regarded as private member functions
340 
341  // Splice together a broken list.
342 
343  static void splice(EST_Item *a, EST_Item *b)
344  { if(a !=NULL) a->n = b; if (b != NULL) b->p=a; }
345 
346  // Internal traversal - nnot recommended - use relation traversal functions
347  //
348  EST_Item *next() const { return this == 0 ? 0 : n; }
349  //
350  EST_Item *prev() const { return this == 0 ? 0 : p; }
351  //
352  EST_Item *down() const { return this == 0 ? 0 : d; }
353  //
354  EST_Item *up() const { return this == 0 ? 0 : u; }
355  // Last item (most next) at this level
356  EST_Item *last() const;
357  // First item (most prev) at this level
358  EST_Item *first() const;
359  // Highest (most up)
360  EST_Item *top() const;
361  // Lowest (most down)
362  EST_Item *bottom() const;
363  // First item which has no down, within the descendants of this item
364  EST_Item *first_leaf() const;
365  // Next item which has no down, following above this item if necessary
366  EST_Item *next_leaf() const;
367  // Last item which has no down, following above this item if necessary
368  EST_Item *last_leaf() const;
369  // Next item in pre order (root, daughters, siblings)
370  EST_Item *next_item() const;
371 
372 
373  // Insert a new item after this, with li's contents
375  // Insert a new item before this, with li's contents
377  // Insert a new item below this, with li's contents (see tree methods)
379  // Insert a new item above this, with li's contents (see tree methods)
381 
382  // Append a new daughter to this, with li's contents
384  // Prepend a new daughter to this, with li's contents
386  // Insert a new parent above this, with li's contents
388 
389  // Delete this item and all its occurrences in other relations
390  void unref_all();
391 
392  // Verification, double links are consistent (used after reading in)
393  int verify() const;
394 
395  friend int i_same_item(const EST_Item *l1,const EST_Item *l2);
396  friend int move_item(EST_Item *from, EST_Item *to);
397  friend int merge_item(EST_Item *from, EST_Item *to);
398  friend int move_sub_tree(EST_Item *from, EST_Item *to);
399  friend int exchange_sub_trees(EST_Item *from,EST_Item *to);
400 
401  EST_Item &operator=(const EST_Item &s);
402  friend ostream& operator << (ostream &s, const EST_Item &a);
403  friend bool operator !=(const EST_Item &a, const EST_Item &b)
404  { return !i_same_item(&a,&b); }
405  friend bool operator ==(const EST_Item &a, const EST_Item &b)
406  { return i_same_item(&a,&b); }
407 
408  friend class EST_Relation;
409  friend class ling_class_init;
410 };
411 
412 inline int i_same_item(const EST_Item *l1,const EST_Item *l2)
413 {
414  return l1->contents() && l2->contents() &&
415  (l1->contents() == l2->contents());
416 }
417 
418 
419 inline EST_Item *as(const EST_Item *n,const char *relname)
420  { return n->as_relation(relname); }
421 
422 // Relation structure functions
426 
427 inline EST_Item *next_item(const EST_Item *node)
428  { return node->next_item(); }
429 
430 void remove_item(EST_Item *l, const char *relname);
431 
432 void copy_node_tree(EST_Item *from, EST_Item *to);
433 void copy_node_tree_contents(EST_Item *from, EST_Item *to);
434 
435 void evaluate(EST_Item *a,EST_Features &f);
436 
437 
439 
440 // Feature function support
441 void EST_register_feature_function_package(const char *name, void (*init_fn)(EST_FeatureFunctionPackage &p));
442 
443 void register_featfunc(const EST_String &name, const EST_Item_featfunc func);
444 EST_Item_featfunc get_featfunc(const EST_String &name,int must=0);
446 
447 #define EST_register_feature_functions(PACKAGE) \
448  do { \
449  extern void register_ ## PACKAGE ## _feature_functions(EST_FeatureFunctionPackage &p); \
450  EST_register_feature_function_package( #PACKAGE , register_ ## PACKAGE ## _feature_functions); \
451  } while(0)
452 
453 
454 #endif
EST_Item * insert_below(EST_Item *li=0)
Definition: EST_Item.cc:280
int Int(void) const
Definition: EST_Val.h:141
EST_Item * Relation(const char *name)
EST_Item_featfunc featfunc(const EST_Val &v)
Definition: item_feats.cc:100
void set_function(const EST_String &name, const EST_String &funcname)
Definition: EST_Item.h:207
EST_Val(* EST_Item_featfunc)(EST_Item *s)
Definition: EST_Item.h:51
EST_Item * last_leaf() const
Definition: EST_Item.cc:397
void evaluate_features()
Definition: EST_Item.cc:172
void set_function(const EST_String &name, const EST_String &f)
friend bool operator==(const EST_Item &a, const EST_Item &b)
Definition: EST_Item.h:405
const EST_String name() const
Definition: EST_Item.h:250
EST_Item & operator=(const EST_Item &s)
Definition: EST_Item.cc:623
int same_item(const EST_Item *li) const
True if li is the same item ignoring its relation viewpoint.
Definition: EST_Item.h:334
void set_contents(EST_Item_Content *li)
Definition: EST_Item.cc:202
EST_Item * as(const EST_Item *n, const char *relname)
Definition: EST_Item.h:419
EST_Item * last() const
Definition: EST_Item.cc:329
EST_Item * first() const
Definition: EST_Item.cc:339
EST_Item * insert_above(EST_Item *li=0)
Definition: EST_Item.cc:294
int I(const EST_String &name) const
Definition: EST_Item.h:155
EST_Item * append_daughter(EST_Item *li=0)
Definition: EST_Item.cc:425
int in_relation(const EST_String &relname) const
TRUE if this item is in named relation.
Definition: EST_Item.h:320
~EST_Item()
Deletes it and references to it in its contents.
Definition: EST_Item.cc:108
void EST_register_feature_function_package(const char *name, void(*init_fn)(EST_FeatureFunctionPackage &p))
Definition: item_feats.cc:88
EST_Item * bottom() const
EST_String get_featname(const EST_Item_featfunc func)
Definition: item_feats.cc:76
const char * val_type
Definition: EST_Val.h:57
int I(const EST_String &name, int def) const
Definition: EST_Item.h:159
EST_Item * next_item() const
Definition: EST_Item.cc:368
int ssize_t
void register_featfunc(const EST_String &name, const EST_Item_featfunc func)
Definition: item_feats.cc:65
EST_Item * as_relation(const char *relname) const
View item from another relation (const char *) method.
Definition: EST_Item.h:316
EST_Features f
General features for this item.
EST_Item * insert_parent(EST_Item *li=0)
Definition: EST_Item.cc:313
void unref_all()
Definition: EST_Item.cc:189
const EST_Val f(const EST_String &name) const
Definition: EST_Item.h:260
void set_path(const EST_String &name, const EST_Val &sval)
EST_Val est_val(const EST_Item_featfunc f)
Definition: item_feats.cc:122
EST_Item()
Default constructor.
Definition: EST_Item.cc:81
EST_TKVL< EST_String, EST_Val > relations
EST_Item * up() const
Definition: EST_Item.h:354
friend int exchange_sub_trees(EST_Item *from, EST_Item *to)
Definition: item_aux.cc:158
void set_name(const EST_String &name) const
Definition: EST_Item.h:254
val_type val_type_featfunc
Definition: item_feats.cc:99
EST_Item * prev() const
Definition: EST_Item.h:350
float F(const EST_String &name) const
Definition: EST_Item.h:135
const EST_String & relation_name() const
The relation name of this particular item.
Definition: EST_Item.cc:196
EST_TKVL< EST_String, EST_Val > & relations()
Access to the relation links.
Definition: EST_Item.h:324
void evaluate(EST_Item *a, EST_Features &f)
Definition: EST_Item.cc:636
EST_Item_Content * contents() const
Definition: EST_Item.h:244
#define l2
val_type type(void) const
Definition: EST_Val.h:137
void set_val(const EST_String &name, const EST_Val &sval)
Definition: EST_Item.h:220
EST_Item * insert_before(EST_Item *li=0)
Definition: EST_Item.cc:255
friend int i_same_item(const EST_Item *l1, const EST_Item *l2)
Definition: EST_Item.h:412
int verify() const
Definition: EST_Item.cc:559
void remove(const EST_String &name)
Definition: EST_Features.h:247
void copy_node_tree_contents(EST_Item *from, EST_Item *to)
Definition: EST_Item.cc:540
NULL
Definition: EST_WFST.cc:55
void copy_node_tree(EST_Item *from, EST_Item *to)
Definition: EST_Item.cc:528
int present(const EST_String &name) const
#define EST_error
Definition: EST_error.h:104
EST_Item * top() const
Definition: EST_Item.cc:349
EST_Item * insert_after(EST_Item *li=0)
Definition: EST_Item.cc:236
f
Definition: EST_item_aux.cc:48
EST_Item * down() const
Definition: EST_Item.h:352
const EST_Val & val_path(const EST_String &path) const
EST_Features & features() const
Definition: EST_Item.h:258
const EST_String & string(void) const
Definition: EST_Val.h:161
EST_Relation * relation(void) const
The relation of this particular item.
Definition: EST_Item.h:330
void set_name(const EST_String &s)
set name
EST_Features & A(const EST_String &name) const
Definition: EST_Item.h:164
static void splice(EST_Item *a, EST_Item *b)
Definition: EST_Item.h:343
EST_Item * next() const
Definition: EST_Item.h:348
float F(const EST_String &name, float def) const
Definition: EST_Item.h:139
EST_Item * prepend_daughter(EST_Item *li=0)
Definition: EST_Item.cc:464
friend bool operator!=(const EST_Item &a, const EST_Item &b)
Definition: EST_Item.h:403
friend int move_sub_tree(EST_Item *from, EST_Item *to)
Definition: item_aux.cc:128
int in_relation(const EST_String &name) const
friend int move_item(EST_Item *from, EST_Item *to)
Definition: item_aux.cc:115
friend int merge_item(EST_Item *from, EST_Item *to)
Definition: item_aux.cc:85
EST_Item_featfunc get_featfunc(const EST_String &name, int must=0)
Definition: item_feats.cc:58
EST_Features & A(const EST_String &name, EST_Features &def) const
Definition: EST_Item.h:170
const EST_String S(const EST_String &name, const EST_String &def) const
Definition: EST_Item.h:150
const EST_String S(const EST_String &name) const
Definition: EST_Item.h:144
#define l1
friend ostream & operator<<(ostream &s, const EST_Item &a)
Definition: EST_Item.cc:629
EST_Item * first_leaf() const
Definition: EST_Item.cc:386
void f_remove(const EST_String &name)
Definition: EST_Item.h:228
int length() const
Definition: EST_Item.cc:228
EST_Item * next_leaf() const
Definition: EST_Item.cc:358
void remove_item(EST_Item *l, const char *relname)
Definition: EST_Item.cc:614
static const EST_String Empty
Constant empty string.
Definition: EST_String.h:110
const EST_Val f(const EST_String &name, const EST_Val &def) const
Definition: EST_Item.h:296
float Float(void) const
Definition: EST_Val.h:149
static void class_init(void)
Definition: EST_Item.cc:66
int f_present(const EST_String &name) const
Definition: EST_Item.h:236