Debian Codegraph @

lang/usenglish/us_text.c

raw
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 /* US English text analysis functions */
38 /* */
39 /*************************************************************************/
40
41 #include <ctype.h>
42 #include "flite.h"
43 #include "usenglish.h"
44 #include "us_text.h"
45 #include "cst_regex.h"
46
47 static int text_splitable(const char *s,int i);
48 static cst_val *state_name(const char *name,cst_item *t);
49
50 /* compiled us regexes */
51 #include "us_regexes.h"
52
53 /* Note you need to also update the wandm regex in make_us_regexes too */
54 static const char * const wandm_abbrevs[99][2] =
55 {
56 { "LB", "pounds" },
57 { "LBS", "pounds" },
58 { "lb", "pounds" },
59 { "lbs", "pounds" },
60 { "ft", "feet" },
61 { "FT", "feet" },
62 { "kg", "kilograms" },
63 { "km", "kilometers" },
64 { "cm", "centimeters" },
65 { "mm", "millimeters" },
66 { "ml", "milliliters" },
67 { "oz", "ounces" },
68 { "hz", "hertz" },
69 { "Hz", "hertz" },
70 { "HZ", "hertz" },
71 { "KHz", "kilohertz" },
72 { "MHz", "megahertz" },
73 { "GHz", "gigahertz" },
74 { "KB", "kilobytes" },
75 { "GB", "gigabytes" },
76 { "MB", "megabytes" },
77 { "TB", "terabytes" },
78 { NULL, NULL },
79 };
80
81 static const char * const eedwords[] = {
82 "to",
83 "can",
84 "can't",
85 "cannot",
86 "cant",
87 "could",
88 "couldn't",
89 "couldnt",
90 "will",
91 "shall",
92 NULL};
93
94 static int rex_like(const cst_item *t)
95 {
96 /* returns 1 if this is in a king like context */
97 char *pn = cst_downcase(ffeature_string(t,"p.name"));
98 char *ppn = cst_downcase(ffeature_string(t,"p.p.name"));
99 int v = 0;
100
101 if (cst_streq(pn,"louis") ||
102 cst_streq(pn,"henry") ||
103 cst_streq(pn,"charles") ||
104 cst_streq(pn,"philip") ||
105 cst_streq(pn,"george") ||
106 cst_streq(pn,"edward") ||
107 cst_streq(pn,"pius") ||
108 cst_streq(pn,"william") ||
109 cst_streq(pn,"richard") ||
110 cst_streq(pn,"ptolemy") ||
111 cst_streq(pn,"john") ||
112 cst_streq(pn,"paul") ||
113 cst_streq(pn,"peter") ||
114 cst_streq(pn,"nicholas") ||
115 cst_streq(pn,"frederick") ||
116 cst_streq(pn,"james") ||
117 cst_streq(pn,"alfonso") ||
118 cst_streq(pn,"ivan") ||
119 cst_streq(pn,"napolean") ||
120 cst_streq(pn,"leo") ||
121 cst_streq(pn,"gregory") ||
122 cst_streq(pn,"catherine") ||
123 cst_streq(pn,"alexandria") ||
124 cst_streq(pn,"pierre") ||
125 cst_streq(pn,"elizabeth") ||
126 cst_streq(pn,"mary"))
127 v = 1;
128 else if (cst_streq(ppn,"king") ||
129 cst_streq(ppn,"queen") ||
130 cst_streq(ppn,"pope") ||
131 cst_streq(ppn,"duke") ||
132 cst_streq(ppn,"tsar") ||
133 cst_streq(ppn,"emperor") ||
134 cst_streq(ppn,"shah") ||
135 cst_streq(ppn,"ceasar") ||
136 cst_streq(ppn,"duchess") ||
137 cst_streq(ppn,"tsarina") ||
138 cst_streq(ppn,"empress") ||
139 cst_streq(ppn,"baron") ||
140 cst_streq(ppn,"baroness") ||
141 cst_streq(ppn,"sultan") ||
142 cst_streq(ppn,"count") ||
143 cst_streq(ppn,"countess"))
144 v = 1;
145
146 cst_free(pn);
147 cst_free(ppn);
148 return v;
149 }
150
151 static int section_like(const cst_item *t)
152 {
153 /* returns 1 if this is in a king like context */
154 char *pn = cst_downcase(ffeature_string(t,"p.name"));
155 int v = 0;
156
157 if (cst_streq(pn,"section") ||
158 cst_streq(pn,"chapter") ||
159 cst_streq(pn,"part") ||
160 cst_streq(pn,"phrase") ||
161 cst_streq(pn,"verse") ||
162 cst_streq(pn,"scene") ||
163 cst_streq(pn,"act") ||
164 cst_streq(pn,"book") ||
165 cst_streq(pn,"volume") ||
166 cst_streq(pn,"chap") ||
167 cst_streq(pn,"war") ||
168 cst_streq(pn,"apollo") ||
169 cst_streq(pn,"trek") ||
170 cst_streq(pn,"fortran"))
171 v = 1;
172
173 cst_free(pn);
174
175 return v;
176 }
177
178 cst_utterance *us_textanalysis(cst_utterance *u)
179 {
180 if (!feat_present(u->features, "tokentowords_func"))
181 utt_set_feat(u, "tokentowords_func", itemfunc_val(us_tokentowords));
182
183 return default_textanalysis(u);
184 }
185
186 static cst_val *us_tokentowords_one(cst_item *token, const char *name);
187 cst_val *us_tokentowords(cst_item *token)
188 {
189 return us_tokentowords_one(token, item_feat_string(token, "name"));
190 }
191
192 static cst_val *add_break(cst_val *l)
193 {
194 /* add feature (break 1) to last item in this list */
195 const cst_val *i;
196 cst_val *t;
197 cst_features *f;
198
199 for (i=l; val_cdr(i); i=val_cdr(i));
200
201 if (i) /* might be empty list */
202 {
203 f = new_features();
204 feat_set_string(f,"break","1");
205 t = cons_val(val_car(i),features_val(f));
206 set_car((cst_val *)i,t);
207 }
208
209 return l;
210 }
211
212 static int contains_unicode_single_quote(const char *name)
213 {
214 static const char *unicode_single_quote = "’";
215 int i;
216
217 for (i=0; name[i]; i++)
218 {
219 /* No check if name is long enough as it'll have NULL before end */
220 if ((name[i] == unicode_single_quote[0]) &&
221 (name[i+1] == unicode_single_quote[1]) &&
222 (name[i+2] == unicode_single_quote[2]))
223 return TRUE;
224 }
225 return FALSE;
226 }
227
228 static char *map_unicode_single_quote(const char *name)
229 {
230 static const char *unicode_single_quote = "’";
231 int i,j;
232 char *aaa = cst_strdup(name); /* it'll always get shorter */
233
234 for (i=0,j=0; name[i]; i++,j++)
235 {
236 if ((name[i] == unicode_single_quote[0]) &&
237 (name[i+1] == unicode_single_quote[1]) &&
238 (name[i+2] == unicode_single_quote[2]))
239 {
240 aaa[j] = '\'';
241 i+=2;
242 }
243 else
244 aaa[j] = name[i];
245 }
246 aaa[j] = '\0';
247
248 return aaa;
249 }
250
251 static cst_val *us_tokentowords_one(cst_item *token, const char *name)
252 {
253 /* Return list of words that expand token/name */
254 const char *p;
255 char *aaa, *bbb, *ccc, *ppp;
256 int i,j,k,l;
257 cst_val *r, *s, *ss;
258 const cst_val *rr;
259 const char *nsw = "";
260 const char *ssml_alias = "";
261 const char *token_name = "";
262 cst_lexicon *lex;
263 cst_utterance *utt;
264 /* printf("token_name %s name %s\n",item_name(token),name); */
265 /* FIXME: For SAPI and friends, any tokens with explicit
266 pronunciations need to be passed through as-is. This should be
267 done in the interface code rather than here once the
268 tokentowords hook is accessible. AWB: no, they should set the
269 nsw feature and this function should deal with it (doesn't yet though)*/
270 if (item_feat_present(token,"phones"))
271 return cons_val(string_val(name),NULL);
272
273 if (item_feat_present(token,"nsw"))
274 nsw = item_feat_string(token,"nsw");
275
276 token_name = item_name(token);
277
278 if ((item_feat_present(token,"ssml_alias")) &&
279 (!cst_streq(token_name,name))) /* and we are not recursing */
280 {
281 /* SSML has given a substitute for this (and more) tokens */
282 /* NOTE: the alias is not put through text normalization */
283 ssml_alias = item_feat_string(token,"ssml_alias");
284 if (cst_streq(ssml_alias,ffeature_string(token,"p.ssml_alias")))
285 /* The first token gets the substitution */
286 return NULL;
287 else
288 {
289 return cons_val(string_val(ssml_alias),NULL);
290 }
291 }
292
293 utt = item_utt(token);
294 lex = val_lexicon(feat_val(utt->features,"lexicon"));
295
296 if (cst_streq("1",get_param_string(item_feats(token),"ssml_comment","0")))
297 r = NULL;
298 else if ((cst_streq("a",name) || cst_streq("A",name)) &&
299 ((item_next(token) == 0) ||
300 (!cst_streq(name,item_name(token))) ||
301 (!cst_streq("",ffeature_string(token,"punc")))))
302 { /* if A is a sub part of a token, then its ey not ah */
303 r = cons_val(string_val("_a"),0);
304 }
305 else if (cst_strlen(name) == 0)
306 r = NULL;
307 else if (cst_regex_match(dottedabbrevs,name))
308 { /* X.X.X */
309 aaa = cst_strdup(name);
310 for (i=j=0; aaa[i]; i++)
311 if (aaa[i] != '.')
312 {
313 aaa[j] = aaa[i];
314 j++;
315 }
316 aaa[j] = '\0';
317 r = en_exp_letters(aaa);
318 cst_free(aaa);
319 }
320 else if (cst_regex_match(cst_rx_commaint,name))
321 { /* 99,999,999 */
322 aaa = cst_strdup(name);
323 for (j=i=0; i < (signed int)cst_strlen(name); i++)
324 if (name[i] != ',')
325 {
326 aaa[j] = name[i];
327 j++;
328 }
329 aaa[j] = '\0';
330 r = en_exp_real(aaa);
331 cst_free(aaa);
332 }
333 else if (cst_regex_match(sevenphonenumber,name))
334 { /* 234-3434 telephone numbers */
335 p=strchr(name,'-');
336 aaa = cst_strdup(name);
337 aaa[cst_strlen(name)-cst_strlen(p)] = '\0';
338 bbb = cst_strdup(p+1);
339 r = val_append(add_break(en_exp_digits(aaa)),
340 en_exp_digits(bbb));
341 cst_free(aaa);
342 cst_free(bbb);
343 }
344 else if
345 ((cst_regex_match(threedigits,name) &&
346 ((!cst_regex_match(cst_rx_digits,ffeature_string(token,"p.name"))
347 && cst_regex_match(threedigits,ffeature_string(token,"n.name"))
348 && cst_regex_match(fourdigits,ffeature_string(token,"n.n.name"))) ||
349 (cst_regex_match(sevenphonenumber,ffeature_string(token,"n.name"))) ||
350 (!cst_regex_match(cst_rx_digits,ffeature_string(token,"p.p.name"))
351 && cst_regex_match(threedigits,ffeature_string(token,"p.name"))
352 && cst_regex_match(fourdigits,ffeature_string(token,"n.name"))))) ||
353 (cst_regex_match(fourdigits,name) &&
354 (!cst_regex_match(cst_rx_digits,ffeature_string(token,"n.name"))
355 && cst_regex_match(threedigits,ffeature_string(token,"p.name"))
356 && cst_regex_match(threedigits,ffeature_string(token,"p.p.name")))))
357 {
358 /* part of a telephone number */
359 if (cst_streq("",ffeature_string(token,"punc")))
360 item_set_string(token,"punc",",");
361 r = add_break(en_exp_digits(name));
362 }
363 else if (cst_regex_match(numbertime,name))
364 {
365 p=strchr(name,':');
366 aaa = cst_strdup(name);
367 aaa[cst_strlen(name)-cst_strlen(p)] = '\0';
368 bbb = cst_strdup(p+1);
369
370 r = en_exp_number(aaa);
371 if (!cst_streq("00",bbb))
372 r = val_append(r,en_exp_id(bbb));
373 /* r = add_break(r); */
374
375 cst_free(aaa);
376 cst_free(bbb);
377 }
378 else if (cst_regex_match(numbertimexm,name))
379 {
380 p=strchr(name,':');
381 if (!p) p=strchr(name,'.');
382 aaa = cst_strdup(name);
383 aaa[cst_strlen(name)-cst_strlen(p)] = '\0';
384 bbb = cst_strdup(p+1);
385 bbb[2] = '\0';
386 ccc = cst_strdup(p+3);
387
388 r = en_exp_number(aaa);
389 if (!cst_streq("00",bbb))
390 r = val_append(r,en_exp_id(bbb));
391 /* r = add_break(r); */
392
393 r = val_append(r,en_exp_letters(ccc));
394
395 cst_free(aaa);
396 cst_free(bbb);
397 cst_free(ccc);
398 }
399 else if (cst_regex_match(digits2dash,name))
400 { /* 999-999-999 etc */
401 bbb = cst_strdup(name);
402 for (ss=0,ppp=aaa=bbb; *ppp; ppp++)
403 {
404 if (*ppp == '-')
405 {
406 /* skip however many - there are */
407 while ((*ppp != '\0') && (*ppp == '-'))
408 {
409 *ppp = '\0';
410 ppp++;
411 }
412 ss = cons_val(string_val(aaa),ss);
413 aaa = ppp;
414 }
415 }
416 ss = cons_val(string_val(aaa),ss); /* note the order is now reversed */
417 if ((val_length(ss) == 2) &&
418 (atoi(val_string(val_car(val_cdr(ss)))) <
419 atoi(val_string(val_car(ss)))) && /* small to large */
420 (abs(cst_strlen(val_string(val_car(val_cdr(ss)))) -
421 cst_strlen(val_string(val_car(ss)))) < 2)) /* not too diff */
422 {
423 /* Should get 22-23 November, or 1998-1999 right */
424 r =
425 val_append(us_tokentowords_one(token,val_string(val_car(val_cdr(ss)))),
426 cons_val(string_val("to"),
427 us_tokentowords_one(token,val_string(val_car(ss)))));
428 }
429 else
430 { /* Its just a bunch of ids */
431 r = 0;
432 for (rr=ss; rr; rr=val_cdr(rr))
433 {
434 r = val_append(
435 add_break(en_exp_digits(val_string(val_car(rr)))),r);
436
437 }
438 }
439 delete_val(ss);
440 cst_free(bbb);
441 }
442 else if (cst_regex_match(leadingzerodigits,name))
443 { /* a leading zero and digits */
444 r = en_exp_digits(name);
445 }
446 else if (cst_regex_match(cst_rx_digits,name))
447 { /* string of digits (use cart to disambiguate) */
448 if (cst_streq("nide",nsw))
449 r = en_exp_id(name);
450 else {
451 const cst_val *tv;
452 const char *ts;
453 char *rname;
454
455 rname = cst_strdup(item_feat_string(token,"name"));
456 if (cst_streq(name,rname))
457 tv = cart_interpret(token,&us_nums_cart);
458 else
459 { /* in a recursive call */
460 item_set_string(token,"name",name);
461 tv = cart_interpret(token,&us_nums_cart);
462 item_set_string(token,"name",rname);
463 }
464 cst_free(rname);
465 ts = val_string(tv);
466 if (cst_streq(ts,"ordinal"))
467 r = en_exp_ordinal(name);
468 else if (cst_streq(ts,"digits"))
469 r = en_exp_digits(name);
470 else if (cst_streq(ts,"year"))
471 r = en_exp_id(name);
472 else
473 r = en_exp_number(name);
474 }
475 }
476 else if (cst_regex_match(romannums,name))
477 { /* Roman numerals */
478 if (cst_streq("",ffeature_string(token,"p.punc")))
479 { /* no preceeding punc */
480 char n[10];
481 cst_sprintf(n,"%d",en_exp_roman(name));
482 if (rex_like(token))
483 r = cons_val(string_val("the"),
484 en_exp_ordinal(n));
485 else if (section_like(token))
486 r = en_exp_number(n);
487 else
488 r = en_exp_letters(name);
489 }
490 else
491 r = en_exp_letters(name);
492 }
493 else if (cst_regex_match(drst,name))
494 { /* St Andrew's St, Dr King Dr */
495 const char *street;
496 const char *saint;
497 if ((name[0] == 's') || (name[0] == 'S'))
498 {
499 street = "street";
500 saint = "saint";
501 }
502 else
503 {
504 street = "drive";
505 saint = "doctor";
506 }
507 if ((item_next(token) == 0) ||
508 strchr(item_feat_string(token,"punc"),','))
509 r = cons_val(string_val(street),NULL);
510 else if (strchr(ffeature_string(token,"punc"),','))
511 r = cons_val(string_val(saint),NULL);
512 else
513 {
514 const char *pname = ffeature_string(token,"p.name");
515 const char *nname = ffeature_string(token,"n.name");
516 if ((pname[0] >= 'A') && (pname[0] <= 'Z') &&
517 (nname[0] >= 'a') && (nname[0] <= 'z'))
518 r = cons_val(string_val(street),NULL);
519 else if ((pname[0] >= '0') && (pname[0] <= '9') &&
520 (nname[0] >= 'a') && (nname[0] <= 'z'))
521 r = cons_val(string_val(street),NULL);
522 else if ((pname[0] >= 'a') && (pname[0] <= 'z') &&
523 (nname[0] >= 'A') && (nname[0] <= 'Z'))
524 r = cons_val(string_val(saint),NULL);
525 else if (cst_streq(ffeature_string(token,"n.whitespace")," "))
526 r = cons_val(string_val(saint),NULL);
527 else
528 r = cons_val(string_val(street),NULL);
529 }
530 if (cst_streq(item_feat_string(token,"punc"),"."))
531 item_set_string(token,"punc","");
532 }
533 else if (cst_streq(name,"Mr"))
534 {
535 item_set_string(token,"punc","");
536 r = cons_val(string_val("mister"),NULL);
537 }
538 else if (cst_streq(name,"Mrs"))
539 {
540 item_set_string(token,"punc","");
541 r = cons_val(string_val("missus"),NULL);
542 }
543 else if ((cst_streq(name,"read")) ||
544 (cst_streq(name,"lead")))
545 { /* checking WSJ examples, this seems a quick and easy way to */
546 /* get many of these correct */
547 const char *pname = ffeature_string(token,"p.name");
548
549 for (i=0; eedwords[i]; i++)
550 if (cst_streq(pname,eedwords[i]))
551 break;
552
553 if (eedwords[i])
554 { /* reed or leed */
555 if (name[0] == 'r')
556 r = cons_val(string_val("reed"),NULL);
557 else
558 r = cons_val(string_val("leed"),NULL);
559 }
560 else /* red or led */
561 {
562 if (name[0] == 'r')
563 r = cons_val(string_val("red"),NULL);
564 else
565 r = cons_val(string_val("led"),NULL);
566 }
567 }
568 else if (cst_streq(name,"am") || cst_streq(name,"AM"))
569 {
570 if (!cst_streq(name,item_name(token)))
571 r = en_exp_letters(name);
572 else if (item_prev(token) &&
573 (cst_regex_match(numbertime,ffeature_string(token,"p.name")) ||
574 cst_regex_match(cst_rx_digits,ffeature_string(token,"p.name"))))
575 r = en_exp_letters(name);
576 else
577 r = cons_val(string_val(name),NULL);
578 }
579 else if ((cst_strlen(name) == 1) &&
580 (name[0] >= 'A') &&
581 (name[0] <= 'Z') &&
582 (cst_streq(" ",ffeature_string(token,"n.whitespace"))) &&
583 (ffeature_string(token,"n.name")[0] >= 'A') &&
584 (ffeature_string(token,"n.name")[0] <= 'Z'))
585 {
586 item_set_string(token,"punc","");
587 aaa = cst_downcase(name);
588 if (cst_streq(aaa,"a"))
589 r = cons_val(string_val("_a"),0);
590 else
591 r = cons_val(string_val(aaa),0);
592 cst_free(aaa);
593 }
594 else if (cst_regex_match(cst_rx_double,name))
595 { /* real numbers */
596 r = en_exp_real(name);
597 }
598 else if (cst_regex_match(ordinal_number,name))
599 { /* explicit ordinals */
600 aaa = cst_strdup(name);
601 aaa[cst_strlen(name)-2] = '\0';
602 r = en_exp_ordinal(aaa);
603 cst_free(aaa);
604 }
605 else if ((cst_regex_match(illion,name)) &&
606 (cst_regex_match(usmoney,ffeature_string(token,"p.name"))))
607 {
608 r = cons_val(string_val(name),
609 cons_val(string_val("dollars"),NULL));
610 }
611 else if (cst_regex_match(usmoney,name))
612 {
613 /* US money */
614 /* printf("money, money, money %s\n", name); */
615 p = strchr(name,'.');
616
617 if (cst_regex_match(illion,ffeature_string(token,"n.name")))
618 { /* carl sagan's billions and billions */
619 r = en_exp_real(&name[1]);
620 }
621 else if (!p)
622 {
623 aaa = cst_strdup(&name[1]);
624 if (cst_streq("1",aaa))
625 r = cons_val(string_val("dollar"),NULL);
626 else
627 r = cons_val(string_val("dollars"),NULL);
628 r = val_append(us_tokentowords_one(token,aaa),r);
629 cst_free(aaa);
630 }
631 else if ((cst_strlen(p) == 1) || (cst_strlen(p) > 3))
632 { /* simply read as mumble point mumble */
633 r = val_append(en_exp_real(&name[1]),
634 cons_val(string_val("dollars"),NULL));
635 }
636 else
637 {
638 aaa = cst_strdup(name);
639 aaa[cst_strlen(name)-cst_strlen(p)] = '\0';
640 for (i=j=0; aaa[i] != '\0'; i++)
641 {
642 if (aaa[i] != ',')
643 {
644 aaa[j] = aaa[i];
645 j++;
646 }
647 }
648 aaa[j] = '\0';
649 if (cst_streq("00",p+1))
650 r = 0;
651 else if (cst_streq("01",p+1))
652 r = val_append(en_exp_number(p+1),
653 cons_val(string_val("cent"),NULL));
654 else
655 r = val_append(en_exp_number(p+1),
656 cons_val(string_val("cents"),NULL));
657
658 if (cst_streq("1",aaa+1))
659 r = cons_val(string_val("dollar"),r);
660 else
661 r = cons_val(string_val("dollars"),r);
662
663 r = val_append(en_exp_number(aaa+1),r);
664 cst_free(aaa);
665 }
666 }
667 else if (name[cst_strlen(name)-1] == '%')
668 {
669 aaa = cst_strdup(name);
670 aaa[cst_strlen(aaa)-1] = '\0';
671 r = val_append(us_tokentowords_one(token,aaa),
672 cons_val(string_val("per"),
673 cons_val(string_val("cent"),NULL)));
674 cst_free(aaa);
675
676 }
677 else if (cst_regex_match(numess,name))
678 { /* 60s and 7s and 9s */
679 aaa = cst_strdup(name);
680 aaa[cst_strlen(name)-1] = '\0';
681 r = val_append(us_tokentowords_one(token,aaa),
682 cons_val(string_val("'s"),0));
683 cst_free(aaa);
684 }
685 else if (contains_unicode_single_quote(name))
686 {
687 /* A single quote is sometimes rendered as unicode "’" */
688 /* so we map it back to an ascii single quote ' */
689 aaa = map_unicode_single_quote(name);
690 r = us_tokentowords_one(token, aaa);
691 cst_free(aaa);
692 return r;
693 }
694 else if ((p=(cst_strrchr(name,'\''))))
695 {
696 static const char * const pc[] = { "'s", "'ll", "'ve", "'d", NULL };
697
698 bbb = cst_downcase(p);
699 if (cst_member_string(bbb, pc))
700 {
701 aaa = cst_strdup(name);
702 aaa[cst_strlen(name)-cst_strlen(p)] = '\0';
703 r = val_append(us_tokentowords_one(token,aaa),
704 cons_val(string_val(bbb),0));
705 cst_free(aaa);
706 }
707 else if (cst_streq(p,"'tve")) /* admittedly rare and weird */
708 {
709 aaa = cst_strdup(name);
710 aaa[cst_strlen(name)-cst_strlen(p)+2] = '\0';
711 r = val_append(us_tokentowords_one(token,aaa),
712 cons_val(string_val("'ve"),0));
713 cst_free(aaa);
714 }
715 else
716 {
717 aaa = cst_strdup(name);
718 strcpy(&aaa[cst_strlen(name)-cst_strlen(p)],p+1);
719 r = us_tokentowords_one(token,aaa);
720 cst_free(aaa);
721 }
722 cst_free(bbb);
723 }
724 else if ((cst_regex_match(digitsslashdigits,name)) &&
725 (cst_streq(name,item_name(token))))
726 { /* might be fraction, or not */
727 p=strchr(name,'/');
728 aaa = cst_strdup(name);
729 aaa[cst_strlen(name)-cst_strlen(p)] = '\0';
730 bbb = cst_strdup(p+1);
731 if ((cst_streq("1",aaa)) && (cst_streq("2",bbb)))
732 r = cons_val(string_val("a"),
733 cons_val(string_val("half"),0));
734 else if (atoi(aaa) < (atoi(bbb)))
735 {
736 r = val_append(en_exp_number(aaa),
737 en_exp_ordinal(bbb));
738 if (atoi(aaa) > 1)
739 r = val_append(r,cons_val(string_val("'s"),0));
740 }
741 else
742 r = val_append(en_exp_number(aaa),
743 cons_val(string_val("slash"),
744 en_exp_number(bbb)));
745
746 if ((cst_regex_match(cst_rx_digits,ffeature_string(token,"p.name")))
747 && (item_prev(token))) /* don't mistake "0" as a number */
748 r = cons_val(string_val("and"),r);
749 cst_free(aaa);
750 cst_free(bbb);
751 }
752 else if ((p=(strchr(name,'-'))))
753 { /* aaa-bbb */
754 aaa = cst_strdup(name);
755 aaa[cst_strlen(name)-cst_strlen(p)] = '\0';
756 bbb = cst_strdup(p+1);
757 if (cst_regex_match(cst_rx_digits,aaa) &&
758 cst_regex_match(cst_rx_digits,bbb))
759 {
760 ccc = cst_strdup(name);
761 item_set_string(token,"name",bbb);
762 r = us_tokentowords_one(token,bbb);
763 item_set_string(token,"name",aaa);
764 r = val_append(us_tokentowords_one(token,aaa),
765 cons_val(string_val("to"),r));
766 item_set_string(token,"name",ccc);
767 cst_free(ccc);
768 }
769 else
770 r = val_append(us_tokentowords_one(token,aaa),
771 us_tokentowords_one(token,bbb));
772 cst_free(aaa);
773 cst_free(bbb);
774 }
775 else if (cst_regex_match(wandm,name))
776 { /* weights and measures */
777 for (j=cst_strlen(name)-1; j > 0; j--)
778 if (cst_strchr("0123456789",name[j]))
779 break;
780 j += 1;
781 for (i=0; wandm_abbrevs[i][0]; i++)
782 if (cst_streq(name+j,wandm_abbrevs[i][0]))
783 break;
784 aaa = cst_strdup(name);
785 aaa[j] = '\0';
786 /* remove any commas */
787 for (k=0,l=0; aaa[l]; k++,l++)
788 {
789 if (aaa[l] == ',') l++;
790 aaa[k] = aaa[l];
791 }
792 aaa[k] = '\0';
793 if (!wandm_abbrevs[i][0]) /* didn't find an expansion */
794 r = val_append(en_exp_number(aaa),
795 us_tokentowords_one(token,name+j));
796 else
797 r = val_append(en_exp_number(aaa),
798 cons_val(string_val(wandm_abbrevs[i][1]),NULL));
799
800 cst_free(aaa);
801 }
802 else if ((cst_strlen(name) > 1) && (!cst_regex_match(cst_rx_alpha,name)))
803 { /* its not just alphas */
804 for (i=0; name[i] != '\0'; i++)
805 if (text_splitable(name,i))
806 break;
807 aaa = cst_strdup(name);
808 aaa[i+1] = '\0';
809 bbb = cst_strdup(&name[i+1]);
810 item_set_string(token,"nsw","nide");
811 r = val_append(us_tokentowords_one(token,aaa),
812 us_tokentowords_one(token,bbb));
813 cst_free(aaa);
814 cst_free(bbb);
815 }
816 else if ((s = state_name(name,token)))
817 {
818 r = s;
819 }
820 else if ((cst_strlen(name) > 1) &&
821 (cst_regex_match(cst_rx_alpha,name)) &&
822 /* AUP: Added 4th argument (voice feats) as NULL, needs to be revisited later. */
823 (!in_lex(lex,name,NULL,NULL)) &&
824 (!us_aswd(name)))
825 /* Still not quiet right, if there is a user_lex we need to check */
826 /* it too -- but user_lex isn't user setable yet */
827 /* Need common exception list */
828 /* unpronouncable list of alphas */
829 r = en_exp_letters(name);
830
831 /* buckets of other stuff missing */
832
833 else /* just a word */
834 {
835 aaa = cst_downcase(name);
836 r = cons_val(string_val(aaa),0);
837 cst_free(aaa);
838 }
839 return r;
840 }
841
842 static int text_splitable(const char *s,int i)
843 {
844 /* should token be split after this */
845
846 if (strchr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",s[i]) &&
847 strchr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",s[i+1]))
848 return FALSE;
849 else if (strchr("0123456789",s[i]) &&
850 strchr("0123456789",s[i+1]))
851 return FALSE;
852 else
853 return TRUE;
854 }
855
856 static const char * const states[99][5] =
857 {
858 { "AL", "ambiguous", "alabama" , NULL, NULL },
859 { "Al", "ambiguous", "alabama" , NULL, NULL },
860 { "Ala", "", "alabama" , NULL, NULL },
861 { "AK", "", "alaska" , NULL, NULL },
862 { "Ak", "", "alaska" , NULL, NULL },
863 { "AZ", "", "arizona" , NULL, NULL },
864 { "Az", "", "arizona" , NULL, NULL },
865 { "CA", "", "california" , NULL, NULL },
866 { "Ca", "", "california" , NULL, NULL },
867 { "Cal", "ambiguous", "california" , NULL, NULL },
868 { "Calif", "", "california" , NULL, NULL },
869 { "CO", "ambiguous", "colorado" , NULL, NULL },
870 { "Co", "ambiguous", "colorado" , NULL, NULL },
871 { "Colo", "", "colorado" , NULL, NULL },
872 { "DC", "", "d" , "c", NULL },
873 { "DE", "", "delaware" , NULL, NULL },
874 { "De", "ambiguous", "delaware" , NULL, NULL },
875 { "Del", "ambiguous", "delaware" , NULL, NULL },
876 { "FL", "", "florida" , NULL, NULL },
877 { "Fl", "ambiguous", "florida" , NULL, NULL },
878 { "Fla", "", "florida" , NULL, NULL },
879 { "GA", "", "georgia" , NULL, NULL },
880 { "Ga", "", "georgia" , NULL, NULL },
881 { "HI", "", "hawaii" , NULL, NULL },
882 { "Hi", "ambiguous", "hawaii" , NULL, NULL },
883 { "IA", "", "iowa" , NULL, NULL },
884 { "Ia", "ambiguous", "iowa" , NULL, NULL },
885 { "Ind", "ambiguous", "indiana" , NULL, NULL },
886 { "ID", "ambiguous", "idaho" , NULL, NULL },
887 { "IL", "ambiguous", "illinois" , NULL, NULL },
888 { "Il", "ambiguous", "illinois" , NULL, NULL },
889 { "ILL", "ambiguous", "illinois" , NULL, NULL },
890 { "KS", "", "kansas" , NULL, NULL },
891 { "Ks", "", "kansas" , NULL, NULL },
892 { "Kans", "", "kansas" , NULL, NULL },
893 { "KY", "ambiguous", "kentucky" , NULL, NULL },
894 { "Ky", "ambiguous", "kentucky" , NULL, NULL },
895 { "LA", "ambiguous", "louisiana" , NULL, NULL },
896 { "La", "ambiguous", "louisiana" , NULL, NULL },
897 { "Lou", "ambiguous", "louisiana" , NULL, NULL },
898 { "Lous", "ambiguous", "louisiana" , NULL, NULL },
899 { "MA", "ambiguous", "massachusetts" , NULL, NULL },
900 { "Mass", "ambiguous", "massachusetts" , NULL, NULL },
901 { "Ma", "ambiguous", "massachusetts" , NULL, NULL },
902 { "MD", "ambiguous", "maryland" , NULL, NULL },
903 { "Md", "ambiguous", "maryland" , NULL, NULL },
904 { "ME", "ambiguous", "maine" , NULL, NULL },
905 { "Me", "ambiguous", "maine" , NULL, NULL },
906 { "MI", "", "michigan" , NULL, NULL },
907 { "Mi", "ambiguous", "michigan" , NULL, NULL },
908 { "Mich", "ambiguous", "michigan" , NULL, NULL },
909 { "MN", "ambiguous", "minnestota" , NULL, NULL },
910 { "Minn", "ambiguous", "minnestota" , NULL, NULL },
911 { "MS", "ambiguous", "mississippi" , NULL, NULL },
912 { "Miss", "ambiguous", "mississippi" , NULL, NULL },
913 { "MT", "ambiguous", "montanna" , NULL, NULL },
914 { "Mt", "ambiguous", "montanna" , NULL, NULL },
915 { "MO", "ambiguous", "missouri" , NULL, NULL },
916 { "Mo", "ambiguous", "missouri" , NULL, NULL },
917 { "NC", "ambiguous", "north" , "carolina", NULL },
918 { "ND", "ambiguous", "north" , "dakota", NULL },
919 { "NE", "ambiguous", "nebraska" , NULL, NULL },
920 { "Ne", "ambiguous", "nebraska" , NULL, NULL },
921 { "Neb", "ambiguous", "nebraska" , NULL, NULL },
922 { "NH", "ambiguous", "new" , "hampshire", NULL },
923 { "NV", "", "nevada" , NULL, NULL },
924 { "Nev", "", "nevada" , NULL, NULL },
925 { "NY", "", "new" , "york", NULL },
926 { "OH", "ambiguous", "ohio" , NULL, NULL },
927 { "OK", "ambiguous", "oklahoma" , NULL, NULL },
928 { "Okla", "", "oklahoma" , NULL, NULL },
929 { "OR", "ambiguous", "oregon" , NULL, NULL },
930 { "Or", "ambiguous", "oregon" , NULL, NULL },
931 { "Ore", "ambiguous", "oregon" , NULL, NULL },
932 { "PA", "ambiguous", "pennsylvania" , NULL, NULL },
933 { "Pa", "ambiguous", "pennsylvania" , NULL, NULL },
934 { "Penn", "ambiguous", "pennsylvania" , NULL, NULL },
935 { "RI", "ambiguous", "rhode" , "island", NULL },
936 { "SC", "ambiguous", "south" , "carlolina", NULL },
937 { "SD", "ambiguous", "south" , "dakota", NULL },
938 { "TN", "ambiguous", "tennesee" , NULL, NULL },
939 { "Tn", "ambiguous", "tennesee" , NULL, NULL },
940 { "Tenn", "ambiguous", "tennesee" , NULL, NULL },
941 { "TX", "ambiguous", "texas" , NULL, NULL },
942 { "Tx", "ambiguous", "texas" , NULL, NULL },
943 { "Tex", "ambiguous", "texas" , NULL, NULL },
944 { "UT", "ambiguous", "utah" , NULL, NULL },
945 { "VA", "ambiguous", "virginia" , NULL, NULL },
946 { "WA", "ambiguous", "washington" , NULL, NULL },
947 { "Wa", "ambiguous", "washington" , NULL, NULL },
948 { "Wash", "ambiguous", "washington" , NULL, NULL },
949 { "WI", "ambiguous", "wisconsin" , NULL, NULL },
950 { "Wi", "ambiguous", "wisconsin" , NULL, NULL },
951 { "WV", "ambiguous", "west" , "virginia", NULL },
952 { "WY", "ambiguous", "wyoming" , NULL, NULL },
953 { "Wy", "ambiguous", "wyoming" , NULL, NULL },
954 { "Wyo", "", "wyoming" , NULL, NULL },
955 { "PR", "ambiguous", "puerto" , "rico", NULL },
956 { NULL, NULL, "puerto" , "rico", NULL }
957 };
958
959 static cst_val *state_name(const char *name,cst_item *t)
960 {
961 int s,j;
962 int do_it = 0;
963 cst_val *r = 0;
964
965 for (s=0; states[s][0]; s++)
966 {
967 if (cst_streq(states[s][0],name))
968 {
969 if (cst_streq(states[s][1],"ambiguous"))
970 {
971 const char *pname = ffeature_string(t,"p.name");
972 const char *nname = ffeature_string(t,"n.name");
973 /* previous name is capitalized */
974 if (((strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",pname[0])) &&
975 (cst_strlen(pname) > 2) &&
976 (cst_regex_match(cst_rx_alpha,pname))) &&
977 ((strchr("abcdefghijklmnopqrstuvwxyz",nname[0])) ||
978 (item_next(t) == 0) ||
979 (cst_streq(".",item_feat_string(t,"punc"))) ||
980 (((cst_strlen(nname) == 5 || (cst_strlen(nname) == 10)) &&
981 cst_regex_match(cst_rx_digits,nname)))))
982 do_it = 1;
983 else
984 do_it = 0;
985 }
986 else
987 do_it = 1;
988
989 if (do_it)
990 {
991 for (j=2; states[s][j]; j++)
992 r = cons_val(string_val(states[s][j]),r);
993 return val_reverse(r);
994 }
995 }
996 }
997 return r;
998
999 }
1000
1001
1002