Debian Codegraph @

3rdparty/flite_hts_engine/flite/lang/usenglish/us_expand.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 /* English text expanders */
38 /* */
39 /* numbers, digits, ids (years), money */
40 /* */
41 /*************************************************************************/
42
43 #include <ctype.h>
44 #include "us_text.h"
45
46 static const char * const digit2num[] = {
47 "zero",
48 "one",
49 "two",
50 "three",
51 "four",
52 "five",
53 "six",
54 "seven",
55 "eight",
56 "nine" };
57
58 static const char * const digit2teen[] = {
59 "ten", /* shouldn't get called */
60 "eleven",
61 "twelve",
62 "thirteen",
63 "fourteen",
64 "fifteen",
65 "sixteen",
66 "seventeen",
67 "eighteen",
68 "nineteen" };
69
70 static const char * const digit2enty[] = {
71 "zero", /* shouldn't get called */
72 "ten",
73 "twenty",
74 "thirty",
75 "forty",
76 "fifty",
77 "sixty",
78 "seventy",
79 "eighty",
80 "ninety" };
81
82 static const char * const ord2num[] = {
83 "zeroth",
84 "first",
85 "second",
86 "third",
87 "fourth",
88 "fifth",
89 "sixth",
90 "seventh",
91 "eighth",
92 "ninth" };
93
94 static const char * const ord2teen[] = {
95 "tenth", /* shouldn't get called */
96 "eleventh",
97 "twelfth",
98 "thirteenth",
99 "fourteenth",
100 "fifteenth",
101 "sixteenth",
102 "seventeenth",
103 "eighteenth",
104 "nineteenth" };
105
106 static const char * const ord2enty[] = {
107 "zeroth", /* shouldn't get called */
108 "tenth",
109 "twentieth",
110 "thirtieth",
111 "fortieth",
112 "fiftieth",
113 "sixtieth",
114 "seventieth",
115 "eightieth",
116 "ninetieth" };
117
118 cst_val *en_exp_number(const char *numstring)
119 {
120 /* Expand given token to list of words pronouncing it as a number */
121 int num_digits = cst_strlen(numstring);
122 char part[4];
123 cst_val *p;
124 int i;
125
126 if (num_digits == 0)
127 return NULL;
128 else if (num_digits == 1)
129 return en_exp_digits(numstring);
130 else if (num_digits == 2)
131 {
132 if (numstring[0] == '0')
133 {
134 if (numstring[1] == '0')
135 return 0;
136 else
137 return cons_val(string_val(digit2num[numstring[1]-'0']),0);
138 }
139 else if (numstring[1] == '0')
140 return cons_val(string_val(digit2enty[numstring[0]-'0']),0);
141 else if (numstring[0] == '1')
142 return cons_val(string_val(digit2teen[numstring[1]-'0']),0);
143 else
144 return cons_val(string_val(digit2enty[numstring[0]-'0']),
145 en_exp_digits(numstring+1));
146 }
147 else if (num_digits == 3)
148 {
149 if (numstring[0] == '0')
150 return en_exp_number(numstring+1);
151 else
152 return cons_val(string_val(digit2num[numstring[0]-'0']),
153 cons_val(string_val("hundred"),
154 en_exp_number(numstring+1)));
155 }
156 else if (num_digits < 7)
157 {
158 for (i=0; i < num_digits-3; i++)
159 part[i] = numstring[i];
160 part[i]='\0';
161 p = en_exp_number(part);
162 if (p == 0) /* no thousands */
163 return en_exp_number(numstring+i);
164 else
165 return val_append(p,cons_val(string_val("thousand"),
166 en_exp_number(numstring+i)));
167 }
168 else if (num_digits < 10)
169 {
170 for (i=0; i < num_digits-6; i++)
171 part[i] = numstring[i];
172 part[i]='\0';
173 p = en_exp_number(part);
174 if (p == 0) /* no millions */
175 return en_exp_number(numstring+i);
176 else
177 return val_append(p,cons_val(string_val("million"),
178 en_exp_number(numstring+i)));
179 }
180 else if (num_digits < 13)
181 { /* If there are pedantic brits out there, tough!, 10^9 is a billion */
182 for (i=0; i < num_digits-9; i++)
183 part[i] = numstring[i];
184 part[i]='\0';
185 p = en_exp_number(part);
186 if (p == 0) /* no billions */
187 return en_exp_number(numstring+i);
188 else
189 return val_append(p,cons_val(string_val("billion"),
190 en_exp_number(numstring+i)));
191 }
192 else /* Way too many digits here, to be a number */
193 {
194 return en_exp_digits(numstring);
195 }
196 }
197
198 cst_val *en_exp_ordinal(const char *rawnumstring)
199 {
200 /* return ordinal for digit string */
201 cst_val *card, *o;
202 const cst_val *t;
203 const char *l;
204 const char *ord;
205 char *numstring;
206 int i,j;
207
208 numstring = cst_strdup(rawnumstring);
209 for (j=i=0; i < cst_strlen(rawnumstring); i++)
210 if (rawnumstring[i] != ',')
211 {
212 numstring[j] = rawnumstring[i];
213 j++;
214 }
215 numstring[j] = '\0';
216 card = val_reverse(en_exp_number(numstring));
217 cst_free(numstring);
218
219 l = val_string(val_car(card));
220 ord = 0;
221 for (i=0; i<10; i++)
222 if (cst_streq(l,digit2num[i]))
223 ord = ord2num[i];
224 if (!ord)
225 for (i=0; i<10; i++)
226 if (cst_streq(l,digit2teen[i]))
227 ord = ord2teen[i];
228 if (!ord)
229 for (i=0; i<10; i++)
230 if (cst_streq(l,digit2enty[i]))
231 ord = ord2enty[i];
232 if (cst_streq(l,"hundred"))
233 ord = "hundredth";
234 if (cst_streq(l,"thousand"))
235 ord = "thousandth";
236 if (cst_streq(l,"billion"))
237 ord = "billtionth";
238 if (!ord) /* dunno, so don't convert anything */
239 return card;
240 o = cons_val(string_val(ord),0);
241 for (t=val_cdr(card); t; t=val_cdr(t))
242 o = cons_val(val_car(t),o);
243 delete_val(card);
244 return o;
245 }
246
247 cst_val *en_exp_id(const char *numstring)
248 {
249 /* Expand numstring as pairs as in years or ids */
250 char aaa[3];
251
252 if ((cst_strlen(numstring) == 4) &&
253 (numstring[2] == '0') &&
254 (numstring[3] == '0'))
255 {
256 if (numstring[1] == '0')
257 return en_exp_number(numstring); /* 2000, 3000 */
258 else
259 {
260 aaa[0] = numstring[0];
261 aaa[1] = numstring[1];
262 aaa[2] = '\0';
263 return val_append(en_exp_number(aaa),
264 cons_val(string_val("hundred"),0));
265 }
266 }
267 else if ((cst_strlen(numstring) == 3) &&
268 (numstring[0] != '0') &&
269 (numstring[1] == '0') &&
270 (numstring[2] == '0'))
271 {
272 return cons_val(string_val(digit2num[numstring[0]-'0']),
273 cons_val(string_val("hundred"),0));
274 }
275 else if ((cst_strlen(numstring) == 2) && (numstring[0] == '0')
276 && (numstring[1] == '0'))
277 return cons_val(string_val("zero"),
278 cons_val(string_val("zero"),NULL));
279 else if ((cst_strlen(numstring) == 2) && (numstring[0] == '0'))
280 return cons_val(string_val("oh"),
281 en_exp_digits(&numstring[1]));
282 else if (((cst_strlen(numstring) == 4) &&
283 ((numstring[1] == '0'))) ||
284 (cst_strlen(numstring) < 3))
285 return en_exp_number(numstring);
286 else if (cst_strlen(numstring)%2 == 1)
287 {
288 return cons_val(string_val(digit2num[numstring[0]-'0']),
289 en_exp_id(&numstring[1]));
290 }
291 else
292 {
293 aaa[0] = numstring[0];
294 aaa[1] = numstring[1];
295 aaa[2] = '\0';
296 return val_append(en_exp_number(aaa),en_exp_id(&numstring[2]));
297 }
298 }
299
300 cst_val *en_exp_real(const char *numstring)
301 {
302 char *aaa, *p;
303 cst_val *r;
304
305 if (numstring && (numstring[0] == '-'))
306 r = cons_val(string_val("minus"),
307 en_exp_real(&numstring[1]));
308 else if (numstring && (numstring[0] == '+'))
309 r = cons_val(string_val("plus"),
310 en_exp_real(&numstring[1]));
311 else if (((p=strchr(numstring,'e')) != 0) ||
312 ((p=strchr(numstring,'E')) != 0))
313 {
314 aaa = cst_strdup(numstring);
315 aaa[cst_strlen(numstring)-cst_strlen(p)] = '\0';
316 r = val_append(en_exp_real(aaa),
317 cons_val(string_val("e"),
318 en_exp_real(p+1)));
319 cst_free(aaa);
320 }
321 else if ((p=strchr(numstring,'.')) != 0)
322 {
323 aaa = cst_strdup(numstring);
324 aaa[cst_strlen(numstring)-cst_strlen(p)] = '\0';
325 r = val_append(en_exp_number(aaa),
326 cons_val(string_val("point"),
327 en_exp_digits(p+1)));
328 cst_free(aaa);
329 }
330 else
331 r = en_exp_number(numstring); /* I don't think you can get here */
332
333 return r;
334 }
335
336 cst_val *en_exp_digits(const char *numstring)
337 {
338 /* Expand given token to list of words pronouncing it as digits */
339 cst_val *d = 0;
340 const char *p;
341
342 for (p=numstring; *p; p++)
343 {
344 if ((*p >= '0') && (*p <= '9'))
345 d = cons_val(string_val(digit2num[*p-'0']),d);
346 else
347 d = cons_val(string_val("umpty"),d);
348 }
349
350 return val_reverse(d);
351 }
352
353 cst_val *en_exp_letters(const char *lets)
354 {
355 /* returns these as list of single char symbols */
356 char *aaa;
357 cst_val *r;
358 int i;
359
360 aaa = cst_alloc(char,2);
361 aaa[1] = '\0';
362 for (r=0,i=0; lets[i] != '\0'; i++)
363 {
364 aaa[0] = lets[i];
365 if (isupper((int)aaa[0]))
366 aaa[0] = tolower((int)aaa[0]);
367 if (strchr("0123456789",aaa[0]))
368 r = cons_val(string_val(digit2num[aaa[0]-'0']),r);
369 else if (cst_streq(aaa,"a"))
370 r = cons_val(string_val("_a"),r);
371 else
372 r = cons_val(string_val(aaa),r);
373 }
374 cst_free(aaa);
375
376 return val_reverse(r);
377 }
378
379 int en_exp_roman(const char *roman)
380 {
381 int val;
382 const char *p;
383 val = 0;
384
385 for (p=roman; *p != 0; p++)
386 {
387 if (*p == 'X')
388 val += 10;
389 else if (*p == 'V')
390 val += 5;
391 else if (*p == 'I')
392 {
393 if (p[1] == 'V')
394 {
395 val += 4;
396 p++;
397 }
398 else if (p[1] == 'X')
399 {
400 val += 9;
401 p++;
402 }
403 else
404 val += 1;
405 }
406 }
407 return val;
408 }
409
410