Edinburgh Speech Tools 2.4-release
 
Loading...
Searching...
No Matches
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
46void EST_FeatureFunctionContext::class_init(void)
47{
48 ling_class_init::use();
49
50 global = new EST_FeatureFunctionContext();
51}
52
53EST_FeatureFunctionContext *EST_FeatureFunctionContext::global;
54
55const EST_String EST_FeatureFunctionContext::separator = "+";
56
57EST_FeatureFunctionContext::EST_FeatureFunctionContext(void)
58 : cache(100)
59{
60}
61
62EST_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
75EST_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
88EST_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
111void EST_FeatureFunctionContext::clear_cache(void)
112{
113 cache.clear();
114}
115
116void 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
135void EST_FeatureFunctionContext::add_package(EST_FeatureFunctionPackage *package)
136{
137 packages.prepend(package);
138
139 clear_cache();
140}
141
142bool EST_FeatureFunctionContext::package_included(const EST_String name) const
143{
144 return get_package(name) != NULL;
145}
146
147const 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
192const 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
212static EST_Val Dummy_Func(EST_Item *) { return EST_Val(); }
213template <> EST_String
215template <> EST_Item_featfunc
218
219
220#if defined(INSTANTIATE_TEMPLATES)
221
222#include "../base_class/EST_THash.cc"
223
224Instantiate_TStringHash(EST_Item_featfunc)
225
226#include "../base_class/EST_TList.cc"
228
229#endif
EST_String before(int pos, int len=0) const
Part before position.
Definition EST_String.h:286
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)
int search(const char *s, int len, int &mlen, int pos=0) const
Find a substring.
Definition EST_String.h:334
EST_String after(int pos, int len=1) const
Part after pos+len.
Definition EST_String.h:318
V & val(const K &key, int &found) const
Definition EST_THash.cc:114
int add_item(const K &key, const V &value, int no_search=0)
Add an entry to the table.
Definition EST_THash.cc:167
int present(const K &key) const
Does the key have an entry?
Definition EST_THash.cc:96
void clear(void)
Empty the table.
Definition EST_THash.cc:66
void prepend(const T &item)
add item onto start of list
Definition EST_TList.h:194