Edinburgh Speech Tools  2.1-release
EST_FeatureFunctionContext.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,1997 */
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  /* */
34  /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */
35  /* -------------------------------------------------------------------- */
36  /* A complete lookup table for feature functions. */
37  /* */
38  /*************************************************************************/
39 
40 #include "ling_class/EST_Item.h"
43 
44 #include "ling_class_init.h"
45 
47 {
49 
51 }
52 
54 
56 
58  : cache(100)
59 {
60 }
61 
63 {
65 
66  for(p.begin(packages); p; ++p)
67  {
68  // Only the global list owns it's packages.
69  if (this == global)
70  delete *p;
71  *p = NULL;
72  }
73 }
74 
76 {
78 
79  for(p.begin(packages); p; ++p)
80  {
81  EST_FeatureFunctionPackage *package = *p;
82  if (package->name() == name)
83  return package;
84  }
85  return NULL;
86 }
87 
89 {
91 
92  found=0;
93 
94  for(p.begin(packages); p; ++p)
95  {
96  EST_FeatureFunctionPackage *package = *p;
97 
98  EST_String name = package->lookup(func, found);
99 
100  if (found)
101  {
102  return EST_String::cat(package->name(), separator, name);
103  }
104  }
105 
106  found=0;
107  return "";
108 }
109 
110 
112 {
113  cache.clear();
114 }
115 
117 {
118  if (this == global)
119  EST_error("Attempt to add package '%s' to global list",
120  (const char *)name
121  );
122 
123  EST_FeatureFunctionPackage *package = global->get_package(name);
124 
125  if (package == NULL)
126  EST_error("package '%s' not loaded",
127  (const char *)name
128  );
129 
130  packages.prepend(package);
131 
132  clear_cache();
133 }
134 
136 {
137  packages.prepend(package);
138 
139  clear_cache();
140 }
141 
143 {
144  return get_package(name) != NULL;
145 }
146 
148  int must)
149 {
150  size_t pos, len;
151 
152 
153  if (cache.present(name))
154  return cache.val(name);
155 
156  if ((pos= name.search(separator, len, 0))!= EST_STRING_ERR_IDX)
157  {
158  const EST_Item_featfunc func2 =
159  get_featfunc(name.before(pos,separator.length()),
160  name.after(pos,separator.length()), must);
161 
162  if (func2 != NULL)
163  cache.add_item(name, func2);
164  return func2;
165  }
166 
167  // No package name so look up directly.
168 
170 
171  for(p.begin(packages); p; ++p)
172  {
173  EST_FeatureFunctionPackage *package = *p;
174 
175  int found;
176 
177  const EST_FeatureFunctionPackage::Entry &ent = package->lookup(name, found);
178 
179  if (found)
180  {
181  cache.add_item(name, ent.func);
182  return ent.func;
183  }
184  }
185 
186  if (must)
187  EST_error("No feature function '%s'", (const char *)name);
188 
189  return NULL;
190 }
191 
193  const EST_String name,
194  int must)
195 {
196  EST_FeatureFunctionPackage *package = get_package(pname);
197 
198  int found;
199 
201  package->lookup(name, found);
202 
203  if (found)
204  return ent.func;
205 
206  if (must)
207  EST_error("No feature function '%s'", (const char *)name);
208 
209  return NULL;
210 }
211 
212 static EST_Val Dummy_Func(EST_Item *) { return EST_Val(); }
213 template <> EST_String
215 template <> EST_Item_featfunc
217 Declare_TList_T(EST_FeatureFunctionPackage *, EST_FeatureFunctionPackageP)
218 
219 
220 #if defined(INSTANTIATE_TEMPLATES)
221 
222 #include "../base_class/EST_THash.cc"
223 
225 
226 #include "../base_class/EST_TList.cc"
227 Instantiate_TList_T(EST_FeatureFunctionPackage *, EST_FeatureFunctionPackageP)
228 
229 #endif
static void use(void)
EST_Val(* EST_Item_featfunc)(class EST_Item *)
Definition: EST_Features.h:53
#define Instantiate_TStringHash(VAL)
EST_Item_featfunc get_featfunc(const EST_String name, int must=0)
bool package_included(const EST_String name) const
static EST_String cat(const EST_String s1, const EST_String s2=Empty, const EST_String s3=Empty, const EST_String s4=Empty, const EST_String s5=Empty, const EST_String s6=Empty, const EST_String s7=Empty, const EST_String s8=Empty, const EST_String s9=Empty)
Definition: EST_String.cc:1084
size_t search(const char *s, size_t len, size_t &mlen, ssize_t pos=0) const
Find a substring.
Definition: EST_String.h:324
EST_FeatureFunctionPackage * get_package(const EST_String name) const
#define EST_STRING_ERR_IDX
Definition: EST_String.h:116
EST_String get_featfunc_name(const EST_Item_featfunc func, int &found) const
NULL
Definition: EST_WFST.cc:55
#define EST_error
Definition: EST_error.h:104
#define Declare_TList_T(TYPE, TAG)
Definition: EST_TListI.h:68
static EST_FeatureFunctionContext * global
size_t length(void) const
Length of string ({not} length of underlying chunk)
Definition: EST_String.h:231
An open hash table. The number of buckets should be set to allow enough space that there are relative...
Definition: EST_THash.h:69
#define Instantiate_TList_T(TYPE, TAG)
Definition: EST_TListI.h:58
EST_String after(int pos, int len=1) const
Part after pos+len.
Definition: EST_String.h:308
EST_String before(int pos, int len=0) const
Part before position.
Definition: EST_String.h:276
void add_package(EST_FeatureFunctionPackage *package)