Debian Codegraph @

lang/cmu_grapheme_lex/cmu_grapheme_lex.c

raw
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 /* grapheme 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_grapheme_lex.h"
43
44 static int cst_find_u2sampa(char *ord)
45 {
46 int i;
47
48 /* Now this is inefficient ... */
49 for (i=0; i<num_unicode_sampa_mapping; i++)
50 if (cst_streq(ord,unicode_sampa_mapping[i][0]))
51 return i;
52 return -1;
53 }
54
55 static int cst_utf8_as_hex(const char *in,char *out)
56 {
57 int o;
58
59 o = cst_utf8_ord_string(in);
60 if ((o > 96) && (o < 123))
61 {
62 cst_sprintf(out,"let_%c",(unsigned char)o);
63 }
64 else if ((o > 64) && (o < 91))
65 { /* Map uppercase to lowercase */
66 cst_sprintf(out,"let_%c",(unsigned char)o+32);
67 }
68 else
69 cst_sprintf(out,"u%04Xp",o);
70 return o;
71 }
72
73 cst_val *cmu_grapheme_lex_lts_function(const struct lexicon_struct *l,
74 const char *word, const char *pos,
75 const cst_features *feats)
76 {
77 cst_val *phones = 0;
78 cst_val *utflets = 0;
79 const cst_val *v;
80 char ord[10];
81 int i,phindex;
82
83 /* string to utf8 chars */
84 utflets = cst_utf8_explode(word);
85
86 for (v=utflets; v; v=val_cdr(v))
87 {
88 /* We will add the found phones in reverse order and reverse then */
89 /* afterwards */
90 cst_utf8_as_hex(val_string(val_car(v)),ord);
91 phindex = cst_find_u2sampa(ord);
92 printf("awb_debug lookup sampa %s %s\n",val_string(val_car(v)),ord);
93 #if 0
94 if (phindex < 0)
95 printf("awb_debug no sampa %s %s\n",val_string(val_car(v)),ord);
96 #endif
97 for (i=4; (phindex>=0) && (i>0); i--)
98 {
99 if (unicode_sampa_mapping[phindex][i])
100 phones = cons_val(string_val(unicode_sampa_mapping[phindex][i]),
101 phones);
102 }
103 }
104
105 phones = val_reverse(phones);
106 #if 0
107 printf("cmu_grapheme_lex.c: word \"%s\" ",word);
108 val_print(stdout,phones);
109 printf("\n");
110 #endif
111
112 delete_val(utflets);
113
114 return phones;
115 }
116
117 cst_utterance *cmu_grapheme_postlex(cst_utterance *u)
118 {
119 return u;
120 }
121
122 static int cmu_grapheme_is_vowel(const char *p)
123 {
124 /* This a place holder, we know its sampa, so I should look it up */
125 /* in the list, not guess from typographical conventions */
126 if (strchr("aeiouAEIOU",p[0]) == NULL)
127 return FALSE;
128 else
129 return TRUE;
130 }
131
132 static int cmu_grapheme_contains_vowel(const cst_val *r)
133 {
134 const cst_val *x;
135
136 for (x=r; x; x=val_cdr(x))
137 {
138 if (cmu_grapheme_is_vowel(val_string(val_car(x))))
139 return TRUE;
140 }
141
142 return FALSE;
143 }
144
145 static int cmu_grapheme_has_vowel_in_syl(const cst_item *i)
146 {
147 const cst_item *n;
148
149 for (n=i; n; n=item_prev(n))
150 if (cmu_grapheme_is_vowel(ffeature_string(n,"name")))
151 return TRUE;
152 return FALSE;
153 }
154
155 int cmu_grapheme_syl_boundary(const cst_item *i,const cst_val *rest)
156 {
157 if (!rest)
158 return TRUE;
159 else if (!cmu_grapheme_contains_vowel(rest))
160 return FALSE;
161 else if (!cmu_grapheme_has_vowel_in_syl(i))
162 return FALSE;
163 #if 0
164 else if (rest && val_cdr(rest) &&
165 cst_streq("n",val_string(val_car(rest))) &&
166 !cmu_grapheme_is_vowel(val_string(val_car(rest))))
167 return FALSE;
168 else if (rest && val_cdr(rest) &&
169 cmu_grapheme_is_vowel(ffeature_string(i,"name")) &&
170 !cmu_grapheme_is_vowel(val_string(val_car(rest))) &&
171 !cmu_grapheme_is_vowel(val_string(val_car(val_cdr(rest)))))
172 return FALSE;
173 else if (rest && val_cdr(rest) && val_cdr(val_cdr(rest)) &&
174 !cmu_grapheme_is_vowel(val_string(val_car(rest))) &&
175 !cmu_grapheme_is_vowel(val_string(val_car(val_cdr(rest)))) &&
176 !cmu_grapheme_is_vowel(val_string(val_car(val_cdr(val_cdr(rest))))))
177 return FALSE;
178 else if (rest && val_cdr(rest) &&
179 (cst_streq(val_string(val_car(rest)),
180 val_string(val_car(val_cdr(rest))))))
181 return FALSE;
182 #endif
183 else
184 return TRUE;
185 }
186
187 cst_lexicon cmu_grapheme_lex;
188
189 cst_lexicon *cmu_grapheme_lex_init(void)
190 {
191 /* Should it be global const or dynamic */
192 /* Can make lts_rules just a cart tree like others */
193 cst_lexicon *l;
194
195 if (cmu_grapheme_lex.lts_function)
196 return &cmu_grapheme_lex;
197
198 l = &cmu_grapheme_lex;
199 l->name = cst_strdup("cmu_grapheme_lex");
200
201 l->lts_function = cmu_grapheme_lex_lts_function;
202 l->syl_boundary = cmu_grapheme_syl_boundary;
203 l->postlex = cmu_grapheme_postlex;
204
205 return l;
206
207 }