numbers.cpp 56 KB

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