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

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