| 1 |
/*************************************************************************/ |
| 2 |
/* */ |
| 3 |
/* Language Technologies Institute */ |
| 4 |
/* Carnegie Mellon University */ |
| 5 |
/* Copyright (c) 2007 */ |
| 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: November 2007 */ |
| 35 |
/*************************************************************************/ |
| 36 |
/* */ |
| 37 |
/* Some language independent features */ |
| 38 |
/* */ |
| 39 |
|
| 40 |
#include "cst_hrg.h" |
| 41 |
#include "cst_phoneset.h" |
| 42 |
#include "cst_regex.h" |
| 43 |
|
| 44 |
static const cst_val *word_break(const cst_item *word); |
| 45 |
static const cst_val *word_punc(const cst_item *word); |
| 46 |
static const cst_val *word_numsyls(const cst_item *word); |
| 47 |
static const cst_val *ssyl_in(const cst_item *syl); |
| 48 |
static const cst_val *syl_in(const cst_item *syl); |
| 49 |
static const cst_val *syl_out(const cst_item *syl); |
| 50 |
static const cst_val *syl_break(const cst_item *syl); |
| 51 |
static const cst_val *syl_codasize(const cst_item *syl); |
| 52 |
static const cst_val *syl_onsetsize(const cst_item *syl); |
| 53 |
static const cst_val *accented(const cst_item *p); |
| 54 |
|
| 55 |
DEF_STATIC_CONST_VAL_STRING(val_string_onset,"onset"); |
| 56 |
DEF_STATIC_CONST_VAL_STRING(val_string_coda,"coda"); |
| 57 |
DEF_STATIC_CONST_VAL_STRING(val_string_initial,"initial"); |
| 58 |
DEF_STATIC_CONST_VAL_STRING(val_string_single,"single"); |
| 59 |
DEF_STATIC_CONST_VAL_STRING(val_string_final,"final"); |
| 60 |
DEF_STATIC_CONST_VAL_STRING(val_string_mid,"mid"); |
| 61 |
DEF_STATIC_CONST_VAL_STRING(val_string_empty,""); |
| 62 |
|
| 63 |
const cst_val *ph_vc(const cst_item *p) |
| 64 |
{ |
| 65 |
return phone_feature(item_phoneset(p),item_name(p),"vc"); |
| 66 |
} |
| 67 |
const cst_val *ph_vlng(const cst_item *p) |
| 68 |
{ |
| 69 |
return phone_feature(item_phoneset(p),item_name(p),"vlng"); |
| 70 |
} |
| 71 |
const cst_val *ph_vheight(const cst_item *p) |
| 72 |
{ |
| 73 |
return phone_feature(item_phoneset(p),item_name(p),"vheight"); |
| 74 |
} |
| 75 |
const cst_val *ph_vrnd(const cst_item *p) |
| 76 |
{ |
| 77 |
return phone_feature(item_phoneset(p),item_name(p),"vrnd"); |
| 78 |
} |
| 79 |
const cst_val *ph_vfront(const cst_item *p) |
| 80 |
{ |
| 81 |
return phone_feature(item_phoneset(p),item_name(p),"vfront"); |
| 82 |
} |
| 83 |
const cst_val *ph_ctype(const cst_item *p) |
| 84 |
{ |
| 85 |
return phone_feature(item_phoneset(p),item_name(p),"ctype"); |
| 86 |
} |
| 87 |
const cst_val *ph_cplace(const cst_item *p) |
| 88 |
{ |
| 89 |
return phone_feature(item_phoneset(p),item_name(p),"cplace"); |
| 90 |
} |
| 91 |
const cst_val *ph_cvox(const cst_item *p) |
| 92 |
{ |
| 93 |
return phone_feature(item_phoneset(p),item_name(p),"cvox"); |
| 94 |
} |
| 95 |
|
| 96 |
const cst_val *cg_duration(const cst_item *p) |
| 97 |
{ |
| 98 |
/* Note this constructs float vals, these will be freed when the */ |
| 99 |
/* cart cache is freed, so this should only be used in carts */ |
| 100 |
if (!p) |
| 101 |
return float_val(0.0); |
| 102 |
else if (!item_prev(p)) |
| 103 |
return item_feat(p,"end"); |
| 104 |
else |
| 105 |
return float_val(item_feat_float(p,"end") |
| 106 |
- item_feat_float(item_prev(p),"end")); |
| 107 |
} |
| 108 |
|
| 109 |
DEF_STATIC_CONST_VAL_STRING(val_string_pos_b,"b"); |
| 110 |
DEF_STATIC_CONST_VAL_STRING(val_string_pos_m,"m"); |
| 111 |
DEF_STATIC_CONST_VAL_STRING(val_string_pos_e,"e"); |
| 112 |
|
| 113 |
const cst_val *cg_state_pos(const cst_item *p) |
| 114 |
{ |
| 115 |
const char *name; |
| 116 |
name = item_feat_string(p,"name"); |
| 117 |
if (!cst_streq(name,ffeature_string(p,"p.name"))) |
| 118 |
return (cst_val *)&val_string_pos_b; |
| 119 |
if (cst_streq(name,ffeature_string(p,"n.name"))) |
| 120 |
return (cst_val *)&val_string_pos_m; |
| 121 |
else |
| 122 |
return (cst_val *)&val_string_pos_e; |
| 123 |
} |
| 124 |
|
| 125 |
const cst_val *cg_state_place(const cst_item *p) |
| 126 |
{ |
| 127 |
float start, end; |
| 128 |
int thisone; |
| 129 |
start = (float)ffeature_int(p,"R:mcep_link.parent.daughter1.frame_number"); |
| 130 |
end = (float)ffeature_int(p,"R:mcep_link.parent.daughtern.frame_number"); |
| 131 |
thisone = item_feat_int(p,"frame_number"); |
| 132 |
if ((end-start) == 0.0) |
| 133 |
return float_val(0.0); |
| 134 |
else |
| 135 |
return float_val((thisone-start)/(end-start)); |
| 136 |
} |
| 137 |
|
| 138 |
const cst_val *cg_state_index(const cst_item *p) |
| 139 |
{ |
| 140 |
float start; |
| 141 |
int thisone; |
| 142 |
start = (float)ffeature_int(p,"R:mcep_link.parent.daughter1.frame_number"); |
| 143 |
thisone = item_feat_int(p,"frame_number"); |
| 144 |
return float_val(thisone-start); |
| 145 |
} |
| 146 |
|
| 147 |
const cst_val *cg_state_rindex(const cst_item *p) |
| 148 |
{ |
| 149 |
float end; |
| 150 |
int thisone; |
| 151 |
end = (float)ffeature_int(p,"R:mcep_link.parent.daughtern.frame_number"); |
| 152 |
thisone = item_feat_int(p,"frame_number"); |
| 153 |
return float_val(end-thisone); |
| 154 |
} |
| 155 |
|
| 156 |
const cst_val *cg_phone_place(const cst_item *p) |
| 157 |
{ |
| 158 |
float start, end; |
| 159 |
int thisone; |
| 160 |
start = (float)ffeature_int(p,"R:mcep_link.parent.R:segstate.parent.daughter1.R:mcep_link.daughter1.frame_number"); |
| 161 |
end = (float)ffeature_int(p,"R:mcep_link.parent.R:segstate.parent.daughtern.R:mcep_link.daughtern.frame_number"); |
| 162 |
thisone = item_feat_int(p,"frame_number"); |
| 163 |
if ((end-start) == 0.0) |
| 164 |
return float_val(0.0); |
| 165 |
else |
| 166 |
return float_val((thisone-start)/(end-start)); |
| 167 |
} |
| 168 |
|
| 169 |
const cst_val *cg_phone_index(const cst_item *p) |
| 170 |
{ |
| 171 |
float start; |
| 172 |
int thisone; |
| 173 |
start = (float)ffeature_int(p,"R:mcep_link.parent.R:segstate.parent.daughter1.R:mcep_link.daughter1.frame_number"); |
| 174 |
thisone = item_feat_int(p,"frame_number"); |
| 175 |
return float_val(thisone-start); |
| 176 |
} |
| 177 |
|
| 178 |
const cst_val *cg_phone_rindex(const cst_item *p) |
| 179 |
{ |
| 180 |
float end; |
| 181 |
int thisone; |
| 182 |
end = (float)ffeature_int(p,"R:mcep_link.parent.R:segstate.parent.daughtern.R:mcep_link.daughtern.frame_number"); |
| 183 |
thisone = item_feat_int(p,"frame_number"); |
| 184 |
return float_val(end-thisone); |
| 185 |
} |
| 186 |
|
| 187 |
const cst_val *cg_is_pau(const cst_item *p) |
| 188 |
{ |
| 189 |
if (p && cst_streq("pau",item_feat_string(p,"name"))) |
| 190 |
return &val_int_1; |
| 191 |
else |
| 192 |
return &val_int_0; |
| 193 |
} |
| 194 |
|
| 195 |
const cst_val *cg_find_phrase_number(const cst_item *p) |
| 196 |
{ |
| 197 |
const cst_item *v; |
| 198 |
int x = 0; |
| 199 |
|
| 200 |
for (v=item_prev(p); v; v=item_prev(v)) |
| 201 |
x++; |
| 202 |
|
| 203 |
return val_int_n(x); |
| 204 |
} |
| 205 |
|
| 206 |
const cst_val *cg_position_in_phrasep(const cst_item *p) |
| 207 |
{ |
| 208 |
float pstart, pend, phrasenumber; |
| 209 |
float x; |
| 210 |
#define CG_FRAME_SHIFT 0.005 |
| 211 |
|
| 212 |
pstart = ffeature_float(p,"R:mcep_link.parent.R:segstate.parent.R:SylStructure.parent.parent.R:Phrase.parent.daughter1.R:SylStructure.daughter1.daughter1.R:Segment.p.end"); |
| 213 |
pend = ffeature_float(p,"R:mcep_link.parent.R:segstate.parent.R:SylStructure.parent.parent.R:Phrase.parent.daughtern.R:SylStructure.daughtern.daughtern.R:Segment.end"); |
| 214 |
phrasenumber = ffeature_float(p,"R:mcep_link.parent.R:segstate.parent.R:SylStructure.parent.parent.R:Phrase.parent.lisp_cg_find_phrase_number"); |
| 215 |
if ((pend - pstart) == 0.0) |
| 216 |
return float_val(-1.0); |
| 217 |
else |
| 218 |
{ |
| 219 |
x = phrasenumber + |
| 220 |
((CG_FRAME_SHIFT*item_feat_float(p,"frame_number"))-pstart) / |
| 221 |
(pend - pstart); |
| 222 |
return float_val(x); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
const cst_val *cg_position_in_phrase(const cst_item *p) |
| 227 |
{ |
| 228 |
float pstart, pend; |
| 229 |
float x; |
| 230 |
#define CG_FRAME_SHIFT 0.005 |
| 231 |
|
| 232 |
pstart = ffeature_float(p,"R:mcep_link.parent.R:segstate.parent.R:SylStructure.parent.parent.R:Phrase.parent.daughter1.R:SylStructure.daughter1.daughter1.R:Segment.p.end"); |
| 233 |
pend = ffeature_float(p,"R:mcep_link.parent.R:segstate.parent.R:SylStructure.parent.parent.R:Phrase.parent.daughtern.R:SylStructure.daughtern.daughtern.R:Segment.end"); |
| 234 |
if ((pend - pstart) == 0.0) |
| 235 |
return float_val(-1.0); |
| 236 |
else |
| 237 |
{ |
| 238 |
x = 0 + |
| 239 |
((CG_FRAME_SHIFT*item_feat_float(p,"frame_number"))-pstart) / |
| 240 |
(pend - pstart); |
| 241 |
return float_val(x); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
const cst_val *cg_position_in_sentence(const cst_item *p) |
| 246 |
{ |
| 247 |
float sstart, send; |
| 248 |
float x; |
| 249 |
#define CG_FRAME_SHIFT 0.005 |
| 250 |
|
| 251 |
sstart = ffeature_float(p,"R:mcep_link.parent.R:segstate.parent.R:SylStructure.parent.parent.R:Word.first.R:SylStructure.daughter1.daughter1.R:Segment.p.end"); |
| 252 |
send = ffeature_float(p,"R:mcep_link.parent.R:segstate.parent.R:SylStructure.parent.parent.R:Word.last.R:SylStructure.daughtern.daughtern.R:Segment.end"); |
| 253 |
if ((send - sstart) == 0.0) |
| 254 |
return float_val(-1.0); |
| 255 |
else |
| 256 |
{ |
| 257 |
x = ((CG_FRAME_SHIFT*item_feat_float(p,"frame_number"))-sstart) / |
| 258 |
(send - sstart); |
| 259 |
return float_val(x); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
/* Spam specific features, but may be useful for others */ |
| 264 |
const cst_val *pos_in_word(const cst_item *p) |
| 265 |
{ |
| 266 |
const cst_item *s; |
| 267 |
int i=0; |
| 268 |
p=item_as(p,"Syllable"); |
| 269 |
s=item_as(path_to_item(p,"R:SylStructure.parent.daughter1"),"Syllable"); |
| 270 |
for (;s && !item_equal(p,s);s=item_next(s),i++){} |
| 271 |
return val_string_n(i); |
| 272 |
} |
| 273 |
|
| 274 |
const cst_val *syllable_duration(const cst_item *p) |
| 275 |
{ |
| 276 |
return float_val(ffeature_float(p,"R:SylStructure.daughtern.R:Segment.end") - ffeature_float(p,"R:SylStructure.daughter1.R:Segment.p.end")); |
| 277 |
} |
| 278 |
|
| 279 |
const cst_val *syl_vowel(const cst_item *p) |
| 280 |
{ |
| 281 |
const cst_item *s,*ls; |
| 282 |
s=item_as(path_to_item(p,"R:SylStructure.daughter1"),"Segment"); |
| 283 |
ls=item_as(path_to_item(p,"R:SylStructure.daughtern"),"Segment"); |
| 284 |
for(;s && !item_equal(s,ls);s=item_next(s)) |
| 285 |
{ |
| 286 |
if (cst_streq("+",val_string(ph_vc(s)))) |
| 287 |
{ |
| 288 |
return string_val(item_name(s)); |
| 289 |
} |
| 290 |
} |
| 291 |
if (cst_streq("+",val_string(ph_vc(s)))) |
| 292 |
{ |
| 293 |
return string_val(item_name(s)); |
| 294 |
} |
| 295 |
return (cst_val *) NULL; |
| 296 |
} |
| 297 |
|
| 298 |
const cst_val *syl_numphones(const cst_item *p) |
| 299 |
{ |
| 300 |
int i; |
| 301 |
const cst_item *s,*ls; |
| 302 |
s=item_as(path_to_item(p,"R:SylStructure.daughter1"),"Segment"); |
| 303 |
ls=item_as(path_to_item(p,"R:SylStructure.daughtern"),"Segment"); |
| 304 |
for(i=1;s && !item_equal(s,ls);s=item_next(s)){i++;} |
| 305 |
return val_string_n(i); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
const cst_val *pos_in_phrase(const cst_item *p) |
| 310 |
{ |
| 311 |
const cst_item *s; |
| 312 |
int i=0; |
| 313 |
p=item_as(p,"Word"); |
| 314 |
s=item_as(path_to_item(p,"R:SylStructure.R:Phrase.parent.daughter1"),"Word"); |
| 315 |
for (;s && !item_equal(p,s);s=item_next(s),i++){} |
| 316 |
return val_string_n(i); |
| 317 |
} |
| 318 |
|
| 319 |
const cst_val *cg_syl_ratio(const cst_item *p) |
| 320 |
{ |
| 321 |
return float_val (( 1 + ffeature_float(p,"syl_in"))/(1 + ffeature_float(p,"syl_in") + ffeature_float(p,"syl_out"))); |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
const cst_val *cg_phrase_ratio(const cst_item *p) |
| 326 |
{ |
| 327 |
const cst_item *lp=p; |
| 328 |
while(item_next(lp)){lp=item_next(lp);} |
| 329 |
return float_val ((1 + ffeature_float(p,"lisp_cg_find_phrase_number"))/(1 + ffeature_float(lp,"lisp_cg_find_phrase_number"))); |
| 330 |
} |
| 331 |
|
| 332 |
const cst_val *cg_syls_in_phrase(const cst_item *p) |
| 333 |
{ |
| 334 |
cst_item *s=item_as(item_daughter(p),"Word"); |
| 335 |
return float_val(1 + ffeature_float(s,"R:SylStructure.daughter1.R:Syllable.syl_out")); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
static const cst_val *accented(const cst_item *syl) |
| 340 |
{ |
| 341 |
if ((item_feat_present(syl,"accent")) || |
| 342 |
(item_feat_present(syl,"endtone"))) |
| 343 |
return VAL_STRING_1; |
| 344 |
else |
| 345 |
return VAL_STRING_0; |
| 346 |
} |
| 347 |
|
| 348 |
static const cst_val *seg_coda_ctype(const cst_item *seg, const char *ctype) |
| 349 |
{ |
| 350 |
const cst_item *s; |
| 351 |
const cst_phoneset *ps = item_phoneset(seg); |
| 352 |
|
| 353 |
for (s=item_last_daughter(item_parent(item_as(seg,"SylStructure"))); |
| 354 |
s; |
| 355 |
s=item_prev(s)) |
| 356 |
{ |
| 357 |
if (cst_streq("+",phone_feature_string(ps,item_feat_string(s,"name"), |
| 358 |
"vc"))) |
| 359 |
return VAL_STRING_0; |
| 360 |
if (cst_streq(ctype,phone_feature_string(ps,item_feat_string(s,"name"), |
| 361 |
"ctype"))) |
| 362 |
return VAL_STRING_1; |
| 363 |
} |
| 364 |
|
| 365 |
return VAL_STRING_0; |
| 366 |
} |
| 367 |
|
| 368 |
static const cst_val *seg_onset_ctype(const cst_item *seg, const char *ctype) |
| 369 |
{ |
| 370 |
const cst_item *s; |
| 371 |
const cst_phoneset *ps = item_phoneset(seg); |
| 372 |
|
| 373 |
for (s=item_daughter(item_parent(item_as(seg,"SylStructure"))); |
| 374 |
s; |
| 375 |
s=item_next(s)) |
| 376 |
{ |
| 377 |
if (cst_streq("+",phone_feature_string(ps,item_feat_string(s,"name"), |
| 378 |
"vc"))) |
| 379 |
return VAL_STRING_0; |
| 380 |
if (cst_streq(ctype,phone_feature_string(ps,item_feat_string(s,"name"), |
| 381 |
"ctype"))) |
| 382 |
return VAL_STRING_1; |
| 383 |
} |
| 384 |
|
| 385 |
return VAL_STRING_0; |
| 386 |
} |
| 387 |
|
| 388 |
static const cst_val *seg_coda_fric(const cst_item *seg) |
| 389 |
{ |
| 390 |
return seg_coda_ctype(seg,"f"); |
| 391 |
} |
| 392 |
|
| 393 |
static const cst_val *seg_onset_fric(const cst_item *seg) |
| 394 |
{ |
| 395 |
return seg_onset_ctype(seg,"f"); |
| 396 |
} |
| 397 |
|
| 398 |
static const cst_val *seg_coda_stop(const cst_item *seg) |
| 399 |
{ |
| 400 |
return seg_coda_ctype(seg,"s"); |
| 401 |
} |
| 402 |
|
| 403 |
static const cst_val *seg_onset_stop(const cst_item *seg) |
| 404 |
{ |
| 405 |
return seg_onset_ctype(seg,"s"); |
| 406 |
} |
| 407 |
|
| 408 |
static const cst_val *seg_coda_nasal(const cst_item *seg) |
| 409 |
{ |
| 410 |
return seg_coda_ctype(seg,"n"); |
| 411 |
} |
| 412 |
|
| 413 |
static const cst_val *seg_onset_nasal(const cst_item *seg) |
| 414 |
{ |
| 415 |
return seg_onset_ctype(seg,"n"); |
| 416 |
} |
| 417 |
|
| 418 |
static const cst_val *seg_coda_glide(const cst_item *seg) |
| 419 |
{ |
| 420 |
if (seg_coda_ctype(seg,"r") == VAL_STRING_0) |
| 421 |
return seg_coda_ctype(seg,"l"); |
| 422 |
return VAL_STRING_1; |
| 423 |
} |
| 424 |
|
| 425 |
static const cst_val *seg_onset_glide(const cst_item *seg) |
| 426 |
{ |
| 427 |
if (seg_onset_ctype(seg,"r") == VAL_STRING_0) |
| 428 |
return seg_onset_ctype(seg,"l"); |
| 429 |
return VAL_STRING_1; |
| 430 |
} |
| 431 |
|
| 432 |
static const cst_val *seg_onsetcoda(const cst_item *seg) |
| 433 |
{ |
| 434 |
const cst_item *s; |
| 435 |
const cst_phoneset *ps = item_phoneset(seg); |
| 436 |
|
| 437 |
if (!seg) return VAL_STRING_0; |
| 438 |
for (s=item_next(item_as(seg,"SylStructure")); |
| 439 |
s; |
| 440 |
s=item_next(s)) |
| 441 |
{ |
| 442 |
if (cst_streq("+",phone_feature_string(ps,item_feat_string(s,"name"), |
| 443 |
"vc"))) |
| 444 |
return (cst_val *)&val_string_onset; |
| 445 |
} |
| 446 |
return (cst_val *)&val_string_coda; |
| 447 |
} |
| 448 |
|
| 449 |
static const cst_val *pos_in_syl(const cst_item *seg) |
| 450 |
{ |
| 451 |
const cst_item *s; |
| 452 |
int c; |
| 453 |
|
| 454 |
for (c=-1,s=item_as(seg,"SylStructure"); |
| 455 |
s; |
| 456 |
s=item_prev(s),c++); |
| 457 |
|
| 458 |
return val_string_n(c); |
| 459 |
} |
| 460 |
|
| 461 |
static const cst_val *position_type(const cst_item *syl) |
| 462 |
{ |
| 463 |
const cst_item *s = item_as(syl,"SylStructure"); |
| 464 |
|
| 465 |
if (s == 0) |
| 466 |
return (cst_val *)&val_string_single; |
| 467 |
else if (item_next(s) == 0) |
| 468 |
{ |
| 469 |
if (item_prev(s) == 0) |
| 470 |
return (cst_val *)&val_string_single; |
| 471 |
else |
| 472 |
return (cst_val *)&val_string_final; |
| 473 |
} |
| 474 |
else if (item_prev(s) == 0) |
| 475 |
return (cst_val *)&val_string_initial; |
| 476 |
else |
| 477 |
return (cst_val *)&val_string_mid; |
| 478 |
} |
| 479 |
|
| 480 |
static const cst_val *sub_phrases(const cst_item *syl) |
| 481 |
{ |
| 482 |
const cst_item *s; |
| 483 |
int c; |
| 484 |
|
| 485 |
for (c=0,s=path_to_item(syl,"R:SylStructure.parent.R:Phrase.parent.p"); |
| 486 |
s && (c < CST_CONST_INT_MAX); |
| 487 |
s=item_prev(s),c++); |
| 488 |
|
| 489 |
return val_string_n(c); |
| 490 |
} |
| 491 |
|
| 492 |
static const cst_val *last_accent(const cst_item *syl) |
| 493 |
{ |
| 494 |
const cst_item *s; |
| 495 |
int c; |
| 496 |
|
| 497 |
for (c=0,s=item_as(syl,"Syllable"); |
| 498 |
s && (c < CST_CONST_INT_MAX); |
| 499 |
s=item_prev(s),c++) |
| 500 |
{ |
| 501 |
if (val_int(accented(s))) |
| 502 |
return val_string_n(c); |
| 503 |
} |
| 504 |
|
| 505 |
return val_string_n(c); |
| 506 |
} |
| 507 |
|
| 508 |
static const cst_val *next_accent(const cst_item *syl) |
| 509 |
{ |
| 510 |
const cst_item *s; |
| 511 |
int c; |
| 512 |
|
| 513 |
s=item_as(syl,"Syllable"); |
| 514 |
if (s) |
| 515 |
s = item_next(s); |
| 516 |
else |
| 517 |
return val_string_n(0); |
| 518 |
for (c=0; |
| 519 |
s && (c < CST_CONST_INT_MAX); |
| 520 |
s=item_next(s),c++) |
| 521 |
{ |
| 522 |
if (val_int(accented(s))) |
| 523 |
return val_string_n(c); |
| 524 |
} |
| 525 |
|
| 526 |
return val_string_n(c); |
| 527 |
} |
| 528 |
|
| 529 |
static const cst_val *syl_final(const cst_item *seg) |
| 530 |
{ /* last segment in a syllable */ |
| 531 |
const cst_item *s = item_as(seg,"SylStructure"); |
| 532 |
|
| 533 |
if (!s || (item_next(s) == NULL)) |
| 534 |
return VAL_STRING_1; |
| 535 |
else |
| 536 |
return VAL_STRING_0; |
| 537 |
} |
| 538 |
|
| 539 |
static const cst_val *syl_initial(const cst_item *seg) |
| 540 |
{ /* first segment in a syllable */ |
| 541 |
const cst_item *s = item_as(seg,"SylStructure"); |
| 542 |
|
| 543 |
if (!s || (item_prev(s) == NULL)) |
| 544 |
return VAL_STRING_1; |
| 545 |
else |
| 546 |
return VAL_STRING_0; |
| 547 |
} |
| 548 |
|
| 549 |
static const cst_val *word_punc(const cst_item *word) |
| 550 |
{ |
| 551 |
cst_item *ww; |
| 552 |
const cst_val *v; |
| 553 |
|
| 554 |
ww = item_as(word,"Token"); |
| 555 |
|
| 556 |
if ((ww != NULL) && (item_next(ww) != 0)) |
| 557 |
v = &val_string_empty; |
| 558 |
else |
| 559 |
v = ffeature(item_parent(ww),"punc"); |
| 560 |
|
| 561 |
/* printf("word_punc word %s punc %s\n", |
| 562 |
item_feat_string(ww,"name"), |
| 563 |
val_string(v)); */ |
| 564 |
|
| 565 |
return v; |
| 566 |
|
| 567 |
} |
| 568 |
|
| 569 |
static const cst_val *word_break(const cst_item *word) |
| 570 |
{ |
| 571 |
cst_item *ww,*pp; |
| 572 |
const char *pname; |
| 573 |
|
| 574 |
ww = item_as(word,"Phrase"); |
| 575 |
|
| 576 |
if ((ww == NULL) || (item_next(ww) != 0)) |
| 577 |
return VAL_STRING_1; |
| 578 |
else |
| 579 |
{ |
| 580 |
pp = item_parent(ww); |
| 581 |
pname = val_string(item_feat(pp,"name")); |
| 582 |
if (cst_streq("BB",pname)) |
| 583 |
return VAL_STRING_4; |
| 584 |
else if (cst_streq("B",pname)) |
| 585 |
return VAL_STRING_3; |
| 586 |
else |
| 587 |
return VAL_STRING_1; |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
static const cst_val *word_numsyls(const cst_item *word) |
| 592 |
{ |
| 593 |
cst_item *d; |
| 594 |
int c; |
| 595 |
|
| 596 |
for (c=0,d=item_daughter(item_as(word,"SylStructure")); |
| 597 |
d; |
| 598 |
d=item_next(d),c++); |
| 599 |
|
| 600 |
return val_int_n(c); |
| 601 |
} |
| 602 |
|
| 603 |
static const cst_val *ssyl_in(const cst_item *syl) |
| 604 |
{ |
| 605 |
/* Number of stressed syllables since last major break */ |
| 606 |
const cst_item *ss,*p,*fs; |
| 607 |
int c; |
| 608 |
|
| 609 |
ss = item_as(syl,"Syllable"); |
| 610 |
|
| 611 |
fs = path_to_item(syl,"R:SylStructure.parent.R:Phrase.parent.daughter.R:SylStructure.daughter"); |
| 612 |
|
| 613 |
#if 1 /* fix by uratec */ |
| 614 |
if(item_equal(ss,fs)) |
| 615 |
return val_string_n(0); |
| 616 |
#else |
| 617 |
/* This should actually include the first syllable, but Festival's |
| 618 |
doesn't. */ |
| 619 |
#endif |
| 620 |
for (c=0, p=item_prev(ss); |
| 621 |
p && (!item_equal(p,fs)) && (c < CST_CONST_INT_MAX); |
| 622 |
p=item_prev(p)) |
| 623 |
{ |
| 624 |
if (cst_streq("1",ffeature_string(p,"stress"))) |
| 625 |
c++; |
| 626 |
} |
| 627 |
|
| 628 |
return val_string_n(c); /* its used randomly as int and float */ |
| 629 |
} |
| 630 |
|
| 631 |
static const cst_val *ssyl_out(const cst_item *syl) |
| 632 |
{ |
| 633 |
/* Number of stressed syllables until last major break */ |
| 634 |
const cst_item *ss,*p,*fs; |
| 635 |
int c; |
| 636 |
|
| 637 |
ss = item_as(syl,"Syllable"); |
| 638 |
|
| 639 |
fs = path_to_item(syl,"R:SylStructure.parent.R:Phrase.parent.daughtern.R:SylStructure.daughtern"); |
| 640 |
|
| 641 |
#if 1 /* fix by uratec */ |
| 642 |
if(item_equal(ss,fs)) |
| 643 |
return val_string_n(0); |
| 644 |
#endif |
| 645 |
for (c=0, p=item_next(ss); |
| 646 |
p && (c < CST_CONST_INT_MAX); |
| 647 |
p=item_next(p)) |
| 648 |
{ |
| 649 |
if (cst_streq("1",ffeature_string(p,"stress"))) |
| 650 |
c++; |
| 651 |
if (item_equal(p,fs)) |
| 652 |
break; |
| 653 |
} |
| 654 |
|
| 655 |
return val_string_n(c); /* its used randomly as int and float */ |
| 656 |
} |
| 657 |
|
| 658 |
static const cst_val *syl_in(const cst_item *syl) |
| 659 |
{ |
| 660 |
/* Number of syllables since last major break */ |
| 661 |
const cst_item *ss,*p,*fs; |
| 662 |
int c; |
| 663 |
|
| 664 |
ss = item_as(syl,"Syllable"); |
| 665 |
|
| 666 |
fs = path_to_item(syl,"R:SylStructure.parent.R:Phrase.parent.daughter.R:SylStructure.daughter"); |
| 667 |
|
| 668 |
for (c=0, p=ss; |
| 669 |
p && (c < CST_CONST_INT_MAX); |
| 670 |
p=item_prev(p),c++) |
| 671 |
if (item_equal(p,fs)) |
| 672 |
break; |
| 673 |
return val_string_n(c); |
| 674 |
} |
| 675 |
|
| 676 |
static const cst_val *syl_out(const cst_item *syl) |
| 677 |
{ |
| 678 |
/* Number of syllables until next major break */ |
| 679 |
cst_item *ss,*p; |
| 680 |
const cst_item *fs; |
| 681 |
int c; |
| 682 |
|
| 683 |
ss = item_as(syl,"Syllable"); |
| 684 |
|
| 685 |
fs = path_to_item(syl,"R:SylStructure.parent.R:Phrase.parent.daughtern.R:SylStructure.daughtern"); |
| 686 |
|
| 687 |
for (c=0, p=ss; |
| 688 |
p && (c < CST_CONST_INT_MAX); |
| 689 |
p=item_next(p),c++) |
| 690 |
if (item_equal(p,fs)) |
| 691 |
break; |
| 692 |
return val_string_n(c); |
| 693 |
} |
| 694 |
|
| 695 |
static const cst_val *syl_break(const cst_item *syl) |
| 696 |
{ |
| 697 |
/* Break level after this syllable */ |
| 698 |
cst_item *ss; |
| 699 |
|
| 700 |
ss = item_as(syl,"SylStructure"); |
| 701 |
|
| 702 |
if (ss == NULL) |
| 703 |
return VAL_STRING_1; /* hmm, no sylstructure */ |
| 704 |
else if (item_next(ss) != NULL) |
| 705 |
return VAL_STRING_0; /* word internal */ |
| 706 |
else if (item_parent(ss) == NULL) /* no parent */ |
| 707 |
return VAL_STRING_1; |
| 708 |
else |
| 709 |
return word_break(item_parent(ss)); |
| 710 |
} |
| 711 |
|
| 712 |
static const cst_val *cg_break(const cst_item *syl) |
| 713 |
{ |
| 714 |
/* phrase prediction is so different between flite and festival */ |
| 715 |
/* we go with this more robust technique */ |
| 716 |
cst_item *ss; |
| 717 |
|
| 718 |
ss = item_as(syl,"SylStructure"); |
| 719 |
|
| 720 |
if (ss == NULL) |
| 721 |
return VAL_STRING_0; /* hmm, no sylstructure */ |
| 722 |
else if (item_next(ss) != NULL) |
| 723 |
return VAL_STRING_0; /* word internal */ |
| 724 |
else if (path_to_item(ss,"R:SylStructure.parent.R:Word.n") == NULL) |
| 725 |
return VAL_STRING_4; /* utterance final */ |
| 726 |
else if (path_to_item(ss,"R:SylStructure.parent.R:Phrase.n") == NULL) |
| 727 |
return VAL_STRING_3; /* phrase final */ |
| 728 |
else |
| 729 |
return VAL_STRING_1; /* word final */ |
| 730 |
} |
| 731 |
|
| 732 |
static const cst_val *syl_codasize(const cst_item *syl) |
| 733 |
{ |
| 734 |
cst_item *d; |
| 735 |
int c; |
| 736 |
|
| 737 |
for (c=1,d=item_last_daughter(item_as(syl,"SylStructure")); |
| 738 |
d; |
| 739 |
d=item_prev(d),c++) |
| 740 |
{ |
| 741 |
if (cst_streq("+",val_string(ph_vc(d)))) |
| 742 |
break; |
| 743 |
} |
| 744 |
|
| 745 |
return val_string_n(c); |
| 746 |
} |
| 747 |
|
| 748 |
static const cst_val *syl_onsetsize(const cst_item *syl) |
| 749 |
{ |
| 750 |
cst_item *d; |
| 751 |
int c; |
| 752 |
|
| 753 |
for (c=0,d=item_daughter(item_as(syl,"SylStructure")); |
| 754 |
d; |
| 755 |
d=item_next(d),c++) |
| 756 |
{ |
| 757 |
if (cst_streq("+",val_string(ph_vc(d)))) |
| 758 |
break; |
| 759 |
} |
| 760 |
|
| 761 |
return val_string_n(c); |
| 762 |
} |
| 763 |
|
| 764 |
static const cst_val *asyl_in(const cst_item *syl) |
| 765 |
{ |
| 766 |
/* Number of accented syllables since last major break */ |
| 767 |
const cst_item *ss,*p,*fs; |
| 768 |
int c; |
| 769 |
|
| 770 |
ss = item_as(syl,"Syllable"); |
| 771 |
|
| 772 |
fs = path_to_item(syl,"R:SylStructure.parent.R:Phrase.parent.daughter.R:SylStructure.daughter"); |
| 773 |
|
| 774 |
#if 1 /* fix by uratec */ |
| 775 |
if(item_equal(ss,fs)) |
| 776 |
return val_string_n(0); |
| 777 |
for (c=0, p=item_prev(ss); |
| 778 |
#else |
| 779 |
for (c=0, p=ss; |
| 780 |
#endif |
| 781 |
p && (c < CST_CONST_INT_MAX); |
| 782 |
p=item_prev(p)) |
| 783 |
{ |
| 784 |
if (val_int(accented(p)) == 1) |
| 785 |
c++; |
| 786 |
if (item_equal(p,fs)) |
| 787 |
break; |
| 788 |
} |
| 789 |
|
| 790 |
return val_string_n(c); |
| 791 |
} |
| 792 |
|
| 793 |
static const cst_val *asyl_out(const cst_item *syl) |
| 794 |
{ |
| 795 |
/* Number of accented syllables until next major break */ |
| 796 |
cst_item *ss,*p; |
| 797 |
const cst_item *fs; |
| 798 |
int c; |
| 799 |
|
| 800 |
ss = item_as(syl,"Syllable"); |
| 801 |
|
| 802 |
fs = path_to_item(syl,"R:SylStructure.parent.R:Phrase.parent.daughtern.R:SylStructure.daughtern"); |
| 803 |
|
| 804 |
#if 1 /* fix by uratec */ |
| 805 |
if(item_equal(ss,fs)) |
| 806 |
return val_string_n(0); |
| 807 |
for (c=0, p=item_next(ss); |
| 808 |
#else |
| 809 |
for (c=0, p=ss; |
| 810 |
#endif |
| 811 |
p && (c < CST_CONST_INT_MAX); |
| 812 |
p=item_next(p)) |
| 813 |
{ |
| 814 |
if (val_int(accented(p)) == 1) |
| 815 |
c++; |
| 816 |
if (item_equal(p,fs)) |
| 817 |
break; |
| 818 |
} |
| 819 |
return val_string_n(c); |
| 820 |
} |
| 821 |
|
| 822 |
static const cst_val *segment_duration(const cst_item *seg) |
| 823 |
{ |
| 824 |
const cst_item *s = item_as(seg,"Segment"); |
| 825 |
|
| 826 |
if (!s) |
| 827 |
return VAL_STRING_0; |
| 828 |
else if (item_prev(s) == NULL) |
| 829 |
return item_feat(s,"end"); |
| 830 |
else |
| 831 |
{ |
| 832 |
/* It should be okay to construct this as it will get |
| 833 |
dereferenced when the CART interpreter frees its feature |
| 834 |
cache. */ |
| 835 |
return float_val(item_feat_float(s,"end") |
| 836 |
- item_feat_float(item_prev(s),"end")); |
| 837 |
} |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
void basic_ff_register(cst_features *ffunctions) |
| 842 |
{ |
| 843 |
ff_register(ffunctions, "ph_vc",ph_vc); |
| 844 |
ff_register(ffunctions, "ph_vlng",ph_vlng); |
| 845 |
ff_register(ffunctions, "ph_vheight",ph_vheight); |
| 846 |
ff_register(ffunctions, "ph_vfront",ph_vfront); |
| 847 |
ff_register(ffunctions, "ph_vrnd",ph_vrnd); |
| 848 |
ff_register(ffunctions, "ph_ctype",ph_ctype); |
| 849 |
ff_register(ffunctions, "ph_cplace",ph_cplace); |
| 850 |
ff_register(ffunctions, "ph_cvox",ph_cvox); |
| 851 |
|
| 852 |
ff_register(ffunctions, "lisp_cg_duration", cg_duration); |
| 853 |
ff_register(ffunctions, "lisp_cg_state_pos", cg_state_pos); |
| 854 |
ff_register(ffunctions, "lisp_cg_state_place", cg_state_place); |
| 855 |
ff_register(ffunctions, "lisp_cg_state_index", cg_state_index); |
| 856 |
ff_register(ffunctions, "lisp_cg_state_rindex", cg_state_rindex); |
| 857 |
ff_register(ffunctions, "lisp_cg_phone_place", cg_phone_place); |
| 858 |
ff_register(ffunctions, "lisp_cg_phone_index", cg_phone_index); |
| 859 |
ff_register(ffunctions, "lisp_cg_phone_rindex", cg_phone_rindex); |
| 860 |
|
| 861 |
ff_register(ffunctions, "lisp_cg_position_in_phrasep", cg_position_in_phrasep); |
| 862 |
ff_register(ffunctions, "lisp_cg_position_in_phrase", cg_position_in_phrase); |
| 863 |
ff_register(ffunctions, "lisp_cg_position_in_sentence", cg_position_in_sentence); |
| 864 |
ff_register(ffunctions, "lisp_cg_find_phrase_number", cg_find_phrase_number); |
| 865 |
ff_register(ffunctions, "lisp_is_pau", cg_is_pau); |
| 866 |
|
| 867 |
ff_register(ffunctions, "word_numsyls",word_numsyls); |
| 868 |
ff_register(ffunctions, "word_break",word_break); |
| 869 |
ff_register(ffunctions, "word_punc",word_punc); |
| 870 |
ff_register(ffunctions, "ssyl_in",ssyl_in); |
| 871 |
ff_register(ffunctions, "ssyl_out",ssyl_out); |
| 872 |
ff_register(ffunctions, "syl_in",syl_in); |
| 873 |
ff_register(ffunctions, "syl_out",syl_out); |
| 874 |
ff_register(ffunctions, "syl_break",syl_break); |
| 875 |
ff_register(ffunctions, "lisp_cg_break",cg_break); |
| 876 |
ff_register(ffunctions, "old_syl_break",syl_break); |
| 877 |
ff_register(ffunctions, "syl_onsetsize",syl_onsetsize); |
| 878 |
ff_register(ffunctions, "syl_codasize",syl_codasize); |
| 879 |
ff_register(ffunctions, "accented",accented); |
| 880 |
ff_register(ffunctions, "asyl_in",asyl_in); |
| 881 |
ff_register(ffunctions, "asyl_out",asyl_out); |
| 882 |
ff_register(ffunctions, "lisp_coda_fric",seg_coda_fric); |
| 883 |
ff_register(ffunctions, "lisp_onset_fric",seg_onset_fric); |
| 884 |
ff_register(ffunctions, "lisp_coda_stop",seg_coda_stop); |
| 885 |
ff_register(ffunctions, "lisp_onset_stop",seg_onset_stop); |
| 886 |
ff_register(ffunctions, "lisp_coda_nasal",seg_coda_nasal); |
| 887 |
ff_register(ffunctions, "lisp_onset_nasal",seg_onset_nasal); |
| 888 |
ff_register(ffunctions, "lisp_coda_glide",seg_coda_glide); |
| 889 |
ff_register(ffunctions, "lisp_onset_glide",seg_onset_glide); |
| 890 |
ff_register(ffunctions, "seg_onsetcoda",seg_onsetcoda); |
| 891 |
ff_register(ffunctions, "pos_in_syl",pos_in_syl); |
| 892 |
ff_register(ffunctions, "position_type",position_type); |
| 893 |
ff_register(ffunctions, "sub_phrases",sub_phrases); |
| 894 |
ff_register(ffunctions, "last_accent",last_accent); |
| 895 |
ff_register(ffunctions, "next_accent",next_accent); |
| 896 |
ff_register(ffunctions, "syl_final",syl_final); |
| 897 |
ff_register(ffunctions, "syl_initial",syl_initial); |
| 898 |
ff_register(ffunctions, "segment_duration",segment_duration); |
| 899 |
ff_register(ffunctions, "lisp_cg_syl_ratio",cg_syl_ratio); |
| 900 |
ff_register(ffunctions, "lisp_cg_phrase_ratio",cg_phrase_ratio); |
| 901 |
ff_register(ffunctions, "lisp_cg_syls_in_phrase",cg_syls_in_phrase); |
| 902 |
ff_register(ffunctions, "pos_in_phrase",pos_in_phrase); |
| 903 |
ff_register(ffunctions, "pos_in_word",pos_in_word); |
| 904 |
ff_register(ffunctions, "syllable_duration",syllable_duration); |
| 905 |
ff_register(ffunctions, "syl_vowel",syl_vowel); |
| 906 |
ff_register(ffunctions, "syl_numphones",syl_numphones); |
| 907 |
} |