Debian Codegraph @

3rdparty/flite_hts_engine/flite/lang/cmulex/cmu_lex.c

raw symbols SCIP
1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 2001 */
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: January 2001 */
35 /*************************************************************************/
36 /* */
37 /* CMU Lexicon definition */
38 /* */
39 /*************************************************************************/
40
41 #include "flite.h"
42
43 #include "cmu_lex.h"
44
45 extern const int cmu_lex_entry[];
46 extern const unsigned char cmu_lex_data[];
47 extern const int cmu_lex_num_entries;
48 extern const int cmu_lex_num_bytes;
49 extern const char * const cmu_lex_phone_table[54];
50 extern const char * const cmu_lex_phones_huff_table[];
51 extern const char * const cmu_lex_entries_huff_table[];
52
53 static int cmu_is_vowel(const char *p);
54 static int cmu_is_silence(const char *p);
55 static int cmu_has_vowel_in_list(const cst_val *v);
56 static int cmu_has_vowel_in_syl(const cst_item *i);
57 static int cmu_sonority(const char *p);
58
59 static const char * const addenda0[] = { "p,", NULL };
60 static const char * const addenda1[] = { "p.", NULL };
61 static const char * const addenda2[] = { "p(", NULL };
62 static const char * const addenda3[] = { "p)", NULL };
63 static const char * const addenda4[] = { "p[", NULL };
64 static const char * const addenda5[] = { "p]", NULL };
65 static const char * const addenda6[] = { "p{", NULL };
66 static const char * const addenda7[] = { "p}", NULL };
67 static const char * const addenda8[] = { "p:", NULL };
68 static const char * const addenda9[] = { "p;", NULL };
69 static const char * const addenda10[] = { "p?", NULL};
70 static const char * const addenda11[] = { "p!", NULL };
71 static const char * const addenda12[] = { "n@", "ae1", "t", NULL };
72 static const char * const addenda13[] = { "n#", "hh", "ae1","sh", NULL };
73 static const char * const addenda14[] = { "n$", "d", "aa1", "l", "er", NULL };
74 static const char * const addenda15[] = { "n%", "p", "er", "s", "eh1", "n", "t", NULL };
75 static const char * const addenda16[] = { "n^", "k", "eh1", "r", "eh1", "t", NULL };
76 static const char * const addenda17[] = { "n&","ae1","m","p","er","s","ae1","n","d", NULL };
77 static const char * const addenda18[] = { "n*","ae1","s","t","er","ih1","s","k",NULL };
78 static const char * const addenda19[] = { "n|","b","aa1","r",NULL };
79 static const char * const addenda20[] = { "n\\","b","ae1","k","s","l","ae1","sh",NULL };
80 static const char * const addenda21[] = { "n=","iy1","k","w","ax","l","z",NULL};
81 static const char * const addenda22[] = { "n+","p","l","ah1","s",NULL};
82 static const char * const addenda23[] = { "n~","t","ih1","l","d","ax",NULL};
83 static const char * const addenda24[] = { "p'",NULL};
84 static const char * const addenda25[] = { "p`",NULL};
85 static const char * const addenda26[] = { "p\"",NULL};
86 static const char * const addenda27[] = { "p-",NULL};
87 static const char * const addenda28[] = { "p<",NULL};
88 static const char * const addenda29[] = { "p>",NULL};
89 static const char * const addenda30[] = { "n_","ah1","n","d","er","s","k","ao1","r",NULL};
90 static const char * const addenda31[] = { "s's","z",NULL};
91 static const char * const addenda32[] = { "nim","ay1","m",NULL};
92 static const char * const addenda33[] = { "vdoesnt","d","ah1","z","n","t",NULL};
93 static const char * const addenda34[] = { "vyoull","y","uw1","l",NULL};
94 static const char * const addenda35[] = { "n/","s","l","ae1","sh",NULL};
95
96 static const char * const addenda36[] = { "nin","ih","n",NULL};
97 static const char * const addenda37[] = { "nto","t","ax",NULL};
98 static const char * const addenda38[] = { "n_a","ey",NULL};
99 static const char * const addenda39[] = { "vhavent","hh","ae1","v","ax","n","t",NULL};
100
101 static const char * const * const addenda[] = {
102 addenda0,
103 addenda1,
104 addenda2,
105 addenda3,
106 addenda4,
107 addenda5,
108 addenda6,
109 addenda7,
110 addenda8,
111 addenda9,
112 addenda10,
113 addenda11,
114 addenda12,
115 addenda13,
116 addenda14,
117 addenda15,
118 addenda16,
119 addenda17,
120 addenda18,
121 addenda19,
122 addenda20,
123 addenda21,
124 addenda22,
125 addenda23,
126 addenda24,
127 addenda25,
128 addenda26,
129 addenda27,
130 addenda28,
131 addenda29,
132 addenda30,
133 addenda31,
134 addenda32,
135 addenda33,
136 addenda34,
137 addenda35,
138
139 addenda36,
140 addenda37,
141 addenda38,
142 addenda39,
143 NULL };
144
145 static int cmu_is_silence(const char *p)
146 {
147 if (cst_streq(p,"pau"))
148 return TRUE;
149 else
150 return FALSE;
151 }
152
153 static int cmu_has_vowel_in_list(const cst_val *v)
154 {
155 const cst_val *t;
156
157 for (t=v; t; t=val_cdr(t))
158 if (cmu_is_vowel(val_string(val_car(t))))
159 return TRUE;
160 return FALSE;
161 }
162
163 static int cmu_has_vowel_in_syl(const cst_item *i)
164 {
165 const cst_item *n;
166
167 for (n=i; n; n=item_prev(n))
168 if (cmu_is_vowel(item_feat_string(n,"name")))
169 return TRUE;
170 return FALSE;
171 }
172
173 static int cmu_is_vowel(const char *p)
174 {
175 /* this happens to work for US English phoneset */
176 if (strchr("aeiou",p[0]) == NULL)
177 return FALSE;
178 else
179 return TRUE;
180 }
181
182 static int cmu_sonority(const char *p)
183 {
184 /* A bunch of hacks for US English phoneset */
185 if (cmu_is_vowel(p) || (cmu_is_silence(p)))
186 return 5;
187 else if (strchr("wylr",p[0]) != NULL)
188 return 4; /* glides/liquids */
189 else if (strchr("nm",p[0]) != NULL)
190 return 3; /* nasals */
191 else if (strchr("bdgjlmnnnrvwyz",p[0]) != NULL)
192 return 2; /* voiced obstruents */
193 else
194 return 1;
195 }
196
197 int cmu_syl_boundary(const cst_item *i,const cst_val *rest)
198 {
199 /* Returns TRUE if this should be a syllable boundary */
200 /* This is of course phone set dependent */
201 int p, n, nn;
202
203 if (rest == NULL)
204 return TRUE;
205 else if (cmu_is_silence(val_string(val_car(rest))))
206 return TRUE;
207 else if (!cmu_has_vowel_in_list(rest)) /* no more vowels so rest *all* coda */
208 return FALSE;
209 else if (!cmu_has_vowel_in_syl(i)) /* need a vowel */
210 return FALSE;
211 else if (cmu_is_vowel(val_string(val_car(rest))))
212 return TRUE;
213 else if (val_cdr(rest) == NULL)
214 return FALSE;
215 else
216 { /* so there is following vowel, and multiple phones left */
217 p = cmu_sonority(item_feat_string(i,"name"));
218 n = cmu_sonority(val_string(val_car(rest)));
219 nn = cmu_sonority(val_string(val_car(val_cdr(rest))));
220
221 if ((p <= n) && (n <= nn))
222 return TRUE;
223 else
224 return FALSE;
225 }
226 }
227
228 static int cmulex_dist_to_vowel(const cst_val *rest)
229 {
230 if (rest == 0)
231 return 0; /* shouldn't get here */
232 else if (cmu_is_vowel(val_string(val_car(rest))))
233 return 0;
234 else
235 return 1+cmulex_dist_to_vowel(val_cdr(rest));
236 }
237
238 static const char * const cmulex_onset_trigrams[] = {
239 "str", "spy", "spr", "spl", "sky", "skw", "skr", "skl", NULL
240 };
241 static const char * const cmulex_onset_bigrams[] = {
242 "zw", "zl",
243 "vy", "vr", "vl",
244 "thw", "thr",
245 "ty", "tw",
246 "tr", /* "ts", */
247 "shw", "shr", "shn", "shm", "shl",
248 "sw", "sv", "st", "sr", "sp", "sn", "sm", "sl", "sk", "sf",
249 "py", "pw", "pr", "pl",
250 "ny",
251 "my", "mr",
252 "ly",
253 "ky", "kw", "kr", "kl",
254 "hhy", "hhw", "hhr", "hhl",
255 "gy", "gw", "gr", "gl",
256 "fy", "fr", "fl",
257 "dy", "dw", "dr",
258 "by", "bw", "br", "bl",
259 NULL
260 };
261
262 static int cmulex_onset_bigram(const cst_val *rest)
263 {
264 char x[10];
265 int i;
266
267 cst_sprintf(x,"%s%s",val_string(val_car(rest)),
268 val_string(val_car(val_cdr(rest))));
269 for (i=0; cmulex_onset_bigrams[i]; i++)
270 if (cst_streq(x,cmulex_onset_bigrams[i]))
271 return TRUE;
272 return FALSE;
273 }
274
275 static int cmulex_onset_trigram(const cst_val *rest)
276 {
277 char x[15];
278 int i;
279
280 cst_sprintf(x,"%s%s%s",val_string(val_car(rest)),
281 val_string(val_car(val_cdr(rest))),
282 val_string(val_car(val_cdr(val_cdr(rest)))));
283 for (i=0; cmulex_onset_trigrams[i]; i++)
284 if (cst_streq(x,cmulex_onset_trigrams[i]))
285 return TRUE;
286 return FALSE;
287 }
288
289 int cmu_syl_boundary_mo(const cst_item *i,const cst_val *rest)
290 {
291 /* syl boundary maximal onset */
292 int d2v;
293
294 if (rest == NULL)
295 return TRUE;
296 else if (cmu_is_silence(val_string(val_car(rest))))
297 return TRUE;
298 else if (!cmu_has_vowel_in_list(rest))
299 /* no more vowels so rest *all* coda */
300 return FALSE;
301 else if (!cmu_has_vowel_in_syl(i)) /* need a vowel */
302 /* no vowel yet in syl so keep copying */
303 return FALSE;
304 else if (cmu_is_vowel(val_string(val_car(rest))))
305 /* next is a vowel, syl has vowel, so this is a break */
306 return TRUE;
307 else if (cst_streq("ng",val_string(val_car(rest))))
308 /* next is "ng" which can't start a word internal syl */
309 return FALSE;
310 else
311 {
312 /* want to know if from rest to the next vowel is a valid onset */
313 d2v = cmulex_dist_to_vowel(rest);
314 if (d2v < 2)
315 return TRUE;
316 else if (d2v > 3)
317 return FALSE;
318 else if (d2v == 2)
319 return cmulex_onset_bigram(rest);
320 else /* if (d2v == 3) */
321 return cmulex_onset_trigram(rest);
322 return TRUE;
323 }
324
325 }
326
327 cst_lexicon cmu_lex;
328 cst_lts_rules cmu_lts_rules;
329 extern const char * const cmu_lts_phone_table[];
330 extern const char * const cmu_lts_letter_table[];
331 extern const cst_lts_addr cmu_lts_letter_index[];
332 extern const cst_lts_model cmu_lts_model[];
333
334 cst_lexicon *cmu_lex_init()
335 {
336 /* I'd like to do this as a const but it needs everything in this */
337 /* file and already the bits are too big for some compilers */
338
339 if (cmu_lts_rules.name)
340 return &cmu_lex; /* Already initialized */
341
342 cmu_lts_rules.name = "cmu";
343 cmu_lts_rules.letter_index = cmu_lts_letter_index;
344 #ifdef CST_NO_STATIC_LTS_MODEL
345 /* cmu_lts_rules.models will be set elsewhere */
346 #else
347 cmu_lts_rules.models = cmu_lts_model;
348 #endif
349 cmu_lts_rules.phone_table = cmu_lts_phone_table;
350 cmu_lts_rules.context_window_size = 4;
351 cmu_lts_rules.context_extra_feats = 1;
352 cmu_lts_rules.letter_table = 0 /* cmu_lts_letter_table */;
353
354 cmu_lex.name = "cmu";
355 cmu_lex.num_entries = cmu_lex_num_entries;
356 #ifdef CST_NO_STATIC_LEX
357 /* cmu_lex.data will be set elsewhere */
358 #else
359 cmu_lex.data = cmu_lex_data;
360 #endif
361 cmu_lex.num_bytes = cmu_lex_num_bytes;
362 cmu_lex.phone_table = (char **) cmu_lex_phone_table;
363 cmu_lex.syl_boundary = cmu_syl_boundary_mo;
364 cmu_lex.addenda = (char ***) addenda;
365 cmu_lex.lts_rule_set = (cst_lts_rules *) &cmu_lts_rules;
366
367 cmu_lex.phone_hufftable = cmu_lex_phones_huff_table;
368 cmu_lex.entry_hufftable = cmu_lex_entries_huff_table;
369
370 cmu_lex.postlex = cmu_postlex;
371
372 return &cmu_lex;
373
374 }