| 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 |
#include "cst_features.h" |
| 86 |
#include "cst_lexicon.h" |
| 87 |
#include "cst_tokenstream.h" |
| 88 |
|
| 89 |
CST_VAL_REGISTER_TYPE_NODEL(lexicon,cst_lexicon) |
| 90 |
|
| 91 |
#define WP_SIZE 64 |
| 92 |
|
| 93 |
static int no_syl_boundaries(const cst_item *i, const cst_val *p); |
| 94 |
static cst_val *lex_lookup_addenda(const char *wp,const cst_lexicon *l, |
| 95 |
int *found); |
| 96 |
|
| 97 |
static int lex_match_entry(const char *a, const char *b); |
| 98 |
static int lex_lookup_bsearch(const cst_lexicon *l,const char *word); |
| 99 |
static int find_full_match(const cst_lexicon *l, |
| 100 |
int i,const char *word); |
| 101 |
|
| 102 |
#ifndef FLITE_PLUS_HTS_ENGINE |
| 103 |
cst_lexicon *new_lexicon() |
| 104 |
{ |
| 105 |
cst_lexicon *l = cst_alloc(cst_lexicon,1); |
| 106 |
l->syl_boundary = no_syl_boundaries; |
| 107 |
return l; |
| 108 |
} |
| 109 |
|
| 110 |
void delete_lexicon(cst_lexicon *lex) |
| 111 |
{ |
| 112 |
|
| 113 |
if (lex) |
| 114 |
{ |
| 115 |
cst_free(lex->data); |
| 116 |
cst_free(lex); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
cst_val *cst_lex_load_addenda(const cst_lexicon *lex, const char *lexfile) |
| 121 |
{ |
| 122 |
cst_tokenstream *lf; |
| 123 |
const cst_string *line; |
| 124 |
cst_val *e = NULL; |
| 125 |
cst_val *na = NULL; |
| 126 |
int i; |
| 127 |
|
| 128 |
lf = ts_open(lexfile,"\n","","",""); |
| 129 |
if (lf == NULL) |
| 130 |
{ |
| 131 |
cst_errmsg("lex_add_addenda: cannot open lexicon file\n"); |
| 132 |
return NULL;; |
| 133 |
} |
| 134 |
|
| 135 |
while (!ts_eof(lf)) |
| 136 |
{ |
| 137 |
line = ts_get(lf); |
| 138 |
if (line[0] == '#') |
| 139 |
continue; |
| 140 |
for (i=0; line[i]; i++) |
| 141 |
{ |
| 142 |
if (line[i] != ' ') |
| 143 |
break; |
| 144 |
} |
| 145 |
if (line[i]) |
| 146 |
{ |
| 147 |
e = cst_lex_make_entry(lex,line); |
| 148 |
if (e) |
| 149 |
na = cons_val(e,na); |
| 150 |
} |
| 151 |
else |
| 152 |
continue; |
| 153 |
} |
| 154 |
|
| 155 |
ts_close(lf); |
| 156 |
return val_reverse(na); |
| 157 |
} |
| 158 |
|
| 159 |
cst_val *cst_lex_make_entry(const cst_lexicon *lex, const cst_string *entry) |
| 160 |
{ |
| 161 |
|
| 162 |
cst_tokenstream *e; |
| 163 |
cst_val *phones = NULL; |
| 164 |
cst_val *ventry; |
| 165 |
const cst_string *w, *p; |
| 166 |
cst_string *word; |
| 167 |
cst_string *pos; |
| 168 |
int i; |
| 169 |
|
| 170 |
e = ts_open_string(entry, |
| 171 |
cst_ts_default_whitespacesymbols, |
| 172 |
"","",""); |
| 173 |
|
| 174 |
w = ts_get(e); |
| 175 |
if (w[0] == '"') |
| 176 |
{ |
| 177 |
ts_close(e); |
| 178 |
e = ts_open_string(entry, |
| 179 |
cst_ts_default_whitespacesymbols, |
| 180 |
"","",""); |
| 181 |
w = ts_get_quoted_token(e,'"','\\'); |
| 182 |
} |
| 183 |
|
| 184 |
word = cst_strdup(w); |
| 185 |
p = ts_get(e); |
| 186 |
if (!cst_streq(":",p)) |
| 187 |
{ |
| 188 |
pos = cst_strdup(p); |
| 189 |
p = ts_get(e); |
| 190 |
if (!cst_streq(":",p)) |
| 191 |
{ |
| 192 |
cst_fprintf(stdout,"add_addenda: lex %s: expected \":\" in %s\n", |
| 193 |
lex->name, |
| 194 |
word); |
| 195 |
cst_free(word); cst_free(pos); ts_close(e); |
| 196 |
return NULL; |
| 197 |
} |
| 198 |
} |
| 199 |
else |
| 200 |
pos = cst_strdup("nil"); |
| 201 |
|
| 202 |
while (!ts_eof(e)) |
| 203 |
{ |
| 204 |
p = ts_get(e); |
| 205 |
|
| 206 |
for (i=0; lex->phone_table[i]; i++) |
| 207 |
if (cst_streq(p,lex->phone_table[i])) |
| 208 |
break; |
| 209 |
if (cst_streq("#",p)) |
| 210 |
break; |
| 211 |
else if (lex->phone_table[i]) |
| 212 |
|
| 213 |
phones = cons_val(string_val(p),phones); |
| 214 |
else |
| 215 |
{ |
| 216 |
cst_fprintf(stdout,"add_addenda: lex: %s word %s phone %s not in lexicon phoneset\n", |
| 217 |
lex->name, |
| 218 |
word, |
| 219 |
p); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
ventry = cons_val(string_val(word),cons_val(string_val(pos), |
| 224 |
val_reverse(phones))); |
| 225 |
cst_free(word); cst_free(pos); ts_close(e); |
| 226 |
#if 0 |
| 227 |
printf("entry: "); |
| 228 |
val_print(stdout,ventry); |
| 229 |
printf("\n"); |
| 230 |
#endif |
| 231 |
|
| 232 |
return ventry; |
| 233 |
} |
| 234 |
|
| 235 |
#if 0 |
| 236 |
void lexicon_register(cst_lexicon *lex) |
| 237 |
{ |
| 238 |
|
| 239 |
cst_lexicon **old_lexs; |
| 240 |
int i; |
| 241 |
|
| 242 |
old_lexs = flite_lexicons; |
| 243 |
flite_num_lexicons++; |
| 244 |
flite_lexicons = cst_alloc(cst_lexicon *,flite_num_lexicons); |
| 245 |
for (i=0; i<flite_num_lexicons-1; i++) |
| 246 |
flite_lexicons[i] = old_lexs[i]; |
| 247 |
flite_lexicons[i] = lex; |
| 248 |
cst_free(old_lexs); |
| 249 |
} |
| 250 |
|
| 251 |
cst_lexicon *lexicon_select(const char *name) |
| 252 |
{ |
| 253 |
int i; |
| 254 |
|
| 255 |
for (i=0; i < flite_num_lexicons; i++) |
| 256 |
if (cst_streq(name,flite_lexicons[i]->name)) |
| 257 |
return flite_lexicons[i]; |
| 258 |
return NULL; |
| 259 |
} |
| 260 |
#endif |
| 261 |
|
| 262 |
static int no_syl_boundaries(const cst_item *i, const cst_val *p) |
| 263 |
{ |
| 264 |
|
| 265 |
|
| 266 |
(void)i; |
| 267 |
(void)p; |
| 268 |
return FALSE; |
| 269 |
} |
| 270 |
#endif |
| 271 |
|
| 272 |
int in_lex(const cst_lexicon *l, const char *word, const char *pos) |
| 273 |
{ |
| 274 |
|
| 275 |
int r = FALSE, i; |
| 276 |
char *wp; |
| 277 |
|
| 278 |
wp = cst_alloc(char,cst_strlen(word)+2); |
| 279 |
cst_sprintf(wp,"%c%s",(pos ? pos[0] : '0'),word); |
| 280 |
|
| 281 |
for (i=0; l->addenda && l->addenda[i]; i++) |
| 282 |
{ |
| 283 |
if (((wp[0] == '0') || (wp[0] == l->addenda[i][0][0])) && |
| 284 |
(cst_streq(wp+1,l->addenda[i][0]+1))) |
| 285 |
{ |
| 286 |
r = TRUE; |
| 287 |
break; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
if (!r && (lex_lookup_bsearch(l,wp) >= 0)) |
| 292 |
r = TRUE; |
| 293 |
|
| 294 |
cst_free(wp); |
| 295 |
return r; |
| 296 |
} |
| 297 |
|
| 298 |
cst_val *lex_lookup(const cst_lexicon *l, const char *word, const char *pos) |
| 299 |
{ |
| 300 |
int index; |
| 301 |
int p; |
| 302 |
const char *q; |
| 303 |
char *wp; |
| 304 |
cst_val *phones = 0; |
| 305 |
int found = FALSE; |
| 306 |
|
| 307 |
wp = cst_alloc(char,cst_strlen(word)+2); |
| 308 |
cst_sprintf(wp,"%c%s",(pos ? pos[0] : '0'),word); |
| 309 |
|
| 310 |
if (l->addenda) |
| 311 |
phones = lex_lookup_addenda(wp,l,&found); |
| 312 |
|
| 313 |
if (!found) |
| 314 |
{ |
| 315 |
index = lex_lookup_bsearch(l,wp); |
| 316 |
|
| 317 |
if (index >= 0) |
| 318 |
{ |
| 319 |
if (l->phone_hufftable) |
| 320 |
{ |
| 321 |
for (p=index-2; l->data[p]; p--) |
| 322 |
for (q=l->phone_hufftable[l->data[p]]; *q; q++) |
| 323 |
phones = cons_val(string_val(l->phone_table[(unsigned char)*q]), |
| 324 |
phones); |
| 325 |
} |
| 326 |
else |
| 327 |
{ |
| 328 |
for (p=index-2; l->data[p]; p--) |
| 329 |
phones = cons_val(string_val(l->phone_table[l->data[p]]), |
| 330 |
phones); |
| 331 |
} |
| 332 |
phones = val_reverse(phones); |
| 333 |
} |
| 334 |
else if (l->lts_function) |
| 335 |
{ |
| 336 |
phones = (l->lts_function)(l,word,""); |
| 337 |
} |
| 338 |
else if (l->lts_rule_set) |
| 339 |
{ |
| 340 |
phones = lts_apply(word, |
| 341 |
"", |
| 342 |
l->lts_rule_set); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
cst_free(wp); |
| 347 |
|
| 348 |
return phones; |
| 349 |
} |
| 350 |
|
| 351 |
static cst_val *lex_lookup_addenda(const char *wp,const cst_lexicon *l, |
| 352 |
int *found) |
| 353 |
{ |
| 354 |
|
| 355 |
int i,j; |
| 356 |
cst_val *phones; |
| 357 |
|
| 358 |
phones = NULL; |
| 359 |
|
| 360 |
for (i=0; l->addenda[i]; i++) |
| 361 |
{ |
| 362 |
if (((wp[0] == '0') || (wp[0] == l->addenda[i][0][0])) && |
| 363 |
(cst_streq(wp+1,l->addenda[i][0]+1))) |
| 364 |
{ |
| 365 |
for (j=1; l->addenda[i][j]; j++) |
| 366 |
phones = cons_val(string_val(l->addenda[i][j]),phones); |
| 367 |
*found = TRUE; |
| 368 |
return val_reverse(phones); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
return NULL; |
| 373 |
} |
| 374 |
|
| 375 |
static int lex_uncompress_word(char *ucword,int max_size, |
| 376 |
int p,const cst_lexicon *l) |
| 377 |
{ |
| 378 |
int i,j=0,length; |
| 379 |
unsigned char *cword; |
| 380 |
|
| 381 |
if (l->entry_hufftable == 0) |
| 382 |
|
| 383 |
cst_sprintf(ucword,"%s",&l->data[p]); |
| 384 |
else |
| 385 |
{ |
| 386 |
cword = &l->data[p]; |
| 387 |
for (i=0,j=0; cword[i]; i++) |
| 388 |
{ |
| 389 |
length = cst_strlen(l->entry_hufftable[cword[i]]); |
| 390 |
if (j+length+1<max_size) |
| 391 |
{ |
| 392 |
memmove(ucword+j,l->entry_hufftable[cword[i]],length); |
| 393 |
j += length; |
| 394 |
} |
| 395 |
else |
| 396 |
break; |
| 397 |
} |
| 398 |
ucword[j] = '\0'; |
| 399 |
} |
| 400 |
|
| 401 |
return j; |
| 402 |
} |
| 403 |
static int lex_data_next_entry(const cst_lexicon *l,int p,int end) |
| 404 |
{ |
| 405 |
for (p++; p < end; p++) |
| 406 |
if (l->data[p-1] == 255) |
| 407 |
return p; |
| 408 |
return end; |
| 409 |
} |
| 410 |
static int lex_data_prev_entry(const cst_lexicon *l,int p,int start) |
| 411 |
{ |
| 412 |
for (p--; p > start; p--) |
| 413 |
if (l->data[p-1] == 255) |
| 414 |
return p; |
| 415 |
return start; |
| 416 |
} |
| 417 |
static int lex_data_closest_entry(const cst_lexicon *l,int p,int start,int end) |
| 418 |
{ |
| 419 |
int d; |
| 420 |
|
| 421 |
d=0; |
| 422 |
while ((p-d > start) && |
| 423 |
(p+d < end)) |
| 424 |
{ |
| 425 |
if (l->data[(p+d)-1] == 255) |
| 426 |
return p+d; |
| 427 |
else if (l->data[(p-d)-1] == 255) |
| 428 |
return p-d; |
| 429 |
d++; |
| 430 |
} |
| 431 |
return p-d; |
| 432 |
} |
| 433 |
|
| 434 |
static int lex_lookup_bsearch(const cst_lexicon *l, const char *word) |
| 435 |
{ |
| 436 |
int start,mid,end,c; |
| 437 |
|
| 438 |
char word_pos[WP_SIZE]; |
| 439 |
|
| 440 |
start = 0; |
| 441 |
end = l->num_bytes; |
| 442 |
while (start < end) { |
| 443 |
mid = (start + end)/2; |
| 444 |
|
| 445 |
|
| 446 |
mid = lex_data_closest_entry(l,mid,start,end); |
| 447 |
lex_uncompress_word(word_pos,WP_SIZE,mid,l); |
| 448 |
|
| 449 |
c = lex_match_entry(word_pos,word); |
| 450 |
|
| 451 |
if (c == 0) |
| 452 |
return find_full_match(l,mid,word); |
| 453 |
else if (c > 0) |
| 454 |
end = mid; |
| 455 |
else |
| 456 |
start = lex_data_next_entry(l,mid + 1,end); |
| 457 |
|
| 458 |
#if 0 |
| 459 |
if (l->data[start-1] == 255) |
| 460 |
{ |
| 461 |
lex_uncompress_word(word_pos,WP_SIZE,start,l); |
| 462 |
printf("start %s %d ",word_pos,start); |
| 463 |
} |
| 464 |
else |
| 465 |
printf("start NULL %d ",start); |
| 466 |
if (l->data[mid-1] == 255) |
| 467 |
{ |
| 468 |
lex_uncompress_word(word_pos,WP_SIZE,mid,l); |
| 469 |
printf("mid %s %d ",word_pos,mid); |
| 470 |
} |
| 471 |
else |
| 472 |
printf("mid NULL %d ",mid); |
| 473 |
if (l->data[end-1] == 255) |
| 474 |
{ |
| 475 |
lex_uncompress_word(word_pos,WP_SIZE,end,l); |
| 476 |
printf("end %s %d ",word_pos,end); |
| 477 |
} |
| 478 |
else |
| 479 |
printf("end NULL %d ",end); |
| 480 |
printf("\n"); |
| 481 |
#endif |
| 482 |
|
| 483 |
} |
| 484 |
return -1; |
| 485 |
} |
| 486 |
|
| 487 |
static int find_full_match(const cst_lexicon *l, |
| 488 |
int i,const char *word) |
| 489 |
|
| 490 |
{ |
| 491 |
|
| 492 |
int w, match=i; |
| 493 |
|
| 494 |
char word_pos[WP_SIZE]; |
| 495 |
|
| 496 |
for (w=i; w > 0; ) |
| 497 |
{ |
| 498 |
lex_uncompress_word(word_pos,WP_SIZE,w,l); |
| 499 |
if (!cst_streq(word+1,word_pos+1)) |
| 500 |
break; |
| 501 |
else if (cst_streq(word,word_pos)) |
| 502 |
return w; |
| 503 |
match = w; |
| 504 |
|
| 505 |
w = lex_data_prev_entry(l,w,0); |
| 506 |
} |
| 507 |
|
| 508 |
for (w=i; w < l->num_bytes;) |
| 509 |
{ |
| 510 |
lex_uncompress_word(word_pos,WP_SIZE,w,l); |
| 511 |
if (!cst_streq(word+1,word_pos+1)) |
| 512 |
break; |
| 513 |
else if (cst_streq(word,word_pos)) |
| 514 |
return w; |
| 515 |
|
| 516 |
w = lex_data_next_entry(l,w,l->num_bytes); |
| 517 |
} |
| 518 |
|
| 519 |
return match; |
| 520 |
} |
| 521 |
|
| 522 |
static int lex_match_entry(const char *a, const char *b) |
| 523 |
{ |
| 524 |
int c; |
| 525 |
|
| 526 |
c = strcmp(a+1,b+1); |
| 527 |
|
| 528 |
return c; |
| 529 |
} |