| 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 < (signed int)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 |
if (!card) |
| 218 |
card = cons_val(string_val("zero"),0); |
| 219 |
cst_free(numstring); |
| 220 |
|
| 221 |
l = val_string(val_car(card)); |
| 222 |
ord = 0; |
| 223 |
for (i=0; i<10; i++) |
| 224 |
if (cst_streq(l,digit2num[i])) |
| 225 |
ord = ord2num[i]; |
| 226 |
if (!ord) |
| 227 |
for (i=0; i<10; i++) |
| 228 |
if (cst_streq(l,digit2teen[i])) |
| 229 |
ord = ord2teen[i]; |
| 230 |
if (!ord) |
| 231 |
for (i=0; i<10; i++) |
| 232 |
if (cst_streq(l,digit2enty[i])) |
| 233 |
ord = ord2enty[i]; |
| 234 |
if (cst_streq(l,"hundred")) |
| 235 |
ord = "hundredth"; |
| 236 |
if (cst_streq(l,"thousand")) |
| 237 |
ord = "thousandth"; |
| 238 |
if (cst_streq(l,"billion")) |
| 239 |
ord = "billtionth"; |
| 240 |
if (!ord) /* dunno, so don't convert anything */ |
| 241 |
return card; |
| 242 |
o = cons_val(string_val(ord),0); |
| 243 |
for (t=val_cdr(card); t; t=val_cdr(t)) |
| 244 |
o = cons_val(val_car(t),o); |
| 245 |
delete_val(card); |
| 246 |
return o; |
| 247 |
} |
| 248 |
|
| 249 |
cst_val *en_exp_id(const char *numstring) |
| 250 |
{ |
| 251 |
/* Expand numstring as pairs as in years or ids */ |
| 252 |
char aaa[3]; |
| 253 |
|
| 254 |
if ((cst_strlen(numstring) == 4) && |
| 255 |
(numstring[2] == '0') && |
| 256 |
(numstring[3] == '0')) |
| 257 |
{ |
| 258 |
if (numstring[1] == '0') |
| 259 |
return en_exp_number(numstring); /* 2000, 3000 */ |
| 260 |
else |
| 261 |
{ |
| 262 |
aaa[0] = numstring[0]; |
| 263 |
aaa[1] = numstring[1]; |
| 264 |
aaa[2] = '\0'; |
| 265 |
return val_append(en_exp_number(aaa), |
| 266 |
cons_val(string_val("hundred"),0)); |
| 267 |
} |
| 268 |
} |
| 269 |
else if ((cst_strlen(numstring) == 3) && |
| 270 |
(numstring[0] != '0') && |
| 271 |
(numstring[1] == '0') && |
| 272 |
(numstring[2] == '0')) |
| 273 |
{ |
| 274 |
return cons_val(string_val(digit2num[numstring[0]-'0']), |
| 275 |
cons_val(string_val("hundred"),0)); |
| 276 |
} |
| 277 |
else if ((cst_strlen(numstring) == 2) && (numstring[0] == '0') |
| 278 |
&& (numstring[1] == '0')) |
| 279 |
return cons_val(string_val("zero"), |
| 280 |
cons_val(string_val("zero"),NULL)); |
| 281 |
else if ((cst_strlen(numstring) == 2) && (numstring[0] == '0')) |
| 282 |
return cons_val(string_val("oh"), |
| 283 |
en_exp_digits(&numstring[1])); |
| 284 |
else if (((cst_strlen(numstring) == 4) && |
| 285 |
(numstring[1] == '0') && (numstring[2] == '0')) || |
| 286 |
(cst_strlen(numstring) < 3)) |
| 287 |
return en_exp_number(numstring); |
| 288 |
else if (cst_strlen(numstring)%2 == 1) |
| 289 |
{ |
| 290 |
return cons_val(string_val(digit2num[numstring[0]-'0']), |
| 291 |
en_exp_id(&numstring[1])); |
| 292 |
} |
| 293 |
else |
| 294 |
{ |
| 295 |
aaa[0] = numstring[0]; |
| 296 |
aaa[1] = numstring[1]; |
| 297 |
aaa[2] = '\0'; |
| 298 |
return val_append(en_exp_number(aaa),en_exp_id(&numstring[2])); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
cst_val *en_exp_real(const char *numstring) |
| 303 |
{ |
| 304 |
char *aaa; |
| 305 |
const char *p; |
| 306 |
cst_val *r; |
| 307 |
|
| 308 |
if (numstring && (numstring[0] == '-')) |
| 309 |
r = cons_val(string_val("minus"), |
| 310 |
en_exp_real(&numstring[1])); |
| 311 |
else if (numstring && (numstring[0] == '+')) |
| 312 |
r = cons_val(string_val("plus"), |
| 313 |
en_exp_real(&numstring[1])); |
| 314 |
else if (((p=strchr(numstring,'e')) != 0) || |
| 315 |
((p=strchr(numstring,'E')) != 0)) |
| 316 |
{ |
| 317 |
aaa = cst_strdup(numstring); |
| 318 |
aaa[cst_strlen(numstring)-cst_strlen(p)] = '\0'; |
| 319 |
r = val_append(en_exp_real(aaa), |
| 320 |
cons_val(string_val("e"), |
| 321 |
en_exp_real(p+1))); |
| 322 |
cst_free(aaa); |
| 323 |
} |
| 324 |
else if ((p=strchr(numstring,'.')) != 0) |
| 325 |
{ |
| 326 |
aaa = cst_strdup(numstring); |
| 327 |
aaa[cst_strlen(numstring)-cst_strlen(p)] = '\0'; |
| 328 |
r = val_append(en_exp_number(aaa), |
| 329 |
cons_val(string_val("point"), |
| 330 |
en_exp_digits(p+1))); |
| 331 |
cst_free(aaa); |
| 332 |
} |
| 333 |
else |
| 334 |
r = en_exp_number(numstring); /* I don't think you can get here */ |
| 335 |
|
| 336 |
return r; |
| 337 |
} |
| 338 |
|
| 339 |
cst_val *en_exp_digits(const char *numstring) |
| 340 |
{ |
| 341 |
/* Expand given token to list of words pronouncing it as digits */ |
| 342 |
cst_val *d = 0; |
| 343 |
const char *p; |
| 344 |
|
| 345 |
for (p=numstring; *p; p++) |
| 346 |
{ |
| 347 |
if ((*p >= '0') && (*p <= '9')) |
| 348 |
d = cons_val(string_val(digit2num[*p-'0']),d); |
| 349 |
else |
| 350 |
d = cons_val(string_val("umpty"),d); |
| 351 |
} |
| 352 |
|
| 353 |
return val_reverse(d); |
| 354 |
} |
| 355 |
|
| 356 |
cst_val *en_exp_letters(const char *lets) |
| 357 |
{ |
| 358 |
/* returns these as list of single char symbols */ |
| 359 |
char *aaa; |
| 360 |
cst_val *r; |
| 361 |
int i; |
| 362 |
|
| 363 |
aaa = cst_alloc(char,2); |
| 364 |
aaa[1] = '\0'; |
| 365 |
for (r=0,i=0; lets[i] != '\0'; i++) |
| 366 |
{ |
| 367 |
aaa[0] = lets[i]; |
| 368 |
if (isupper((int)aaa[0])) |
| 369 |
aaa[0] = tolower((int)aaa[0]); |
| 370 |
if (strchr("0123456789",aaa[0])) |
| 371 |
r = cons_val(string_val(digit2num[aaa[0]-'0']),r); |
| 372 |
else if (cst_streq(aaa,"a")) |
| 373 |
r = cons_val(string_val("_a"),r); |
| 374 |
else |
| 375 |
r = cons_val(string_val(aaa),r); |
| 376 |
} |
| 377 |
cst_free(aaa); |
| 378 |
|
| 379 |
return val_reverse(r); |
| 380 |
} |
| 381 |
|
| 382 |
int en_exp_roman(const char *roman) |
| 383 |
{ |
| 384 |
int val; |
| 385 |
const char *p; |
| 386 |
val = 0; |
| 387 |
|
| 388 |
for (p=roman; *p != 0; p++) |
| 389 |
{ |
| 390 |
if (*p == 'X') |
| 391 |
val += 10; |
| 392 |
else if (*p == 'V') |
| 393 |
val += 5; |
| 394 |
else if (*p == 'I') |
| 395 |
{ |
| 396 |
if (p[1] == 'V') |
| 397 |
{ |
| 398 |
val += 4; |
| 399 |
p++; |
| 400 |
} |
| 401 |
else if (p[1] == 'X') |
| 402 |
{ |
| 403 |
val += 9; |
| 404 |
p++; |
| 405 |
} |
| 406 |
else |
| 407 |
val += 1; |
| 408 |
} |
| 409 |
} |
| 410 |
return val; |
| 411 |
} |
| 412 |
|
| 413 |
|