Debian Codegraph @

external/espeak/numbers.cpp

raw
1 /*
2 * Copyright (C) 2005 to 2015 by Jonathan Duddington
3 * email: [email protected]
4 * Copyright (C) 2015-2016, 2020 Reece H. Dunn
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see: <http://www.gnu.org/licenses/>.
18 */
19
20 #include "espeak__config.h"
21
22 #include <ctype.h>
23 #include <stdbool.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "wctype_portable.h"
29 #include <errno.h>
30 #include <limits.h>
31
32 #include "espeak_ng.h"
33 #include "speak_lib.h"
34 #include "encoding.h"
35
36 #include "numbers.h"
37 #include "common.h"
38 #include "dictionary.h" // for Lookup, TranslateRules, EncodePhonemes, Look...
39 #include "phoneme.h" // for phonSWITCH, PHONEME_TAB, phonEND_WORD, phonP...
40 #include "readclause.h" // for WordToString2
41 #include "synthdata.h" // for SelectPhonemeTable
42 #include "synthesize.h" // for phoneme_tab
43 #include "translate.h" // for Translator, LANGUAGE_OPTIONS, WOR...
44 #include "voice.h" // for voice, voice_t
45
46 #define M_LIGATURE 0x8000
47 #define M_NAME 0
48 #define M_SMALLCAP 1
49 #define M_TURNED 2
50 #define M_REVERSED 3
51 #define M_CURL 4
52
53 #define M_ACUTE 5
54 #define M_BREVE 6
55 #define M_CARON 7
56 #define M_CEDILLA 8
57 #define M_CIRCUMFLEX 9
58 #define M_DIAERESIS 10
59 #define M_DOUBLE_ACUTE 11
60 #define M_DOT_ABOVE 12
61 #define M_GRAVE 13
62 #define M_MACRON 14
63 #define M_OGONEK 15
64 #define M_RING 16
65 #define M_STROKE 17
66 #define M_TILDE 18
67
68 #define M_BAR 19
69 #define M_RETROFLEX 20
70 #define M_HOOK 21
71
72 #define M_MIDDLE_DOT M_DOT_ABOVE // duplicate of M_DOT_ABOVE
73 #define M_IMPLOSIVE M_HOOK
74
75 static int n_digit_lookup;
76 static char *digit_lookup;
77 static int speak_missing_thousands;
78 static int number_control;
79
80 typedef struct {
81 const char *name;
82 int accent_flags; // bit 0, say before the letter name
83 } ACCENTS;
84
85 // these are tokens to look up in the *_list file.
86 static const ACCENTS accents_tab[] = {
87 { "_lig", 1 },
88 { "_smc", 0 }, // smallcap
89 { "_tur", 0 }, // turned
90 { "_rev", 0 }, // reversed
91 { "_crl", 0 }, // curl
92
93 { "_acu", 0 }, // acute
94 { "_brv", 0 }, // breve
95 { "_hac", 0 }, // caron/hacek
96 { "_ced", 0 }, // cedilla
97 { "_cir", 0 }, // circumflex
98 { "_dia", 0 }, // diaeresis
99 { "_ac2", 0 }, // double acute
100 { "_dot", 0 }, // dot
101 { "_grv", 0 }, // grave
102 { "_mcn", 0 }, // macron
103 { "_ogo", 0 }, // ogonek
104 { "_rng", 0 }, // ring
105 { "_stk", 0 }, // stroke
106 { "_tld", 0 }, // tilde
107
108 { "_bar", 0 }, // bar
109 { "_rfx", 0 }, // retroflex
110 { "_hok", 0 }, // hook
111 };
112
113 #define CAPITAL 0
114 #define LETTER(ch, mod1, mod2) (ch-59)+(mod1 << 6)+(mod2 << 11)
115 #define LIGATURE(ch1, ch2, mod1) (ch1-59)+((ch2-59) << 6)+(mod1 << 12)+M_LIGATURE
116
117 #define L_ALPHA 60 // U+3B1
118 #define L_SCHWA 61 // U+259
119 #define L_OPEN_E 62 // U+25B
120 #define L_GAMMA 63 // U+3B3
121 #define L_IOTA 64 // U+3B9
122
123 #define L_PHI 67 // U+3C6
124 #define L_ESH 68 // U+283
125 #define L_UPSILON 69 // U+3C5
126 #define L_EZH 70 // U+292
127 #define L_GLOTTAL 71 // U+294
128 #define L_RTAP 72 // U+27E
129 #define L_RLONG 73 // U+27C
130
131 static const short non_ascii_tab[] = {
132 0,
133 0x3b1, 0x259, 0x25b, 0x3b3, 0x3b9, 0x153, 0x3c9,
134 0x3c6, 0x283, 0x3c5, 0x292, 0x294, 0x27e, 0x27c
135 };
136
137 // characters U+00e0 to U+017f
138 static const unsigned short letter_accents_0e0[] = {
139 LETTER('a', M_GRAVE, 0), // U+00e0
140 LETTER('a', M_ACUTE, 0),
141 LETTER('a', M_CIRCUMFLEX, 0),
142 LETTER('a', M_TILDE, 0),
143 LETTER('a', M_DIAERESIS, 0),
144 LETTER('a', M_RING, 0),
145 LIGATURE('a', 'e', 0),
146 LETTER('c', M_CEDILLA, 0),
147 LETTER('e', M_GRAVE, 0),
148 LETTER('e', M_ACUTE, 0),
149 LETTER('e', M_CIRCUMFLEX, 0),
150 LETTER('e', M_DIAERESIS, 0),
151 LETTER('i', M_GRAVE, 0),
152 LETTER('i', M_ACUTE, 0),
153 LETTER('i', M_CIRCUMFLEX, 0),
154 LETTER('i', M_DIAERESIS, 0),
155 LETTER('d', M_NAME, 0), // eth U+00f0
156 LETTER('n', M_TILDE, 0),
157 LETTER('o', M_GRAVE, 0),
158 LETTER('o', M_ACUTE, 0),
159 LETTER('o', M_CIRCUMFLEX, 0),
160 LETTER('o', M_TILDE, 0),
161 LETTER('o', M_DIAERESIS, 0),
162 0, // division sign
163 LETTER('o', M_STROKE, 0),
164 LETTER('u', M_GRAVE, 0),
165 LETTER('u', M_ACUTE, 0),
166 LETTER('u', M_CIRCUMFLEX, 0),
167 LETTER('u', M_DIAERESIS, 0),
168 LETTER('y', M_ACUTE, 0),
169 LETTER('t', M_NAME, 0), // thorn
170 LETTER('y', M_DIAERESIS, 0),
171 CAPITAL, // U+0100
172 LETTER('a', M_MACRON, 0),
173 CAPITAL,
174 LETTER('a', M_BREVE, 0),
175 CAPITAL,
176 LETTER('a', M_OGONEK, 0),
177 CAPITAL,
178 LETTER('c', M_ACUTE, 0),
179 CAPITAL,
180 LETTER('c', M_CIRCUMFLEX, 0),
181 CAPITAL,
182 LETTER('c', M_DOT_ABOVE, 0),
183 CAPITAL,
184 LETTER('c', M_CARON, 0),
185 CAPITAL,
186 LETTER('d', M_CARON, 0),
187 CAPITAL, // U+0110
188 LETTER('d', M_STROKE, 0),
189 CAPITAL,
190 LETTER('e', M_MACRON, 0),
191 CAPITAL,
192 LETTER('e', M_BREVE, 0),
193 CAPITAL,
194 LETTER('e', M_DOT_ABOVE, 0),
195 CAPITAL,
196 LETTER('e', M_OGONEK, 0),
197 CAPITAL,
198 LETTER('e', M_CARON, 0),
199 CAPITAL,
200 LETTER('g', M_CIRCUMFLEX, 0),
201 CAPITAL,
202 LETTER('g', M_BREVE, 0),
203 CAPITAL, // U+0120
204 LETTER('g', M_DOT_ABOVE, 0),
205 CAPITAL,
206 LETTER('g', M_CEDILLA, 0),
207 CAPITAL,
208 LETTER('h', M_CIRCUMFLEX, 0),
209 CAPITAL,
210 LETTER('h', M_STROKE, 0),
211 CAPITAL,
212 LETTER('i', M_TILDE, 0),
213 CAPITAL,
214 LETTER('i', M_MACRON, 0),
215 CAPITAL,
216 LETTER('i', M_BREVE, 0),
217 CAPITAL,
218 LETTER('i', M_OGONEK, 0),
219 CAPITAL, // U+0130
220 LETTER('i', M_NAME, 0), // dotless i
221 CAPITAL,
222 LIGATURE('i', 'j', 0),
223 CAPITAL,
224 LETTER('j', M_CIRCUMFLEX, 0),
225 CAPITAL,
226 LETTER('k', M_CEDILLA, 0),
227 LETTER('k', M_NAME, 0), // kra
228 CAPITAL,
229 LETTER('l', M_ACUTE, 0),
230 CAPITAL,
231 LETTER('l', M_CEDILLA, 0),
232 CAPITAL,
233 LETTER('l', M_CARON, 0),
234 CAPITAL,
235 LETTER('l', M_MIDDLE_DOT, 0), // U+0140
236 CAPITAL,
237 LETTER('l', M_STROKE, 0),
238 CAPITAL,
239 LETTER('n', M_ACUTE, 0),
240 CAPITAL,
241 LETTER('n', M_CEDILLA, 0),
242 CAPITAL,
243 LETTER('n', M_CARON, 0),
244 LETTER('n', M_NAME, 0), // apostrophe n
245 CAPITAL,
246 LETTER('n', M_NAME, 0), // eng
247 CAPITAL,
248 LETTER('o', M_MACRON, 0),
249 CAPITAL,
250 LETTER('o', M_BREVE, 0),
251 CAPITAL, // U+0150
252 LETTER('o', M_DOUBLE_ACUTE, 0),
253 CAPITAL,
254 LIGATURE('o', 'e', 0),
255 CAPITAL,
256 LETTER('r', M_ACUTE, 0),
257 CAPITAL,
258 LETTER('r', M_CEDILLA, 0),
259 CAPITAL,
260 LETTER('r', M_CARON, 0),
261 CAPITAL,
262 LETTER('s', M_ACUTE, 0),
263 CAPITAL,
264 LETTER('s', M_CIRCUMFLEX, 0),
265 CAPITAL,
266 LETTER('s', M_CEDILLA, 0),
267 CAPITAL, // U+0160
268 LETTER('s', M_CARON, 0),
269 CAPITAL,
270 LETTER('t', M_CEDILLA, 0),
271 CAPITAL,
272 LETTER('t', M_CARON, 0),
273 CAPITAL,
274 LETTER('t', M_STROKE, 0),
275 CAPITAL,
276 LETTER('u', M_TILDE, 0),
277 CAPITAL,
278 LETTER('u', M_MACRON, 0),
279 CAPITAL,
280 LETTER('u', M_BREVE, 0),
281 CAPITAL,
282 LETTER('u', M_RING, 0),
283 CAPITAL, // U+0170
284 LETTER('u', M_DOUBLE_ACUTE, 0),
285 CAPITAL,
286 LETTER('u', M_OGONEK, 0),
287 CAPITAL,
288 LETTER('w', M_CIRCUMFLEX, 0),
289 CAPITAL,
290 LETTER('y', M_CIRCUMFLEX, 0),
291 CAPITAL, // Y-DIAERESIS
292 CAPITAL,
293 LETTER('z', M_ACUTE, 0),
294 CAPITAL,
295 LETTER('z', M_DOT_ABOVE, 0),
296 CAPITAL,
297 LETTER('z', M_CARON, 0),
298 LETTER('s', M_NAME, 0), // long-s U+17f
299 };
300
301 // characters U+0250 to U+029F
302 static const unsigned short letter_accents_250[] = {
303 LETTER('a', M_TURNED, 0), // U+250
304 LETTER(L_ALPHA, 0, 0),
305 LETTER(L_ALPHA, M_TURNED, 0),
306 LETTER('b', M_IMPLOSIVE, 0),
307 0, // open-o
308 LETTER('c', M_CURL, 0),
309 LETTER('d', M_RETROFLEX, 0),
310 LETTER('d', M_IMPLOSIVE, 0),
311 LETTER('e', M_REVERSED, 0), // U+258
312 0, // schwa
313 LETTER(L_SCHWA, M_HOOK, 0),
314 0, // open-e
315 LETTER(L_OPEN_E, M_REVERSED, 0),
316 LETTER(L_OPEN_E, M_HOOK, M_REVERSED),
317 0,
318 LETTER('j', M_BAR, 0),
319 LETTER('g', M_IMPLOSIVE, 0), // U+260
320 LETTER('g', 0, 0),
321 LETTER('g', M_SMALLCAP, 0),
322 LETTER(L_GAMMA, 0, 0),
323 0, // ramshorn
324 LETTER('h', M_TURNED, 0),
325 LETTER('h', M_HOOK, 0),
326 0,
327 LETTER('i', M_BAR, 0), // U+268
328 LETTER(L_IOTA, 0, 0),
329 LETTER('i', M_SMALLCAP, 0),
330 LETTER('l', M_TILDE, 0),
331 LETTER('l', M_BAR, 0),
332 LETTER('l', M_RETROFLEX, 0),
333 LIGATURE('l', 'z', 0),
334 LETTER('m', M_TURNED, 0),
335 0,
336 LETTER('m', M_HOOK, 0),
337 0,
338 LETTER('n', M_RETROFLEX, 0),
339 LETTER('n', M_SMALLCAP, 0),
340 LETTER('o', M_BAR, 0),
341 LIGATURE('o', 'e', M_SMALLCAP),
342 0,
343 LETTER(L_PHI, 0, 0), // U+278
344 LETTER('r', M_TURNED, 0),
345 LETTER(L_RLONG, M_TURNED, 0),
346 LETTER('r', M_RETROFLEX, M_TURNED),
347 0,
348 LETTER('r', M_RETROFLEX, 0),
349 0, // r-tap
350 LETTER(L_RTAP, M_REVERSED, 0),
351 LETTER('r', M_SMALLCAP, 0), // U+280
352 LETTER('r', M_TURNED, M_SMALLCAP),
353 LETTER('s', M_RETROFLEX, 0),
354 0, // esh
355 LETTER('j', M_HOOK, 0),
356 LETTER(L_ESH, M_REVERSED, 0),
357 LETTER(L_ESH, M_CURL, 0),
358 LETTER('t', M_TURNED, 0),
359 LETTER('t', M_RETROFLEX, 0), // U+288
360 LETTER('u', M_BAR, 0),
361 LETTER(L_UPSILON, 0, 0),
362 LETTER('v', M_HOOK, 0),
363 LETTER('v', M_TURNED, 0),
364 LETTER('w', M_TURNED, 0),
365 LETTER('y', M_TURNED, 0),
366 LETTER('y', M_SMALLCAP, 0),
367 LETTER('z', M_RETROFLEX, 0), // U+290
368 LETTER('z', M_CURL, 0),
369 0, // ezh
370 LETTER(L_EZH, M_CURL, 0),
371 0, // glottal stop
372 LETTER(L_GLOTTAL, M_REVERSED, 0),
373 LETTER(L_GLOTTAL, M_TURNED, 0),
374 0,
375 0, // bilabial click U+298
376 LETTER('b', M_SMALLCAP, 0),
377 0,
378 LETTER('g', M_IMPLOSIVE, M_SMALLCAP),
379 LETTER('h', M_SMALLCAP, 0),
380 LETTER('j', M_CURL, 0),
381 LETTER('k', M_TURNED, 0),
382 LETTER('l', M_SMALLCAP, 0),
383 LETTER('q', M_HOOK, 0), // U+2a0
384 LETTER(L_GLOTTAL, M_STROKE, 0),
385 LETTER(L_GLOTTAL, M_STROKE, M_REVERSED),
386 LIGATURE('d', 'z', 0),
387 0, // dezh
388 LIGATURE('d', 'z', M_CURL),
389 LIGATURE('t', 's', 0),
390 0, // tesh
391 LIGATURE('t', 's', M_CURL),
392 };
393
394 static int LookupLetter2(Translator *tr, unsigned int letter, char *ph_buf)
395 {
396 int len;
397 char single_letter[10];
398
399 single_letter[0] = 0;
400 single_letter[1] = '_';
401 len = utf8_out(letter, &single_letter[2]);
402 single_letter[len+2] = ' ';
403 single_letter[len+3] = 0;
404
405 if (Lookup(tr, &single_letter[1], ph_buf) == 0) {
406 single_letter[1] = ' ';
407 if (Lookup(tr, &single_letter[2], ph_buf) == 0)
408 TranslateRules(tr, &single_letter[2], ph_buf, 20, NULL, 0, NULL);
409 }
410 return ph_buf[0];
411 }
412
413 void LookupAccentedLetter(Translator *tr, unsigned int letter, char *ph_buf)
414 {
415 // lookup the character in the accents table
416 int accent_data = 0;
417 int accent1 = 0;
418 int accent2 = 0;
419 int flags1, flags2;
420 int basic_letter;
421 int letter2 = 0;
422 char ph_letter1[30];
423 char ph_letter2[30];
424 char ph_accent1[30];
425 char ph_accent2[30];
426
427 ph_accent2[0] = 0;
428
429 if ((letter >= 0xe0) && (letter < 0x17f))
430 accent_data = letter_accents_0e0[letter - 0xe0];
431 else if ((letter >= 0x250) && (letter <= 0x2a8))
432 accent_data = letter_accents_250[letter - 0x250];
433
434 if (accent_data != 0) {
435 basic_letter = (accent_data & 0x3f) + 59;
436 if (basic_letter < 'a')
437 basic_letter = non_ascii_tab[basic_letter-59];
438
439 if (accent_data & M_LIGATURE) {
440 letter2 = (accent_data >> 6) & 0x3f;
441 letter2 += 59;
442 accent2 = (accent_data >> 12) & 0x7;
443 } else {
444 accent1 = (accent_data >> 6) & 0x1f;
445 accent2 = (accent_data >> 11) & 0xf;
446 }
447
448 if ((accent1 == 0) && !(accent_data & M_LIGATURE)) {
449 // just a letter name, not an accented character or ligature
450 return;
451 }
452
453 if ((flags1 = Lookup(tr, accents_tab[accent1].name, ph_accent1)) != 0) {
454 if (LookupLetter2(tr, basic_letter, ph_letter1) != 0) {
455 if (accent2 != 0) {
456 flags2 = Lookup(tr, accents_tab[accent2].name, ph_accent2);
457 if (flags2 & FLAG_ACCENT_BEFORE) {
458 strcpy(ph_buf, ph_accent2);
459 ph_buf += strlen(ph_buf);
460 ph_accent2[0] = 0;
461 }
462 }
463 if (letter2 != 0) {
464 // ligature
465 LookupLetter2(tr, letter2, ph_letter2);
466 sprintf(ph_buf, "%s%c%s%c%s%s", ph_accent1, phonPAUSE_VSHORT, ph_letter1, phonSTRESS_P, ph_letter2, ph_accent2);
467 } else {
468 if (accent1 == 0)
469 strcpy(ph_buf, ph_letter1);
470 else if ((tr->langopts.accents & 1) || (flags1 & FLAG_ACCENT_BEFORE) || (accents_tab[accent1].accent_flags & 1))
471 sprintf(ph_buf, "%s%c%c%s", ph_accent1, phonPAUSE_VSHORT, phonSTRESS_P, ph_letter1);
472 else
473 sprintf(ph_buf, "%c%s%c%s%c", phonSTRESS_2, ph_letter1, phonPAUSE_VSHORT, ph_accent1, phonPAUSE_VSHORT);
474 }
475 }
476 }
477 }
478 }
479
480 void LookupLetter(Translator *tr, unsigned int letter, int next_byte, char *ph_buf1, int control)
481 {
482 // control, bit 0: not the first letter of a word
483
484 int len;
485 char single_letter[10] = { 0, 0 };
486 unsigned int dict_flags[2];
487 char ph_buf3[40];
488
489 ph_buf1[0] = 0;
490 len = utf8_out(letter, &single_letter[2]);
491 single_letter[len+2] = ' ';
492
493 if (next_byte == -1) {
494 // speaking normal text, not individual characters
495 if (Lookup(tr, &single_letter[2], ph_buf1) != 0)
496 return;
497
498 single_letter[1] = '_';
499 if (Lookup(tr, &single_letter[1], ph_buf3) != 0)
500 return; // the character is specified as _* so ignore it when speaking normal text
501
502 // check whether this character is specified for English
503 if (tr->translator_name == L('e', 'n'))
504 return; // we are already using English
505
506 SetTranslator3(ESPEAKNG_DEFAULT_VOICE);
507 if (Lookup(translator3, &single_letter[2], ph_buf3) != 0) {
508 // yes, switch to English and re-translate the word
509 sprintf(ph_buf1, "%c", phonSWITCH);
510 }
511 SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table
512 return;
513 }
514
515 if ((letter <= 32) || iswspace_portable(letter)) {
516 // lookup space as _&32 etc.
517 sprintf(&single_letter[1], "_#%d ", letter);
518 Lookup(tr, &single_letter[1], ph_buf1);
519 return;
520 }
521
522 if (next_byte != ' ')
523 next_byte = RULE_SPELLING;
524 single_letter[3+len] = next_byte; // follow by space-space if the end of the word, or space-31
525
526 single_letter[1] = '_';
527
528 // if the $accent flag is set for this letter, use the accents table (below)
529 dict_flags[1] = 0;
530
531 if (Lookup(tr, &single_letter[1], ph_buf3) == 0) {
532 single_letter[1] = ' ';
533 if (Lookup(tr, &single_letter[2], ph_buf3) == 0)
534 TranslateRules(tr, &single_letter[2], ph_buf3, sizeof(ph_buf3), NULL, FLAG_NO_TRACE, NULL);
535 }
536
537 if (ph_buf3[0] == 0)
538 LookupAccentedLetter(tr, letter, ph_buf3);
539
540 strcpy(ph_buf1, ph_buf3);
541 if ((ph_buf1[0] == 0) || (ph_buf1[0] == phonSWITCH))
542 return;
543
544 dict_flags[0] = 0;
545 dict_flags[1] = 0;
546 SetWordStress(tr, ph_buf1, dict_flags, -1, control & 1);
547 }
548
549
550 #define L_SUB 0x4000 // subscript
551 #define L_SUP 0x8000 // superscript
552
553
554
555 // this list must be in ascending order
556 static const unsigned short derived_letters[] = {
557 0x00aa, 'a'+L_SUP,
558 0x00b2, '2'+L_SUP,
559 0x00b3, '3'+L_SUP,
560 0x00b9, '1'+L_SUP,
561 0x00ba, 'o'+L_SUP,
562 0x02b0, 'h'+L_SUP,
563 0x02b1, 0x266+L_SUP,
564 0x02b2, 'j'+L_SUP,
565 0x02b3, 'r'+L_SUP,
566 0x02b4, 0x279+L_SUP,
567 0x02b5, 0x27b+L_SUP,
568 0x02b6, 0x281+L_SUP,
569 0x02b7, 'w'+L_SUP,
570 0x02b8, 'y'+L_SUP,
571 0x02c0, 0x294+L_SUP,
572 0x02c1, 0x295+L_SUP,
573 0x02e0, 0x263+L_SUP,
574 0x02e1, 'l'+L_SUP,
575 0x02e2, 's'+L_SUP,
576 0x02e3, 'x'+L_SUP,
577 0x2070, '0'+L_SUP,
578 0x2071, 'i'+L_SUP,
579 0x2074, '4'+L_SUP,
580 0x2075, '5'+L_SUP,
581 0x2076, '6'+L_SUP,
582 0x2077, '7'+L_SUP,
583 0x2078, '8'+L_SUP,
584 0x2079, '9'+L_SUP,
585 0x207a, '+'+L_SUP,
586 0x207b, '-'+L_SUP,
587 0x207c, '='+L_SUP,
588 0x207d, '('+L_SUP,
589 0x207e, ')'+L_SUP,
590 0x207f, 'n'+L_SUP,
591 0x2080, '0'+L_SUB,
592 0x2081, '1'+L_SUB,
593 0x2082, '2'+L_SUB,
594 0x2083, '3'+L_SUB,
595 0x2084, '4'+L_SUB,
596 0x2085, '5'+L_SUB,
597 0x2086, '6'+L_SUB,
598 0x2087, '7'+L_SUB,
599 0x2088, '8'+L_SUB,
600 0x2089, '9'+L_SUB,
601 0x208a, '+'+L_SUB,
602 0x208b, '-'+L_SUB,
603 0x208c, '='+L_SUB,
604 0x208d, '('+L_SUB,
605 0x208e, ')'+L_SUB,
606 0x2090, 'a'+L_SUB,
607 0x2091, 'e'+L_SUB,
608 0x2092, 'o'+L_SUB,
609 0x2093, 'x'+L_SUB,
610 0x2094, 0x259+L_SUB,
611 0x2095, 'h'+L_SUB,
612 0x2096, 'k'+L_SUB,
613 0x2097, 'l'+L_SUB,
614 0x2098, 'm'+L_SUB,
615 0x2099, 'n'+L_SUB,
616 0x209a, 'p'+L_SUB,
617 0x209b, 's'+L_SUB,
618 0x209c, 't'+L_SUB,
619 0, 0
620 };
621
622
623
624 int IsSuperscript(int letter)
625 {
626 // is this a subscript or superscript letter ?
627 int ix;
628 int c;
629
630 for (ix = 0; (c = derived_letters[ix]) != 0; ix += 2) {
631 if (c > letter)
632 break;
633 if (c == letter)
634 return derived_letters[ix+1];
635 }
636 return 0;
637 }
638
639 void SetSpellingStress(Translator *tr, char *phonemes, int control, int n_chars)
640 {
641 // Individual letter names, reduce the stress of some.
642 int ix;
643 unsigned int c;
644 int n_stress = 0;
645 int prev = 0;
646 int count;
647 unsigned char buf[N_WORD_PHONEMES];
648
649 for (ix = 0; (c = phonemes[ix]) != 0; ix++) {
650 if ((c == phonSTRESS_P) && (prev != phonSWITCH))
651 n_stress++;
652 buf[ix] = prev = c;
653 }
654 buf[ix] = 0;
655
656 count = 0;
657 prev = 0;
658 for (ix = 0; (c = buf[ix]) != 0; ix++) {
659 if ((c == phonSTRESS_P) && (n_chars > 1) && (prev != phonSWITCH)) {
660 count++;
661
662 if (tr->langopts.spelling_stress) {
663 // stress on initial letter when spelling
664 if (count > 1)
665 c = phonSTRESS_3;
666 } else {
667 if (count != n_stress) {
668 if (((count % 3) != 0) || (count == n_stress-1))
669 c = phonSTRESS_3; // reduce to secondary stress
670 }
671 }
672 } else if (c == 0xff) {
673 if ((control < 2) || (ix == 0))
674 continue; // don't insert pauses
675
676 if (((count % 3) == 0) || (control > 2))
677 c = phonPAUSE_NOLINK; // pause following a primary stress
678 else
679 c = phonPAUSE_VSHORT;
680 }
681 *phonemes++ = prev = c;
682 }
683 if (control >= 2)
684 *phonemes++ = phonPAUSE_NOLINK;
685 *phonemes = 0;
686 }
687
688 // Numbers
689
690 static char ph_ordinal2[12];
691 static char ph_ordinal2x[12];
692
693 static int CheckDotOrdinal(Translator *tr, char *word, char *word_end, WORD_TAB *wtab, int roman)
694 {
695 int ordinal = 0;
696 int c2;
697 int nextflags;
698
699 if ((tr->langopts.numbers & NUM_ORDINAL_DOT) && ((word_end[0] == '.') || (wtab[0].flags & FLAG_HAS_DOT)) && !(wtab[1].flags & FLAG_NOSPACE)) {
700 if (roman || !(wtab[1].flags & FLAG_FIRST_UPPER)) {
701 if (word_end[0] == '.')
702 utf8_in(&c2, &word_end[2]);
703 else
704 utf8_in(&c2, &word_end[0]);
705
706 if ((word_end[0] != 0) && (word_end[1] != 0) && ((c2 == 0) || (wtab[0].flags & FLAG_COMMA_AFTER) || IsAlpha(c2))) {
707 // ordinal number is indicated by dot after the number
708 // but not if the next word starts with an upper-case letter
709 // (c2 == 0) is for cases such as, "2.,"
710 ordinal = 2;
711 if (word_end[0] == '.')
712 word_end[0] = ' ';
713
714 if ((roman == 0) && (tr->translator_name == L('h', 'u'))) {
715 // lang=hu don't treat dot as ordinal indicator if the next word is a month name ($alt). It may have a suffix.
716 nextflags = 0;
717 if (IsAlpha(c2))
718 nextflags = TranslateWord(tr, &word_end[2], NULL, NULL);
719
720 if ((tr->prev_dict_flags[0] & FLAG_ALT_TRANS) && ((c2 == 0) || (wtab[0].flags & FLAG_COMMA_AFTER) || iswdigit_portable(c2)))
721 ordinal = 0; // TEST 09.02.10
722
723 if (nextflags & FLAG_ALT_TRANS)
724 ordinal = 0;
725
726 if (nextflags & FLAG_ALT3_TRANS) {
727 if (word[-2] == '-')
728 ordinal = 0; // e.g. december 2-5. között
729
730 if (tr->prev_dict_flags[0] & (FLAG_ALT_TRANS | FLAG_ALT3_TRANS))
731 ordinal = 0x22;
732 }
733 }
734 }
735 }
736 }
737 return ordinal;
738 }
739
740 static int hu_number_e(const char *word, int thousandplex, int value)
741 {
742 // lang-hu: variant form of numbers when followed by hyphen and a suffix starting with 'a' or 'e' (but not a, e, az, ez, azt, ezt, att. ett
743
744 if ((word[0] == 'a') || (word[0] == 'e')) {
745 if ((word[1] == ' ') || (word[1] == 'z') || ((word[1] == 't') && (word[2] == 't')))
746 return 0;
747
748 if (((thousandplex == 1) || ((value % 1000) == 0)) && (word[1] == 'l'))
749 return 0; // 1000-el
750
751 return 1;
752 }
753 return 0;
754 }
755
756 int TranslateRoman(Translator *tr, char *word, char *ph_out, WORD_TAB *wtab)
757 {
758 int c;
759 char *p;
760 const char *p2;
761 int acc;
762 int prev;
763 int value;
764 int subtract;
765 int repeat = 0;
766 char *word_start;
767 int num_control = 0;
768 unsigned int flags[2];
769 char ph_roman[30];
770 char number_chars[N_WORD_BYTES];
771
772 static const char roman_numbers[] = "ixcmvld";
773 static const int roman_values[] = { 1, 10, 100, 1000, 5, 50, 500 };
774
775 acc = 0;
776 prev = 0;
777 subtract = 0x7fff;
778 ph_out[0] = 0;
779 flags[0] = 0;
780 flags[1] = 0;
781
782 if (((tr->langopts.numbers & NUM_ROMAN_CAPITALS) && !(wtab[0].flags & FLAG_ALL_UPPER)) || IsDigit09(word[-2]))
783 return 0; // not '2xx'
784
785 if (word[1] == ' ') {
786 if ((tr->langopts.numbers & (NUM_ROMAN_CAPITALS | NUM_ROMAN_ORDINAL | NUM_ORDINAL_DOT)) && (wtab[0].flags & FLAG_HAS_DOT)) {
787 // allow single letter Roman ordinal followed by dot.
788 } else
789 return 0; // only one letter, don't speak as a Roman Number
790 }
791
792 word_start = word;
793 while ((c = *word++) != ' ') {
794 if ((p2 = strchr(roman_numbers, c)) == NULL)
795 return 0;
796
797 value = roman_values[p2 - roman_numbers];
798 if (value == prev) {
799 repeat++;
800 if (repeat >= 3)
801 return 0;
802 } else
803 repeat = 0;
804
805 if ((prev > 1) && (prev != 10) && (prev != 100)) {
806 if (value >= prev)
807 return 0;
808 }
809 if ((prev != 0) && (prev < value)) {
810 if (((acc % 10) != 0) || ((prev*10) < value))
811 return 0;
812 subtract = prev;
813 value -= subtract;
814 } else if (value >= subtract)
815 return 0;
816 else
817 acc += prev;
818 prev = value;
819 }
820
821 if (IsDigit09(word[0]))
822 return 0; // e.g. 'xx2'
823
824 acc += prev;
825 if (acc < tr->langopts.min_roman)
826 return 0;
827
828 if (acc > tr->langopts.max_roman)
829 return 0;
830
831 Lookup(tr, "_roman", ph_roman); // precede by "roman" if _rom is defined in *_list
832 p = &ph_out[0];
833
834 if ((tr->langopts.numbers & NUM_ROMAN_AFTER) == 0) {
835 strcpy(ph_out, ph_roman);
836 p = &ph_out[strlen(ph_roman)];
837 }
838
839 sprintf(number_chars, " %d %s ", acc, tr->langopts.roman_suffix);
840
841 if (word[0] == '.') {
842 // dot has not been removed. This implies that there was no space after it
843 return 0;
844 }
845
846 if (CheckDotOrdinal(tr, word_start, word, wtab, 1))
847 wtab[0].flags |= FLAG_ORDINAL;
848
849 if (tr->langopts.numbers & NUM_ROMAN_ORDINAL) {
850 if (tr->translator_name == L('h', 'u')) {
851 if (!(wtab[0].flags & FLAG_ORDINAL)) {
852 if ((wtab[0].flags & FLAG_HYPHEN_AFTER) && hu_number_e(word, 0, acc)) {
853 // should use the 'e' form of the number
854 num_control |= 1;
855 } else
856 return 0;
857 }
858 } else
859 wtab[0].flags |= FLAG_ORDINAL;
860 }
861
862 tr->prev_dict_flags[0] = 0;
863 tr->prev_dict_flags[1] = 0;
864 TranslateNumber(tr, &number_chars[2], p, flags, wtab, num_control);
865
866 if (tr->langopts.numbers & NUM_ROMAN_AFTER)
867 strcat(ph_out, ph_roman);
868
869 return 1;
870 }
871
872 static const char *M_Variant(int value)
873 {
874 // returns M, or perhaps MA or MB for some cases
875
876 bool teens = false;
877
878 if (((value % 100) > 10) && ((value % 100) < 20))
879 teens = true;
880
881 switch (translator->langopts.numbers2 & NUM2_THOUSANDS_VAR_BITS)
882 {
883 case NUM2_THOUSANDS_VAR1: // lang=ru
884 if (teens == false) {
885 if ((value % 10) == 1)
886 return "1MA";
887 if (((value % 10) >= 2) && ((value % 10) <= 4))
888 return "0MA";
889 }
890 break;
891 case NUM2_THOUSANDS_VAR2: // lang=cs,sk
892 if ((value >= 2) && (value <= 4))
893 return "0MA";
894 break;
895 case NUM2_THOUSANDS_VAR3: // lang=pl
896 if ((teens == false) && (((value % 10) >= 2) && ((value % 10) <= 4)))
897 return "0MA";
898 break;
899 case NUM2_THOUSANDS_VAR4: // lang=lt
900 if ((teens == true) || ((value % 10) == 0))
901 return "0MB";
902 if ((value % 10) == 1)
903 return "0MA";
904 break;
905 case NUM2_THOUSANDS_VAR5: // lang=bs,hr,sr
906 if (teens == false) {
907 if ((value % 10) == 1)
908 return "1M";
909 if (((value % 10) >= 2) && ((value % 10) <= 4))
910 return "0MA";
911 }
912 break;
913 }
914 return "0M";
915 }
916
917 static int LookupThousands(Translator *tr, int value, int thousandplex, int thousands_exact, char *ph_out)
918 {
919 // thousands_exact: bit 0 no hundreds,tens,or units, bit 1 ordinal numberr
920 int found;
921 int found_value = 0;
922 char string[14];
923 char ph_of[12];
924 char ph_thousands[40];
925 char ph_buf[40];
926
927 ph_of[0] = 0;
928
929 // first look for a match with the exact value of thousands
930 if (value > 0) {
931 if (thousands_exact & 1) {
932 if (thousands_exact & 2) {
933 // ordinal number
934 sprintf(string, "_%dM%do", value, thousandplex);
935 found_value = Lookup(tr, string, ph_thousands);
936 }
937 if (!found_value && (number_control & 1)) {
938 // look for the 'e' variant
939 sprintf(string, "_%dM%de", value, thousandplex);
940 found_value = Lookup(tr, string, ph_thousands);
941 }
942 if (!found_value) {
943 // is there a different pronunciation if there are no hundreds,tens,or units ? (LANG=ta)
944 sprintf(string, "_%dM%dx", value, thousandplex);
945 found_value = Lookup(tr, string, ph_thousands);
946 }
947 }
948 if (found_value == 0) {
949 sprintf(string, "_%dM%d", value, thousandplex);
950 found_value = Lookup(tr, string, ph_thousands);
951 }
952 }
953
954 if (found_value == 0) {
955 if ((value % 100) >= 20)
956 Lookup(tr, "_0of", ph_of);
957
958 found = 0;
959 if (thousands_exact & 1) {
960 if (thousands_exact & 2) {
961 // ordinal number
962 sprintf(string, "_%s%do", M_Variant(value), thousandplex);
963 found = Lookup(tr, string, ph_thousands);
964 }
965 if (!found && (number_control & 1)) {
966 // look for the 'e' variant
967 sprintf(string, "_%s%de", M_Variant(value), thousandplex);
968 found = Lookup(tr, string, ph_thousands);
969 }
970 if (!found) {
971 // is there a different pronunciation if there are no hundreds,tens,or units ?
972 sprintf(string, "_%s%dx", M_Variant(value), thousandplex);
973 found = Lookup(tr, string, ph_thousands);
974 }
975 }
976 if (found == 0) {
977 sprintf(string, "_%s%d", M_Variant(value), thousandplex);
978
979 if (Lookup(tr, string, ph_thousands) == 0) {
980 if (thousandplex > 3) {
981 sprintf(string, "_0M%d", thousandplex-1);
982 if (Lookup(tr, string, ph_buf) == 0) {
983 // say "millions" if this name is not available and neither is the next lower
984 Lookup(tr, "_0M2", ph_thousands);
985 speak_missing_thousands = 3;
986 }
987 }
988 if (ph_thousands[0] == 0) {
989 // repeat "thousand" if higher order names are not available
990 sprintf(string, "_%dM1", value);
991 if ((found_value = Lookup(tr, string, ph_thousands)) == 0)
992 Lookup(tr, "_0M1", ph_thousands);
993 speak_missing_thousands = 2;
994 }
995 }
996 }
997 }
998 sprintf(ph_out, "%s%s", ph_of, ph_thousands);
999
1000 if ((value == 1) && (thousandplex == 1) && (tr->langopts.numbers & NUM_OMIT_1_THOUSAND))
1001 return 1;
1002
1003 return found_value;
1004 }
1005
1006 static int LookupNum2(Translator *tr, int value, int thousandplex, const int control, char *ph_out)
1007 {
1008 // Lookup a 2 digit number
1009 // control bit 0: ordinal number
1010 // control bit 1: final tens and units (not number of thousands) (use special form of '1', LANG=de "eins")
1011 // control bit 2: tens and units only, no higher digits
1012 // control bit 3: use feminine form of '2' (for thousands
1013 // control bit 4: speak zero tens
1014 // control bit 5: variant of ordinal number (lang=hu)
1015 // bit 8 followed by decimal fraction
1016 // bit 9: use #f form for both tens and units (lang=ml)
1017
1018 int found;
1019 int ix;
1020 int units;
1021 int tens;
1022 int is_ordinal;
1023 int used_and = 0;
1024 int found_ordinal = 0;
1025 int next_phtype;
1026 int ord_type = 'o';
1027 char string[12]; // for looking up entries in *_list
1028 char ph_ordinal[20];
1029 char ph_tens[50];
1030 char ph_digits[50];
1031 char ph_and[12];
1032
1033 units = value % 10;
1034 tens = value / 10;
1035
1036 found = 0;
1037 ph_ordinal[0] = 0;
1038 ph_tens[0] = 0;
1039 ph_digits[0] = 0;
1040 ph_and[0] = 0;
1041
1042 if (control & 0x20)
1043 ord_type = 'q';
1044
1045 is_ordinal = control & 1;
1046
1047 if ((control & 2) && (n_digit_lookup == 2)) {
1048 // pronunciation of the final 2 digits has already been found
1049 strcpy(ph_out, digit_lookup);
1050 } else {
1051 if (digit_lookup[0] == 0) {
1052 // is there a special pronunciation for this 2-digit number
1053 if (control & 8) {
1054 // is there a feminine or thousands-variant form?
1055 sprintf(string, "_%dfx", value);
1056 if ((found = Lookup(tr, string, ph_digits)) == 0) {
1057 sprintf(string, "_%df", value);
1058 found = Lookup(tr, string, ph_digits);
1059 }
1060 } else if (is_ordinal) {
1061 strcpy(ph_ordinal, ph_ordinal2);
1062
1063 if (control & 4) {
1064 sprintf(string, "_%d%cx", value, ord_type); // LANG=hu, special word for 1. 2. when there are no higher digits
1065 if ((found = Lookup(tr, string, ph_digits)) != 0) {
1066 if (ph_ordinal2x[0] != 0)
1067 strcpy(ph_ordinal, ph_ordinal2x); // alternate pronunciation (lang=an)
1068 }
1069 }
1070 if (found == 0) {
1071 sprintf(string, "_%d%c", value, ord_type);
1072 found = Lookup(tr, string, ph_digits);
1073 }
1074 found_ordinal = found;
1075 }
1076
1077 if (found == 0) {
1078 if (control & 2) {
1079 // the final tens and units of a number
1080 if (number_control & 1) {
1081 // look for 'e' variant
1082 sprintf(string, "_%de", value);
1083 found = Lookup(tr, string, ph_digits);
1084 }
1085 } else {
1086 // followed by hundreds or thousands etc
1087 if ((tr->langopts.numbers2 & NUM2_ORDINAL_AND_THOUSANDS) && (thousandplex <= 1))
1088 sprintf(string, "_%do", value); // LANG=TA
1089 else
1090 sprintf(string, "_%da", value);
1091 found = Lookup(tr, string, ph_digits);
1092 }
1093
1094 if (!found) {
1095 if ((is_ordinal) && (tr->langopts.numbers2 & NUM2_NO_TEEN_ORDINALS)) {
1096 // don't use numbers 10-99 to make ordinals, always use _1Xo etc (lang=pt)
1097 } else {
1098 sprintf(string, "_%d", value);
1099 found = Lookup(tr, string, ph_digits);
1100 }
1101 }
1102 }
1103 }
1104
1105 // no, speak as tens+units
1106
1107 if ((value < 10) && (control & 0x10)) {
1108 // speak leading zero
1109 Lookup(tr, "_0", ph_tens);
1110 } else {
1111 if (found)
1112 ph_tens[0] = 0;
1113 else {
1114 if (is_ordinal) {
1115 sprintf(string, "_%dX%c", tens, ord_type);
1116 if (Lookup(tr, string, ph_tens) != 0) {
1117 found_ordinal = 1;
1118
1119 if ((units != 0) && (tr->langopts.numbers2 & NUM2_MULTIPLE_ORDINAL)) {
1120 // Use the ordinal form of tens as well as units. Add the ordinal ending
1121 strcat(ph_tens, ph_ordinal2);
1122 }
1123 }
1124 }
1125 if (found_ordinal == 0) {
1126 if (control & 0x200)
1127 sprintf(string, "_%dXf", tens);
1128 else
1129 sprintf(string, "_%dX", tens);
1130 Lookup(tr, string, ph_tens);
1131 }
1132
1133 if ((ph_tens[0] == 0) && (tr->langopts.numbers & NUM_VIGESIMAL)) {
1134 // tens not found, (for example) 73 is 60+13
1135 units = (value % 20);
1136 sprintf(string, "_%dX", tens & 0xfe);
1137 Lookup(tr, string, ph_tens);
1138 }
1139
1140 ph_digits[0] = 0;
1141 if (units > 0) {
1142 found = 0;
1143
1144 if ((control & 2) && (digit_lookup[0] != 0)) {
1145 // we have an entry for this digit (possibly together with the next word)
1146 strcpy(ph_digits, digit_lookup);
1147 found_ordinal = 1;
1148 ph_ordinal[0] = 0;
1149 } else {
1150 if (control & 8) {
1151 // is there a variant form of this number?
1152 sprintf(string, "_%df", units);
1153 found = Lookup(tr, string, ph_digits);
1154 }
1155 if ((is_ordinal) && ((tr->langopts.numbers & NUM_SWAP_TENS) == 0)) {
1156 // ordinal
1157 sprintf(string, "_%d%c", units, ord_type);
1158 if ((found = Lookup(tr, string, ph_digits)) != 0)
1159 found_ordinal = 1;
1160 }
1161 if (found == 0) {
1162 if ((number_control & 1) && (control & 2)) {
1163 // look for 'e' variant
1164 sprintf(string, "_%de", units);
1165 found = Lookup(tr, string, ph_digits);
1166 } else if (((control & 2) == 0) || ((tr->langopts.numbers & NUM_SWAP_TENS) != 0)) {
1167 // followed by hundreds or thousands (or tens)
1168 if ((tr->langopts.numbers2 & NUM2_ORDINAL_AND_THOUSANDS) && (thousandplex <= 1))
1169 sprintf(string, "_%do", units); // LANG=TA, only for 100s, 1000s
1170 else
1171 sprintf(string, "_%da", units);
1172 found = Lookup(tr, string, ph_digits);
1173 }
1174 }
1175 if (found == 0) {
1176 sprintf(string, "_%d", units);
1177 Lookup(tr, string, ph_digits);
1178 }
1179 }
1180 }
1181 }
1182 }
1183
1184 if ((is_ordinal) && (found_ordinal == 0) && (ph_ordinal[0] == 0)) {
1185 if ((value >= 20) && (((value % 10) == 0) || (tr->langopts.numbers & NUM_SWAP_TENS)))
1186 Lookup(tr, "_ord20", ph_ordinal);
1187 if (ph_ordinal[0] == 0)
1188 Lookup(tr, "_ord", ph_ordinal);
1189 }
1190
1191 if ((tr->langopts.numbers & (NUM_SWAP_TENS | NUM_AND_UNITS)) && (ph_tens[0] != 0) && (ph_digits[0] != 0)) {
1192 Lookup(tr, "_0and", ph_and);
1193
1194 if ((is_ordinal) && (tr->langopts.numbers2 & NUM2_ORDINAL_NO_AND))
1195 ph_and[0] = 0;
1196
1197 if (tr->langopts.numbers & NUM_SWAP_TENS)
1198 sprintf(ph_out, "%s%s%s%s", ph_digits, ph_and, ph_tens, ph_ordinal);
1199 else
1200 sprintf(ph_out, "%s%s%s%s", ph_tens, ph_and, ph_digits, ph_ordinal);
1201 used_and = 1;
1202 } else {
1203 if (tr->langopts.numbers & NUM_SINGLE_VOWEL) {
1204 // remove vowel from the end of tens if units starts with a vowel (LANG=Italian)
1205 if (((ix = strlen(ph_tens)-1) >= 0) && (ph_digits[0] != 0)) {
1206 if ((next_phtype = phoneme_tab[(unsigned int)(ph_digits[0])]->type) == phSTRESS)
1207 next_phtype = phoneme_tab[(unsigned int)(ph_digits[1])]->type;
1208
1209 if ((phoneme_tab[(unsigned int)(ph_tens[ix])]->type == phVOWEL) && (next_phtype == phVOWEL))
1210 ph_tens[ix] = 0;
1211 }
1212 }
1213
1214 if ((tr->langopts.numbers2 & NUM2_ORDINAL_DROP_VOWEL) && (ph_ordinal[0] != 0)) {
1215 ix = sprintf(ph_out, "%s%s", ph_tens, ph_digits);
1216 if ((ix > 0) && (phoneme_tab[(unsigned char)(ph_out[ix-1])]->type == phVOWEL))
1217 ix--;
1218 sprintf(&ph_out[ix], "%s", ph_ordinal);
1219 } else
1220 sprintf(ph_out, "%s%s%s", ph_tens, ph_digits, ph_ordinal);
1221 }
1222 }
1223
1224 if (tr->langopts.numbers & NUM_SINGLE_STRESS_L) {
1225 // only one primary stress, on the first part (tens)
1226 found = 0;
1227 for (ix = 0; ix < (signed)strlen(ph_out); ix++) {
1228 if (ph_out[ix] == phonSTRESS_P) {
1229 if (found)
1230 ph_out[ix] = phonSTRESS_3;
1231 else
1232 found = 1;
1233 }
1234 }
1235 } else if (tr->langopts.numbers & NUM_SINGLE_STRESS) {
1236 // only one primary stress
1237 found = 0;
1238 for (ix = strlen(ph_out)-1; ix >= 0; ix--) {
1239 if (ph_out[ix] == phonSTRESS_P) {
1240 if (found)
1241 ph_out[ix] = phonSTRESS_3;
1242 else
1243 found = 1;
1244 }
1245 }
1246 }
1247 return used_and;
1248 }
1249
1250 static int LookupNum3(Translator *tr, int value, char *ph_out, bool suppress_null, int thousandplex, int control)
1251 {
1252 // Translate a 3 digit number
1253 // control bit 0, previous thousands
1254 // bit 1, ordinal number
1255 // bit 5 variant form of ordinal number
1256 // bit 8 followed by decimal fraction
1257
1258 int found;
1259 int hundreds;
1260 int tensunits;
1261 int x;
1262 int ix;
1263 int exact;
1264 int ordinal;
1265 int tplex;
1266 bool say_zero_hundred = false;
1267 bool say_one_hundred;
1268 char string[12]; // for looking up entries in **_list
1269 char buf1[100];
1270 char buf2[100];
1271 char ph_100[20];
1272 char ph_10T[20];
1273 char ph_digits[50];
1274 char ph_thousands[50];
1275 char ph_hundred_and[12];
1276 char ph_thousand_and[12];
1277
1278 ordinal = control & 0x22;
1279 hundreds = value / 100;
1280 tensunits = value % 100;
1281 buf1[0] = 0;
1282
1283 ph_thousands[0] = 0;
1284 ph_thousand_and[0] = 0;
1285
1286 if ((tr->langopts.numbers & NUM_ZERO_HUNDRED) && ((control & 1) || (hundreds >= 10)))
1287 say_zero_hundred = true; // lang=vi
1288
1289 if ((hundreds > 0) || say_zero_hundred) {
1290 found = 0;
1291 if (ordinal && (tensunits == 0)) {
1292 // ordinal number, with no tens or units
1293 found = Lookup(tr, "_0Co", ph_100);
1294 }
1295 if (found == 0) {
1296 if (tensunits == 0) {
1297 // special form for exact hundreds?
1298 found = Lookup(tr, "_0C0", ph_100);
1299 }
1300 if (!found)
1301 Lookup(tr, "_0C", ph_100);
1302 }
1303
1304 if (((tr->langopts.numbers & NUM_1900) != 0) && (hundreds == 19)) {
1305 // speak numbers such as 1984 as years: nineteen-eighty-four
1306 } else if (hundreds >= 10) {
1307 ph_digits[0] = 0;
1308
1309 exact = 0;
1310 if ((value % 1000) == 0)
1311 exact = 1;
1312
1313 tplex = thousandplex+1;
1314 if (tr->langopts.numbers2 & NUM2_MYRIADS)
1315 tplex = 0;
1316
1317 if (LookupThousands(tr, hundreds / 10, tplex, exact | ordinal, ph_10T) == 0) {
1318 x = 0;
1319 if (tr->langopts.numbers2 & (1 << tplex) && tplex <= 3)
1320 x = 8; // use variant (feminine) for before thousands and millions
1321 if (tr->translator_name == L('m', 'l'))
1322 x = 0x208;
1323 LookupNum2(tr, hundreds/10, thousandplex, x, ph_digits);
1324 }
1325
1326 if (tr->langopts.numbers2 & NUM2_SWAP_THOUSANDS)
1327 sprintf(ph_thousands, "%s%c%s%c", ph_10T, phonEND_WORD, ph_digits, phonEND_WORD);
1328 else
1329 sprintf(ph_thousands, "%s%c%s%c", ph_digits, phonEND_WORD, ph_10T, phonEND_WORD);
1330
1331 hundreds %= 10;
1332 if ((hundreds == 0) && (say_zero_hundred == false))
1333 ph_100[0] = 0;
1334 suppress_null = true;
1335 control |= 1;
1336 }
1337
1338 ph_digits[0] = 0;
1339
1340 if ((hundreds > 0) || say_zero_hundred) {
1341 if ((tr->langopts.numbers & NUM_AND_HUNDRED) && ((control & 1) || (ph_thousands[0] != 0)))
1342 Lookup(tr, "_0and", ph_thousand_and);
1343
1344 suppress_null = true;
1345
1346 found = 0;
1347 if ((ordinal)
1348 && ((tensunits == 0) || (tr->langopts.numbers2 & NUM2_MULTIPLE_ORDINAL))) {
1349 // ordinal number
1350 sprintf(string, "_%dCo", hundreds);
1351 found = Lookup(tr, string, ph_digits);
1352
1353 if ((tr->langopts.numbers2 & NUM2_MULTIPLE_ORDINAL) && (tensunits > 0)) {
1354 // Use ordinal form of hundreds, as well as for tens and units
1355 // Add ordinal suffix to the hundreds
1356 strcat(ph_digits, ph_ordinal2);
1357 }
1358 }
1359
1360 if ((hundreds == 0) && say_zero_hundred)
1361 Lookup(tr, "_0", ph_digits);
1362 else {
1363 if ((hundreds == 1) && (tr->langopts.numbers2 & NUM2_OMIT_1_HUNDRED_ONLY) && ((control & 1) == 0)) {
1364 // only look for special 100 if there are previous thousands
1365 } else {
1366 if ((!found) && (tensunits == 0)) {
1367 // is there a special pronunciation for exactly n00 ?
1368 sprintf(string, "_%dC0", hundreds);
1369 found = Lookup(tr, string, ph_digits);
1370 }
1371
1372 if (!found) {
1373 sprintf(string, "_%dC", hundreds);
1374 found = Lookup(tr, string, ph_digits); // is there a specific pronunciation for n-hundred ?
1375 }
1376 }
1377
1378 if (found)
1379 ph_100[0] = 0;
1380 else {
1381 say_one_hundred = true;
1382 if (hundreds == 1) {
1383 if ((tr->langopts.numbers & NUM_OMIT_1_HUNDRED) != 0)
1384 say_one_hundred = false;
1385 }
1386
1387 if (say_one_hundred == true)
1388 LookupNum2(tr, hundreds, thousandplex, 0, ph_digits);
1389 }
1390 }
1391 }
1392
1393 sprintf(buf1, "%s%s%s%s", ph_thousands, ph_thousand_and, ph_digits, ph_100);
1394 }
1395
1396 ph_hundred_and[0] = 0;
1397 if (tensunits > 0) {
1398 if ((control & 2) && (tr->langopts.numbers2 & NUM2_MULTIPLE_ORDINAL)) {
1399 // Don't use "and" if we apply ordinal to both hundreds and units
1400 } else {
1401 if ((value > 100) || ((control & 1) && (thousandplex == 0))) {
1402 if ((tr->langopts.numbers & NUM_HUNDRED_AND) || ((tr->langopts.numbers & NUM_HUNDRED_AND_DIGIT) && (tensunits < 10)))
1403 Lookup(tr, "_0and", ph_hundred_and);
1404 }
1405 if ((tr->langopts.numbers & NUM_THOUSAND_AND) && (hundreds == 0) && ((control & 1) || (ph_thousands[0] != 0)))
1406 Lookup(tr, "_0and", ph_hundred_and);
1407 }
1408 }
1409
1410 buf2[0] = 0;
1411
1412 if ((tensunits != 0) || (suppress_null == false)) {
1413 x = 0;
1414 if (thousandplex == 0) {
1415 x = 2; // allow "eins" for 1 rather than "ein"
1416 if (ordinal)
1417 x = 3; // ordinal number
1418 if ((value < 100) && !(control & 1))
1419 x |= 4; // tens and units only, no higher digits
1420 if (ordinal & 0x20)
1421 x |= 0x20; // variant form of ordinal number
1422 } else if (tr->langopts.numbers2 & (1 << thousandplex) && thousandplex <= 3)
1423 x = 8; // use variant (feminine) for before thousands and millions
1424
1425 if ((tr->translator_name == L('m', 'l')) && (thousandplex == 1))
1426 x |= 0x208; // use #f form for both tens and units
1427
1428 if ((tr->langopts.numbers2 & NUM2_ZERO_TENS) && ((control & 1) || (hundreds > 0))) {
1429 // LANG=zh,
1430 x |= 0x10;
1431 }
1432
1433 if (LookupNum2(tr, tensunits, thousandplex, x | (control & 0x100), buf2) != 0) {
1434 if (tr->langopts.numbers & NUM_SINGLE_AND)
1435 ph_hundred_and[0] = 0; // don't put 'and' after 'hundred' if there's 'and' between tens and units
1436 }
1437 } else {
1438 if (ph_ordinal2[0] != 0) {
1439 ix = strlen(buf1);
1440 if ((ix > 0) && (buf1[ix-1] == phonPAUSE_SHORT))
1441 buf1[ix-1] = 0; // remove pause before adding ordinal suffix
1442 strcpy(buf2, ph_ordinal2);
1443 }
1444 }
1445
1446 sprintf(ph_out, "%s%s%c%s", buf1, ph_hundred_and, phonEND_WORD, buf2);
1447
1448 return 0;
1449 }
1450
1451 static bool CheckThousandsGroup(char *word, int group_len)
1452 {
1453 // Is this a group of 3 digits which looks like a thousands group?
1454 int ix;
1455
1456 for (ix = 0; ix < group_len; ix++) {
1457 if (!IsDigit09(word[ix]))
1458 return false;
1459 }
1460
1461 if (IsDigit09(word[group_len]) || IsDigit09(word[-1]))
1462 return false;
1463
1464 return true;
1465 }
1466
1467 static int TranslateNumber_1(Translator *tr, char *word, char *ph_out, unsigned int *flags, WORD_TAB *wtab, int control)
1468 {
1469 // Number translation with various options
1470 // the "word" may be up to 4 digits
1471 // "words" of 3 digits may be preceded by another number "word" for thousands or millions
1472
1473 int n_digits;
1474 long value;
1475 int ix;
1476 int digix;
1477 unsigned char c;
1478 bool suppress_null = false;
1479 int decimal_point = 0;
1480 int thousandplex = 0;
1481 int thousands_exact = 1;
1482 int thousands_inc = 0;
1483 int prev_thousands = 0;
1484 int ordinal = 0;
1485 long this_value;
1486 int decimal_count;
1487 int max_decimal_count;
1488 int decimal_mode;
1489 int suffix_ix;
1490 int skipwords = 0;
1491 int group_len;
1492 int len;
1493 char *p;
1494 char string[32]; // for looking up entries in **_list
1495 char buf1[100];
1496 char ph_append[50];
1497 char ph_buf[200];
1498 char ph_buf2[50];
1499 char ph_zeros[50];
1500 char suffix[30]; // string[] must be long enough for sizeof(suffix)+2
1501 char buf_digit_lookup[50];
1502
1503 static const char str_pause[2] = { phonPAUSE_NOLINK, 0 };
1504
1505 char *end;
1506
1507 *flags = 0;
1508 n_digit_lookup = 0;
1509 buf_digit_lookup[0] = 0;
1510 digit_lookup = buf_digit_lookup;
1511 number_control = control;
1512
1513 for (ix = 0; IsDigit09(word[ix]); ix++) ;
1514 n_digits = ix;
1515 errno = 0;
1516 this_value = strtol(word, &end, 10);
1517 if (errno || end == word || this_value > INT_MAX)
1518 return 0; // long number, speak as individual digits
1519 value = this_value;
1520
1521 group_len = 3;
1522 if (tr->langopts.numbers2 & NUM2_MYRIADS)
1523 group_len = 4;
1524
1525 // is there a previous thousands part (as a previous "word") ?
1526 if ((n_digits == group_len) && (word[-2] == tr->langopts.thousands_sep) && IsDigit09(word[-3]))
1527 prev_thousands = 1;
1528 else if ((tr->langopts.thousands_sep == ' ') || (tr->langopts.numbers & NUM_ALLOW_SPACE)) {
1529 // thousands groups can be separated by spaces
1530 if ((n_digits == 3) && !(wtab->flags & FLAG_MULTIPLE_SPACES) && IsDigit09(word[-2]))
1531 prev_thousands = 1;
1532 }
1533 if (prev_thousands == 0)
1534 speak_missing_thousands = 0;
1535
1536 ph_ordinal2[0] = 0;
1537 ph_zeros[0] = 0;
1538
1539 if (prev_thousands || (word[0] != '0')) {
1540 // don't check for ordinal if the number has a leading zero
1541 ordinal = CheckDotOrdinal(tr, word, &word[ix], wtab, 0);
1542 }
1543
1544 if ((word[ix] == '.') && !IsDigit09(word[ix+1]) && !IsDigit09(word[ix+2]) && !(wtab[1].flags & FLAG_NOSPACE)) {
1545 // remove dot unless followed by another number
1546 word[ix] = 0;
1547 }
1548
1549 if ((ordinal == 0) || (tr->translator_name == L('h', 'u'))) {
1550 // NOTE lang=hu, allow both dot and ordinal suffix, eg. "december 21.-én"
1551 // look for an ordinal number suffix after the number
1552 ix++;
1553 p = suffix;
1554 if (wtab[0].flags & FLAG_HYPHEN_AFTER) {
1555 *p++ = '-';
1556 ix++;
1557 }
1558 while ((word[ix] != 0) && (word[ix] != ' ') && (ix < (int)(sizeof(suffix)-1)))
1559 *p++ = word[ix++];
1560 *p = 0;
1561
1562 if (suffix[0] != 0) {
1563 if ((tr->langopts.ordinal_indicator != NULL) && (strcmp(suffix, tr->langopts.ordinal_indicator) == 0))
1564 ordinal = 2;
1565 else if (!IsDigit09(suffix[0])) { // not _#9 (tab)
1566 sprintf(string, "_#%s", suffix);
1567 if (Lookup(tr, string, ph_ordinal2)) {
1568 // this is an ordinal suffix
1569 ordinal = 2;
1570 flags[0] |= FLAG_SKIPWORDS;
1571 skipwords = 1;
1572 sprintf(string, "_x#%s", suffix);
1573 Lookup(tr, string, ph_ordinal2x); // is there an alternate pronunciation?
1574 }
1575 }
1576 }
1577 }
1578
1579 if (wtab[0].flags & FLAG_ORDINAL)
1580 ordinal = 2;
1581
1582 ph_append[0] = 0;
1583 ph_buf2[0] = 0;
1584
1585 if ((word[0] == '0') && (prev_thousands == 0) && (word[1] != ' ') && (word[1] != tr->langopts.decimal_sep)) {
1586 if ((n_digits == 2) && (word[3] == ':') && IsDigit09(word[5]) && isspace(word[7])) {
1587 // looks like a time 02:30, omit the leading zero
1588 } else {
1589 if (n_digits > 3) {
1590 flags[0] &= ~FLAG_SKIPWORDS;
1591 return 0; // long number string with leading zero, speak as individual digits
1592 }
1593
1594 // speak leading zeros
1595 for (ix = 0; (word[ix] == '0') && (ix < (n_digits-1)); ix++)
1596 Lookup(tr, "_0", &ph_zeros[strlen(ph_zeros)]);
1597 }
1598 }
1599
1600 if ((tr->langopts.numbers & NUM_ALLOW_SPACE) && (word[n_digits] == ' '))
1601 thousands_inc = 1;
1602 else if (word[n_digits] == tr->langopts.thousands_sep)
1603 thousands_inc = 2;
1604
1605 suffix_ix = n_digits+2;
1606 if (thousands_inc > 0) {
1607 // if the following "words" are three-digit groups, count them and add
1608 // a "thousand"/"million" suffix to this one
1609 digix = n_digits + thousands_inc;
1610
1611 while (((wtab[thousandplex+1].flags & FLAG_MULTIPLE_SPACES) == 0) && CheckThousandsGroup(&word[digix], group_len)) {
1612 for (ix = 0; ix < group_len; ix++) {
1613 if (word[digix+ix] != '0') {
1614 thousands_exact = 0;
1615 break;
1616 }
1617 }
1618
1619 thousandplex++;
1620 digix += group_len;
1621 if ((word[digix] == tr->langopts.thousands_sep) || ((tr->langopts.numbers & NUM_ALLOW_SPACE) && (word[digix] == ' '))) {
1622 suffix_ix = digix+2;
1623 digix += thousands_inc;
1624 } else
1625 break;
1626 }
1627 }
1628
1629 if ((value == 0) && prev_thousands)
1630 suppress_null = true;
1631
1632 if (tr->translator_name == L('h', 'u')) {
1633 // variant form of numbers when followed by hyphen and a suffix starting with 'a' or 'e' (but not a, e, az, ez, azt, ezt
1634 if ((wtab[thousandplex].flags & FLAG_HYPHEN_AFTER) && (thousands_exact == 1) && hu_number_e(&word[suffix_ix], thousandplex, value))
1635 number_control |= 1; // use _1e variant of number
1636 }
1637
1638 if ((word[n_digits] == tr->langopts.decimal_sep) && IsDigit09(word[n_digits+1])) {
1639 // this "word" ends with a decimal point
1640 Lookup(tr, "_dpt", ph_append);
1641 decimal_point = 0x100;
1642 } else if (suppress_null == false) {
1643 if (thousands_inc > 0) {
1644 if (thousandplex > 0) {
1645 if ((suppress_null == false) && (LookupThousands(tr, value, thousandplex, thousands_exact, ph_append))) {
1646 // found an exact match for N thousand
1647 value = 0;
1648 suppress_null = true;
1649 }
1650 }
1651 }
1652 } else if (speak_missing_thousands == 1) {
1653 // speak this thousandplex if there was no word for the previous thousandplex
1654 sprintf(string, "_0M%d", thousandplex+1);
1655 if (Lookup(tr, string, buf1) == 0) {
1656 sprintf(string, "_0M%d", thousandplex);
1657 Lookup(tr, string, ph_append);
1658 }
1659 }
1660
1661 if ((ph_append[0] == 0) && (word[n_digits] == '.') && (thousandplex == 0))
1662 Lookup(tr, "_.", ph_append);
1663
1664 if (thousandplex == 0) {
1665 char *p2;
1666 // look for combinations of the number with the next word
1667 p = word;
1668 while (IsDigit09(p[1])) p++; // just use the last digit
1669 if (IsDigit09(p[-1])) {
1670 p2 = p - 1;
1671 if (LookupDictList(tr, &p2, buf_digit_lookup, flags, FLAG_SUFX, wtab)) // lookup 2 digits
1672 n_digit_lookup = 2;
1673 }
1674
1675 if ((buf_digit_lookup[0] == 0) && (*p != '0')) {
1676 // LANG=hu ?
1677 // not found, lookup only the last digit (?? but not if dot-ordinal has been found)
1678 if (LookupDictList(tr, &p, buf_digit_lookup, flags, FLAG_SUFX, wtab)) // don't match '0', or entries with $only
1679 n_digit_lookup = 1;
1680 }
1681
1682 if (prev_thousands == 0) {
1683 if ((decimal_point == 0) && (ordinal == 0)) {
1684 // Look for special pronunciation for this number in isolation (LANG=kl)
1685 sprintf(string, "_%ldn", value);
1686 if (Lookup(tr, string, ph_out))
1687 return 1;
1688 }
1689
1690 if (tr->langopts.numbers2 & NUM2_PERCENT_BEFORE) {
1691 // LANG=si, say "percent" before the number
1692 p2 = word;
1693 while ((*p2 != ' ') && (*p2 != 0))
1694 p2++;
1695 if (p2[1] == '%') {
1696 Lookup(tr, "%", ph_out);
1697 ph_out += strlen(ph_out);
1698 p2[1] = ' ';
1699 }
1700 }
1701 }
1702
1703 }
1704
1705 LookupNum3(tr, value, ph_buf, suppress_null, thousandplex, prev_thousands | ordinal | decimal_point);
1706 if ((thousandplex > 0) && (tr->langopts.numbers2 & NUM2_SWAP_THOUSANDS))
1707 sprintf(ph_out, "%s%s%c%s%s", ph_zeros, ph_append, phonEND_WORD, ph_buf2, ph_buf);
1708 else
1709 sprintf(ph_out, "%s%s%s%c%s", ph_zeros, ph_buf2, ph_buf, phonEND_WORD, ph_append);
1710
1711
1712 while (decimal_point) {
1713 n_digits++;
1714
1715 decimal_count = 0;
1716 while (IsDigit09(word[n_digits+decimal_count]))
1717 decimal_count++;
1718
1719 max_decimal_count = 2;
1720 switch (decimal_mode = (tr->langopts.numbers & NUM_DFRACTION_BITS))
1721 {
1722 case NUM_DFRACTION_4:
1723 max_decimal_count = 5;
1724 // fallthrough:
1725 case NUM_DFRACTION_2:
1726 // French/Polish decimal fraction
1727 while (word[n_digits] == '0') {
1728 Lookup(tr, "_0", buf1);
1729 strcat(ph_out, buf1);
1730 decimal_count--;
1731 n_digits++;
1732 }
1733 if ((decimal_count <= max_decimal_count) && IsDigit09(word[n_digits])) {
1734 LookupNum3(tr, atoi(&word[n_digits]), buf1, false, 0, 0);
1735 strcat(ph_out, buf1);
1736 n_digits += decimal_count;
1737 }
1738 break;
1739 case NUM_DFRACTION_1: // italian, say "hundredths" if leading zero
1740 case NUM_DFRACTION_5: // hungarian, always say "tenths" etc.
1741 case NUM_DFRACTION_6: // kazakh, always say "tenths" etc, before the decimal fraction
1742 LookupNum3(tr, atoi(&word[n_digits]), ph_buf, false, 0, 0);
1743 if ((word[n_digits] == '0') || (decimal_mode != NUM_DFRACTION_1)) {
1744 // decimal part has leading zeros, so add a "hundredths" or "thousandths" suffix
1745 sprintf(string, "_0Z%d", decimal_count);
1746 if (Lookup(tr, string, buf1) == 0)
1747 break; // revert to speaking single digits
1748
1749 if (decimal_mode == NUM_DFRACTION_6)
1750 strcat(ph_out, buf1);
1751 else
1752 strcat(ph_buf, buf1);
1753 }
1754 strcat(ph_out, ph_buf);
1755 n_digits += decimal_count;
1756 break;
1757 case NUM_DFRACTION_3:
1758 // Romanian decimal fractions
1759 if ((decimal_count <= 4) && (word[n_digits] != '0')) {
1760 LookupNum3(tr, atoi(&word[n_digits]), buf1, false, 0, 0);
1761 strcat(ph_out, buf1);
1762 n_digits += decimal_count;
1763 }
1764 break;
1765 case NUM_DFRACTION_7:
1766 // alternative form of decimal fraction digits, except the final digit
1767 while (decimal_count-- > 1) {
1768 sprintf(string, "_%cd", word[n_digits]);
1769 if (Lookup(tr, string, buf1) == 0)
1770 break;
1771 n_digits++;
1772 strcat(ph_out, buf1);
1773 }
1774 }
1775
1776 while (IsDigit09(c = word[n_digits]) && (strlen(ph_out) < (N_WORD_PHONEMES - 10))) {
1777 // speak any remaining decimal fraction digits individually
1778 value = word[n_digits++] - '0';
1779 LookupNum2(tr, value, 0, 2, buf1);
1780 len = strlen(ph_out);
1781 sprintf(&ph_out[len], "%c%s", phonEND_WORD, buf1);
1782 }
1783
1784 // something after the decimal part ?
1785 if (Lookup(tr, "_dpt2", buf1))
1786 strcat(ph_out, buf1);
1787
1788 if ((c == tr->langopts.decimal_sep) && IsDigit09(word[n_digits+1])) {
1789 Lookup(tr, "_dpt", buf1);
1790 strcat(ph_out, buf1);
1791 } else
1792 decimal_point = 0;
1793 }
1794 if ((ph_out[0] != 0) && (ph_out[0] != phonSWITCH)) {
1795 int next_char;
1796 char *p1;
1797 p1 = &word[n_digits+1];
1798
1799 p1 += utf8_in(&next_char, p1);
1800 if ((tr->langopts.numbers & NUM_NOPAUSE) && (next_char == ' '))
1801 utf8_in(&next_char, p1);
1802
1803 if (!iswalpha_portable(next_char) && (thousands_exact == 0))
1804 strcat(ph_out, str_pause); // don't add pause for 100s, 6th, etc.
1805 }
1806
1807 *flags |= FLAG_FOUND;
1808 speak_missing_thousands--;
1809
1810 if (skipwords)
1811 dictionary_skipwords = skipwords;
1812 return 1;
1813 }
1814
1815 int TranslateNumber(Translator *tr, char *word1, char *ph_out, unsigned int *flags, WORD_TAB *wtab, int control)
1816 {
1817 if ((option_sayas == SAYAS_DIGITS1) || (wtab[0].flags & FLAG_INDIVIDUAL_DIGITS))
1818 return 0; // speak digits individually
1819
1820 if (tr->langopts.numbers != 0)
1821 return TranslateNumber_1(tr, word1, ph_out, flags, wtab, control);
1822 return 0;
1823 }