Debian Codegraph @

src/lexicon/cst_lexicon.c

raw
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 #include "cst_features.h"
42 #include "cst_lexicon.h"
43 #include "cst_tokenstream.h"
44
45 CST_VAL_REGISTER_TYPE_NODEL(lexicon,cst_lexicon)
46
47 #define WP_SIZE 64
48
49 static int no_syl_boundaries(const cst_item *i, const cst_val *p);
50 static cst_val *lex_lookup_addenda(const char *wp,const cst_lexicon *l,
51 int *found);
52
53 static int lex_match_entry(const char *a, const char *b);
54 static int lex_lookup_bsearch(const cst_lexicon *l,const char *word);
55 static int find_full_match(const cst_lexicon *l,
56 int i,const char *word);
57
58 cst_lexicon *new_lexicon()
59 {
60 cst_lexicon *l = cst_alloc(cst_lexicon,1);
61 l->syl_boundary = no_syl_boundaries;
62 return l;
63 }
64
65 void delete_lexicon(cst_lexicon *lex)
66 { /* But I doubt if this will ever be called, lexicons are mapped */
67 /* This probably isn't complete */
68 if (lex)
69 {
70 cst_free(lex->data);
71 cst_free(lex);
72 }
73 }
74
75 cst_val *cst_lex_load_addenda(const cst_lexicon *lex, const char *lexfile)
76 { /* Load an addend from given file, check its phones wrt lex */
77 cst_tokenstream *lf;
78 const cst_string *line;
79 cst_val *e = NULL;
80 cst_val *na = NULL;
81 int i;
82
83 lf = ts_open(lexfile,"\n","","","");
84 if (lf == NULL)
85 {
86 cst_errmsg("lex_add_addenda: cannot open lexicon file\n");
87 return NULL;;
88 }
89
90 while (!ts_eof(lf))
91 {
92 line = ts_get(lf);
93 if (line[0] == '#')
94 continue; /* a comment */
95 for (i=0; line[i]; i++)
96 {
97 if (line[i] != ' ')
98 break;
99 }
100 if (line[i])
101 {
102 e = cst_lex_make_entry(lex,line);
103 if (e)
104 na = cons_val(e,na);
105 }
106 else
107 continue; /* a blank line */
108 }
109
110 ts_close(lf);
111 return val_reverse(na);
112 }
113
114 cst_val *cst_lex_make_entry(const cst_lexicon *lex, const cst_string *entry)
115 { /* if replace then replace entry in addenda of lex with entry */
116 /* else append entry to addenda of lex */
117 cst_tokenstream *e;
118 cst_val *phones = NULL;
119 cst_val *ventry;
120 const cst_string *w, *p;
121 cst_string *word;
122 cst_string *pos;
123 int i;
124
125 e = ts_open_string(entry,
126 cst_ts_default_whitespacesymbols,
127 "","","");
128
129 w = ts_get(e);
130 if (w[0] == '"') /* it was a quoted entry */
131 { /* so reparse it */
132 ts_close(e);
133 e = ts_open_string(entry,
134 cst_ts_default_whitespacesymbols,
135 "","","");
136 w = ts_get_quoted_token(e,'"','\\');
137 }
138
139 word = cst_strdup(w);
140 p = ts_get(e);
141 if (!cst_streq(":",p)) /* there is a real pos */
142 {
143 pos = cst_strdup(p);
144 p = ts_get(e);
145 if (!cst_streq(":",p)) /* there is a real pos */
146 {
147 cst_fprintf(stdout,"add_addenda: lex %s: expected \":\" in %s\n",
148 lex->name,
149 word);
150 cst_free(word); cst_free(pos); ts_close(e);
151 return NULL;
152 }
153 }
154 else
155 pos = cst_strdup("nil");
156
157 while (!ts_eof(e))
158 {
159 p = ts_get(e);
160 /* Check its a legal phone */
161 for (i=0; lex->phone_table[i]; i++)
162 {
163 if (cst_streq(p,lex->phone_table[i]))
164 break;
165 }
166 if (cst_streq("#",p)) /* comment to end of line */
167 break;
168 else if (cst_streq("",p)) /* trailing ws at eoln causes this */
169 break;
170 else if (lex->phone_table[i])
171 /* Only add it if its a valid phone */
172 phones = cons_val(string_val(p),phones);
173 else
174 {
175 cst_fprintf(stdout,"add_addenda: lex: %s word %s phone %s not in lexicon phoneset\n",
176 lex->name,
177 word,
178 p);
179 }
180 }
181
182 ventry = cons_val(string_val(word),cons_val(string_val(pos),
183 val_reverse(phones)));
184 cst_free(word); cst_free(pos); ts_close(e);
185 #if 0
186 printf("entry: ");
187 val_print(stdout,ventry);
188 printf("\n");
189 #endif
190
191 return ventry;
192 }
193
194 #if 0
195 void lexicon_register(cst_lexicon *lex)
196 {
197 /* Add given lexicon to list of known lexicons */
198 cst_lexicon **old_lexs;
199 int i;
200
201 old_lexs = flite_lexicons;
202 flite_num_lexicons++;
203 flite_lexicons = cst_alloc(cst_lexicon *,flite_num_lexicons);
204 for (i=0; i<flite_num_lexicons-1; i++)
205 flite_lexicons[i] = old_lexs[i];
206 flite_lexicons[i] = lex;
207 cst_free(old_lexs);
208 }
209
210 cst_lexicon *lexicon_select(const char *name)
211 {
212 int i;
213
214 for (i=0; i < flite_num_lexicons; i++)
215 if (cst_streq(name,flite_lexicons[i]->name))
216 return flite_lexicons[i];
217 return NULL;
218 }
219 #endif
220
221 static int no_syl_boundaries(const cst_item *i, const cst_val *p)
222 {
223 /* This is a default function that will normally be replaced */
224 /* for each lexicon */
225 (void)i;
226 (void)p;
227 return FALSE;
228 }
229
230 int in_lex(const cst_lexicon *l, const char *word, const char *pos,
231 const cst_features *feats)
232 {
233 /* return TRUE is its in the lexicon */
234 int r = FALSE, i;
235 char *wp;
236
237 wp = cst_alloc(char,cst_strlen(word)+2);
238 cst_sprintf(wp,"%c%s",(pos ? pos[0] : '0'),word);
239
240 for (i=0; l->addenda && l->addenda[i]; i++)
241 {
242 if (((wp[0] == '0') || (wp[0] == l->addenda[i][0][0])) &&
243 (cst_streq(wp+1,l->addenda[i][0]+1)))
244 {
245 r = TRUE;
246 break;
247 }
248 }
249
250 if (!r && (lex_lookup_bsearch(l,wp) >= 0))
251 r = TRUE;
252
253 cst_free(wp);
254 return r;
255 }
256
257 cst_val *lex_lookup(const cst_lexicon *l, const char *word, const char *pos,
258 const cst_features *feats)
259 {
260 int index;
261 int p;
262 const char *q;
263 char *wp;
264 cst_val *phones = 0;
265 int found = FALSE;
266
267 wp = cst_alloc(char,cst_strlen(word)+2);
268 cst_sprintf(wp,"%c%s",(pos ? pos[0] : '0'),word);
269
270 if (l->addenda)
271 phones = lex_lookup_addenda(wp,l,&found);
272
273 if (!found)
274 {
275 index = lex_lookup_bsearch(l,wp);
276
277 if (index >= 0)
278 {
279 if (l->phone_hufftable)
280 {
281 for (p=index-2; l->data[p]; p--)
282 for (q=l->phone_hufftable[l->data[p]]; *q; q++)
283 phones = cons_val(string_val(l->phone_table[(unsigned char)*q]),
284 phones);
285 }
286 else /* no compression -- should we still support this ? */
287 {
288 for (p=index-2; l->data[p]; p--)
289 phones = cons_val(string_val(l->phone_table[l->data[p]]),
290 phones);
291 }
292 phones = val_reverse(phones);
293 }
294 else if (l->lts_function)
295 {
296 phones = (l->lts_function)(l,word,"",feats);
297 }
298 else if (l->lts_rule_set)
299 {
300 phones = lts_apply(word,
301 "", /* more features if we had them */
302 l->lts_rule_set);
303 }
304 }
305
306 cst_free(wp);
307
308 return phones;
309 }
310
311 static cst_val *lex_lookup_addenda(const char *wp,const cst_lexicon *l,
312 int *found)
313 {
314 /* For those other words */
315 int i,j;
316 cst_val *phones;
317
318 phones = NULL;
319
320 for (i=0; l->addenda[i]; i++)
321 {
322 if (((wp[0] == '0') ||
323 (wp[0] == l->addenda[i][0][0]) ||
324 (l->addenda[i][0][0] == '0')) &&
325 (cst_streq(wp+1,l->addenda[i][0]+1)))
326 {
327 for (j=1; l->addenda[i][j]; j++)
328 phones = cons_val(string_val(l->addenda[i][j]),phones);
329 *found = TRUE;
330 return val_reverse(phones);
331 }
332 }
333
334 return NULL;
335 }
336
337 static int lex_uncompress_word(char *ucword,int max_size,
338 int p,const cst_lexicon *l)
339 {
340 int i,j=0,length;
341 unsigned char *cword;
342
343 if (l->entry_hufftable == 0)
344 /* can have "compressed" lexicons without compression */
345 cst_sprintf(ucword,"%s",&l->data[p]);
346 else
347 {
348 cword = &l->data[p];
349 for (i=0,j=0; cword[i]; i++)
350 {
351 length = cst_strlen(l->entry_hufftable[cword[i]]);
352 if (j+length+1<max_size)
353 {
354 memmove(ucword+j,l->entry_hufftable[cword[i]],length);
355 j += length;
356 }
357 else
358 break;
359 }
360 ucword[j] = '\0';
361 }
362
363 return j;
364 }
365 static int lex_data_next_entry(const cst_lexicon *l,int p,int end)
366 {
367 for (p++; p < end; p++)
368 if (l->data[p-1] == 255)
369 return p;
370 return end;
371 }
372 static int lex_data_prev_entry(const cst_lexicon *l,int p,int start)
373 {
374 for (p--; p > start; p--)
375 if (l->data[p-1] == 255)
376 return p;
377 return start;
378 }
379 static int lex_data_closest_entry(const cst_lexicon *l,int p,int start,int end)
380 {
381 int d;
382
383 d=0;
384 while ((p-d > start) &&
385 (p+d < end))
386 {
387 if (l->data[(p+d)-1] == 255)
388 {
389 return p+d;
390 }
391 else if (l->data[(p-d)-1] == 255)
392 {
393 return p-d;
394 }
395 d++;
396 }
397 return p-d;
398 }
399
400 static int lex_lookup_bsearch(const cst_lexicon *l, const char *word)
401 {
402 int start,mid,end,c;
403 /* needs to be longer than longest word in lexicon */
404 char word_pos[WP_SIZE];
405
406 start = 0;
407 end = l->num_bytes;
408 while (start < end) {
409 mid = (start + end)/2;
410
411 /* find previous entry start */
412 mid = lex_data_closest_entry(l,mid,start,end);
413 lex_uncompress_word(word_pos,WP_SIZE,mid,l);
414
415 c = lex_match_entry(word_pos,word);
416
417 if (c == 0)
418 {
419 return find_full_match(l,mid,word);
420 }
421 else if (c > 0)
422 end = mid;
423 else
424 {
425 start = lex_data_next_entry(l,mid + 1,end);
426 }
427
428 #if 0
429 if (l->data[start-1] == 255)
430 {
431 lex_uncompress_word(word_pos,WP_SIZE,start,l);
432 printf("start %s %d ",word_pos,start);
433 }
434 else
435 printf("start NULL %d ",start);
436 if (l->data[mid-1] == 255)
437 {
438 lex_uncompress_word(word_pos,WP_SIZE,mid,l);
439 printf("mid %s %d ",word_pos,mid);
440 }
441 else
442 printf("mid NULL %d ",mid);
443 if (l->data[end-1] == 255)
444 {
445 lex_uncompress_word(word_pos,WP_SIZE,end,l);
446 printf("end %s %d ",word_pos,end);
447 }
448 else
449 printf("end NULL %d ",end);
450 printf("\n");
451 #endif
452
453 }
454 return -1;
455 }
456
457 static int find_full_match(const cst_lexicon *l,
458 int i,const char *word)
459
460 {
461 /* found word, now look for actual match including pos */
462 int w, match=i;
463 /* needs to be longer than longest word in lexicon */
464 char word_pos[WP_SIZE];
465
466 for (w=i; w > 0; )
467 {
468 lex_uncompress_word(word_pos,WP_SIZE,w,l);
469 if (!cst_streq(word+1,word_pos+1))
470 break;
471 else if (cst_streq(word,word_pos))
472 return w;
473 match = w; /* if we can't find an exact match we'll take this one */
474 /* go back to last entry */
475 w = lex_data_prev_entry(l,w,0);
476 }
477
478 for (w=i; w < l->num_bytes;)
479 {
480 lex_uncompress_word(word_pos,WP_SIZE,w,l);
481 if (!cst_streq(word+1,word_pos+1))
482 break;
483 else if (cst_streq(word,word_pos))
484 return w;
485 /* go to next entry */
486 w = lex_data_next_entry(l,w,l->num_bytes);
487 }
488
489 return match;
490 }
491
492 static int lex_match_entry(const char *a, const char *b)
493 {
494 int c;
495
496 c = strcmp(a+1,b+1);
497
498 return c;
499 }
500
501