Debian Codegraph @

3rdparty/flite_hts_engine/flite/src/utils/cst_features.c

raw symbols SCIP
1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 1999 */
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 /* CARNEGIE MELLON UNIVERSITY 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 CARNEGIE MELLON UNIVERSITY 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 ([email protected]) */
34 /* Date: December 1999 */
35 /*************************************************************************/
36 /* */
37 /* Feature support */
38 /* */
39 /*************************************************************************/
40
41 /* ----------------------------------------------------------------- */
42 /* The English TTS System "Flite+hts_engine" */
43 /* developed by HTS Working Group */
44 /* http://hts-engine.sourceforge.net/ */
45 /* ----------------------------------------------------------------- */
46 /* */
47 /* Copyright (c) 2005-2013 Nagoya Institute of Technology */
48 /* Department of Computer Science */
49 /* */
50 /* 2005-2008 Tokyo Institute of Technology */
51 /* Interdisciplinary Graduate School of */
52 /* Science and Engineering */
53 /* */
54 /* All rights reserved. */
55 /* */
56 /* Redistribution and use in source and binary forms, with or */
57 /* without modification, are permitted provided that the following */
58 /* conditions are met: */
59 /* */
60 /* - Redistributions of source code must retain the above copyright */
61 /* notice, this list of conditions and the following disclaimer. */
62 /* - Redistributions in binary form must reproduce the above */
63 /* copyright notice, this list of conditions and the following */
64 /* disclaimer in the documentation and/or other materials provided */
65 /* with the distribution. */
66 /* - Neither the name of the HTS working group nor the names of its */
67 /* contributors may be used to endorse or promote products derived */
68 /* from this software without specific prior written permission. */
69 /* */
70 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND */
71 /* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, */
72 /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
73 /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
74 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS */
75 /* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, */
76 /* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
77 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
78 /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
79 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, */
80 /* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY */
81 /* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
82 /* POSSIBILITY OF SUCH DAMAGE. */
83 /* ----------------------------------------------------------------- */
84
85 #include "cst_error.h"
86 #include "cst_features.h"
87
88 CST_VAL_REGISTER_TYPE(features,cst_features)
89
90 static cst_featvalpair *feat_find_featpair(const cst_features *f,
91 const char *name)
92 {
93 cst_featvalpair *n;
94
95 if (f == NULL)
96 return NULL;
97 else
98 {
99 for (n=f->head; n; n=n->next)
100 if (cst_streq(name,n->name))
101 return n;
102 return NULL;
103 }
104 }
105
106 cst_features *new_features(void)
107 {
108 cst_features *f;
109
110 f = cst_alloc(cst_features,1);
111 f->head = NULL;
112 f->ctx = NULL;
113 return f;
114 }
115
116 cst_features *new_features_local(cst_alloc_context ctx)
117 {
118 cst_features *f;
119
120 f = (cst_features *)cst_local_alloc(ctx, sizeof(*f));
121 f->head = NULL;
122 f->ctx = ctx;
123 return f;
124 }
125
126 void delete_features(cst_features *f)
127 {
128 cst_featvalpair *n, *np;
129
130 if (f)
131 {
132 for (n=f->head; n; n=np)
133 {
134 np = n->next;
135 delete_val(n->val);
136 cst_local_free(f->ctx,n);
137 }
138 cst_local_free(f->ctx,f);
139 }
140 }
141
142 int feat_present(const cst_features *f, const char *name)
143 {
144 return (feat_find_featpair(f,name) != NULL);
145 }
146
147 int feat_length(const cst_features *f)
148 {
149 int i=0;
150 cst_featvalpair *p;
151 if (f)
152 for (i=0,p=f->head; p; p=p->next)
153 i++;
154 return i;
155 }
156
157 int feat_remove(cst_features *f, const char *name)
158 {
159 cst_featvalpair *n,*p,*np;
160
161 if (f == NULL)
162 return FALSE; /* didn't remove it */
163 else
164 {
165 for (p=NULL,n=f->head; n; p=n,n=np)
166 {
167 np = n->next;
168 if (cst_streq(name,n->name))
169 {
170 if (p == 0)
171 f->head = np;
172 else
173 p->next = np;
174 delete_val(n->val);
175 cst_local_free(f->ctx,n);
176 return TRUE;
177 }
178 }
179 return FALSE;
180 }
181 }
182
183 int feat_int(const cst_features *f, const char *name)
184 {
185 return val_int(feat_val(f,name));
186 }
187
188 float feat_float(const cst_features *f, const char *name)
189 {
190 return val_float(feat_val(f,name));
191 }
192
193 const char *feat_string(const cst_features *f, const char *name)
194 {
195 return val_string(feat_val(f,name));
196 }
197
198 const cst_val *feat_val(const cst_features *f, const char *name)
199 {
200 cst_featvalpair *n;
201 n = feat_find_featpair(f,name);
202
203 if (n == NULL)
204 return NULL;
205 else
206 return n->val;
207 }
208
209 int get_param_int(const cst_features *f, const char *name,int def)
210 {
211 const cst_val *v;
212
213 v = feat_val(f,name);
214 if (v != NULL)
215 return val_int(v);
216 else
217 return def;
218 }
219
220 float get_param_float(const cst_features *f, const char *name, float def)
221 {
222 const cst_val *v;
223
224 v = feat_val(f,name);
225 if (v != NULL)
226 return val_float(v);
227 else
228 return def;
229 }
230 const char *get_param_string(const cst_features *f, const char *name, const char *def)
231 {
232 const cst_val *v;
233
234 v = feat_val(f,name);
235 if (v != NULL)
236 return val_string(v);
237 else
238 return def;
239 }
240
241 const cst_val *get_param_val(const cst_features *f, const char *name, cst_val *def)
242 {
243 const cst_val *v;
244
245 v = feat_val(f,name);
246 if (v != NULL)
247 return v;
248 else
249 return def;
250 }
251
252 void feat_set_int(cst_features *f, const char *name, int v)
253 {
254 feat_set(f,name,int_val(v));
255 }
256
257 void feat_set_float(cst_features *f, const char *name, float v)
258 {
259 feat_set(f,name,float_val(v));
260 }
261
262 void feat_set_string(cst_features *f, const char *name, const char *v)
263 {
264 feat_set(f,name,string_val(v));
265 }
266
267 void feat_set(cst_features *f, const char* name, const cst_val *val)
268 {
269 cst_featvalpair *n;
270 n = feat_find_featpair(f,name);
271
272 if (val == NULL)
273 {
274 cst_errmsg("cst_val: trying to set a NULL val for feature \"%s\"\n",
275 name);
276 }
277 else if (n == NULL)
278 { /* first reference to this feature so create new fpair */
279 cst_featvalpair *p;
280 p = (cst_featvalpair *)cst_local_alloc(f->ctx, sizeof(*p));
281 p->next = f->head;
282 p->name = name;
283 p->val = val_inc_refcount(val);
284 f->head = p;
285 }
286 else
287 {
288 delete_val(n->val);
289 n->val = val_inc_refcount(val);
290 }
291 }
292
293 int feat_copy_into(const cst_features *from,cst_features *to)
294 {
295 /* Copy all features in from into to */
296 cst_featvalpair *p;
297 int i;
298
299 for (i=0,p=from->head; p; p=p->next,i++)
300 feat_set(to,p->name,p->val);
301
302 return i;
303 }
304
305 #ifndef FLITE_PLUS_HTS_ENGINE
306 int feat_print(cst_file fd,const cst_features *f)
307 {
308 cst_featvalpair *p;
309
310 for (p=f->head; p; p=p->next)
311 {
312 cst_fprintf(fd, "%s ",p->name);
313 val_print(fd,p->val);
314 cst_fprintf(fd,"\n");
315 }
316
317 return 0;
318 }
319 #endif /* !FLITE_PLUS_HTS_ENGINE */