Debian Codegraph @

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