Debian Codegraph @

3rdparty/flite_hts_engine/flite/lang/usenglish/us_ffeatures.c

raw symbols SCIP
1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 2001 */
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: January 2001 */
35 /*************************************************************************/
36 /* Feature functions used by various cart trees etc */
37 /* These have been create as needed, and as some of the trees are */
38 /* from University of Edinburgh's Festival system their names and */
39 /* semantics follow them */
40 /*************************************************************************/
41
42 /* ----------------------------------------------------------------- */
43 /* The English TTS System "Flite+hts_engine" */
44 /* developed by HTS Working Group */
45 /* http://hts-engine.sourceforge.net/ */
46 /* ----------------------------------------------------------------- */
47 /* */
48 /* Copyright (c) 2005-2013 Nagoya Institute of Technology */
49 /* Department of Computer Science */
50 /* */
51 /* 2005-2008 Tokyo Institute of Technology */
52 /* Interdisciplinary Graduate School of */
53 /* Science and Engineering */
54 /* */
55 /* All rights reserved. */
56 /* */
57 /* Redistribution and use in source and binary forms, with or */
58 /* without modification, are permitted provided that the following */
59 /* conditions are met: */
60 /* */
61 /* - Redistributions of source code must retain the above copyright */
62 /* notice, this list of conditions and the following disclaimer. */
63 /* - Redistributions in binary form must reproduce the above */
64 /* copyright notice, this list of conditions and the following */
65 /* disclaimer in the documentation and/or other materials provided */
66 /* with the distribution. */
67 /* - Neither the name of the HTS working group nor the names of its */
68 /* contributors may be used to endorse or promote products derived */
69 /* from this software without specific prior written permission. */
70 /* */
71 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND */
72 /* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, */
73 /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
74 /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
75 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS */
76 /* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, */
77 /* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
78 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
79 /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
80 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, */
81 /* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY */
82 /* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
83 /* POSSIBILITY OF SUCH DAMAGE. */
84 /* ----------------------------------------------------------------- */
85
86 #include "cst_hrg.h"
87 #include "cst_phoneset.h"
88 #include "cst_regex.h"
89 #include "cst_ffeatures.h"
90 #include "us_ffeatures.h"
91
92 static const cst_val *gpos(const cst_item *word);
93
94 DEF_STATIC_CONST_VAL_STRING(val_string_numeric,"numeric");
95 DEF_STATIC_CONST_VAL_STRING(val_string_number,"number");
96 DEF_STATIC_CONST_VAL_STRING(val_string_month,"month");
97 DEF_STATIC_CONST_VAL_STRING(val_string_day,"day");
98 DEF_STATIC_CONST_VAL_STRING(val_string_other,"_other_");
99 DEF_STATIC_CONST_VAL_STRING(val_string_a,"a");
100 DEF_STATIC_CONST_VAL_STRING(val_string_flight,"flight");
101 DEF_STATIC_CONST_VAL_STRING(val_string_to,"to");
102
103 DEF_STATIC_CONST_VAL_STRING(val_string_content,"content");
104
105 static const cst_val *gpos(const cst_item *word)
106 {
107 /* Guess at part of speech (function/content) */
108 const char *w;
109 int s,t;
110
111 w = item_feat_string(word,"name");
112
113 for (s=0; us_gpos[s]; s++)
114 {
115 for (t=1; us_gpos[s][t]; t++)
116 if (cst_streq(w,val_string(us_gpos[s][t])))
117 return us_gpos[s][0];
118 }
119
120 return (cst_val *)&val_string_content;
121 }
122
123 static const cst_val *num_digits(const cst_item *token)
124 {
125 const char *name = item_feat_string(token,"name");
126
127 return val_int_n(cst_strlen(name));
128 }
129
130 static const cst_val *month_range(const cst_item *token)
131 {
132 int v = item_feat_int(token,"name");
133
134 if ((v > 0) && ( v < 32))
135 return VAL_STRING_1;
136 else
137 return VAL_STRING_0;
138 }
139
140 static const cst_val* token_pos_guess(const cst_item* token)
141 {
142 const char* name = item_feat_string(token, "name");
143 char* dc = cst_downcase(name);
144 const cst_val* r;
145
146 if (cst_regex_match(cst_rx_digits, dc))
147 r = (cst_val*)&val_string_numeric;
148 else if ((cst_regex_match(cst_rx_double, dc)) ||
149 (cst_regex_match(cst_rx_double, dc)))
150 r = (cst_val*)&val_string_number;
151 else if (cst_streq(dc, "jan") ||
152 cst_streq(dc, "january") ||
153 cst_streq(dc, "feb") ||
154 cst_streq(dc, "february") ||
155 cst_streq(dc, "mar") ||
156 cst_streq(dc, "march") ||
157 cst_streq(dc, "apr") ||
158 cst_streq(dc, "april") ||
159 cst_streq(dc, "may") ||
160 cst_streq(dc, "jun") ||
161 cst_streq(dc, "june") ||
162 cst_streq(dc, "jul") ||
163 cst_streq(dc, "july") ||
164 cst_streq(dc, "aug") ||
165 cst_streq(dc, "august") ||
166 cst_streq(dc, "sep") ||
167 cst_streq(dc, "sept") ||
168 cst_streq(dc, "september") ||
169 cst_streq(dc, "oct") ||
170 cst_streq(dc, "october") ||
171 cst_streq(dc, "nov") ||
172 cst_streq(dc, "november") ||
173 cst_streq(dc, "dec") ||
174 cst_streq(dc, "december"))
175 r = (cst_val*)&val_string_month;
176 else if (cst_streq(dc, "sun") ||
177 cst_streq(dc, "sunday") ||
178 cst_streq(dc, "mon") ||
179 cst_streq(dc, "monday") ||
180 cst_streq(dc, "tue") ||
181 cst_streq(dc, "tues") ||
182 cst_streq(dc, "tuesday") ||
183 cst_streq(dc, "wed") ||
184 cst_streq(dc, "wednesday") ||
185 cst_streq(dc, "thu") ||
186 cst_streq(dc, "thurs") ||
187 cst_streq(dc, "thursday") ||
188 cst_streq(dc, "fri") ||
189 cst_streq(dc, "friday") ||
190 cst_streq(dc, "sat") ||
191 cst_streq(dc, "saturday"))
192 r = (cst_val*)&val_string_day;
193 /* ignoring the "token_most_common" condition, does get used */
194 else if (cst_streq(dc, "a"))
195 r = (cst_val*)&val_string_a;
196 else if (cst_streq(dc, "flight"))
197 r = (cst_val*)&val_string_flight;
198 else if (cst_streq(dc, "to"))
199 r = (cst_val*)&val_string_to;
200 else
201 r = (cst_val*)&val_string_other;
202
203 cst_free(dc);
204
205 return r;
206 }
207
208 #ifdef FLITE_PLUS_HTS_ENGINE
209
210 const cst_val *accented(const cst_item *p); /* defined in cst_ffeatures.c */
211
212 int item_after_length(const cst_item *n){
213 int i = 0;
214
215 if(n == NULL) return 0;
216
217 for(;n;n=n->n,i++);
218
219 return i;
220 }
221
222 const cst_item *item_first(const cst_item *n){
223 if(n == 0) return 0;
224 for(;n->p != 0;n=n->p);
225 return n;
226 }
227
228 /* 11 12 13 53 54 */
229 static const cst_val *syl_numphones(const cst_item *syl){
230 return val_string_n(item_after_length(item_daughter(item_as(syl,"SylStructure"))));
231 }
232
233 /* 14 */
234 static const cst_val *pos_in_word(const cst_item *syl){
235 const cst_item *ss,*p;
236 int c = 0;
237
238 ss = item_as(syl,"SylStructure");
239 for(p = item_daughter(item_parent(ss));p;p = item_next(p),c++){
240 if(p == ss) return val_string_n(c);
241 }
242 return val_string_n(c);
243 }
244
245 /* 21 by Toda-san */
246 static const cst_val *lisp_distance_to_p_stress(const cst_item *syl){
247 const cst_item *s, *fs;
248 int c;
249
250 s=item_as(syl,"Syllable");
251 fs = path_to_item(syl,"R:SylStructure.parent.R:Phrase.parent.daughter.R:SylStructure.daughter");
252 if (item_equal(s,fs)) return val_string_n(0);
253 s=item_prev(s);
254 for (c=1; s && (!item_equal(s,fs)) && (c < CST_CONST_INT_MAX);
255 s=item_prev(s),c++)
256 if (strcmp("1", ffeature_string(s,"stress")) == 0) return val_string_n(c);
257 if (strcmp("1", ffeature_string(s,"stress")) == 0) return val_string_n(c);
258 else return val_string_n(0);
259 }
260
261 /* 22 by Toda-san */
262 static const cst_val *lisp_distance_to_n_stress(const cst_item *syl){
263 const cst_item *s, *fs;
264 int c;
265
266 s=item_as(syl,"Syllable");
267 fs = path_to_item(syl,"R:SylStructure.parent.R:Phrase.parent.daughtern.R:SylStructure.daughtern");
268 if (item_equal(s,fs)) return val_string_n(0);
269 s=item_next(s);
270 for (c=1; s && (!item_equal(s,fs)) && (c < CST_CONST_INT_MAX);
271 s=item_next(s),c++)
272 if (strcmp("1", ffeature_string(s,"stress")) == 0) return val_string_n(c);
273 if (strcmp("1", ffeature_string(s,"stress")) == 0) return val_string_n(c);
274 else return val_string_n(0);
275 }
276
277 /* 23 by Toda-san */
278 static const cst_val *lisp_distance_to_p_accent(const cst_item *syl){
279 const cst_item *s, *fs;
280 int c;
281
282 s=item_as(syl,"Syllable");
283 fs = path_to_item(syl,"R:SylStructure.parent.R:Phrase.parent.daughter.R:SylStructure.daughter");
284 if (item_equal(s,fs)) return val_string_n(0);
285 s=item_prev(s);
286 for (c=1; s && (!item_equal(s,fs)) && (c < CST_CONST_INT_MAX);
287 s=item_prev(s),c++)
288 if (val_int(accented(s))) return val_string_n(c);
289 if (val_int(accented(s))) return val_string_n(c);
290 else return val_string_n(0);
291 }
292
293 /* 24 by Toda-san */
294 static const cst_val *lisp_distance_to_n_accent(const cst_item *syl){
295 const cst_item *s, *fs;
296 int c;
297
298 s=item_as(syl,"Syllable");
299 fs = path_to_item(syl,"R:SylStructure.parent.R:Phrase.parent.daughtern.R:SylStructure.daughtern");
300 if (item_equal(s,fs)) return val_string_n(0);
301 s=item_next(s);
302 for (c=1; s && (!item_equal(s,fs)) && (c < CST_CONST_INT_MAX);
303 s=item_next(s),c++)
304 if (val_int(accented(s))) return val_string_n(c);
305 if (val_int(accented(s))) return val_string_n(c);
306 else return val_string_n(0);
307 }
308
309 /* 25 */
310 static char syl_vowel_str[16];
311 static cst_val syl_vowel_val;
312 static const cst_val *syl_vowel(const cst_item *syl){
313 /* the vowel in the syllable */
314 const cst_item *ss,*p;
315 int size;
316
317 CST_VAL_TYPE(&syl_vowel_val) = CST_VAL_TYPE_STRING;
318 ss = item_as(syl,"SylStructure");
319 for(p = item_daughter(ss),size = 0;p;p = item_next(p),size++){
320 if(strchr("aeiou",item_name(p)[0]) != NULL){
321 strcpy(syl_vowel_str,item_name(p));
322 CST_VAL_STRING_LVAL(&syl_vowel_val) = syl_vowel_str;
323 return &syl_vowel_val;
324 }
325 }
326 /* no vowel */
327 strcpy(syl_vowel_str,"novowel");
328 CST_VAL_STRING_LVAL(&syl_vowel_val) = syl_vowel_str;
329 return &syl_vowel_val;
330 }
331
332 /* 32 */
333 static const cst_val *pos_in_phrase(const cst_item *syl){
334 const cst_item *ss,*p;
335 int c = 0;
336
337 ss = item_as(syl,"Phrase");
338 for (p = item_first(ss);p;p=item_next(p),c++)
339 if(p == ss) return val_string_n(c);
340 return val_string_n(c);
341 }
342
343 /* 33 */
344 static const cst_val *words_out(const cst_item *syl){
345 const cst_item *ss,*p;
346 int c = 0;
347
348 ss = item_as(syl,"Phrase");
349 for(p = ss;p;p=item_next(p),c++);
350 return val_string_n(c);
351 }
352
353 /* 34 by Toda-san */
354 static const cst_val *content_words_in(const cst_item *word){
355 const cst_item *ss,*p,*fs;
356 int c;
357
358 ss = word;
359 fs = path_to_item(word,"R:Phrase.parent.daughter");
360
361 for (c=0, p=ss; p && (!item_equal(p,fs)) && (c < CST_CONST_INT_MAX);
362 p=item_prev(p))
363 if (cst_streq("content", ffeature_string(p,"gpos"))) c++;
364
365 return val_string_n(c); /* its used randomly as int and float */
366 }
367
368 /* 35 by Toda-san */
369 static const cst_val *content_words_out(const cst_item *word){
370 const cst_item *ss,*p,*fs;
371 int c;
372
373 ss = word;
374 fs = path_to_item(word,"R:Phrase.parent.daughtern");
375
376 for (c=0, p=ss; p && (!item_equal(p,fs)) && (c < CST_CONST_INT_MAX);
377 p=item_next(p))
378 if (cst_streq("content", ffeature_string(p,"gpos"))) c++;
379
380 return val_string_n(c); /* its used randomly as int and float */
381 }
382
383 /* 36 */
384 static const cst_val *lisp_distance_to_p_content(const cst_item *syl){
385 const cst_item *p;
386 int c = 0;
387
388 for(p=item_prev(item_as(syl,"Phrase"));p;p=item_prev(p)){
389 c++;
390 if(gpos(p)==(cst_val*)&val_string_content)
391 break;
392 }
393 return val_string_n(c);
394 }
395
396 /* 37 */
397 static const cst_val *lisp_distance_to_n_content(const cst_item *syl){
398 const cst_item *p;
399 int c = 0;
400
401 for(p=item_next(item_as(syl,"Phrase"));p;p=item_next(p)){
402 c++;
403 if (gpos(p)==(cst_val*)&val_string_content)
404 break;
405 }
406 return val_string_n(c);
407 }
408
409 /* 38 39 40 59 60 by Toda-san */
410 static const cst_val *lisp_num_syls_in_phrase(const cst_item *phrase){
411 const cst_item *sw,*fw;
412 int c;
413
414 sw = path_to_item(phrase,"daughter");
415 fw = path_to_item(phrase,"daughtern");
416
417 for (c=0; sw && (!item_equal(sw,fw)) && (c < CST_CONST_INT_MAX);
418 sw=item_next(sw)) c += ffeature_int(sw, "word_numsyls");
419 c += ffeature_int(sw, "word_numsyls");
420
421 return val_string_n(c);
422 }
423
424 /* 41 42 43 61 62 by Toda-san */
425 static const cst_val *lisp_num_words_in_phrase(const cst_item *phrase){
426 const cst_item *sw,*fw;
427 int c;
428
429 sw = path_to_item(phrase,"daughter");
430 fw = path_to_item(phrase,"daughtern");
431
432 for (c=1; sw && (!item_equal(sw,fw)) && (c < CST_CONST_INT_MAX);
433 sw=item_next(sw)) c++;
434
435 return val_string_n(c);
436 }
437
438 /* 46 by Toda-san */
439 static const cst_val *lisp_total_syls(const cst_item *phrase){
440 const cst_item *sp, *fp;
441 int c;
442
443 sp = phrase;
444 while (item_prev(sp) != NULL) sp = item_prev(sp);
445 fp = phrase;
446 while (item_next(fp) != NULL) fp = item_next(fp);
447
448 for (c = 0; sp && (!item_equal(sp, fp)) && (c < CST_CONST_INT_MAX);
449 sp = item_next(sp)) c += ffeature_int(sp, "lisp_num_syls_in_phrase");
450 c += ffeature_int(sp, "lisp_num_syls_in_phrase");
451 return val_string_n(c);
452 }
453
454 /* 47 by Toda-san */
455 static const cst_val *lisp_total_words(const cst_item *phrase){
456 const cst_item *sp, *fp;
457 int c;
458
459 sp = phrase;
460 while (item_prev(sp) != NULL) sp = item_prev(sp);
461 fp = phrase;
462 while (item_next(fp) != NULL) fp = item_next(fp);
463
464 for (c = 0; sp && (!item_equal(sp, fp)) && (c < CST_CONST_INT_MAX);
465 sp = item_next(sp)) c += ffeature_int(sp, "lisp_num_words_in_phrase");
466 c += ffeature_int(sp, "lisp_num_words_in_phrase");
467 return val_string_n(c);
468 }
469
470 /* 48 by Toda-san */
471 static const cst_val *lisp_total_phrases(const cst_item *phrase){
472 const cst_item *sp, *fp;
473 int c;
474
475 sp = phrase;
476 while (item_prev(sp) != NULL) sp = item_prev(sp);
477 fp = phrase;
478 while (item_next(fp) != NULL) fp = item_next(fp);
479
480 for (c = 1; sp && (!item_equal(sp, fp)) && (c < CST_CONST_INT_MAX);
481 sp = item_next(sp)) c++;
482
483 return val_string_n(c);
484 }
485
486 #endif /* FLITE_PLUS_HTS_ENGINE */
487
488 void us_ff_register(cst_features *ffunctions)
489 {
490
491 /* The language independent ones */
492 basic_ff_register(ffunctions);
493
494 ff_register(ffunctions, "gpos",gpos);
495 ff_register(ffunctions, "num_digits",num_digits);
496 ff_register(ffunctions, "month_range",month_range);
497 ff_register(ffunctions, "token_pos_guess",token_pos_guess);
498
499 #ifdef FLITE_PLUS_HTS_ENGINE
500 ff_register(ffunctions, "syl_numphones",syl_numphones); /* 11 12 13 53 54 */
501 ff_register(ffunctions, "pos_in_word",pos_in_word); /* 14 */
502 ff_register(ffunctions, "lisp_distance_to_p_stress",lisp_distance_to_p_stress); /* 21 */
503 ff_register(ffunctions, "lisp_distance_to_n_stress",lisp_distance_to_n_stress); /* 22 */
504 ff_register(ffunctions, "lisp_distance_to_p_accent",lisp_distance_to_p_accent); /* 23 */
505 ff_register(ffunctions, "lisp_distance_to_n_accent",lisp_distance_to_n_accent); /* 24 */
506 ff_register(ffunctions, "syl_vowel",syl_vowel); /* 25 */
507 ff_register(ffunctions, "pos_in_phrase",pos_in_phrase); /* 32 */
508 ff_register(ffunctions, "words_out",words_out); /* 33 */
509 ff_register(ffunctions, "content_words_in",content_words_in); /* 34 */
510 ff_register(ffunctions, "content_words_out",content_words_out); /* 35 */
511 ff_register(ffunctions, "lisp_distance_to_p_content",lisp_distance_to_p_content); /* 36 */
512 ff_register(ffunctions, "lisp_distance_to_n_content",lisp_distance_to_n_content); /* 37 */
513 ff_register(ffunctions, "lisp_num_syls_in_phrase",lisp_num_syls_in_phrase); /* 38 39 40 59 60 */
514 ff_register(ffunctions, "lisp_num_words_in_phrase",lisp_num_words_in_phrase); /* 41 42 43 61 62 */
515 ff_register(ffunctions, "lisp_total_syls",lisp_total_syls); /* 46 */
516 ff_register(ffunctions, "lisp_total_words",lisp_total_words); /* 47 */
517 ff_register(ffunctions, "lisp_total_phrases",lisp_total_phrases); /* 48 */
518 #endif /* FLITE_PLUS_HTS_ENGINE */
519 }