Edinburgh Speech Tools  2.1-release
EST_Features.cc
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 /* Feature value pairs in a EST_TKVL */
37 /*=======================================================================*/
38 
39 #include <cstdlib>
40 #include "EST_Features.h"
41 #include "ling_class/EST_Item.h"
42 #include "EST_error.h"
43 #include "EST_String.h"
44 #include "EST_Token.h"
45 
46 /* Features may used as Vals */
48 
49 EST_Val EST_Features::feature_default_value("0");
50 // Sometimes we need a set of features even though there aren't any
51 static EST_Features default_features;
52 
54 {
55  features = new EST_TKVL<EST_String, EST_Val>;
56 }
57 
59 {
61  *features = *f.features;
62 }
63 
65 {
66  if (features != NULL)
67  {
68  delete features;
69  features=NULL;
70  }
71 }
72 
73 const EST_Val &EST_Features::val(const char *name, const EST_Val &def) const
74 {
75  // Because so many access are from char* literals we all access
76  // directly rather than requiring the creation of an EST_String
77  EST_Litem *p;
78 
79  for (p=features->list.head(); p; p=p->next())
80  {
81  if (features->list(p).k == name)
82  return features->list(p).v;
83  }
84  return def;
85 }
86 
87 const EST_Val &EST_Features::val(const char *name) const
88 {
89  // Because so many access are from char* literals we all access
90  // directly rather than requiring the creation of an EST_String
91  EST_Litem *p;
92 
93  for (p=features->list.head(); p; p=p->next())
94  {
95  if (features->list(p).k == name)
96  return features->list(p).v;
97  }
98 
99  EST_error("{FND} Feature %s not defined\n", name);
100  return feature_default_value;
101 }
102 
103 const EST_Val &EST_Features::val_path(const EST_String &name, const EST_Val &d) const
104 {
105  // For when name contains references to sub-features
106 
107  if (strchr(name,'.') == NULL)
108  return val(name, d);
109  else
110  {
111  EST_String nname = name;
112  EST_String fname = nname.before(".");
113  const EST_Val &v = val(fname, d);
114  if (v.type() == val_type_feats)
115  return feats(v)->val_path(nname.after("."), d);
116  else
117  return d;
118  }
119 }
120 
121 const EST_Val &EST_Features::val_path(const EST_String &name) const
122 {
123  // For when name contains references to sub-features
124 
125  if (strchr(name,'.') == NULL)
126  return val(name);
127  else
128  {
129  EST_String nname = name;
130  EST_String fname = nname.before(".");
131  const EST_Val &v = val(fname);
132  if (v.type() == val_type_feats)
133  return feats(v)->val_path(nname.after("."));
134  else
135  EST_error("Feature %s not feature valued\n", (const char *)fname);
136  return feature_default_value; // wont get here
137  }
138 }
139 
141 {
142  EST_Features *ff = new EST_Features(def);
143 
144  return feats(val(path,est_val(ff)));
145 }
146 
147 int EST_Features::present(const EST_String &name) const
148 {
149  if (strchr(name,'.') == NULL)
150  return features->present(name);
151  EST_String nname = name;
152  if (features->present(nname.before(".")))
153  {
154  const EST_Val &v = val(nname.before("."));
155  if (v.type() == val_type_feats)
156  return feats(v)->present(nname.after("."));
157  else
158  return FALSE;
159  }
160  else
161  return FALSE;
162 }
163 
164 void EST_Features::set_path(const EST_String &name, const EST_Val &sval)
165 {
166  // Builds sub features (if necessary)
167 
168  if (strchr(name,'.') == NULL)
169  set_val(name,sval);
170  else
171  {
172  EST_String nname = name;
173  EST_String fname = nname.before(".");
174  if (present(fname))
175  {
176  const EST_Val &v = val(fname);
177  if (v.type() == val_type_feats)
178  feats(v)->set_path(nname.after("."),sval);
179  else
180  EST_error("Feature %s not feature valued\n",
181  (const char *)fname);
182  }
183  else
184  {
185  EST_Features f;
186  set(fname,f);
187  A(fname).set_path(nname.after("."),sval);
188  }
189  }
190 }
191 
193 {
194  *features = *x.features;
195  return *this;
196 }
197 
199 {
201 
202  for(p.begin(from); p; ++p)
203  to.set_val(p->k,p->v);
204 }
205 
206 
208 {
209  (void)a;
210  return "<<Features>>";
211 }
212 
213 #if defined(INSTANTIATE_TEMPLATES)
214 typedef EST_TKVI<EST_String, EST_Val> EST_Features_Entry;
215 Instantiate_TStructIterator_T(EST_Features, EST_Features::IPointer, EST_Features_Entry, Features_itt)
216 Instantiate_TIterator_T(EST_Features, EST_Features::IPointer, EST_Features_Entry, Features_itt)
217 #endif
void set_val(const EST_String &name, const EST_Val &sval)
Definition: EST_Features.h:217
#define Instantiate_TStructIterator_T(CONTAINER, IP, ENTRY, TAG)
EST_Features & operator=(const EST_Features &a)
EST_Val est_val(const EST_Item_featfunc f)
Definition: item_feats.cc:122
#define VAL_REGISTER_CLASS(NAME, CLASS)
Definition: EST_Val_defs.h:62
EST_UItem * next()
Definition: EST_UList.h:55
void set_path(const EST_String &name, const EST_Val &sval)
EST_TKVL< EST_String, EST_Val > * features
Definition: EST_Features.h:65
EST_String error_name(const EST_Features &a)
Templated Key-Value Item. Serves as the items in the list of the EST_TKVL class.
Definition: EST_TKVL.h:55
const EST_Val & f(const EST_String &path) const
Definition: EST_Features.h:115
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
val_type type(void) const
Definition: EST_Val.h:137
#define FALSE
Definition: EST_bool.h:119
NULL
Definition: EST_WFST.cc:55
int present(const EST_String &name) const
#define EST_error
Definition: EST_error.h:104
f
Definition: EST_item_aux.cc:48
#define Instantiate_TIterator_T(CONTAINER, IP, ENTRY, TAG)
const EST_Val & val_path(const EST_String &path) const
void begin(const Container &over)
Set the iterator ready to run over this container.
int present(const K &rkey) const
Returns true if key is present.
Definition: EST_TKVL.cc:222
EST_Features & A(const EST_String &path) const
Definition: EST_Features.h:170
EST_UItem * head() const
Definition: EST_UList.h:97
static EST_Val feature_default_value
Definition: EST_Features.h:71
EST_String after(int pos, int len=1) const
Part after pos+len.
Definition: EST_String.h:308
const EST_Val & val(const char *name) const
Definition: EST_Features.cc:87
EST_String before(int pos, int len=0) const
Part before position.
Definition: EST_String.h:276
void merge_features(EST_Features &to, EST_Features &from)