| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 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 |
|
| 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 |
|
| 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); |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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); |
| 366 |
} |
| 367 |
|
| 368 |
|
| 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); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
| 487 |
|
| 488 |
void us_ff_register(cst_features *ffunctions) |
| 489 |
{ |
| 490 |
|
| 491 |
|
| 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); |
| 501 |
ff_register(ffunctions, "pos_in_word",pos_in_word); |
| 502 |
ff_register(ffunctions, "lisp_distance_to_p_stress",lisp_distance_to_p_stress); |
| 503 |
ff_register(ffunctions, "lisp_distance_to_n_stress",lisp_distance_to_n_stress); |
| 504 |
ff_register(ffunctions, "lisp_distance_to_p_accent",lisp_distance_to_p_accent); |
| 505 |
ff_register(ffunctions, "lisp_distance_to_n_accent",lisp_distance_to_n_accent); |
| 506 |
ff_register(ffunctions, "syl_vowel",syl_vowel); |
| 507 |
ff_register(ffunctions, "pos_in_phrase",pos_in_phrase); |
| 508 |
ff_register(ffunctions, "words_out",words_out); |
| 509 |
ff_register(ffunctions, "content_words_in",content_words_in); |
| 510 |
ff_register(ffunctions, "content_words_out",content_words_out); |
| 511 |
ff_register(ffunctions, "lisp_distance_to_p_content",lisp_distance_to_p_content); |
| 512 |
ff_register(ffunctions, "lisp_distance_to_n_content",lisp_distance_to_n_content); |
| 513 |
ff_register(ffunctions, "lisp_num_syls_in_phrase",lisp_num_syls_in_phrase); |
| 514 |
ff_register(ffunctions, "lisp_num_words_in_phrase",lisp_num_words_in_phrase); |
| 515 |
ff_register(ffunctions, "lisp_total_syls",lisp_total_syls); |
| 516 |
ff_register(ffunctions, "lisp_total_words",lisp_total_words); |
| 517 |
ff_register(ffunctions, "lisp_total_phrases",lisp_total_phrases); |
| 518 |
#endif |
| 519 |
} |