Debian Codegraph @

3rdparty/flite_hts_engine/flite/src/lexicon/cst_lexicon.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 /* Lexicon related functions */
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_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 { /* But I doubt if this will ever be called, lexicons are mapped */
112 /* This probably isn't complete */
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 { /* Load an addend from given file, check its phones wrt lex */
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; /* a comment */
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; /* a blank line */
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 { /* if replace then replace entry in addenda of lex with entry */
161 /* else append entry to addenda of lex */
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] == '"') /* it was a quoted entry */
176 { /* so reparse it */
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)) /* there is a real pos */
187 {
188 pos = cst_strdup(p);
189 p = ts_get(e);
190 if (!cst_streq(":",p)) /* there is a real pos */
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 /* Check its a legal phone */
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)) /* comment to end of line */
210 break;
211 else if (lex->phone_table[i])
212 /* Only add it if its a valid phone */
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 /* Add given lexicon to list of known lexicons */
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 /* This is a default function that will normally be replaced */
265 /* for each lexicon */
266 (void)i;
267 (void)p;
268 return FALSE;
269 }
270 #endif /* !FLITE_PLUS_HTS_ENGINE */
271
272 int in_lex(const cst_lexicon *l, const char *word, const char *pos)
273 {
274 /* return TRUE is its in the lexicon */
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 /* no compression -- should we still support this ? */
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 "", /* more features if we had them */
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 /* For those other words */
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 /* can have "compressed" lexicons without compression */
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 /* needs to be longer than longest word in lexicon */
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 /* find previous entry start */
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 /* found word, now look for actual match including pos */
492 int w, match=i;
493 /* needs to be longer than longest word in lexicon */
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; /* if we can't find an exact match we'll take this one */
504 /* go back to last entry */
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 /* go to next entry */
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 }