eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

numbers.c 49KB

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