Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
EST_FeatureFunctionContext.cc
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"
41 #include "ling_class/EST_FeatureFunctionPackage.h"
42 #include "EST_FeatureFunctionContext.h"
43 
44 #include "ling_class_init.h"
45 
46 void EST_FeatureFunctionContext::class_init(void)
47 {
48  ling_class_init::use();
49 
50  global = new EST_FeatureFunctionContext();
51 }
52 
53 EST_FeatureFunctionContext *EST_FeatureFunctionContext::global;
54 
55 const EST_String EST_FeatureFunctionContext::separator = "+";
56 
57 EST_FeatureFunctionContext::EST_FeatureFunctionContext(void)
58  : cache(100)
59 {
60 }
61 
62 EST_FeatureFunctionContext::~EST_FeatureFunctionContext(void)
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 
75 EST_FeatureFunctionPackage *EST_FeatureFunctionContext::get_package(const EST_String name) const
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 
88 EST_String EST_FeatureFunctionContext::get_featfunc_name(const EST_Item_featfunc func, int &found) const
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 
111 void EST_FeatureFunctionContext::clear_cache(void)
112 {
113  cache.clear();
114 }
115 
116 void EST_FeatureFunctionContext::add_package(const EST_String name)
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 
135 void EST_FeatureFunctionContext::add_package(EST_FeatureFunctionPackage *package)
136 {
137  packages.prepend(package);
138 
139  clear_cache();
140 }
141 
142 bool EST_FeatureFunctionContext::package_included(const EST_String name) const
143 {
144  return get_package(name) != NULL;
145 }
146 
147 const EST_Item_featfunc EST_FeatureFunctionContext::get_featfunc(const EST_String name,
148  int must)
149 {
150  int pos, len;
151 
152 
153  if (cache.present(name))
154  return cache.val(name);
155 
156  if ((pos= name.search(separator, len, 0))>=0)
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 
192 const EST_Item_featfunc EST_FeatureFunctionContext::get_featfunc(const EST_String pname,
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 
224 Instantiate_TStringHash(EST_Item_featfunc)
225 
226 #include "../base_class/EST_TList.cc"
227 Instantiate_TList_T(EST_FeatureFunctionPackage *, EST_FeatureFunctionPackageP)
228 
229 #endif