Debian Codegraph @

lang/cmu_indic_lex/cmu_indic_lex.c

raw ◀ ▶
1 Debian patch touch this file
1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 2013 */
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 /* */
34 /* indic Lexical function */
35 /* */
36 /*************************************************************************/
37 #include "flite.h"
38 #include "cst_val.h"
39 #include "cst_voice.h"
40 #include "cst_lexicon.h"
41 #include "cst_ffeatures.h"
42 #include "cmu_indic_lex.h"
43 #include "cst_tokenstream.h"
44 #include "../cmu_indic_lang/cmu_indic_lang.h"
45
46 extern cst_lexicon cmu_lex;
47
48 int utf8_sequence_length(char c0);
49
50 static const struct cmu_indic_char cmu_indic_offset_char[128] = {
51 /*000*/ {IND_IGNORE, ""},
52 /*001*/ {IND_ANUSWAAR, "nX"},
53 /*002*/ {IND_ANUSWAAR, "nX"},
54 /*003*/ {IND_VISARGA, "h"},
55
56 /*004*/ {IND_INDEPENDENT_VOWEL, "A"},
57 /*005*/ {IND_INDEPENDENT_VOWEL, "A"},
58 /*006*/ {IND_INDEPENDENT_VOWEL, "A:"},
59 /*007*/ {IND_INDEPENDENT_VOWEL, "i"},
60 /*008*/ {IND_INDEPENDENT_VOWEL, "i:"},
61 /*009*/ {IND_INDEPENDENT_VOWEL, "u"},
62 /*00A*/ {IND_INDEPENDENT_VOWEL, "u:"},
63 /*00B*/ {IND_INDEPENDENT_VOWEL, "9r="},
64 /*00C*/ {IND_INDEPENDENT_VOWEL, "l="},
65 /*00D*/ {IND_INDEPENDENT_VOWEL, "ay"},
66 /*00E*/ {IND_INDEPENDENT_VOWEL, "e"},
67 /*00F*/ {IND_INDEPENDENT_VOWEL, "e"},
68 /*010*/ {IND_INDEPENDENT_VOWEL, "aI"},
69 /*011*/ {IND_INDEPENDENT_VOWEL, "ow"},
70 /*012*/ {IND_INDEPENDENT_VOWEL, "ow"},
71 /*013*/ {IND_INDEPENDENT_VOWEL, "o"},
72 /*014*/ {IND_INDEPENDENT_VOWEL, "aU"},
73
74 /*015*/ {IND_CONSONANT, "k"},
75 /*016*/ {IND_CONSONANT, "kh"},
76 /*017*/ {IND_CONSONANT, "g"},
77 /*018*/ {IND_CONSONANT, "gh"},
78 /*019*/ {IND_CONSONANT, "N"},
79
80 /*01A*/ {IND_CONSONANT, "c"},
81 /*01B*/ {IND_CONSONANT, "ch"},
82 /*01C*/ {IND_CONSONANT, "J"},
83 /*01D*/ {IND_CONSONANT, "Jh"},
84 /*01E*/ {IND_CONSONANT, "n~"},
85
86 /*01F*/ {IND_CONSONANT, "tr"},
87 /*020*/ {IND_CONSONANT, "tR"},
88 /*021*/ {IND_CONSONANT, "dr"},
89 /*022*/ {IND_CONSONANT, "dR"},
90 /*023*/ {IND_CONSONANT, "nr"},
91
92 /*024*/ {IND_CONSONANT, "tB"},
93 /*025*/ {IND_CONSONANT, "tBh"},
94 /*026*/ {IND_CONSONANT, "dB"},
95 /*027*/ {IND_CONSONANT, "dBh"},
96 /*028*/ {IND_CONSONANT, "nB"},
97 /*029*/ {IND_CONSONANT, "nB"},
98
99 /*02A*/ {IND_CONSONANT, "p"},
100 /*02B*/ {IND_CONSONANT, "ph"},
101 /*02C*/ {IND_CONSONANT, "b"},
102 /*02D*/ {IND_CONSONANT, "bh"},
103 /*02E*/ {IND_CONSONANT, "m"},
104
105 /*02F*/ {IND_CONSONANT, "j"},
106 /*030*/ {IND_CONSONANT, "9r"},
107 /*031*/ {IND_CONSONANT, "9r"},
108 /*032*/ {IND_CONSONANT, "l"},
109 /*033*/ {IND_CONSONANT, "lr"},
110 /*034*/ {IND_CONSONANT, "lr"},
111
112 /*035*/ {IND_CONSONANT, "v"},
113 /*036*/ {IND_CONSONANT, "c}"},
114 /*037*/ {IND_CONSONANT, "sr"},
115 /*038*/ {IND_CONSONANT, "s"},
116 /*039*/ {IND_CONSONANT, "hv"},
117
118 /*03A*/ {IND_VOWEL, "e"},
119 /*03B*/ {IND_VOWEL, "e"},
120
121 /*03C*/ {IND_NUKTA, ""},
122 /*03D*/ {IND_AVAGRAHA, ""},
123
124 /*03E*/ {IND_VOWEL, "A:"},
125 /*03F*/ {IND_VOWEL, "i"},
126 /*040*/ {IND_VOWEL, "i:"},
127 /*041*/ {IND_VOWEL, "u"},
128 /*042*/ {IND_VOWEL, "u:"},
129 /*043*/ {IND_VOWEL, "9r="},
130 /*044*/ {IND_VOWEL, "9r="},
131 /*045*/ {IND_VOWEL, "ay"},
132 /*046*/ {IND_VOWEL, "e"},
133 /*047*/ {IND_VOWEL, "e"},
134 /*048*/ {IND_VOWEL, "aI"},
135 /*049*/ {IND_VOWEL, "ow"},
136 /*04A*/ {IND_VOWEL, "o"},
137 /*04B*/ {IND_VOWEL, "o"},
138 /*04C*/ {IND_VOWEL, "aU"},
139
140 /*04D*/ {IND_HALANT, ""},
141
142 /*04E*/ {IND_IGNORE, ""},
143
144 /*04F*/ {IND_VOWEL, "ow"},
145
146 /*050*/ {IND_INDEPENDENT_VOWEL, "A u m"},
147
148 /*051*/ {IND_IGNORE, ""},
149 /*052*/ {IND_IGNORE, ""},
150 /*053*/ {IND_IGNORE, ""},
151 /*054*/ {IND_IGNORE, ""},
152
153 /*055*/ {IND_VOWEL, "e"},
154 /*056*/ {IND_VOWEL, "u e"},
155 /*057*/ {IND_VOWEL, "u: e"},
156
157 /*058*/ {IND_CONSONANT, "q"},
158 /*059*/ {IND_CONSONANT, "x"},
159 /*05A*/ {IND_CONSONANT, "G"},
160 /*05B*/ {IND_CONSONANT, "z"},
161 /*05C*/ {IND_CONSONANT, "rr"},
162 /*05D*/ {IND_CONSONANT, "rrh"},
163 /*05E*/ {IND_CONSONANT, "f"},
164 /*05F*/ {IND_CONSONANT, "j"},
165
166 /*060*/ {IND_INDEPENDENT_VOWEL, "9r="},
167 /*061*/ {IND_INDEPENDENT_VOWEL, "lr="},
168 /*062*/ {IND_VOWEL, "lr="},
169 /*063*/ {IND_VOWEL, "lr="},
170
171 /*064*/ {IND_PUNC, ""},
172 /*065*/ {IND_PUNC, ""},
173
174 /*066*/ {IND_DIGIT, ""},
175 /*067*/ {IND_DIGIT, ""},
176 /*068*/ {IND_DIGIT, ""},
177 /*069*/ {IND_DIGIT, ""},
178 /*06A*/ {IND_DIGIT, ""},
179 /*06B*/ {IND_DIGIT, ""},
180 /*06C*/ {IND_DIGIT, ""},
181 /*06D*/ {IND_DIGIT, ""},
182 /*06E*/ {IND_DIGIT, ""},
183 /*06F*/ {IND_DIGIT, ""},
184
185 /*070*/ {IND_ANUSWAAR, "nX"},
186 /*071*/ {IND_ADDAK, ""},
187
188 /*072*/ {IND_INDEPENDENT_VOWEL, "ay"},
189 /*073*/ {IND_INDEPENDENT_VOWEL, "u e"},
190 /*074*/ {IND_INDEPENDENT_VOWEL, "u: e"},
191 /*075*/ {IND_INDEPENDENT_VOWEL, "ow"},
192 /*076*/ {IND_INDEPENDENT_VOWEL, "u e"},
193 /*077*/ {IND_INDEPENDENT_VOWEL, "u: e"},
194
195 /*078*/ {IND_IGNORE, ""},
196
197 /*079*/ {IND_CONSONANT, "z"},
198 /*07A*/ {IND_CONSONANT, "j"},
199 /*07B*/ {IND_CONSONANT, "G"},
200 /*07C*/ {IND_CONSONANT, "z"},
201 /*07D*/ {IND_CONSONANT, ""},
202 /*07E*/ {IND_CONSONANT, "dr"},
203 /*07F*/ {IND_CONSONANT, "b"},
204
205 };
206
207 static void replace_car(const cst_val *it,const cst_val *newcar)
208 {
209 /* Destructively replace it's car with newcar, deleting old car */
210 /* This does this by casting out of consts -- something you shouldn't do */
211 cst_val *tbd;
212
213 tbd = (cst_val *)val_car(it);
214 set_car((cst_val *)it,newcar);
215 delete_val(tbd);
216 }
217
218 static void replace_cdr(const cst_val *it,const cst_val *newcdr)
219 {
220 /* Destructively replace it's car with newcar, deleting old cdr */
221 /* This does this by casting out of consts -- something you shouldn't do */
222 cst_val *tbd;
223
224 tbd = (cst_val *)val_cdr(it);
225 set_cdr((cst_val *)it,newcdr);
226 set_cdr((cst_val *)tbd,NULL);
227 delete_val(tbd);
228 }
229
230 static int cmu_indic_is_vowel(const char *p)
231 {
232 /* this happens to work for the vowels in the indic set */
233 if (strchr("aeiouAEIOU",p[0]) == NULL)
234 return FALSE;
235 else
236 return TRUE;
237 }
238
239 static int cmu_indic_lex_ord_to_offset(const int indic_ord) {
240 int output=0;
241 int i;
242 int offset;
243
244 i = indic_ord;
245 if ((i >= 0x0900) && (i <= 0x097F))
246 offset = 0x0900;
247 if ((i >= 0x0980) && (i <= 0x09FF))
248 offset = 0x0980;
249 if ((i >= 0x0A00) && (i <= 0x0A7F))
250 offset = 0x0A00;
251 if ((i >= 0x0A80) && (i <= 0x0AFF))
252 offset = 0x0A80;
253 if ((i >= 0x0B00) && (i <= 0x0B7F))
254 offset = 0x0B00;
255 if ((i >= 0x0B80) && (i <= 0x0BFF))
256 offset = 0x0B80;
257 if ((i >= 0x0C00) && (i <= 0x0C7F))
258 offset = 0x0C00;
259 if ((i >= 0x0C80) && (i <= 0x0CFF))
260 offset = 0x0C80;
261 if ((i >= 0x0D00) && (i <= 0x0D7F))
262 offset = 0x0D00;
263
264 if (!offset) {
265 cst_errmsg("Indic language can not process character 0x%x\n", i);
266 } else {
267 output = i - offset;
268 }
269
270 return output;
271 }
272
273 static int cmu_indic_get_char_type(const cst_val *indic_char)
274 {
275 int c;
276
277 if (!indic_char) return IND_IGNORE;
278
279 c = val_int(indic_char);
280 if ((c < 0x0900) || (c > 0x0D7F))
281 return IND_IGNORE;
282 c = cmu_indic_lex_ord_to_offset(c);
283 return cmu_indic_offset_char[c].type;
284 }
285
286 static const char *cmu_indic_get_char_phoneme(const cst_val *indic_char)
287 {
288 int c;
289
290 if (!indic_char) return "";
291
292 c = val_int(indic_char);
293
294 /* Language-specific exceptions to the g2p mapping, like the
295 length distinction b/w e and e: */
296 /* e/e: & o/o: distinction: */
297 /* Kannada */
298 if ((c == 0x0C8F)||(c == 0x0CC7)) return "e:";
299 if (c == 0x0C92) return "o";
300 if ((c == 0x0C93)||(c == 0x0CCB)) return "o:";
301 /* Malayalam */
302 if ((c == 0x0D0F)||(c == 0x0D47)) return "e:";
303 if (c == 0x0D12) return "o";
304 if ((c == 0x0D13)||(c == 0x0D4B)) return "o:";
305 if (c == 0x0D34) return "zr"; /* Retroflex approximant */
306 if (c == 0x0D31) return "rr"; /* Retroflex flap */
307 /* Punjabi */
308 if (c == 0x0A33) return "l";
309 /* Tamil */
310 if ((c == 0x0B8F)||(c == 0x0BC7)) return "e:";
311 if (c == 0x0B92) return "o";
312 if ((c == 0x0B93)||(c == 0x0BCB)) return "o:";
313 if (c == 0x0BA9) return "n"; /* Tamil alveolar nasal */
314 if (c == 0x0BB1) return "rr";
315 if (c == 0x0BB4) return "zr";
316
317 /* Telugu */
318 if ((c == 0x0C0F)||(c == 0x0C47)) return "e:";
319 if (c == 0x0C12) return "o";
320 if ((c == 0x0C13)||(c == 0x0C4B)) return "o:";
321
322 /* Not a special case */
323 c = cmu_indic_lex_ord_to_offset(c);
324
325 /* printf("awb_debug get_char_phone %x %s\n",c,cmu_indic_offset_char[c].phoneme);*/
326 return cmu_indic_offset_char[c].phoneme;
327 }
328
329 static cst_val *cmu_indic_lex_remove_ignore_chars(const cst_val *indic_ords) {
330 cst_val *output = 0;
331 const cst_val *v;
332
333 for(v=indic_ords; v; v=val_cdr(v)) {
334 if (cmu_indic_get_char_type(val_car(v)) == IND_IGNORE)
335 continue;
336 output = cons_val(val_car(v), output);
337 }
338 return val_reverse(output);
339 }
340
341 static cst_val *cmu_indic_lex_map_nukta_chars(const cst_val *indic_ords) {
342 cst_val *output = 0;
343 const cst_val *v;
344 int i;
345 int mapped_val;
346
347 for(v=indic_ords; v; v=val_cdr(v)) {
348 i = val_int(val_car(v));
349 if (val_cdr(v) && cmu_indic_get_char_type(val_car(val_cdr(v))) == IND_NUKTA) {
350 switch (i) {
351 /* Devanagari */
352 case 2325: mapped_val=2392; break; /* क़ */
353 case 2326: mapped_val=2393; break; /* ख़ */
354 case 2327: mapped_val=2394; break; /* ग़ */
355 case 2332: mapped_val=2395; break; /* ज़ */
356 case 2337: mapped_val=2396; break; /* ड़ */
357 case 2338: mapped_val=2397; break; /* ढ़ */
358 case 2347: mapped_val=2398; break; /* फ़ */
359 case 2351: mapped_val=2399; break; /* य़ */
360
361 /* Bengali */
362 case 2465: mapped_val=2524; break;
363 case 2566: mapped_val=2525; break;
364 case 2479: mapped_val=2527; break;
365
366 /* Tamil */
367 case 2962: mapped_val=2964; break;
368
369 /* Gurmukhi */
370 case 2582: mapped_val=2649; break; /* ਖ਼ */
371 case 2583: mapped_val=2650; break; /* ਗ਼ */
372 case 2588: mapped_val=2651; break; /* ਜ਼ */
373 case 2603: mapped_val=2654; break; /* ਫ਼ */
374 case 2610: mapped_val=2610; break; /* ਲ਼ */
375 case 2616: mapped_val=2614; break; /* ਸ਼ */
376
377 default:
378 mapped_val=i;
379 };
380 output = cons_val(int_val(mapped_val), output);
381 v = val_cdr(v);
382 } else {
383 /* Copy current item into output */
384 output = cons_val(val_car(v), output);
385 }
386 }
387
388 return val_reverse(output);
389 }
390
391 cst_val *cmu_indic_lex_ord_to_phones(const cst_val *ords,
392 int cmu_indic_variant_deletes_word_final_schwa,
393 const cst_features *feats)
394 {
395 cst_val *in_ords = 0;
396 cst_val *t_ords = 0;
397 cst_val *out_phone_strings = 0;
398 cst_val *out_phones = 0;
399
400 const cst_val *prev_char = 0;
401 const cst_val *cur_char = 0;
402 const cst_val *next_char = 0;
403 const cst_val *remainder = 0;
404 const char *indic_variant = 0;
405
406 int cur_char_type = 0;
407 int next_char_type = 0;
408 int prev_char_type = 0;
409
410 indic_variant = get_param_string(feats, "variant", "none");
411
412 /* Ignore chars (filter) */
413 in_ords = cmu_indic_lex_remove_ignore_chars(ords);
414
415 if (!in_ords) {
416 return out_phones;
417 }
418
419 /* Map Nukta Chars (filter) */
420 t_ords = cmu_indic_lex_map_nukta_chars(in_ords);
421 delete_val(in_ords);
422 in_ords = t_ords;
423
424 prev_char = 0;
425 cur_char = val_car(in_ords);
426 if (val_cdr(in_ords)) {
427 next_char = val_car(val_cdr(in_ords));
428 remainder = val_cdr(val_cdr(in_ords));
429 }
430 else {
431 next_char = 0;
432 remainder = 0;
433 }
434
435 while (cur_char) {
436 /* printf("awb_debug out_phone_strings "); val_print(stdout,out_phone_strings); printf("\n"); */
437 cur_char_type = cmu_indic_get_char_type(cur_char);
438 if (prev_char)
439 prev_char_type = cmu_indic_get_char_type(prev_char);
440 if (next_char)
441 next_char_type = cmu_indic_get_char_type(next_char);
442 else
443 next_char_type = IND_IGNORE;
444
445 if (cur_char_type == IND_CONSONANT) {
446 /* Add consonant to the output list */
447
448 /* This part needs to be incorporated into Festvox code! */
449 /* In Tamil, if visarga comes right before p or J, it is not */
450 /* realized as "h" but instead maps p to f, or J to z. This */
451 /* takes care of that. */
452 if ((prev_char) &&
453 ((prev_char_type == IND_VISARGA) &&
454 (((cst_streq("J", cmu_indic_get_char_phoneme(cur_char))) ||
455 (cst_streq("p", cmu_indic_get_char_phoneme(cur_char)))) &&
456 (cst_streq(indic_variant,"tam"))))) {
457 /* Don't add current character to out_phones; */
458 /* the correct mapped character is already added when */
459 /* cur_char is visarga */
460 } else {
461 out_phone_strings =
462 cons_val(string_val(cmu_indic_get_char_phoneme(cur_char)),
463 out_phone_strings);
464 /* If a consonant is followed by a combination vowel, a */
465 /* halant, a punctuation then don't add a schwa after */
466 /* it. Otherwise, insert a schwa. For end-of-word, check */
467 /* whether we should insert schwa in this language. */
468 if (!next_char) { /* We are in last char. Add schwa? */
469 if ((!prev_char) || /* Always add schwa for one-char words */
470 (!cmu_indic_variant_deletes_word_final_schwa)) {
471 out_phone_strings = cons_val(string_val("A"), out_phone_strings);
472 } else {
473 /* Schwa deletion should probably happen depending */
474 /* on whether there is a consonant cluster or not, */
475 /* at the end. But Adding that rule here seems to */
476 /* not have worked properly. Hence, we always */
477 /* delete the final schwa. */
478 }
479 } else { /* Not a final char */
480 if ( (next_char_type != IND_VOWEL) &&
481 (next_char_type != IND_PUNC) &&
482 (next_char_type != IND_HALANT) &&
483 (next_char_type != IND_IGNORE)) {
484 out_phone_strings = cons_val(string_val("A"), out_phone_strings);
485 }
486 }
487 }
488 } else if ((cur_char_type == IND_VOWEL) ||
489 (cur_char_type == IND_INDEPENDENT_VOWEL) ||
490 (cur_char_type == IND_DIGIT)) {
491 /* Add whatever the pronunciation is */
492 out_phone_strings =
493 cons_val(string_val(cmu_indic_get_char_phoneme(cur_char)),
494 out_phone_strings);
495 } else if (cur_char_type == IND_HALANT) {
496 /* Ignore */
497 } else if (cur_char_type == IND_AVAGRAHA) {
498 /* Lengthen previous vowel */
499 if (cmu_indic_get_char_type(prev_char) == IND_VOWEL) {
500 out_phone_strings =
501 cons_val(string_val(cmu_indic_get_char_phoneme(prev_char)),
502 out_phone_strings);
503 }
504 } else if (cur_char_type == IND_ANUSWAAR) {
505 /* The realization of anuswaar is context dependent: We */
506 /* only generate a placeholder symbol and let postlexical */
507 /* rules take care of this. */
508 out_phone_strings = cons_val(string_val("nX"),out_phone_strings);
509
510 } else if ((cur_char_type == IND_VISARGA) &&
511 (!cst_streq(indic_variant,"tam"))) {
512
513 /* If we are not in Tamil, add the glottal fricative ("h") */
514 out_phone_strings = cons_val(string_val("h"),out_phone_strings);
515 }
516 else if ((cur_char_type == IND_VISARGA) &&
517 (cst_streq(indic_variant,"tam"))) {
518 /* If there's a next character */
519 if (next_char) {
520 /* If next character is p or J, do mapping and add to output */
521 if (cst_streq("p", cmu_indic_get_char_phoneme(next_char))) {
522 out_phone_strings = cons_val(string_val("f"),
523 out_phone_strings);
524
525 } else if (cst_streq("J", cmu_indic_get_char_phoneme(next_char))) {
526 out_phone_strings = cons_val(string_val("z"),
527 out_phone_strings);
528 /* Otherwise it's "h" */
529 } else {
530 out_phone_strings = cons_val(string_val("h"),
531 out_phone_strings);
532 }
533 /* If this is the last character, it's "h" */
534 } else {
535 out_phone_strings = cons_val(string_val("h"),
536 out_phone_strings);
537 }
538
539 } else if (cur_char_type == IND_ADDAK) {
540 /* In Gurmukhi, this diacritic geminates the following consonant. */
541 out_phone_strings =
542 cons_val(string_val(cmu_indic_get_char_phoneme(next_char)),
543 out_phone_strings);
544 } else {
545 /* IDEALLY we should warn for unhandled characters! */
546 /* TODO? */
547 }
548
549 prev_char = cur_char;
550 cur_char = next_char;
551 if (remainder) {
552 next_char = val_car(remainder);
553 remainder = val_cdr(remainder);
554 } else {
555 next_char = 0;
556 }
557 }
558
559 /* There may be multiple phones in the expansion, but they are just */
560 /* space separated tokens in the val, so we need to make them into */
561 /* an actual val list of phones */
562 const cst_val *v, *vx;
563 cst_val *ph;
564 for(v=out_phone_strings; v; v=val_cdr(v)) {
565 ph = val_readlist_string(val_string(val_car(v)));
566 for(vx=ph; vx; vx=val_cdr(vx)) {
567 out_phones = cons_val(val_car(vx), out_phones);
568 }
569 delete_val(ph);
570 }
571 delete_val(in_ords);
572 delete_val(out_phone_strings);
573 return val_reverse(out_phones);
574
575 }
576
577 static cst_val *cmu_indic_lex_kannada_spelling_postfixes(cst_val *in_phones)
578 {
579 const cst_val *p;
580
581 p=in_phones;
582
583 for ( ; p && val_cdr(p) ; p=val_cdr(p))
584 {
585 /* Correct spell-errors ( e u:/aI ) => ( o/aI ) */
586 if ((cst_streq(val_string(val_car(p)),"e")) &&
587 ((cst_streq(val_string(val_car(val_cdr(p))),"u:")) ||
588 (cst_streq(val_string(val_car(val_cdr(p))),"aI"))))
589 {
590 if (cst_streq(val_string(val_car(val_cdr(p))),"u:"))
591 replace_car(p,string_val("o"));
592 else
593 replace_car(p,string_val("aI"));
594 set_cdr((cst_val *)p, val_cdr(val_cdr(p)));
595 }
596 }
597 return in_phones;
598 }
599
600 cst_val *cmu_indic_lex_nasal_postfixes(cst_val *in_phones,
601 const cst_features *feats)
602 {
603 /* Given a phone sequence containing a special character nX */
604 /* (contextual nasal), replace it with the appropriate nasal phone */
605 /* based on its context */
606 char *tmpstr;
607 const cst_val *p;
608
609 const char *indic_variant = 0;
610
611 indic_variant = get_param_string(feats, "variant", "none");
612
613 /* printf("awb_debug: pre "); val_print(stdout,in_phones); printf("\n"); */
614 for( p=in_phones; p && val_cdr(p); p=val_cdr(p))
615 {
616 /* Nazalise vowels at ends of words */
617 if ((cmu_indic_is_vowel(val_string(val_car(p)))) &&
618 (cst_streq("nX", val_string(val_car(val_cdr(p))))) &&
619 ((!val_cdr(val_cdr(p))) ||
620 (!val_car(val_cdr(val_cdr(p))))))
621 {
622 if (cst_streq(indic_variant,"kan") ||
623 cst_streq(indic_variant,"tel") || /* Dravidian languages don't nasalize */
624 cst_streq("A", val_string(val_car(p))))
625 { /* If it's a schwa, it's not nasalized. nX becomes m */
626 replace_car(val_cdr(p),string_val("m"));
627 } else {
628 tmpstr = cst_strcat(val_string(val_car(p)),"nas");
629 replace_car(p,string_val(tmpstr));
630 cst_free(tmpstr);
631 replace_cdr(p,val_cdr(val_cdr(p)));
632 }
633 } else if (cst_streq("nX", val_string(val_car(p))))
634 { /* Choose nasal variant based on next consonant */
635 const char *next_c =
636 val_string(phone_feature(&cmu_indic_phoneset,
637 val_string(val_car(val_cdr(p))),
638 "cplace"));
639 const char *repl_ph;
640 if (next_c) {
641 switch (next_c[0]) {
642 case 'v': repl_ph = "N"; break;
643 case 'p': repl_ph = "n~"; break;
644 case 'a': repl_ph = "nr"; break;
645 case 'd': repl_ph = "nB"; break;
646 case 'l': repl_ph = "m"; break;
647 default: repl_ph = "nB";
648 };
649 replace_car(p,string_val(repl_ph));
650 }
651 }
652 }
653
654 /* printf("awb_debug: post "); val_print(stdout,in_phones); printf("\n");*/
655 return in_phones;
656 }
657
658 static cst_val *cmu_indic_lex_jnyan_replacement(cst_val *in_phones,
659 const cst_features *feats)
660 {
661 /* Changes instances of ( J n~ ) to ( g n~ ) or ( g j ) depending on the language*/
662 const cst_val *p;
663
664 const char *indic_variant = 0;
665
666 indic_variant = get_param_string(feats, "variant", "none");
667
668 for (p=in_phones; p && val_cdr(p); p=val_cdr(p))
669 {
670 if ((cst_streq(val_string(val_car(p)),"J")) &&
671 (cst_streq(val_string(val_car(val_cdr(p))),"n~")))
672 { /* Change "J" to "g" */
673 replace_car(p,string_val("g"));
674 if (cst_streq(indic_variant,"hin"))
675 /*Only Hindi pronounces this digraph as ( g j ), AFAIK -shyam*/
676 replace_car(val_cdr(p),string_val("j"));
677 p = val_cdr(p); /* Skip over them */
678 }
679 }
680 return in_phones;
681 }
682
683 static cst_val *cmu_indic_lex_punjabi_vowel_postfixes(cst_val *in_phones)
684 {
685 const cst_val *p;
686
687 p=in_phones;
688
689 /* Provide better approximates for 3rd person singular pronouns */
690 /* Check for orthographic variant of ihn/uhn, written inh/unh */
691 /* Equivalent to the punjabi_pronoun_postfixes function */
692 if (p && val_cdr(p) && val_cdr(val_cdr(p)) &&
693 (cst_streq(val_string(val_car(val_cdr(p))),"nB")) &&
694 (cst_streq(val_string(val_car(val_cdr(val_cdr(p)))),"hv")) &&
695 ((cst_streq(val_string(val_car(p)),"i")) ||
696 (cst_streq(val_string(val_car(p)),"u"))))
697 {
698 if (cst_streq(val_string(val_car(p)),"i"))
699 replace_car(p,string_val("e"));
700 else if (cst_streq(val_string(val_car(p)),"u"))
701 replace_car(p,string_val("o"));
702 p = val_cdr(p);
703 replace_car(p,string_val("hv"));
704 set_cdr((cst_val *)p,cons_val(string_val("nB"),val_cdr(val_cdr(p))));
705 return in_phones;
706 }
707
708 for ( ; p && val_cdr(p); p=val_cdr(p))
709 {
710 /* Change sequences ( A hv i/u ) => ( aI/aU hv ) */
711 if ((cst_streq(val_string(val_car(p)),"A")) &&
712 (cst_streq(val_string(val_car(val_cdr(p))),"hv")) &&
713 val_cdr(val_cdr(p)) && (val_cdr(val_cdr(val_cdr(p)))) &&
714 ((cst_streq(val_string(val_car(val_cdr(val_cdr(p)))),"i")) ||
715 (cst_streq(val_string(val_car(val_cdr(val_cdr(p)))),"u"))))
716 {
717 if (cst_streq(val_string(val_car(val_cdr(val_cdr(p)))),"i"))
718 replace_car(p,string_val("aI"));
719 else if (cst_streq(val_string(val_car(val_cdr(val_cdr(p)))),"u"))
720 replace_car(p,string_val("aU"));
721 set_cdr((cst_val *)p,cons_val(string_val("hv"),val_cdr(val_cdr(val_cdr(p)))));
722 p = val_cdr(val_cdr(p)); /* Skip over them */
723 }
724
725 /* Change sequences ( i/u hv ) => ( e/o hv ) */
726 else if (((cst_streq(val_string(val_car(p)),"i")) ||
727 (cst_streq(val_string(val_car(p)),"u"))) &&
728 val_cdr(val_cdr(p)) &&
729 (cst_streq(val_string(val_car(val_cdr(p))),"hv")))
730 {
731 if (cst_streq(val_string(val_car(p)),"i"))
732 replace_car(p,string_val("e"));
733 else if (cst_streq(val_string(val_car(p)),"u"))
734 replace_car(p,string_val("o"));
735 p = val_cdr(p); /* Skip over them */
736 }
737
738 /* Change sequences ( A: u/A ) => ( aU/A: ) */
739 else if ((cst_streq(val_string(val_car(p)),"A:")) &&
740 val_cdr(val_cdr(p)) &&
741 ((cst_streq(val_string(val_car(val_cdr(p))),"u")) ||
742 (cst_streq(val_string(val_car(val_cdr(p))),"A")))
743 )
744 {
745 if (cst_streq(val_string(val_car(val_cdr(p))),"u"))
746 replace_car(p,string_val("aU"));
747 set_cdr((cst_val *)p,val_cdr(val_cdr(p)));
748 p = val_cdr(p); /* Skip over them */
749 }
750 }
751 return in_phones;
752 }
753
754
755 static cst_val *cmu_indic_lex_punjabi_glide_postfixes(cst_val *in_phones)
756 {
757 const cst_val *p;
758
759 /* Inserts glides/semivowels corresponding to i/u */
760 for (p=in_phones; p && val_cdr(p); p=val_cdr(p))
761 {
762 if (cmu_indic_is_vowel(val_string(val_car(val_cdr(p)))) &&
763 ((cst_streq(val_string(val_car(p)),"i")) ||
764 (cst_streq(val_string(val_car(p)),"u")) ||
765 (cst_streq(val_string(val_car(p)),"i:")) ||
766 (cst_streq(val_string(val_car(p)),"u:"))))
767 {
768 /* Change sequences ( i/u V ) => ( j/v V ) */
769 if (cst_streq(val_string(val_car(p)),"i"))
770 replace_car(p,string_val("j"));
771 else if (cst_streq(val_string(val_car(p)),"u"))
772 replace_car(p,string_val("v"));
773
774 /* Change sequences ( i:/u: V ) => ( i/u j/v V ) */
775 else if (cst_streq(val_string(val_car(p)),"i:"))
776 {
777 replace_car(p,string_val("i"));
778 set_cdr((cst_val *)p,cons_val(string_val("j"),val_cdr(p)));
779 }
780 else if (cst_streq(val_string(val_car(p)),"u:"))
781 {
782 replace_car(p,string_val("u"));
783 set_cdr((cst_val *)p,cons_val(string_val("v"),val_cdr(p)));
784 }
785 p = val_cdr(p); /* Skip over them */
786 }
787
788 /* Change sequences ( V i ) => ( V j ) */
789 else if (cst_streq(val_string(val_car(val_cdr(p))),"i") &&
790 cmu_indic_is_vowel(val_string(val_car(p))))
791 {
792 p = val_cdr(p);
793 replace_car(p,string_val("j"));
794 }
795
796 /* Change sequence ( V i: V ) => ( V j j V ) */
797 else if (cst_streq(val_string(val_car(val_cdr(p))),"i:") &&
798 cmu_indic_is_vowel(val_string(val_car(p))) &&
799 (val_cdr(val_cdr(p))) &&
800 cmu_indic_is_vowel(val_string(val_car(val_cdr(val_cdr(p))))))
801 {
802 p = val_cdr(p);
803 replace_car(p,string_val("j"));
804 set_cdr((cst_val *)p,cons_val(string_val("j"),val_cdr(p)));
805 p = val_cdr(p); /* Skip over them */
806 }
807 }
808 return in_phones;
809 }
810
811 static cst_val *cmu_indic_lex_tamil_tr_replacement(cst_val *in_phones)
812 {
813 /* Changes instances of ( rr rr ) to ( tr tr rr ) */
814 const cst_val *p;
815
816 for (p=in_phones; p && val_cdr(p); p=val_cdr(p))
817 {
818 if ((cst_streq(val_string(val_car(p)),"rr")) &&
819 (cst_streq(val_string(val_car(val_cdr(p))),"rr")))
820 { /* Change the first one to tr tr */
821 replace_car(p,string_val("tr"));
822 set_cdr((cst_val *)p,cons_val(string_val("tr"),val_cdr(p)));
823 p = val_cdr(val_cdr(p)); /* Skip over them */
824 }
825 }
826 return in_phones;
827 }
828
829 static cst_val *cmu_indic_lex_tamil_nr_replacement(cst_val *in_phones)
830 {
831 /* Changes instances of ( n rr ) to ( nr dr rr) */
832 const cst_val *p;
833
834 for (p=in_phones; p && val_cdr(p); p=val_cdr(p))
835 {
836 if ((cst_streq(val_string(val_car(p)),"n")) &&
837 (cst_streq(val_string(val_car(val_cdr(p))),"rr")))
838 { /* Insert dB between them */
839 replace_car(p,string_val("nr"));
840 set_cdr((cst_val *)p,cons_val(string_val("dr"),val_cdr(p)));
841 p = val_cdr(val_cdr(p)); /* Skip over them */
842 }
843 }
844 return in_phones;
845 }
846
847 static cst_val *cmu_indic_lex_tamil_final_u(cst_val *in_phones)
848 {
849 /* Changes instances of final u to uy */
850 const cst_val *p;
851
852 for (p=in_phones; p; p=val_cdr(p))
853 {
854 if ((val_cdr(p) == NULL) &&
855 (cst_streq(val_string(val_car(p)),"u")))
856 { /* change u to uy */
857 replace_car(p,string_val("uy"));
858 }
859 }
860 return in_phones;
861 }
862
863
864 cst_val *cmu_indic_lex_tamil_voicing_postfixes(cst_val *phones)
865 {
866 /* Destructively modify voicing in list of phones */
867 const cst_val *p;
868 const char *next_phone, *this_phone;
869 const char *voice_ph, *len_ph;
870
871 p = phones;
872 /* word-initial c becomes s */
873 if (p && (cst_streq(val_string(val_car(p)),"c")) &&
874 val_cdr(p) && !cst_streq(val_string(val_car(val_cdr(p))),"c"))
875 {
876 replace_car(p,string_val("s"));
877 p = val_cdr(p);
878 }
879
880 for( ; p && val_cdr(p); p=val_cdr(p))
881 {
882 this_phone = val_string(val_car(p));
883 next_phone = val_string(val_car(val_cdr(p)));
884 /* Next phone is a stop that could be mapped. */
885 if ((cst_streq(next_phone,"k")) ||
886 (cst_streq(next_phone,"c")) ||
887 (cst_streq(next_phone,"tr")) ||
888 (cst_streq(next_phone,"tB")) ||
889 (cst_streq(next_phone,"p")))
890 {
891 if (cst_streq(next_phone,"k")) { voice_ph = "g"; len_ph = "G";
892 }
893 else if (cst_streq(next_phone,"c")) { voice_ph = "J"; len_ph =
894 "s"; }
895 else if (cst_streq(next_phone,"tr")) { voice_ph = "dr"; len_ph
896 = "rrh"; }
897 else if (cst_streq(next_phone,"tB")) { voice_ph = "dB"; len_ph
898 = "dh"; }
899 else if (cst_streq(next_phone,"p")) { voice_ph = "b"; len_ph =
900 "B"; }
901 else { voice_ph = next_phone; len_ph = next_phone; }
902
903 /* If current phone is a nasal/voiced stop, add voicing. */
904 if ((!cmu_indic_is_vowel(this_phone)) &&
905
906 (cst_streq(val_string(phone_feature(&cmu_indic_phoneset,
907
908 this_phone,"ctype")),"n")))
909 {
910 replace_car(val_cdr(p),string_val(voice_ph));
911 p=val_cdr(p); /* skip */
912 }
913 /* If current phone is a vowel/approximant and next.next is
914 also a vowel
915 then stop undergoes lenition */
916 else if ((cmu_indic_is_vowel(this_phone)) ||
917
918 (cst_streq(val_string(phone_feature(&cmu_indic_phoneset,
919
920 this_phone,"ctype")),"r")))
921 {
922 if ((val_cdr(val_cdr(p))) &&
923
924 (cmu_indic_is_vowel(val_string(val_car(val_cdr(val_cdr(p)))))))
925 {
926 replace_car(val_cdr(p),string_val(len_ph));
927 p=val_cdr(p); /* skip */
928 }
929 }
930 /* If current is vowel, but this is last syllable,
931 then leave voicing as it is. */
932 else if ((cmu_indic_is_vowel(this_phone)) &&
933 (!val_cdr(val_cdr(p))))
934 {
935 continue;
936 }
937 }
938 }
939
940 return phones;
941 }
942
943 static const char * const eng_to_indic[99][3] =
944 {
945 {"aa", "A:", NULL },
946 {"ae", "aI", NULL },
947 {"ah", "A", NULL },
948 {"ao", "aU", NULL },
949 {"aw", "A:", "u" },
950 {"ax", "A", NULL },
951 {"axr", "A", "9r" },
952 {"ay", "A:", "i" },
953 {"b", "b", NULL },
954 {"ch", "c", NULL },
955 {"d", "dr", NULL },
956 {"dh", "dB", NULL },
957 {"eh", "E", NULL },
958 {"er", "A", "9r" },
959 {"ey", "e", NULL },
960 {"f", "ph", NULL },
961 {"g", "g", NULL },
962 {"hh", "hv", NULL },
963 {"ih", "i", NULL },
964 {"iy", "i:", NULL },
965 {"jh", "J", NULL },
966 {"k", "k", NULL },
967 {"l", "l", NULL },
968 {"m", "m", NULL },
969 {"n", "nB", NULL },
970 {"nx", "nB", NULL },
971 {"ng", "N", NULL },
972 {"ow", "o", NULL },
973 {"oy", "aU", "i" },
974 {"p", "p", NULL },
975 {"r", "9r", NULL },
976 {"s", "s", NULL },
977 {"sh", "c}", NULL },
978 {"t", "tr", NULL },
979 {"th", "tBh", NULL },
980 {"uh", "u", NULL },
981 {"uw", "u:", NULL },
982 {"v", "v", NULL },
983 {"w", "v", NULL },
984 {"y", "j", NULL },
985 {"z", "z", NULL },
986 {"zh", "c}", NULL },
987 {NULL, NULL, NULL }
988 };
989
990 /* Mapping for Tamil taking stress into consideration */
991 /* Shyam Krishna, 2018/03/06 */
992 static const char * const eng_to_tam_stress[99][3] =
993 {
994 {"aa0", "A", NULL },
995 {"aa1", "A:", NULL },
996 {"ae0", "A", NULL },
997 {"ae1", "e", NULL },
998 {"ah1", "A", NULL },
999 {"ao0", "A", NULL },
1000 {"ao1", "o:", NULL }, /*TODO: resolve horse-hoarse merger */
1001 {"aw0", "aU", NULL },
1002 {"aw1", "aU", NULL },
1003 {"ax", "A", NULL },
1004 {"ax0", "A", NULL },
1005 {"ay0", "aI", NULL },
1006 {"ay1", "aI", NULL },
1007 {"b", "b", NULL },
1008 {"ch", "c", NULL },
1009 {"d", "dr", NULL },
1010 {"dh", "dB", NULL },
1011 {"eh0", "e", NULL },
1012 {"eh1", "e", NULL },
1013 {"er", "A", "9r" },
1014 {"er0", "A", "9r" },
1015 {"er1", "A", "9r" },
1016 {"ey0", "e", NULL },
1017 {"ey1", "e:", NULL },
1018 {"f", "p", NULL },
1019 {"g", "g", NULL },
1020 {"hh", "hv", NULL },
1021 {"ih", "i", NULL },
1022 {"ih0", "i", NULL },
1023 {"ih1", "i", NULL },
1024 {"iy0", "i", NULL },
1025 {"iy1", "i:", NULL },
1026 {"jh", "J", NULL },
1027 {"k", "k", NULL },
1028 {"l", "l", NULL },
1029 {"m", "m", NULL },
1030 {"n", "nB", NULL },
1031 {"nx", "nB", NULL },
1032 {"ng", "N", NULL },
1033 {"ow0", "o", NULL },
1034 {"ow1", "o:", NULL },
1035 {"oy0", "o", "j" },
1036 {"oy1", "o:", "j" },
1037 {"p", "p", NULL },
1038 {"r", "9r", NULL },
1039 {"s", "s", NULL },
1040 {"sh", "sr", NULL },
1041 {"t", "tr", NULL },
1042 {"th", "tB", NULL },
1043 {"uh0", "u", NULL },
1044 {"uh1", "u", NULL },
1045 {"uw0", "u", NULL },
1046 {"uw1", "u:", NULL },
1047 {"v", "v", NULL },
1048 {"w", "v", NULL },
1049 {"y", "j", NULL },
1050 {"z", "s", NULL },
1051 {"zh", "sr", NULL },
1052 {NULL, NULL, NULL }
1053 };
1054
1055 /* Mapping for Kannada taking stress into consideration */
1056 /* Shyam Krishna, 2018/04/06 */
1057 static const char * const eng_to_kan_stress[99][3] =
1058 {
1059 {"aa0", "A", NULL },
1060 {"aa1", "A:", NULL },
1061 {"ae0", "A", NULL },
1062 {"ae1", "e", NULL },
1063 {"ah1", "A", NULL },
1064 {"ao0", "A", NULL },
1065 {"ao1", "o:", NULL }, /*TODO: resolve horse-hoarse merger */
1066 {"aw0", "aU", NULL },
1067 {"aw1", "aU", NULL },
1068 {"ax", "A", NULL },
1069 {"ax0", "A", NULL },
1070 {"ay0", "aI", NULL },
1071 {"ay1", "aI", NULL },
1072 {"b", "b", NULL },
1073 {"ch", "c", NULL },
1074 {"d", "dr", NULL },
1075 {"dh", "dB", NULL },
1076 {"eh0", "e", NULL },
1077 {"eh1", "e", NULL },
1078 {"er", "A", "9r" },
1079 {"er0", "A", "9r" },
1080 {"er1", "A", "9r" },
1081 {"ey0", "e", NULL },
1082 {"ey1", "e:", NULL },
1083 {"f", "ph", NULL },
1084 {"g", "g", NULL },
1085 {"hh", "hv", NULL },
1086 {"ih", "i", NULL },
1087 {"ih0", "i", NULL },
1088 {"ih1", "i", NULL },
1089 {"iy0", "i", NULL },
1090 {"iy1", "i:", NULL },
1091 {"jh", "J", NULL },
1092 {"k", "k", NULL },
1093 {"l", "l", NULL },
1094 {"m", "m", NULL },
1095 {"n", "nB", NULL },
1096 {"nx", "nB", NULL },
1097 {"ng", "N", NULL },
1098 {"ow0", "o", NULL },
1099 {"ow1", "o:", NULL },
1100 {"oy0", "o", "j" },
1101 {"oy1", "o:", "j" },
1102 {"p", "p", NULL },
1103 {"r", "9r", NULL },
1104 {"s", "s", NULL },
1105 {"sh", "c}", NULL },
1106 {"t", "tr", NULL },
1107 {"th", "tB", NULL },
1108 {"uh0", "u", NULL },
1109 {"uh1", "u", NULL },
1110 {"uw0", "u", NULL },
1111 {"uw1", "u:", NULL },
1112 {"v", "v", NULL },
1113 {"w", "v", NULL },
1114 {"y", "j", NULL },
1115 {"z", "s", NULL },
1116 {"zh", "c}", NULL },
1117 {NULL, NULL, NULL }
1118 };
1119
1120
1121 cst_val *map_english_to_indic_phones(const char *indic_variant,
1122 const cst_val *english_phones)
1123 {
1124 /* Map English (radio) phones to their Indic equivalent */
1125 cst_val *ip = NULL;
1126 const cst_val *v;
1127 char *english_phone;
1128 int i;
1129
1130 for (v=english_phones; v; v=val_cdr(v))
1131 {
1132 english_phone = cst_strdup(val_string(val_car(v)));
1133 /* *** mapping table should be indic variant specific */
1134 if(cst_streq(indic_variant, "tam"))
1135 {
1136 for (i=0; eng_to_tam_stress[i][0]; i++)
1137 {
1138 if (cst_streq(english_phone,eng_to_tam_stress[i][0]))
1139 {
1140 ip = cons_val(string_val(eng_to_tam_stress[i][1]),ip);
1141 if (eng_to_tam_stress[i][2])
1142 ip = cons_val(string_val(eng_to_tam_stress[i][2]),ip);
1143 }
1144 /* if there is no mapping, we drop the phone */
1145 }
1146 }
1147 else if(cst_streq(indic_variant, "kan"))
1148 {
1149 for (i=0; eng_to_kan_stress[i][0]; i++)
1150 {
1151 if (cst_streq(english_phone,eng_to_kan_stress[i][0]))
1152 {
1153 ip = cons_val(string_val(eng_to_kan_stress[i][1]),ip);
1154 if (eng_to_kan_stress[i][2])
1155 ip = cons_val(string_val(eng_to_kan_stress[i][2]),ip);
1156 }
1157 /* if there is no mapping, we drop the phone */
1158 }
1159 }
1160 else
1161 {
1162 if ((english_phone[cst_strlen(english_phone)-1] == '0') ||
1163 (english_phone[cst_strlen(english_phone)-1] == '1'))
1164 /* It has a stress value on it */
1165 english_phone[cst_strlen(english_phone)-1] = '\0';
1166 for (i=0; eng_to_indic[i][0]; i++)
1167 {
1168 if (cst_streq(english_phone,eng_to_indic[i][0]))
1169 {
1170 ip = cons_val(string_val(eng_to_indic[i][1]),ip);
1171 if (eng_to_indic[i][2])
1172 ip = cons_val(string_val(eng_to_indic[i][2]),ip);
1173 }
1174 /* if there is no mapping, we drop the phone */
1175 }
1176 }
1177 cst_free(english_phone);
1178 }
1179 ip = val_reverse(ip);
1180
1181 return ip;
1182 }
1183
1184 static cst_val *delete_medial_schwa(cst_val *rphones)
1185 {
1186 /* This schwa deletion follows the technique by Narsimhan et al (2001). */
1187 /* 1. Process input from right to left */
1188 /* 2. If a schwa is found in a VC_CV context, then delete it. */
1189
1190 /* There are exceptions to this: (i) Phonotactic constraints of */
1191 /* Hindi not violated, and no (ii) morpheme boundary present on the */
1192 /* left. But I don't know how to handle these yet. So this will be */
1193 /* wrong more often than the 11% reported in that paper. -- AUP */
1194 const cst_val *p;
1195 cst_val *tbd;
1196
1197 for (p=rphones; p && val_cdr(p); p=val_cdr(p))
1198 {
1199 if ((val_length(p) > 4) &&
1200 (cst_streq(val_string(val_car(val_cdr(val_cdr(p)))),"A")))
1201 { /* we have a schwa coming up */
1202 if ((cmu_indic_is_vowel(val_string(val_car(p)))) &&
1203 (!cmu_indic_is_vowel(val_string(val_car(val_cdr(p))))) &&
1204 (!cmu_indic_is_vowel(val_string(val_car(val_cdr(val_cdr(val_cdr(p))))))) &&
1205 (cmu_indic_is_vowel(val_string(val_car(val_cdr(val_cdr(val_cdr(val_cdr(p)))))))))
1206 {
1207 /* delete the schwa */
1208 tbd = (cst_val *)val_cdr(val_cdr(p));
1209 set_cdr((cst_val *)val_cdr(p),val_cdr(val_cdr(val_cdr(p))));
1210 set_cdr(tbd,NULL);
1211 delete_val(tbd);
1212 }
1213 }
1214 }
1215
1216 return rphones;
1217 }
1218
1219
1220 /* TODO */
1221 static cst_val *cmu_indic_hindi_schwa_fixes(cst_val *phones)
1222 {
1223 cst_val *dd;
1224
1225 if ((val_length(phones) > 3) &&
1226 (cst_streq("A",val_string(val_car(val_cdr(phones))))) &&
1227 (cst_streq("hv",val_string(val_car(val_cdr(val_cdr(phones)))))) &&
1228 ((cst_streq("A",val_string(val_car(val_cdr(val_cdr(val_cdr(phones))))))) ||
1229 (!cmu_indic_is_vowel(val_string(val_car(val_cdr(val_cdr(val_cdr(phones)))))))))
1230 {
1231 dd = (cst_val *)(void *)val_cdr(phones);
1232 set_cdr(phones,val_cdr(val_cdr(phones)));
1233 set_cdr(dd,NULL);
1234 delete_val(dd);
1235 return phones;
1236 }
1237 else
1238 return phones;
1239 }
1240
1241 cst_val *cmu_indic_lex_lts_function(const struct lexicon_struct *l,
1242 const char *word, const char *pos,
1243 const cst_features *feats)
1244 {
1245 cst_val *utflets = 0;
1246 cst_val *ords = 0;
1247 cst_val *english_phones;
1248 cst_val *base_phones = NULL;
1249 const char *indic_variant = 0;
1250 const char *eng_bilingual_flag = 0;
1251 int cmu_indic_variant_deletes_word_final_schwa=0;
1252 const cst_val *v;
1253 cst_val *tmpv;
1254
1255 indic_variant = get_param_string(feats, "variant", "none");
1256
1257 if (cst_streq(indic_variant, "hin")) {
1258 cmu_indic_variant_deletes_word_final_schwa = 1;
1259 } else if (cst_streq(indic_variant, "mar")) {
1260 cmu_indic_variant_deletes_word_final_schwa = 1;
1261 } else if (cst_streq(indic_variant, "ben")) {
1262 cmu_indic_variant_deletes_word_final_schwa = 1;
1263 } else if (cst_streq(indic_variant, "raj")) {
1264 cmu_indic_variant_deletes_word_final_schwa = 1;
1265 } else if (cst_streq(indic_variant, "guj")) {
1266 cmu_indic_variant_deletes_word_final_schwa = 1;
1267 } else if (cst_streq(indic_variant, "asm")) {
1268 cmu_indic_variant_deletes_word_final_schwa = 1;
1269 } else if (cst_streq(indic_variant, "pan")) {
1270 cmu_indic_variant_deletes_word_final_schwa = 1;
1271 } else if (cst_streq(indic_variant, "tel")) {
1272 cmu_indic_variant_deletes_word_final_schwa = 0;
1273 } else if (cst_streq(indic_variant, "tam")) {
1274 cmu_indic_variant_deletes_word_final_schwa = 0;
1275 } else if (cst_streq(indic_variant, "kan")) {
1276 cmu_indic_variant_deletes_word_final_schwa = 0;
1277 } else {
1278 cmu_indic_variant_deletes_word_final_schwa = 0;
1279 printf("Unknown indic variant: %s!\n", indic_variant);
1280 }
1281
1282 if (cst_regex_match(cst_rx_not_indic,word))
1283 { /* It's English like so us English Lexicon to find Phones and map
1284 to indic phones */
1285 /* printf("awb_debug cmu_indic_lex: English >%s<\n",word); */
1286 english_phones = lex_lookup(&cmu_lex,word,pos,feats);
1287
1288 eng_bilingual_flag = get_param_string(feats, "eng_shared", "none");
1289
1290 if (cst_streq(eng_bilingual_flag, "1")) { base_phones = english_phones; }
1291 else base_phones = map_english_to_indic_phones(indic_variant,english_phones);
1292 // base_phones = english_phones;
1293 // delete_val(english_phones);
1294 return base_phones;
1295 }
1296 else
1297 {
1298 /* string to utf8 chars */
1299 utflets = cst_utf8_explode(word);
1300 /* chars to ord */
1301 for (v=utflets; v; v=val_cdr(v)) {
1302 tmpv = cst_utf8_ord(val_car(v));
1303 ords = cons_val(tmpv, ords);
1304 }
1305 ords = val_reverse(ords);
1306
1307 /* Ords to Phone List (Map) (with final schwa deletion) */
1308 base_phones =
1309 val_reverse(cmu_indic_lex_ord_to_phones(ords,
1310 cmu_indic_variant_deletes_word_final_schwa,
1311 feats));
1312
1313 delete_val(utflets);
1314 delete_val(ords);
1315 }
1316
1317 if (cst_streq(indic_variant,"tam")){
•1318 printf("Tamil doesn't have anuswara");
1319 }
1320 else
1321 cmu_indic_lex_nasal_postfixes(base_phones, feats);
1322 base_phones = cmu_indic_lex_jnyan_replacement(base_phones,feats);
1323
1324 /* Postfix Indic Nasals, Voicing, Medial Schwa deletion */
1325 if (cst_streq(indic_variant,"tam"))
1326 {
1327 /* Tamil voicing rules */
1328 base_phones = cmu_indic_lex_tamil_voicing_postfixes(base_phones);
1329 /* Tamil tr replacement */
1330 base_phones = cmu_indic_lex_tamil_tr_replacement(base_phones);
1331 /* Tamil nr replacement */
1332 base_phones = cmu_indic_lex_tamil_nr_replacement(base_phones);
1333 /* final u -> uy */
1334 base_phones = cmu_indic_lex_tamil_final_u(base_phones);
1335 }
1336
1337 if (cst_streq(indic_variant,"pan"))
1338 {
1339 /* Punjabi vowel and pronoun rules */
1340 base_phones = cmu_indic_lex_punjabi_vowel_postfixes(base_phones);
1341 /* Punjabi glide rules */
1342 base_phones = cmu_indic_lex_punjabi_glide_postfixes(base_phones);
1343 }
1344
1345 if (cst_streq(indic_variant,"kan"))
1346 cmu_indic_lex_kannada_spelling_postfixes(base_phones);
1347
1348 //if (cst_streq(indic_variant,"san"))
1349 // base_phones=val_reverse(delete_medial_schwa(val_reverse(base_phones)));
1350
1351
1352 if ((cst_streq(indic_variant,"hin")) || (cst_streq(indic_variant,"mar")) ||
1353 (cst_streq(indic_variant,"guj")) || (cst_streq(indic_variant,"raj")) ||
1354 (cst_streq(indic_variant,"pan")))
1355 { /* Do medial schwa deletion */
1356 base_phones=val_reverse(delete_medial_schwa(val_reverse(base_phones)));
1357 base_phones = cmu_indic_hindi_schwa_fixes(base_phones);
1358 }
1359 /* phone list to syls (done as post lexical rules) */
1360
1361 #if 0
1362 printf("cmu_indic_lex.c: indic word \"%s\" \n",word);
1363 val_print(stdout, base_phones);
1364 printf("\n");
1365 #endif
1366
1367 return base_phones;
1368 }
1369
1370 static int cmu_indic_contains_vowel(const cst_val *r)
1371 {
1372 const cst_val *x;
1373
1374 for (x=r; x; x=val_cdr(x))
1375 {
1376 if (cmu_indic_is_vowel(val_string(val_car(x))))
1377 return TRUE;
1378 }
1379
1380 return FALSE;
1381 }
1382
1383 static int cmu_indic_has_vowel_in_syl(const cst_item *i)
1384 {
1385 const cst_item *n;
1386
1387 for (n=i; n; n=item_prev(n))
1388 if (cmu_indic_is_vowel(ffeature_string(n,"name")))
1389 return TRUE;
1390 return FALSE;
1391 }
1392
1393 int cmu_indic_syl_boundary(const cst_item *i,const cst_val *rest)
1394 {
1395 if (!rest)
1396 return TRUE;
1397 else if (!cmu_indic_contains_vowel(rest))
1398 return FALSE;
1399 else if (!cmu_indic_has_vowel_in_syl(i))
1400 return FALSE;
1401 else if (rest && val_cdr(rest) &&
1402 cst_streq("n",val_string(val_car(rest))) &&
1403 !cmu_indic_is_vowel(val_string(val_car(rest))))
1404 return FALSE;
1405 else if (rest && val_cdr(rest) &&
1406 cmu_indic_is_vowel(ffeature_string(i,"name")) &&
1407 !cmu_indic_is_vowel(val_string(val_car(rest))) &&
1408 !cmu_indic_is_vowel(val_string(val_car(val_cdr(rest)))))
1409 return FALSE;
1410 else if (rest && val_cdr(rest) && val_cdr(val_cdr(rest)) &&
1411 !cmu_indic_is_vowel(val_string(val_car(rest))) &&
1412 !cmu_indic_is_vowel(val_string(val_car(val_cdr(rest)))) &&
1413 !cmu_indic_is_vowel(val_string(val_car(val_cdr(val_cdr(rest))))))
1414 return FALSE;
1415 else if (rest && val_cdr(rest) &&
1416 (cst_streq(val_string(val_car(rest)),
1417 val_string(val_car(val_cdr(rest))))))
1418 return FALSE;
1419 else
1420 return TRUE;
1421 }
1422
1423 #if 0
1424 int cmu_indic_syl_boundary(const cst_item *i,const cst_val *rest)
1425 {
1426 /* For debugging the syl boundary code */
1427 int x;
1428
1429 printf("syl boundary %s | ",ffeature_string(i,"name"));
1430 if (rest)
1431 printf("%s ",val_string(val_car(rest)));
1432 if (rest && val_cdr(rest))
1433 printf("%s ",val_string(val_car(val_cdr(rest))));
1434 if (rest && val_cdr(rest) && val_cdr(val_cdr(rest)))
1435 printf("%s ",val_string(val_car(val_cdr(val_cdr(rest)))));
1436
1437 x = cmu_indic_syl_boundary_x(i,rest);
1438 printf("is %d",x);
1439
1440 printf("\n");
1441
1442 return x;
1443 }
1444 #endif
1445
1446 cst_utterance *cmu_indic_assign_lex_stress(cst_utterance *u)
1447 {
1448 /* Assign stress to a lexical entry: from indic_lexicon.scm (aup) */
1449 /* The stress is placed on the syllable with the highest weight. */
1450 /* If there is a tie, the last-most syllable with highest weight */
1451 /* is chosen. However, the last syllable of the word does not */
1452 /* participate in tie-breaking. That is, it is stressed only when */
1453 /* there are no ties. (Hussein 1997) */
1454 /* not sure this code actually follows the above rules, but its */
1455 /* the same as the festival code -- awb 20140606 */
1456 const cst_item *syl, *w;
1457 const char *x1, *x2, *x3;
1458 int syl_weight, best_weight, sw;
1459 const cst_item *stress_position;
1460
1461 for (w=relation_head(utt_relation(u,"SylStructure")); w; w=item_next(w))
1462 {
1463 for (syl=item_daughter(w); syl; syl=item_next(syl))
1464 { /* Assign weight to each syllable */
1465 syl_weight = 0;
1466 x1 = ffeature_string(syl,"R:SylStructure.daughtern.name");
1467 x2 = ffeature_string(syl,"R:SylStructure.daughtern.p.name");
1468 x3 = ffeature_string(syl,"R:SylStructure.daughtern.p.p.name");
1469 if (cmu_indic_is_vowel(x1))
1470 { /* If syllable is open vowel */
1471 if (cst_streq(x1,"A") || cst_streq(x1,"i") ||
1472 cst_streq(x1,"u"))
1473 syl_weight = 1;
1474 else
1475 syl_weight = 2;
1476 }
1477 else
1478 {
1479 if (cmu_indic_is_vowel(x2))
1480 {
1481 if (cst_streq(x2,"A") || cst_streq(x2,"i") ||
1482 cst_streq(x2,"u"))
1483 syl_weight = 2;
1484 else
1485 syl_weight = 3;
1486 }
1487 else if (cmu_indic_is_vowel(x3))
1488 syl_weight = 3;
1489 }
1490 item_set_int(syl,"syl_weight",syl_weight);
1491 }
1492 }
1493
1494 for (w=relation_head(utt_relation(u,"SylStructure")); w; w=item_next(w))
1495 {
1496 best_weight = 0;
1497 stress_position = NULL;
1498 for (syl=item_daughter(w); syl; syl=item_next(syl))
1499 {
1500 sw = ffeature_int(syl,"syl_weight");
1501 if (sw > best_weight)
1502 {
1503 best_weight = sw;
1504 stress_position = syl;
1505 }
1506 else if ((sw == best_weight) && item_next(syl))
1507 stress_position = syl;
1508 }
1509 if (stress_position)
1510 item_set_string(stress_position,"stress","1");
1511 }
1512
1513 return u;
1514 }
1515
1516 cst_utterance *cmu_indic_postlex(cst_utterance *u)
1517 {
1518 /* Post lexical rules */
1519 const char *indic_variant;
1520
1521 indic_variant = get_param_string(u->features, "variant", "none");
1522
1523 if ((cst_streq(indic_variant,"hin")) ||
1524 (cst_streq(indic_variant,"mar")) ||
1525 (cst_streq(indic_variant,"pan")) ||
1526 (cst_streq(indic_variant,"raj")) ||
1527 (cst_streq(indic_variant,"asm")) ||
1528 (cst_streq(indic_variant,"ben")))
1529 cmu_indic_assign_lex_stress(u);
1530
1531 #if 0
1532 /* Print out words as 'festival' lexical entries */
1533 const cst_item *word, *syl, *seg;
1534
1535 for (word=relation_head(utt_relation(u,"Word"));
1536 word; word=item_next(word))
1537 {
1538 printf("( \"%s\" nil (",ffeature_string(word,"name"));
1539 for (syl=item_daughter(item_as(word,"SylStructure"));
1540 syl;syl=item_next(syl))
1541 {
1542 printf("(( ");
1543 for (seg=item_daughter(item_as(syl,"SylStructure")); seg;
1544 seg=item_next(seg))
1545 {
1546 printf("%s ",ffeature_string(seg,"name"));
1547 }
1548 printf(") %s %d) ",
1549 ffeature_string(syl,"stress"),
1550 ffeature_int(syl,"syl_weight"));
1551
1552 }
1553 printf("))\n");
1554 }
1555 #endif
1556
1557 return u;
1558 }
1559
1560 cst_lexicon cmu_indic_lex;
1561
1562 cst_lexicon *cmu_indic_lex_init(void)
1563 {
1564 /* Should it be global const or dynamic */
1565 /* Can make lts_rules just a cart tree like others */
1566 cst_lexicon *l;
1567
1568 if (cmu_indic_lex.lts_function)
1569 return &cmu_indic_lex;
1570 l = &cmu_indic_lex;
1571 l->name = "cmu_indic_lex";
1572
1573 l->lts_function = cmu_indic_lex_lts_function;
1574 l->syl_boundary = cmu_indic_syl_boundary;
1575 l->postlex = cmu_indic_postlex;
1576 return l;
1577
1578 }